1 /* Primitive operations on floating point for GNU Emacs Lisp interpreter.
2 Copyright (C) 1988 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 1, 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. */
26 Lisp_Object Qarith_error
;
28 #ifdef LISP_FLOAT_TYPE
35 /* Avoid traps on VMS from sinh and cosh.
36 All the other functions set errno instead. */
41 #define cosh(x) ((exp(x)+exp(-x))*0.5)
42 #define sinh(x) ((exp(x)-exp(-x))*0.5)
45 static float_error ();
47 /* Nonzero while executing in floating point.
48 This tells float_error what to do. */
52 /* If an argument is out of range for a mathematical function,
53 here is the actual argument value to use in the error message. */
55 static Lisp_Object float_error_arg
;
57 /* Evaluate the floating point expression D, recording NUM
58 as the original argument for error messages.
59 D is normally an assignment expression.
60 Handle errors which may result in signals or may set errno. */
62 #define IN_FLOAT(D, NUM) \
63 (in_float = 1, errno = 0, float_error_arg = NUM, (D), \
64 (errno == ERANGE || errno == EDOM ? float_error () : 0), \
67 /* Extract a Lisp number as a `double', or signal an error. */
73 CHECK_NUMBER_OR_FLOAT (num
, 0);
75 if (XTYPE (num
) == Lisp_Float
)
76 return XFLOAT (num
)->data
;
77 return (double) XINT (num
);
80 DEFUN ("acos", Facos
, Sacos
, 1, 1, 0,
81 "Return the inverse cosine of ARG.")
83 register Lisp_Object num
;
85 double d
= extract_float (num
);
86 IN_FLOAT (d
= acos (d
), num
);
87 return make_float (d
);
90 DEFUN ("acosh", Facosh
, Sacosh
, 1, 1, 0,
91 "Return the inverse hyperbolic cosine of ARG.")
93 register Lisp_Object num
;
95 double d
= extract_float (num
);
96 IN_FLOAT (d
= acosh (d
), num
);
97 return make_float (d
);
100 DEFUN ("asin", Fasin
, Sasin
, 1, 1, 0,
101 "Return the inverse sine of ARG.")
103 register Lisp_Object num
;
105 double d
= extract_float (num
);
106 IN_FLOAT (d
= asin (d
), num
);
107 return make_float (d
);
110 DEFUN ("asinh", Fasinh
, Sasinh
, 1, 1, 0,
111 "Return the inverse hyperbolic sine of ARG.")
113 register Lisp_Object num
;
115 double d
= extract_float (num
);
116 IN_FLOAT (d
= asinh (d
), num
);
117 return make_float (d
);
120 DEFUN ("atan", Fatan
, Satan
, 1, 1, 0,
121 "Return the inverse tangent of ARG.")
123 register Lisp_Object num
;
125 double d
= extract_float (num
);
126 IN_FLOAT (d
= atan (d
), num
);
127 return make_float (d
);
130 DEFUN ("atanh", Fatanh
, Satanh
, 1, 1, 0,
131 "Return the inverse hyperbolic tangent of ARG.")
133 register Lisp_Object num
;
135 double d
= extract_float (num
);
136 IN_FLOAT (d
= atanh (d
), num
);
137 return make_float (d
);
140 DEFUN ("bessel-j0", Fbessel_j0
, Sbessel_j0
, 1, 1, 0,
141 "Return the bessel function j0 of ARG.")
143 register Lisp_Object num
;
145 double d
= extract_float (num
);
146 IN_FLOAT (d
= j0 (d
), num
);
147 return make_float (d
);
150 DEFUN ("bessel-j1", Fbessel_j1
, Sbessel_j1
, 1, 1, 0,
151 "Return the bessel function j1 of ARG.")
153 register Lisp_Object num
;
155 double d
= extract_float (num
);
156 IN_FLOAT (d
= j1 (d
), num
);
157 return make_float (d
);
160 DEFUN ("bessel-jn", Fbessel_jn
, Sbessel_jn
, 2, 2, 0,
161 "Return the order N bessel function output jn of ARG.\n\
162 The first arg (the order) is truncated to an integer.")
164 register Lisp_Object num1
, num2
;
166 int i1
= extract_float (num1
);
167 double f2
= extract_float (num2
);
169 IN_FLOAT (f2
= jn (i1
, f2
), num1
);
170 return make_float (f2
);
173 DEFUN ("bessel-y0", Fbessel_y0
, Sbessel_y0
, 1, 1, 0,
174 "Return the bessel function y0 of ARG.")
176 register Lisp_Object num
;
178 double d
= extract_float (num
);
179 IN_FLOAT (d
= y0 (d
), num
);
180 return make_float (d
);
183 DEFUN ("bessel-y1", Fbessel_y1
, Sbessel_y1
, 1, 1, 0,
184 "Return the bessel function y1 of ARG.")
186 register Lisp_Object num
;
188 double d
= extract_float (num
);
189 IN_FLOAT (d
= y1 (d
), num
);
190 return make_float (d
);
193 DEFUN ("bessel-yn", Fbessel_yn
, Sbessel_yn
, 2, 2, 0,
194 "Return the order N bessel function output yn of ARG.\n\
195 The first arg (the order) is truncated to an integer.")
197 register Lisp_Object num1
, num2
;
199 int i1
= extract_float (num1
);
200 double f2
= extract_float (num2
);
202 IN_FLOAT (f2
= yn (i1
, f2
), num1
);
203 return make_float (f2
);
206 DEFUN ("cube-root", Fcube_root
, Scube_root
, 1, 1, 0,
207 "Return the cube root of ARG.")
209 register Lisp_Object num
;
211 double d
= extract_float (num
);
212 IN_FLOAT (d
= cbrt (d
), num
);
213 return make_float (d
);
216 DEFUN ("cos", Fcos
, Scos
, 1, 1, 0,
217 "Return the cosine of ARG.")
219 register Lisp_Object num
;
221 double d
= extract_float (num
);
222 IN_FLOAT (d
= cos (d
), num
);
223 return make_float (d
);
226 DEFUN ("cosh", Fcosh
, Scosh
, 1, 1, 0,
227 "Return the hyperbolic cosine of ARG.")
229 register Lisp_Object num
;
231 double d
= extract_float (num
);
232 IN_FLOAT (d
= cosh (d
), num
);
233 return make_float (d
);
236 DEFUN ("erf", Ferf
, Serf
, 1, 1, 0,
237 "Return the mathematical error function of ARG.")
239 register Lisp_Object num
;
241 double d
= extract_float (num
);
242 IN_FLOAT (d
= erf (d
), num
);
243 return make_float (d
);
246 DEFUN ("erfc", Ferfc
, Serfc
, 1, 1, 0,
247 "Return the complementary error function of ARG.")
249 register Lisp_Object num
;
251 double d
= extract_float (num
);
252 IN_FLOAT (d
= erfc (d
), num
);
253 return make_float (d
);
256 DEFUN ("exp", Fexp
, Sexp
, 1, 1, 0,
257 "Return the exponential base e of ARG.")
259 register Lisp_Object num
;
261 double d
= extract_float (num
);
262 IN_FLOAT (d
= exp (d
), num
);
263 return make_float (d
);
266 DEFUN ("expm1", Fexpm1
, Sexpm1
, 1, 1, 0,
267 "Return the exp (x)-1 of ARG.")
269 register Lisp_Object num
;
271 double d
= extract_float (num
);
272 IN_FLOAT (d
= expm1 (d
), num
);
273 return make_float (d
);
276 DEFUN ("log-gamma", Flog_gamma
, Slog_gamma
, 1, 1, 0,
277 "Return the log gamma of ARG.")
279 register Lisp_Object num
;
281 double d
= extract_float (num
);
282 IN_FLOAT (d
= lgamma (d
), num
);
283 return make_float (d
);
286 DEFUN ("log", Flog
, Slog
, 1, 1, 0,
287 "Return the natural logarithm of ARG.")
289 register Lisp_Object num
;
291 double d
= extract_float (num
);
292 IN_FLOAT (d
= log (d
), num
);
293 return make_float (d
);
296 DEFUN ("log10", Flog10
, Slog10
, 1, 1, 0,
297 "Return the logarithm base 10 of ARG.")
299 register Lisp_Object num
;
301 double d
= extract_float (num
);
302 IN_FLOAT (d
= log10 (d
), num
);
303 return make_float (d
);
306 DEFUN ("log1p", Flog1p
, Slog1p
, 1, 1, 0,
307 "Return the log (1+x) of ARG.")
309 register Lisp_Object num
;
311 double d
= extract_float (num
);
312 IN_FLOAT (d
= log1p (d
), num
);
313 return make_float (d
);
316 DEFUN ("expt", Fexpt
, Sexpt
, 2, 2, 0,
317 "Return the exponential x ** y.")
319 register Lisp_Object num1
, num2
;
323 CHECK_NUMBER_OR_FLOAT (num1
, 0);
324 CHECK_NUMBER_OR_FLOAT (num2
, 0);
325 if ((XTYPE (num1
) == Lisp_Int
) && /* common lisp spec */
326 (XTYPE (num2
) == Lisp_Int
)) /* don't promote, if both are ints */
327 { /* this can be improved by pre-calculating */
328 int acc
, x
, y
; /* some binary powers of x then acumulating */
329 /* these, therby saving some time. -wsr */
344 return XSET (x
, Lisp_Int
, acc
);
346 f1
= (XTYPE (num1
) == Lisp_Float
) ? XFLOAT (num1
)->data
: XINT (num1
);
347 f2
= (XTYPE (num2
) == Lisp_Float
) ? XFLOAT (num2
)->data
: XINT (num2
);
348 IN_FLOAT (f1
= pow (f1
, f2
), num1
);
349 return make_float (f1
);
352 DEFUN ("sin", Fsin
, Ssin
, 1, 1, 0,
353 "Return the sine of ARG.")
355 register Lisp_Object num
;
357 double d
= extract_float (num
);
358 IN_FLOAT (d
= sin (d
), num
);
359 return make_float (d
);
362 DEFUN ("sinh", Fsinh
, Ssinh
, 1, 1, 0,
363 "Return the hyperbolic sine of ARG.")
365 register Lisp_Object num
;
367 double d
= extract_float (num
);
368 IN_FLOAT (d
= sinh (d
), num
);
369 return make_float (d
);
372 DEFUN ("sqrt", Fsqrt
, Ssqrt
, 1, 1, 0,
373 "Return the square root of ARG.")
375 register Lisp_Object num
;
377 double d
= extract_float (num
);
378 IN_FLOAT (d
= sqrt (d
), num
);
379 return make_float (d
);
382 DEFUN ("tan", Ftan
, Stan
, 1, 1, 0,
383 "Return the tangent of ARG.")
385 register Lisp_Object num
;
387 double d
= extract_float (num
);
388 IN_FLOAT (d
= tan (d
), num
);
389 return make_float (d
);
392 DEFUN ("tanh", Ftanh
, Stanh
, 1, 1, 0,
393 "Return the hyperbolic tangent of ARG.")
395 register Lisp_Object num
;
397 double d
= extract_float (num
);
398 IN_FLOAT (d
= tanh (d
), num
);
399 return make_float (d
);
402 DEFUN ("abs", Fabs
, Sabs
, 1, 1, 0,
403 "Return the absolute value of ARG.")
405 register Lisp_Object num
;
407 CHECK_NUMBER_OR_FLOAT (num
, 0);
409 if (XTYPE (num
) == Lisp_Float
)
410 IN_FLOAT (num
= make_float (fabs (XFLOAT (num
)->data
)), num
);
411 else if (XINT (num
) < 0)
412 XSETINT (num
, - XFASTINT (num
));
417 DEFUN ("float", Ffloat
, Sfloat
, 1, 1, 0,
418 "Return the floating point number equal to ARG.")
420 register Lisp_Object num
;
422 CHECK_NUMBER_OR_FLOAT (num
, 0);
424 if (XTYPE (num
) == Lisp_Int
)
425 return make_float ((double) XINT (num
));
426 else /* give 'em the same float back */
430 DEFUN ("logb", Flogb
, Slogb
, 1, 1, 0,
431 "Returns the integer that is the base 2 log of ARG.\n\
432 This is the same as the exponent of a float.")
439 CHECK_NUMBER_OR_FLOAT (num
, 0);
440 f
= (XTYPE (num
) == Lisp_Float
) ? XFLOAT (num
)->data
: XINT (num
);
441 IN_FLOAT (val
= logb (f
), num
);
442 XSET (val
, Lisp_Int
, val
);
446 /* the rounding functions */
448 DEFUN ("ceiling", Fceiling
, Sceiling
, 1, 1, 0,
449 "Return the smallest integer no less than ARG. (Round toward +inf.)")
451 register Lisp_Object num
;
453 CHECK_NUMBER_OR_FLOAT (num
, 0);
455 if (XTYPE (num
) == Lisp_Float
)
456 IN_FLOAT (XSET (num
, Lisp_Int
, ceil (XFLOAT (num
)->data
)), num
);
461 DEFUN ("floor", Ffloor
, Sfloor
, 1, 1, 0,
462 "Return the largest integer no greater than ARG. (Round towards -inf.)")
464 register Lisp_Object num
;
466 CHECK_NUMBER_OR_FLOAT (num
, 0);
468 if (XTYPE (num
) == Lisp_Float
)
469 IN_FLOAT (XSET (num
, Lisp_Int
, floor (XFLOAT (num
)->data
)), num
);
474 DEFUN ("round", Fround
, Sround
, 1, 1, 0,
475 "Return the nearest integer to ARG.")
477 register Lisp_Object num
;
479 CHECK_NUMBER_OR_FLOAT (num
, 0);
481 if (XTYPE (num
) == Lisp_Float
)
482 IN_FLOAT (XSET (num
, Lisp_Int
, rint (XFLOAT (num
)->data
)), num
);
487 DEFUN ("truncate", Ftruncate
, Struncate
, 1, 1, 0,
488 "Truncate a floating point number to an int.\n\
489 Rounds the value toward zero.")
491 register Lisp_Object num
;
493 CHECK_NUMBER_OR_FLOAT (num
, 0);
495 if (XTYPE (num
) == Lisp_Float
)
496 XSET (num
, Lisp_Int
, (int) XFLOAT (num
)->data
);
506 fatal_error_signal (signo
);
511 #else /* not BSD4_1 */
513 #endif /* not BSD4_1 */
515 /* Must reestablish handler each time it is called. */
516 signal (SIGILL
, float_error
);
521 Fsignal (Qarith_error
, Fcons (float_error_arg
, Qnil
));
526 signal (SIGILL
, float_error
);
538 defsubr (&Sbessel_y0
);
539 defsubr (&Sbessel_y1
);
540 defsubr (&Sbessel_yn
);
541 defsubr (&Sbessel_j0
);
542 defsubr (&Sbessel_j1
);
543 defsubr (&Sbessel_jn
);
544 defsubr (&Scube_root
);
551 defsubr (&Slog_gamma
);
568 defsubr (&Struncate
);
571 #else /* not LISP_FLOAT_TYPE */
579 #endif /* not LISP_FLOAT_TYPE */