1 /* Primitive operations on floating point for GNU Emacs Lisp interpreter.
2 Copyright (C) 1988, 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "syssignal.h"
27 Lisp_Object Qarith_error
;
29 #ifdef LISP_FLOAT_TYPE
36 /* Avoid traps on VMS from sinh and cosh.
37 All the other functions set errno instead. */
42 #define cosh(x) ((exp(x)+exp(-x))*0.5)
43 #define sinh(x) ((exp(x)-exp(-x))*0.5)
46 static SIGTYPE
float_error ();
48 /* Nonzero while executing in floating point.
49 This tells float_error what to do. */
53 /* If an argument is out of range for a mathematical function,
54 here is the actual argument value to use in the error message. */
56 static Lisp_Object float_error_arg
;
58 /* Evaluate the floating point expression D, recording NUM
59 as the original argument for error messages.
60 D is normally an assignment expression.
61 Handle errors which may result in signals or may set errno. */
63 #define IN_FLOAT(D, NUM) \
64 (in_float = 1, errno = 0, float_error_arg = NUM, (D), \
65 (errno == ERANGE || errno == EDOM ? float_error () : (SIGTYPE) 0), \
68 /* Extract a Lisp number as a `double', or signal an error. */
74 CHECK_NUMBER_OR_FLOAT (num
, 0);
76 if (XTYPE (num
) == Lisp_Float
)
77 return XFLOAT (num
)->data
;
78 return (double) XINT (num
);
83 DEFUN ("acos", Facos
, Sacos
, 1, 1, 0,
84 "Return the inverse cosine of ARG.")
86 register Lisp_Object num
;
88 double d
= extract_float (num
);
89 IN_FLOAT (d
= acos (d
), num
);
90 return make_float (d
);
93 DEFUN ("asin", Fasin
, Sasin
, 1, 1, 0,
94 "Return the inverse sine of ARG.")
96 register Lisp_Object num
;
98 double d
= extract_float (num
);
99 IN_FLOAT (d
= asin (d
), num
);
100 return make_float (d
);
103 DEFUN ("atan", Fatan
, Satan
, 1, 1, 0,
104 "Return the inverse tangent of ARG.")
106 register Lisp_Object num
;
108 double d
= extract_float (num
);
109 IN_FLOAT (d
= atan (d
), num
);
110 return make_float (d
);
113 DEFUN ("cos", Fcos
, Scos
, 1, 1, 0,
114 "Return the cosine of ARG.")
116 register Lisp_Object num
;
118 double d
= extract_float (num
);
119 IN_FLOAT (d
= cos (d
), num
);
120 return make_float (d
);
123 DEFUN ("sin", Fsin
, Ssin
, 1, 1, 0,
124 "Return the sine of ARG.")
126 register Lisp_Object num
;
128 double d
= extract_float (num
);
129 IN_FLOAT (d
= sin (d
), num
);
130 return make_float (d
);
133 DEFUN ("tan", Ftan
, Stan
, 1, 1, 0,
134 "Return the tangent of ARG.")
136 register Lisp_Object num
;
138 double d
= extract_float (num
);
139 IN_FLOAT (d
= tan (d
), num
);
140 return make_float (d
);
143 #if 0 /* Leave these out unless we find there's a reason for them. */
145 DEFUN ("bessel-j0", Fbessel_j0
, Sbessel_j0
, 1, 1, 0,
146 "Return the bessel function j0 of ARG.")
148 register Lisp_Object num
;
150 double d
= extract_float (num
);
151 IN_FLOAT (d
= j0 (d
), num
);
152 return make_float (d
);
155 DEFUN ("bessel-j1", Fbessel_j1
, Sbessel_j1
, 1, 1, 0,
156 "Return the bessel function j1 of ARG.")
158 register Lisp_Object num
;
160 double d
= extract_float (num
);
161 IN_FLOAT (d
= j1 (d
), num
);
162 return make_float (d
);
165 DEFUN ("bessel-jn", Fbessel_jn
, Sbessel_jn
, 2, 2, 0,
166 "Return the order N bessel function output jn of ARG.\n\
167 The first arg (the order) is truncated to an integer.")
169 register Lisp_Object num1
, num2
;
171 int i1
= extract_float (num1
);
172 double f2
= extract_float (num2
);
174 IN_FLOAT (f2
= jn (i1
, f2
), num1
);
175 return make_float (f2
);
178 DEFUN ("bessel-y0", Fbessel_y0
, Sbessel_y0
, 1, 1, 0,
179 "Return the bessel function y0 of ARG.")
181 register Lisp_Object num
;
183 double d
= extract_float (num
);
184 IN_FLOAT (d
= y0 (d
), num
);
185 return make_float (d
);
188 DEFUN ("bessel-y1", Fbessel_y1
, Sbessel_y1
, 1, 1, 0,
189 "Return the bessel function y1 of ARG.")
191 register Lisp_Object num
;
193 double d
= extract_float (num
);
194 IN_FLOAT (d
= y1 (d
), num
);
195 return make_float (d
);
198 DEFUN ("bessel-yn", Fbessel_yn
, Sbessel_yn
, 2, 2, 0,
199 "Return the order N bessel function output yn of ARG.\n\
200 The first arg (the order) is truncated to an integer.")
202 register Lisp_Object num1
, num2
;
204 int i1
= extract_float (num1
);
205 double f2
= extract_float (num2
);
207 IN_FLOAT (f2
= yn (i1
, f2
), num1
);
208 return make_float (f2
);
213 #if 0 /* Leave these out unless we see they are worth having. */
215 DEFUN ("erf", Ferf
, Serf
, 1, 1, 0,
216 "Return the mathematical error function of ARG.")
218 register Lisp_Object num
;
220 double d
= extract_float (num
);
221 IN_FLOAT (d
= erf (d
), num
);
222 return make_float (d
);
225 DEFUN ("erfc", Ferfc
, Serfc
, 1, 1, 0,
226 "Return the complementary error function of ARG.")
228 register Lisp_Object num
;
230 double d
= extract_float (num
);
231 IN_FLOAT (d
= erfc (d
), num
);
232 return make_float (d
);
235 DEFUN ("log-gamma", Flog_gamma
, Slog_gamma
, 1, 1, 0,
236 "Return the log gamma of ARG.")
238 register Lisp_Object num
;
240 double d
= extract_float (num
);
241 IN_FLOAT (d
= lgamma (d
), num
);
242 return make_float (d
);
247 DEFUN ("cube-root", Fcube_root
, Scube_root
, 1, 1, 0,
248 "Return the cube root of ARG.")
250 register Lisp_Object num
;
252 double d
= extract_float (num
);
253 IN_FLOAT (d
= cbrt (d
), num
);
254 return make_float (d
);
257 DEFUN ("exp", Fexp
, Sexp
, 1, 1, 0,
258 "Return the exponential base e of ARG.")
260 register Lisp_Object num
;
262 double d
= extract_float (num
);
263 IN_FLOAT (d
= exp (d
), num
);
264 return make_float (d
);
267 DEFUN ("expm1", Fexpm1
, Sexpm1
, 1, 1, 0,
268 "Return the exp (x)-1 of ARG.")
270 register Lisp_Object num
;
272 double d
= extract_float (num
);
273 IN_FLOAT (d
= expm1 (d
), num
);
274 return make_float (d
);
277 DEFUN ("expt", Fexpt
, Sexpt
, 2, 2, 0,
278 "Return the exponential X ** Y.")
280 register Lisp_Object num1
, num2
;
284 CHECK_NUMBER_OR_FLOAT (num1
, 0);
285 CHECK_NUMBER_OR_FLOAT (num2
, 0);
286 if ((XTYPE (num1
) == Lisp_Int
) && /* common lisp spec */
287 (XTYPE (num2
) == Lisp_Int
)) /* don't promote, if both are ints */
288 { /* this can be improved by pre-calculating */
289 int acc
, x
, y
; /* some binary powers of x then acumulating */
290 /* these, therby saving some time. -wsr */
305 return XSET (x
, Lisp_Int
, acc
);
307 f1
= (XTYPE (num1
) == Lisp_Float
) ? XFLOAT (num1
)->data
: XINT (num1
);
308 f2
= (XTYPE (num2
) == Lisp_Float
) ? XFLOAT (num2
)->data
: XINT (num2
);
309 IN_FLOAT (f1
= pow (f1
, f2
), num1
);
310 return make_float (f1
);
313 DEFUN ("log", Flog
, Slog
, 1, 1, 0,
314 "Return the natural logarithm of ARG.")
316 register Lisp_Object num
;
318 double d
= extract_float (num
);
319 IN_FLOAT (d
= log (d
), num
);
320 return make_float (d
);
323 DEFUN ("log10", Flog10
, Slog10
, 1, 1, 0,
324 "Return the logarithm base 10 of ARG.")
326 register Lisp_Object num
;
328 double d
= extract_float (num
);
329 IN_FLOAT (d
= log10 (d
), num
);
330 return make_float (d
);
333 DEFUN ("log1p", Flog1p
, Slog1p
, 1, 1, 0,
334 "Return the log (1+x) of ARG.")
336 register Lisp_Object num
;
338 double d
= extract_float (num
);
339 IN_FLOAT (d
= log1p (d
), num
);
340 return make_float (d
);
343 DEFUN ("sqrt", Fsqrt
, Ssqrt
, 1, 1, 0,
344 "Return the square root of ARG.")
346 register Lisp_Object num
;
348 double d
= extract_float (num
);
349 IN_FLOAT (d
= sqrt (d
), num
);
350 return make_float (d
);
353 #ifndef /* Not clearly worth adding. */
355 DEFUN ("acosh", Facosh
, Sacosh
, 1, 1, 0,
356 "Return the inverse hyperbolic cosine of ARG.")
358 register Lisp_Object num
;
360 double d
= extract_float (num
);
361 IN_FLOAT (d
= acosh (d
), num
);
362 return make_float (d
);
365 DEFUN ("asinh", Fasinh
, Sasinh
, 1, 1, 0,
366 "Return the inverse hyperbolic sine of ARG.")
368 register Lisp_Object num
;
370 double d
= extract_float (num
);
371 IN_FLOAT (d
= asinh (d
), num
);
372 return make_float (d
);
375 DEFUN ("atanh", Fatanh
, Satanh
, 1, 1, 0,
376 "Return the inverse hyperbolic tangent of ARG.")
378 register Lisp_Object num
;
380 double d
= extract_float (num
);
381 IN_FLOAT (d
= atanh (d
), num
);
382 return make_float (d
);
385 DEFUN ("cosh", Fcosh
, Scosh
, 1, 1, 0,
386 "Return the hyperbolic cosine of ARG.")
388 register Lisp_Object num
;
390 double d
= extract_float (num
);
391 IN_FLOAT (d
= cosh (d
), num
);
392 return make_float (d
);
395 DEFUN ("sinh", Fsinh
, Ssinh
, 1, 1, 0,
396 "Return the hyperbolic sine of ARG.")
398 register Lisp_Object num
;
400 double d
= extract_float (num
);
401 IN_FLOAT (d
= sinh (d
), num
);
402 return make_float (d
);
405 DEFUN ("tanh", Ftanh
, Stanh
, 1, 1, 0,
406 "Return the hyperbolic tangent of ARG.")
408 register Lisp_Object num
;
410 double d
= extract_float (num
);
411 IN_FLOAT (d
= tanh (d
), num
);
412 return make_float (d
);
416 DEFUN ("abs", Fabs
, Sabs
, 1, 1, 0,
417 "Return the absolute value of ARG.")
419 register Lisp_Object num
;
421 CHECK_NUMBER_OR_FLOAT (num
, 0);
423 if (XTYPE (num
) == Lisp_Float
)
424 IN_FLOAT (num
= make_float (fabs (XFLOAT (num
)->data
)), num
);
425 else if (XINT (num
) < 0)
426 XSETINT (num
, - XFASTINT (num
));
431 DEFUN ("float", Ffloat
, Sfloat
, 1, 1, 0,
432 "Return the floating point number equal to ARG.")
434 register Lisp_Object num
;
436 CHECK_NUMBER_OR_FLOAT (num
, 0);
438 if (XTYPE (num
) == Lisp_Int
)
439 return make_float ((double) XINT (num
));
440 else /* give 'em the same float back */
444 DEFUN ("logb", Flogb
, Slogb
, 1, 1, 0,
445 "Returns the integer that is the base 2 log of ARG.\n\
446 This is the same as the exponent of a float.")
453 CHECK_NUMBER_OR_FLOAT (num
, 0);
454 f
= (XTYPE (num
) == Lisp_Float
) ? XFLOAT (num
)->data
: XINT (num
);
455 IN_FLOAT (val
= logb (f
), num
);
456 XSET (val
, Lisp_Int
, val
);
460 /* the rounding functions */
462 DEFUN ("ceiling", Fceiling
, Sceiling
, 1, 1, 0,
463 "Return the smallest integer no less than ARG. (Round toward +inf.)")
465 register Lisp_Object num
;
467 CHECK_NUMBER_OR_FLOAT (num
, 0);
469 if (XTYPE (num
) == Lisp_Float
)
470 IN_FLOAT (XSET (num
, Lisp_Int
, ceil (XFLOAT (num
)->data
)), num
);
475 DEFUN ("floor", Ffloor
, Sfloor
, 1, 1, 0,
476 "Return the largest integer no greater than ARG. (Round towards -inf.)")
478 register Lisp_Object num
;
480 CHECK_NUMBER_OR_FLOAT (num
, 0);
482 if (XTYPE (num
) == Lisp_Float
)
483 IN_FLOAT (XSET (num
, Lisp_Int
, floor (XFLOAT (num
)->data
)), num
);
488 DEFUN ("round", Fround
, Sround
, 1, 1, 0,
489 "Return the nearest integer to ARG.")
491 register Lisp_Object num
;
493 CHECK_NUMBER_OR_FLOAT (num
, 0);
495 if (XTYPE (num
) == Lisp_Float
)
496 IN_FLOAT (XSET (num
, Lisp_Int
, rint (XFLOAT (num
)->data
)), num
);
501 DEFUN ("truncate", Ftruncate
, Struncate
, 1, 1, 0,
502 "Truncate a floating point number to an int.\n\
503 Rounds the value toward zero.")
505 register Lisp_Object num
;
507 CHECK_NUMBER_OR_FLOAT (num
, 0);
509 if (XTYPE (num
) == Lisp_Float
)
510 XSET (num
, Lisp_Int
, (int) XFLOAT (num
)->data
);
520 fatal_error_signal (signo
);
525 #else /* not BSD4_1 */
526 sigsetmask (SIGEMPTYMASK
);
527 #endif /* not BSD4_1 */
529 /* Must reestablish handler each time it is called. */
530 signal (SIGILL
, float_error
);
535 Fsignal (Qarith_error
, Fcons (float_error_arg
, Qnil
));
540 signal (SIGILL
, float_error
);
559 defsubr (&Sbessel_y0
);
560 defsubr (&Sbessel_y1
);
561 defsubr (&Sbessel_yn
);
562 defsubr (&Sbessel_j0
);
563 defsubr (&Sbessel_j1
);
564 defsubr (&Sbessel_jn
);
567 defsubr (&Slog_gamma
);
569 defsubr (&Scube_root
);
584 defsubr (&Struncate
);
587 #else /* not LISP_FLOAT_TYPE */
595 #endif /* not LISP_FLOAT_TYPE */