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