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