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