Commit | Line | Data |
---|---|---|
b70021f4 | 1 | /* Primitive operations on floating point for GNU Emacs Lisp interpreter. |
95df8112 | 2 | |
ab422c4d PE |
3 | Copyright (C) 1988, 1993-1994, 1999, 2001-2013 Free Software Foundation, |
4 | Inc. | |
b70021f4 | 5 | |
0a9dd3a7 GM |
6 | Author: Wolfgang Rupprecht |
7 | (according to ack.texi) | |
8 | ||
b70021f4 MR |
9 | This file is part of GNU Emacs. |
10 | ||
9ec0b715 | 11 | GNU Emacs is free software: you can redistribute it and/or modify |
b70021f4 | 12 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
13 | the Free Software Foundation, either version 3 of the License, or |
14 | (at your option) any later version. | |
b70021f4 MR |
15 | |
16 | GNU Emacs is distributed in the hope that it will be useful, | |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 22 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
b70021f4 MR |
23 | |
24 | ||
c990426a PE |
25 | /* C89 requires only the following math.h functions, and Emacs omits |
26 | the starred functions since we haven't found a use for them: | |
27 | acos, asin, atan, atan2, ceil, cos, *cosh, exp, fabs, floor, fmod, | |
28 | frexp, ldexp, log, log10, *modf, pow, sin, *sinh, sqrt, tan, *tanh. | |
4b6baf5f RS |
29 | */ |
30 | ||
18160b98 | 31 | #include <config.h> |
0328b6de | 32 | |
523e9291 | 33 | #include "lisp.h" |
d137ae2f | 34 | |
b70021f4 | 35 | #include <math.h> |
4b6baf5f | 36 | |
c990426a PE |
37 | #ifndef isfinite |
38 | # define isfinite(x) ((x) - (x) == 0) | |
39 | #endif | |
40 | #ifndef isnan | |
41 | # define isnan(x) ((x) != (x)) | |
42 | #endif | |
c26406fe | 43 | |
b70021f4 MR |
44 | /* Extract a Lisp number as a `double', or signal an error. */ |
45 | ||
46 | double | |
d5a3eaaf | 47 | extract_float (Lisp_Object num) |
b70021f4 | 48 | { |
b7826503 | 49 | CHECK_NUMBER_OR_FLOAT (num); |
b70021f4 | 50 | |
207a45c1 | 51 | if (FLOATP (num)) |
70949dac | 52 | return XFLOAT_DATA (num); |
b70021f4 MR |
53 | return (double) XINT (num); |
54 | } | |
c2d4ea74 RS |
55 | \f |
56 | /* Trig functions. */ | |
b70021f4 MR |
57 | |
58 | DEFUN ("acos", Facos, Sacos, 1, 1, 0, | |
335c5470 | 59 | doc: /* Return the inverse cosine of ARG. */) |
f6196b87 | 60 | (Lisp_Object arg) |
b70021f4 | 61 | { |
4b6baf5f | 62 | double d = extract_float (arg); |
f6196b87 | 63 | d = acos (d); |
b70021f4 MR |
64 | return make_float (d); |
65 | } | |
66 | ||
c2d4ea74 | 67 | DEFUN ("asin", Fasin, Sasin, 1, 1, 0, |
335c5470 | 68 | doc: /* Return the inverse sine of ARG. */) |
f6196b87 | 69 | (Lisp_Object arg) |
b70021f4 | 70 | { |
4b6baf5f | 71 | double d = extract_float (arg); |
f6196b87 | 72 | d = asin (d); |
b70021f4 MR |
73 | return make_float (d); |
74 | } | |
75 | ||
250ffca6 EZ |
76 | DEFUN ("atan", Fatan, Satan, 1, 2, 0, |
77 | doc: /* Return the inverse tangent of the arguments. | |
78 | If only one argument Y is given, return the inverse tangent of Y. | |
79 | If two arguments Y and X are given, return the inverse tangent of Y | |
80 | divided by X, i.e. the angle in radians between the vector (X, Y) | |
81 | and the x-axis. */) | |
f6196b87 | 82 | (Lisp_Object y, Lisp_Object x) |
b70021f4 | 83 | { |
250ffca6 EZ |
84 | double d = extract_float (y); |
85 | ||
86 | if (NILP (x)) | |
f6196b87 | 87 | d = atan (d); |
250ffca6 EZ |
88 | else |
89 | { | |
90 | double d2 = extract_float (x); | |
f6196b87 | 91 | d = atan2 (d, d2); |
250ffca6 | 92 | } |
b70021f4 MR |
93 | return make_float (d); |
94 | } | |
95 | ||
c2d4ea74 | 96 | DEFUN ("cos", Fcos, Scos, 1, 1, 0, |
335c5470 | 97 | doc: /* Return the cosine of ARG. */) |
f6196b87 | 98 | (Lisp_Object arg) |
b70021f4 | 99 | { |
4b6baf5f | 100 | double d = extract_float (arg); |
f6196b87 | 101 | d = cos (d); |
b70021f4 MR |
102 | return make_float (d); |
103 | } | |
104 | ||
c2d4ea74 | 105 | DEFUN ("sin", Fsin, Ssin, 1, 1, 0, |
335c5470 | 106 | doc: /* Return the sine of ARG. */) |
f6196b87 | 107 | (Lisp_Object arg) |
b70021f4 | 108 | { |
4b6baf5f | 109 | double d = extract_float (arg); |
f6196b87 | 110 | d = sin (d); |
b70021f4 MR |
111 | return make_float (d); |
112 | } | |
113 | ||
c2d4ea74 | 114 | DEFUN ("tan", Ftan, Stan, 1, 1, 0, |
335c5470 | 115 | doc: /* Return the tangent of ARG. */) |
f6196b87 | 116 | (Lisp_Object arg) |
4b6baf5f RS |
117 | { |
118 | double d = extract_float (arg); | |
f6196b87 | 119 | d = tan (d); |
b70021f4 MR |
120 | return make_float (d); |
121 | } | |
15e12598 | 122 | |
15e12598 VB |
123 | DEFUN ("isnan", Fisnan, Sisnan, 1, 1, 0, |
124 | doc: /* Return non nil iff argument X is a NaN. */) | |
5842a27b | 125 | (Lisp_Object x) |
15e12598 VB |
126 | { |
127 | CHECK_FLOAT (x); | |
128 | return isnan (XFLOAT_DATA (x)) ? Qt : Qnil; | |
129 | } | |
130 | ||
c8199d0f | 131 | #ifdef HAVE_COPYSIGN |
3c2907f7 | 132 | DEFUN ("copysign", Fcopysign, Scopysign, 2, 2, 0, |
15e12598 VB |
133 | doc: /* Copy sign of X2 to value of X1, and return the result. |
134 | Cause an error if X1 or X2 is not a float. */) | |
5842a27b | 135 | (Lisp_Object x1, Lisp_Object x2) |
15e12598 VB |
136 | { |
137 | double f1, f2; | |
138 | ||
139 | CHECK_FLOAT (x1); | |
140 | CHECK_FLOAT (x2); | |
141 | ||
142 | f1 = XFLOAT_DATA (x1); | |
143 | f2 = XFLOAT_DATA (x2); | |
144 | ||
145 | return make_float (copysign (f1, f2)); | |
146 | } | |
c990426a | 147 | #endif |
15e12598 VB |
148 | |
149 | DEFUN ("frexp", Ffrexp, Sfrexp, 1, 1, 0, | |
150 | doc: /* Get significand and exponent of a floating point number. | |
151 | Breaks the floating point number X into its binary significand SGNFCAND | |
152 | \(a floating point value between 0.5 (included) and 1.0 (excluded)) | |
153 | and an integral exponent EXP for 2, such that: | |
154 | ||
155 | X = SGNFCAND * 2^EXP | |
156 | ||
157 | The function returns the cons cell (SGNFCAND . EXP). | |
158 | If X is zero, both parts (SGNFCAND and EXP) are zero. */) | |
5842a27b | 159 | (Lisp_Object x) |
15e12598 VB |
160 | { |
161 | double f = XFLOATINT (x); | |
c990426a PE |
162 | int exponent; |
163 | double sgnfcand = frexp (f, &exponent); | |
164 | return Fcons (make_float (sgnfcand), make_number (exponent)); | |
15e12598 VB |
165 | } |
166 | ||
167 | DEFUN ("ldexp", Fldexp, Sldexp, 1, 2, 0, | |
168 | doc: /* Construct number X from significand SGNFCAND and exponent EXP. | |
169 | Returns the floating point value resulting from multiplying SGNFCAND | |
170 | (the significand) by 2 raised to the power of EXP (the exponent). */) | |
a885e2ed | 171 | (Lisp_Object sgnfcand, Lisp_Object exponent) |
15e12598 | 172 | { |
a885e2ed PE |
173 | CHECK_NUMBER (exponent); |
174 | return make_float (ldexp (XFLOATINT (sgnfcand), XINT (exponent))); | |
15e12598 | 175 | } |
706ac90d | 176 | \f |
c2d4ea74 | 177 | DEFUN ("exp", Fexp, Sexp, 1, 1, 0, |
335c5470 | 178 | doc: /* Return the exponential base e of ARG. */) |
f6196b87 | 179 | (Lisp_Object arg) |
4b6baf5f RS |
180 | { |
181 | double d = extract_float (arg); | |
f6196b87 | 182 | d = exp (d); |
b70021f4 MR |
183 | return make_float (d); |
184 | } | |
185 | ||
b70021f4 | 186 | DEFUN ("expt", Fexpt, Sexpt, 2, 2, 0, |
335c5470 | 187 | doc: /* Return the exponential ARG1 ** ARG2. */) |
f6196b87 | 188 | (Lisp_Object arg1, Lisp_Object arg2) |
b70021f4 | 189 | { |
2742fe30 | 190 | double f1, f2, f3; |
b70021f4 | 191 | |
b7826503 PJ |
192 | CHECK_NUMBER_OR_FLOAT (arg1); |
193 | CHECK_NUMBER_OR_FLOAT (arg2); | |
207a45c1 | 194 | if (INTEGERP (arg1) /* common lisp spec */ |
5a9807a8 | 195 | && INTEGERP (arg2) /* don't promote, if both are ints, and */ |
908589fd | 196 | && XINT (arg2) >= 0) /* we are sure the result is not fractional */ |
b70021f4 | 197 | { /* this can be improved by pre-calculating */ |
125b3835 PE |
198 | EMACS_INT y; /* some binary powers of x then accumulating */ |
199 | EMACS_UINT acc, x; /* Unsigned so that overflow is well defined. */ | |
4be1d460 RS |
200 | Lisp_Object val; |
201 | ||
4b6baf5f RS |
202 | x = XINT (arg1); |
203 | y = XINT (arg2); | |
8d1da888 | 204 | acc = (y & 1 ? x : 1); |
177c0ea7 | 205 | |
8d1da888 | 206 | while ((y >>= 1) != 0) |
b70021f4 | 207 | { |
8d1da888 PE |
208 | x *= x; |
209 | if (y & 1) | |
210 | acc *= x; | |
b70021f4 | 211 | } |
e0cb2a68 | 212 | XSETINT (val, acc); |
4be1d460 | 213 | return val; |
b70021f4 | 214 | } |
70949dac KR |
215 | f1 = FLOATP (arg1) ? XFLOAT_DATA (arg1) : XINT (arg1); |
216 | f2 = FLOATP (arg2) ? XFLOAT_DATA (arg2) : XINT (arg2); | |
f6196b87 | 217 | f3 = pow (f1, f2); |
2742fe30 | 218 | return make_float (f3); |
b70021f4 | 219 | } |
c2d4ea74 | 220 | |
56abb480 | 221 | DEFUN ("log", Flog, Slog, 1, 2, 0, |
335c5470 | 222 | doc: /* Return the natural logarithm of ARG. |
356e6d8d | 223 | If the optional argument BASE is given, return log ARG using that base. */) |
f6196b87 | 224 | (Lisp_Object arg, Lisp_Object base) |
b70021f4 | 225 | { |
4b6baf5f | 226 | double d = extract_float (arg); |
56abb480 JB |
227 | |
228 | if (NILP (base)) | |
f6196b87 | 229 | d = log (d); |
56abb480 JB |
230 | else |
231 | { | |
232 | double b = extract_float (base); | |
233 | ||
4b6baf5f | 234 | if (b == 10.0) |
f6196b87 | 235 | d = log10 (d); |
4b6baf5f | 236 | else |
f6196b87 | 237 | d = log (d) / log (b); |
56abb480 | 238 | } |
b70021f4 MR |
239 | return make_float (d); |
240 | } | |
241 | ||
c2d4ea74 | 242 | DEFUN ("log10", Flog10, Slog10, 1, 1, 0, |
335c5470 | 243 | doc: /* Return the logarithm base 10 of ARG. */) |
f6196b87 | 244 | (Lisp_Object arg) |
b70021f4 | 245 | { |
4b6baf5f | 246 | double d = extract_float (arg); |
f6196b87 | 247 | d = log10 (d); |
c2d4ea74 RS |
248 | return make_float (d); |
249 | } | |
250 | ||
b70021f4 | 251 | DEFUN ("sqrt", Fsqrt, Ssqrt, 1, 1, 0, |
335c5470 | 252 | doc: /* Return the square root of ARG. */) |
f6196b87 | 253 | (Lisp_Object arg) |
b70021f4 | 254 | { |
4b6baf5f | 255 | double d = extract_float (arg); |
f6196b87 | 256 | d = sqrt (d); |
b70021f4 MR |
257 | return make_float (d); |
258 | } | |
c2d4ea74 | 259 | \f |
b70021f4 | 260 | DEFUN ("abs", Fabs, Sabs, 1, 1, 0, |
335c5470 | 261 | doc: /* Return the absolute value of ARG. */) |
5842a27b | 262 | (register Lisp_Object arg) |
b70021f4 | 263 | { |
b7826503 | 264 | CHECK_NUMBER_OR_FLOAT (arg); |
b70021f4 | 265 | |
207a45c1 | 266 | if (FLOATP (arg)) |
7c26cf3c | 267 | arg = make_float (fabs (XFLOAT_DATA (arg))); |
4b6baf5f | 268 | else if (XINT (arg) < 0) |
db37cb37 | 269 | XSETINT (arg, - XINT (arg)); |
b70021f4 | 270 | |
4b6baf5f | 271 | return arg; |
b70021f4 MR |
272 | } |
273 | ||
a7ca3326 | 274 | DEFUN ("float", Ffloat, Sfloat, 1, 1, 0, |
335c5470 | 275 | doc: /* Return the floating point number equal to ARG. */) |
5842a27b | 276 | (register Lisp_Object arg) |
b70021f4 | 277 | { |
b7826503 | 278 | CHECK_NUMBER_OR_FLOAT (arg); |
b70021f4 | 279 | |
207a45c1 | 280 | if (INTEGERP (arg)) |
4b6baf5f | 281 | return make_float ((double) XINT (arg)); |
b70021f4 | 282 | else /* give 'em the same float back */ |
4b6baf5f | 283 | return arg; |
b70021f4 MR |
284 | } |
285 | ||
286 | DEFUN ("logb", Flogb, Slogb, 1, 1, 0, | |
335c5470 PJ |
287 | doc: /* Returns largest integer <= the base 2 log of the magnitude of ARG. |
288 | This is the same as the exponent of a float. */) | |
5842a27b | 289 | (Lisp_Object arg) |
b70021f4 | 290 | { |
340176df | 291 | Lisp_Object val; |
a7bf3c54 | 292 | EMACS_INT value; |
5bf54166 | 293 | double f = extract_float (arg); |
340176df | 294 | |
6694b327 | 295 | if (f == 0.0) |
b916d672 | 296 | value = MOST_NEGATIVE_FIXNUM; |
c990426a | 297 | else if (isfinite (f)) |
6694b327 | 298 | { |
c8bf6cf3 | 299 | int ivalue; |
f6196b87 | 300 | frexp (f, &ivalue); |
c8bf6cf3 | 301 | value = ivalue - 1; |
6694b327 | 302 | } |
c990426a PE |
303 | else |
304 | value = MOST_POSITIVE_FIXNUM; | |
305 | ||
e0cb2a68 | 306 | XSETINT (val, value); |
c26406fe | 307 | return val; |
b70021f4 MR |
308 | } |
309 | ||
fc2157cb | 310 | |
acbbacbe PE |
311 | /* the rounding functions */ |
312 | ||
313 | static Lisp_Object | |
d2aa42f8 DN |
314 | rounding_driver (Lisp_Object arg, Lisp_Object divisor, |
315 | double (*double_round) (double), | |
316 | EMACS_INT (*int_round2) (EMACS_INT, EMACS_INT), | |
8ea90aa3 | 317 | const char *name) |
b70021f4 | 318 | { |
b7826503 | 319 | CHECK_NUMBER_OR_FLOAT (arg); |
b70021f4 | 320 | |
fc2157cb PE |
321 | if (! NILP (divisor)) |
322 | { | |
9a51b24a | 323 | EMACS_INT i1, i2; |
fc2157cb | 324 | |
b7826503 | 325 | CHECK_NUMBER_OR_FLOAT (divisor); |
fc2157cb | 326 | |
207a45c1 | 327 | if (FLOATP (arg) || FLOATP (divisor)) |
fc2157cb PE |
328 | { |
329 | double f1, f2; | |
330 | ||
70949dac KR |
331 | f1 = FLOATP (arg) ? XFLOAT_DATA (arg) : XINT (arg); |
332 | f2 = (FLOATP (divisor) ? XFLOAT_DATA (divisor) : XINT (divisor)); | |
d137ae2f | 333 | if (! IEEE_FLOATING_POINT && f2 == 0) |
edef1631 | 334 | xsignal0 (Qarith_error); |
fc2157cb | 335 | |
f6196b87 PE |
336 | f1 = (*double_round) (f1 / f2); |
337 | if (FIXNUM_OVERFLOW_P (f1)) | |
338 | xsignal3 (Qrange_error, build_string (name), arg, divisor); | |
339 | arg = make_number (f1); | |
fc2157cb PE |
340 | return arg; |
341 | } | |
fc2157cb PE |
342 | |
343 | i1 = XINT (arg); | |
344 | i2 = XINT (divisor); | |
345 | ||
346 | if (i2 == 0) | |
edef1631 | 347 | xsignal0 (Qarith_error); |
fc2157cb | 348 | |
acbbacbe | 349 | XSETINT (arg, (*int_round2) (i1, i2)); |
fc2157cb PE |
350 | return arg; |
351 | } | |
352 | ||
207a45c1 | 353 | if (FLOATP (arg)) |
81a63ccc | 354 | { |
f6196b87 PE |
355 | double d = (*double_round) (XFLOAT_DATA (arg)); |
356 | if (FIXNUM_OVERFLOW_P (d)) | |
357 | xsignal2 (Qrange_error, build_string (name), arg); | |
358 | arg = make_number (d); | |
81a63ccc | 359 | } |
b70021f4 | 360 | |
4b6baf5f | 361 | return arg; |
b70021f4 MR |
362 | } |
363 | ||
acbbacbe PE |
364 | /* With C's /, the result is implementation-defined if either operand |
365 | is negative, so take care with negative operands in the following | |
366 | integer functions. */ | |
367 | ||
368 | static EMACS_INT | |
d2aa42f8 | 369 | ceiling2 (EMACS_INT i1, EMACS_INT i2) |
acbbacbe PE |
370 | { |
371 | return (i2 < 0 | |
372 | ? (i1 < 0 ? ((-1 - i1) / -i2) + 1 : - (i1 / -i2)) | |
373 | : (i1 <= 0 ? - (-i1 / i2) : ((i1 - 1) / i2) + 1)); | |
374 | } | |
375 | ||
376 | static EMACS_INT | |
d2aa42f8 | 377 | floor2 (EMACS_INT i1, EMACS_INT i2) |
acbbacbe PE |
378 | { |
379 | return (i2 < 0 | |
380 | ? (i1 <= 0 ? -i1 / -i2 : -1 - ((i1 - 1) / -i2)) | |
381 | : (i1 < 0 ? -1 - ((-1 - i1) / i2) : i1 / i2)); | |
382 | } | |
383 | ||
384 | static EMACS_INT | |
d2aa42f8 | 385 | truncate2 (EMACS_INT i1, EMACS_INT i2) |
acbbacbe PE |
386 | { |
387 | return (i2 < 0 | |
388 | ? (i1 < 0 ? -i1 / -i2 : - (i1 / -i2)) | |
389 | : (i1 < 0 ? - (-i1 / i2) : i1 / i2)); | |
390 | } | |
391 | ||
392 | static EMACS_INT | |
d2aa42f8 | 393 | round2 (EMACS_INT i1, EMACS_INT i2) |
acbbacbe PE |
394 | { |
395 | /* The C language's division operator gives us one remainder R, but | |
396 | we want the remainder R1 on the other side of 0 if R1 is closer | |
397 | to 0 than R is; because we want to round to even, we also want R1 | |
398 | if R and R1 are the same distance from 0 and if C's quotient is | |
399 | odd. */ | |
400 | EMACS_INT q = i1 / i2; | |
401 | EMACS_INT r = i1 % i2; | |
71376d4b PE |
402 | EMACS_INT abs_r = eabs (r); |
403 | EMACS_INT abs_r1 = eabs (i2) - abs_r; | |
acbbacbe PE |
404 | return q + (abs_r + (q & 1) <= abs_r1 ? 0 : (i2 ^ r) < 0 ? -1 : 1); |
405 | } | |
406 | ||
dca6c914 RS |
407 | /* The code uses emacs_rint, so that it works to undefine HAVE_RINT |
408 | if `rint' exists but does not work right. */ | |
409 | #ifdef HAVE_RINT | |
410 | #define emacs_rint rint | |
411 | #else | |
4b5878a8 | 412 | static double |
d2aa42f8 | 413 | emacs_rint (double d) |
4b5878a8 | 414 | { |
1b65c684 | 415 | return floor (d + 0.5); |
4b5878a8 KH |
416 | } |
417 | #endif | |
418 | ||
acbbacbe | 419 | static double |
d2aa42f8 | 420 | double_identity (double d) |
acbbacbe PE |
421 | { |
422 | return d; | |
423 | } | |
424 | ||
425 | DEFUN ("ceiling", Fceiling, Sceiling, 1, 2, 0, | |
1d6ea92f RS |
426 | doc: /* Return the smallest integer no less than ARG. |
427 | This rounds the value towards +inf. | |
335c5470 | 428 | With optional DIVISOR, return the smallest integer no less than ARG/DIVISOR. */) |
5842a27b | 429 | (Lisp_Object arg, Lisp_Object divisor) |
acbbacbe PE |
430 | { |
431 | return rounding_driver (arg, divisor, ceil, ceiling2, "ceiling"); | |
432 | } | |
433 | ||
434 | DEFUN ("floor", Ffloor, Sfloor, 1, 2, 0, | |
1d6ea92f | 435 | doc: /* Return the largest integer no greater than ARG. |
568b6e41 | 436 | This rounds the value towards -inf. |
335c5470 | 437 | With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR. */) |
5842a27b | 438 | (Lisp_Object arg, Lisp_Object divisor) |
acbbacbe PE |
439 | { |
440 | return rounding_driver (arg, divisor, floor, floor2, "floor"); | |
441 | } | |
442 | ||
443 | DEFUN ("round", Fround, Sround, 1, 2, 0, | |
335c5470 | 444 | doc: /* Return the nearest integer to ARG. |
6ded2c89 EZ |
445 | With optional DIVISOR, return the nearest integer to ARG/DIVISOR. |
446 | ||
a32a4857 EZ |
447 | Rounding a value equidistant between two integers may choose the |
448 | integer closer to zero, or it may prefer an even integer, depending on | |
449 | your machine. For example, \(round 2.5\) can return 3 on some | |
59fe0cee | 450 | systems, but 2 on others. */) |
5842a27b | 451 | (Lisp_Object arg, Lisp_Object divisor) |
acbbacbe | 452 | { |
dca6c914 | 453 | return rounding_driver (arg, divisor, emacs_rint, round2, "round"); |
acbbacbe PE |
454 | } |
455 | ||
a7ca3326 | 456 | DEFUN ("truncate", Ftruncate, Struncate, 1, 2, 0, |
335c5470 PJ |
457 | doc: /* Truncate a floating point number to an int. |
458 | Rounds ARG toward zero. | |
459 | With optional DIVISOR, truncate ARG/DIVISOR. */) | |
5842a27b | 460 | (Lisp_Object arg, Lisp_Object divisor) |
acbbacbe PE |
461 | { |
462 | return rounding_driver (arg, divisor, double_identity, truncate2, | |
463 | "truncate"); | |
464 | } | |
465 | ||
fc2157cb | 466 | |
d137ae2f | 467 | Lisp_Object |
dd4c5104 | 468 | fmod_float (Lisp_Object x, Lisp_Object y) |
d137ae2f PE |
469 | { |
470 | double f1, f2; | |
471 | ||
70949dac KR |
472 | f1 = FLOATP (x) ? XFLOAT_DATA (x) : XINT (x); |
473 | f2 = FLOATP (y) ? XFLOAT_DATA (y) : XINT (y); | |
d137ae2f | 474 | |
f6196b87 | 475 | f1 = fmod (f1, f2); |
d137ae2f PE |
476 | |
477 | /* If the "remainder" comes out with the wrong sign, fix it. */ | |
908589fd | 478 | if (f2 < 0 ? f1 > 0 : f1 < 0) |
f6196b87 PE |
479 | f1 += f2; |
480 | ||
d137ae2f PE |
481 | return make_float (f1); |
482 | } | |
4b6baf5f | 483 | \f |
4b6baf5f | 484 | DEFUN ("fceiling", Ffceiling, Sfceiling, 1, 1, 0, |
335c5470 PJ |
485 | doc: /* Return the smallest integer no less than ARG, as a float. |
486 | \(Round toward +inf.\) */) | |
f6196b87 | 487 | (Lisp_Object arg) |
4b6baf5f RS |
488 | { |
489 | double d = extract_float (arg); | |
f6196b87 | 490 | d = ceil (d); |
4b6baf5f RS |
491 | return make_float (d); |
492 | } | |
493 | ||
494 | DEFUN ("ffloor", Fffloor, Sffloor, 1, 1, 0, | |
335c5470 PJ |
495 | doc: /* Return the largest integer no greater than ARG, as a float. |
496 | \(Round towards -inf.\) */) | |
f6196b87 | 497 | (Lisp_Object arg) |
4b6baf5f RS |
498 | { |
499 | double d = extract_float (arg); | |
f6196b87 | 500 | d = floor (d); |
4b6baf5f RS |
501 | return make_float (d); |
502 | } | |
b70021f4 | 503 | |
4b6baf5f | 504 | DEFUN ("fround", Ffround, Sfround, 1, 1, 0, |
335c5470 | 505 | doc: /* Return the nearest integer to ARG, as a float. */) |
f6196b87 | 506 | (Lisp_Object arg) |
4b6baf5f RS |
507 | { |
508 | double d = extract_float (arg); | |
f6196b87 | 509 | d = emacs_rint (d); |
4b6baf5f RS |
510 | return make_float (d); |
511 | } | |
512 | ||
513 | DEFUN ("ftruncate", Fftruncate, Sftruncate, 1, 1, 0, | |
335c5470 PJ |
514 | doc: /* Truncate a floating point number to an integral float value. |
515 | Rounds the value toward zero. */) | |
f6196b87 | 516 | (Lisp_Object arg) |
4b6baf5f RS |
517 | { |
518 | double d = extract_float (arg); | |
519 | if (d >= 0.0) | |
f6196b87 | 520 | d = floor (d); |
4b6baf5f | 521 | else |
f6196b87 | 522 | d = ceil (d); |
4b6baf5f | 523 | return make_float (d); |
b70021f4 MR |
524 | } |
525 | \f | |
dfcf069d | 526 | void |
d5a3eaaf | 527 | syms_of_floatfns (void) |
b70021f4 MR |
528 | { |
529 | defsubr (&Sacos); | |
b70021f4 | 530 | defsubr (&Sasin); |
b70021f4 | 531 | defsubr (&Satan); |
c2d4ea74 RS |
532 | defsubr (&Scos); |
533 | defsubr (&Ssin); | |
534 | defsubr (&Stan); | |
15e12598 | 535 | defsubr (&Sisnan); |
c8199d0f | 536 | #ifdef HAVE_COPYSIGN |
15e12598 | 537 | defsubr (&Scopysign); |
c990426a | 538 | #endif |
15e12598 VB |
539 | defsubr (&Sfrexp); |
540 | defsubr (&Sldexp); | |
4b6baf5f RS |
541 | defsubr (&Sfceiling); |
542 | defsubr (&Sffloor); | |
543 | defsubr (&Sfround); | |
544 | defsubr (&Sftruncate); | |
b70021f4 | 545 | defsubr (&Sexp); |
c2d4ea74 | 546 | defsubr (&Sexpt); |
b70021f4 MR |
547 | defsubr (&Slog); |
548 | defsubr (&Slog10); | |
b70021f4 | 549 | defsubr (&Ssqrt); |
b70021f4 MR |
550 | |
551 | defsubr (&Sabs); | |
552 | defsubr (&Sfloat); | |
553 | defsubr (&Slogb); | |
554 | defsubr (&Sceiling); | |
acbbacbe | 555 | defsubr (&Sfloor); |
b70021f4 MR |
556 | defsubr (&Sround); |
557 | defsubr (&Struncate); | |
558 | } |