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