1 /* Primitive operations on floating point for GNU Emacs Lisp interpreter.
2 Copyright (C) 1988, 1993, 1994, 1999 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* ANSI C requires only these float functions:
23 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, fmod,
24 frexp, ldexp, log, log10, modf, pow, sin, sinh, sqrt, tan, tanh.
26 Define HAVE_INVERSE_HYPERBOLIC if you have acosh, asinh, and atanh.
27 Define HAVE_CBRT if you have cbrt.
28 Define HAVE_RINT if you have a working rint.
29 If you don't define these, then the appropriate routines will be simulated.
31 Define HAVE_MATHERR if on a system supporting the SysV matherr callback.
32 (This should happen automatically.)
34 Define FLOAT_CHECK_ERRNO if the float library routines set errno.
35 This has no effect if HAVE_MATHERR is defined.
37 Define FLOAT_CATCH_SIGILL if the float library routines signal SIGILL.
38 (What systems actually do this? Please let us know.)
40 Define FLOAT_CHECK_DOMAIN if the float library doesn't handle errors by
41 either setting errno, or signaling SIGFPE/SIGILL. Otherwise, domain and
42 range checking will happen before calling the float routines. This has
43 no effect if HAVE_MATHERR is defined (since matherr will be called when
44 a domain error occurs.)
50 #include "syssignal.h"
56 /* If IEEE_FLOATING_POINT isn't defined, default it from FLT_*. */
57 #ifndef IEEE_FLOATING_POINT
58 #if (FLT_RADIX == 2 && FLT_MANT_DIG == 24 \
59 && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128)
60 #define IEEE_FLOATING_POINT 1
62 #define IEEE_FLOATING_POINT 0
66 /* Work around a problem that happens because math.h on hpux 7
67 defines two static variables--which, in Emacs, are not really static,
68 because `static' is defined as nothing. The problem is that they are
69 defined both here and in lread.c.
70 These macros prevent the name conflict. */
71 #if defined (HPUX) && !defined (HPUX8)
72 #define _MAXLDBL floatfns_maxldbl
73 #define _NMAXLDBL floatfns_nmaxldbl
78 /* This declaration is omitted on some systems, like Ultrix. */
79 #if !defined (HPUX) && defined (HAVE_LOGB) && !defined (logb)
80 extern double logb ();
81 #endif /* not HPUX and HAVE_LOGB and no logb macro */
83 #if defined(DOMAIN) && defined(SING) && defined(OVERFLOW)
84 /* If those are defined, then this is probably a `matherr' machine. */
95 # ifdef FLOAT_CHECK_ERRNO
96 # undef FLOAT_CHECK_ERRNO
98 # ifdef FLOAT_CHECK_DOMAIN
99 # undef FLOAT_CHECK_DOMAIN
103 #ifndef NO_FLOAT_CHECK_ERRNO
104 #define FLOAT_CHECK_ERRNO
107 #ifdef FLOAT_CHECK_ERRNO
115 /* Avoid traps on VMS from sinh and cosh.
116 All the other functions set errno instead. */
121 #define cosh(x) ((exp(x)+exp(-x))*0.5)
122 #define sinh(x) ((exp(x)-exp(-x))*0.5)
125 #ifdef FLOAT_CATCH_SIGILL
126 static SIGTYPE
float_error ();
129 /* Nonzero while executing in floating point.
130 This tells float_error what to do. */
134 /* If an argument is out of range for a mathematical function,
135 here is the actual argument value to use in the error message.
136 These variables are used only across the floating point library call
137 so there is no need to staticpro them. */
139 static Lisp_Object float_error_arg
, float_error_arg2
;
141 static char *float_error_fn_name
;
143 /* Evaluate the floating point expression D, recording NUM
144 as the original argument for error messages.
145 D is normally an assignment expression.
146 Handle errors which may result in signals or may set errno.
148 Note that float_error may be declared to return void, so you can't
149 just cast the zero after the colon to (SIGTYPE) to make the types
152 #ifdef FLOAT_CHECK_ERRNO
153 #define IN_FLOAT(d, name, num) \
155 float_error_arg = num; \
156 float_error_fn_name = name; \
157 in_float = 1; errno = 0; (d); in_float = 0; \
160 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
161 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
162 default: arith_error (float_error_fn_name, float_error_arg); \
165 #define IN_FLOAT2(d, name, num, num2) \
167 float_error_arg = num; \
168 float_error_arg2 = num2; \
169 float_error_fn_name = name; \
170 in_float = 1; errno = 0; (d); in_float = 0; \
173 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
174 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
175 default: arith_error (float_error_fn_name, float_error_arg); \
179 #define IN_FLOAT(d, name, num) (in_float = 1, (d), in_float = 0)
180 #define IN_FLOAT2(d, name, num, num2) (in_float = 1, (d), in_float = 0)
183 /* Convert float to Lisp_Int if it fits, else signal a range error
184 using the given arguments. */
185 #define FLOAT_TO_INT(x, i, name, num) \
188 if (FIXNUM_OVERFLOW_P (x)) \
189 range_error (name, num); \
190 XSETINT (i, (EMACS_INT)(x)); \
193 #define FLOAT_TO_INT2(x, i, name, num1, num2) \
196 if (FIXNUM_OVERFLOW_P (x)) \
197 range_error2 (name, num1, num2); \
198 XSETINT (i, (EMACS_INT)(x)); \
202 #define arith_error(op,arg) \
203 Fsignal (Qarith_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
204 #define range_error(op,arg) \
205 Fsignal (Qrange_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
206 #define range_error2(op,a1,a2) \
207 Fsignal (Qrange_error, Fcons (build_string ((op)), \
208 Fcons ((a1), Fcons ((a2), Qnil))))
209 #define domain_error(op,arg) \
210 Fsignal (Qdomain_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
211 #define domain_error2(op,a1,a2) \
212 Fsignal (Qdomain_error, Fcons (build_string ((op)), \
213 Fcons ((a1), Fcons ((a2), Qnil))))
215 /* Extract a Lisp number as a `double', or signal an error. */
221 CHECK_NUMBER_OR_FLOAT (num
);
224 return XFLOAT_DATA (num
);
225 return (double) XINT (num
);
228 /* Trig functions. */
230 DEFUN ("acos", Facos
, Sacos
, 1, 1, 0,
231 doc
: /* Return the inverse cosine of ARG. */)
233 register Lisp_Object arg
;
235 double d
= extract_float (arg
);
236 #ifdef FLOAT_CHECK_DOMAIN
237 if (d
> 1.0 || d
< -1.0)
238 domain_error ("acos", arg
);
240 IN_FLOAT (d
= acos (d
), "acos", arg
);
241 return make_float (d
);
244 DEFUN ("asin", Fasin
, Sasin
, 1, 1, 0,
245 doc
: /* Return the inverse sine of ARG. */)
247 register Lisp_Object arg
;
249 double d
= extract_float (arg
);
250 #ifdef FLOAT_CHECK_DOMAIN
251 if (d
> 1.0 || d
< -1.0)
252 domain_error ("asin", arg
);
254 IN_FLOAT (d
= asin (d
), "asin", arg
);
255 return make_float (d
);
258 DEFUN ("atan", Fatan
, Satan
, 1, 1, 0,
259 doc
: /* Return the inverse tangent of ARG. */)
261 register Lisp_Object arg
;
263 double d
= extract_float (arg
);
264 IN_FLOAT (d
= atan (d
), "atan", arg
);
265 return make_float (d
);
268 DEFUN ("cos", Fcos
, Scos
, 1, 1, 0,
269 doc
: /* Return the cosine of ARG. */)
271 register Lisp_Object arg
;
273 double d
= extract_float (arg
);
274 IN_FLOAT (d
= cos (d
), "cos", arg
);
275 return make_float (d
);
278 DEFUN ("sin", Fsin
, Ssin
, 1, 1, 0,
279 doc
: /* Return the sine of ARG. */)
281 register Lisp_Object arg
;
283 double d
= extract_float (arg
);
284 IN_FLOAT (d
= sin (d
), "sin", arg
);
285 return make_float (d
);
288 DEFUN ("tan", Ftan
, Stan
, 1, 1, 0,
289 doc
: /* Return the tangent of ARG. */)
291 register Lisp_Object arg
;
293 double d
= extract_float (arg
);
295 #ifdef FLOAT_CHECK_DOMAIN
297 domain_error ("tan", arg
);
299 IN_FLOAT (d
= sin (d
) / c
, "tan", arg
);
300 return make_float (d
);
303 #if 0 /* Leave these out unless we find there's a reason for them. */
305 DEFUN ("bessel-j0", Fbessel_j0
, Sbessel_j0
, 1, 1, 0,
306 doc
: /* Return the bessel function j0 of ARG. */)
308 register Lisp_Object arg
;
310 double d
= extract_float (arg
);
311 IN_FLOAT (d
= j0 (d
), "bessel-j0", arg
);
312 return make_float (d
);
315 DEFUN ("bessel-j1", Fbessel_j1
, Sbessel_j1
, 1, 1, 0,
316 doc
: /* Return the bessel function j1 of ARG. */)
318 register Lisp_Object arg
;
320 double d
= extract_float (arg
);
321 IN_FLOAT (d
= j1 (d
), "bessel-j1", arg
);
322 return make_float (d
);
325 DEFUN ("bessel-jn", Fbessel_jn
, Sbessel_jn
, 2, 2, 0,
326 doc
: /* Return the order N bessel function output jn of ARG.
327 The first arg (the order) is truncated to an integer. */)
329 register Lisp_Object n
, arg
;
331 int i1
= extract_float (n
);
332 double f2
= extract_float (arg
);
334 IN_FLOAT (f2
= jn (i1
, f2
), "bessel-jn", n
);
335 return make_float (f2
);
338 DEFUN ("bessel-y0", Fbessel_y0
, Sbessel_y0
, 1, 1, 0,
339 doc
: /* Return the bessel function y0 of ARG. */)
341 register Lisp_Object arg
;
343 double d
= extract_float (arg
);
344 IN_FLOAT (d
= y0 (d
), "bessel-y0", arg
);
345 return make_float (d
);
348 DEFUN ("bessel-y1", Fbessel_y1
, Sbessel_y1
, 1, 1, 0,
349 doc
: /* Return the bessel function y1 of ARG. */)
351 register Lisp_Object arg
;
353 double d
= extract_float (arg
);
354 IN_FLOAT (d
= y1 (d
), "bessel-y0", arg
);
355 return make_float (d
);
358 DEFUN ("bessel-yn", Fbessel_yn
, Sbessel_yn
, 2, 2, 0,
359 doc
: /* Return the order N bessel function output yn of ARG.
360 The first arg (the order) is truncated to an integer. */)
362 register Lisp_Object n
, arg
;
364 int i1
= extract_float (n
);
365 double f2
= extract_float (arg
);
367 IN_FLOAT (f2
= yn (i1
, f2
), "bessel-yn", n
);
368 return make_float (f2
);
373 #if 0 /* Leave these out unless we see they are worth having. */
375 DEFUN ("erf", Ferf
, Serf
, 1, 1, 0,
376 doc
: /* Return the mathematical error function of ARG. */)
378 register Lisp_Object arg
;
380 double d
= extract_float (arg
);
381 IN_FLOAT (d
= erf (d
), "erf", arg
);
382 return make_float (d
);
385 DEFUN ("erfc", Ferfc
, Serfc
, 1, 1, 0,
386 doc
: /* Return the complementary error function of ARG. */)
388 register Lisp_Object arg
;
390 double d
= extract_float (arg
);
391 IN_FLOAT (d
= erfc (d
), "erfc", arg
);
392 return make_float (d
);
395 DEFUN ("log-gamma", Flog_gamma
, Slog_gamma
, 1, 1, 0,
396 doc
: /* Return the log gamma of ARG. */)
398 register Lisp_Object arg
;
400 double d
= extract_float (arg
);
401 IN_FLOAT (d
= lgamma (d
), "log-gamma", arg
);
402 return make_float (d
);
405 DEFUN ("cube-root", Fcube_root
, Scube_root
, 1, 1, 0,
406 doc
: /* Return the cube root of ARG. */)
408 register Lisp_Object arg
;
410 double d
= extract_float (arg
);
412 IN_FLOAT (d
= cbrt (d
), "cube-root", arg
);
415 IN_FLOAT (d
= pow (d
, 1.0/3.0), "cube-root", arg
);
417 IN_FLOAT (d
= -pow (-d
, 1.0/3.0), "cube-root", arg
);
419 return make_float (d
);
424 DEFUN ("exp", Fexp
, Sexp
, 1, 1, 0,
425 doc
: /* Return the exponential base e of ARG. */)
427 register Lisp_Object arg
;
429 double d
= extract_float (arg
);
430 #ifdef FLOAT_CHECK_DOMAIN
431 if (d
> 709.7827) /* Assume IEEE doubles here */
432 range_error ("exp", arg
);
434 return make_float (0.0);
437 IN_FLOAT (d
= exp (d
), "exp", arg
);
438 return make_float (d
);
441 DEFUN ("expt", Fexpt
, Sexpt
, 2, 2, 0,
442 doc
: /* Return the exponential ARG1 ** ARG2. */)
444 register Lisp_Object arg1
, arg2
;
448 CHECK_NUMBER_OR_FLOAT (arg1
);
449 CHECK_NUMBER_OR_FLOAT (arg2
);
450 if (INTEGERP (arg1
) /* common lisp spec */
451 && INTEGERP (arg2
)) /* don't promote, if both are ints */
452 { /* this can be improved by pre-calculating */
453 EMACS_INT acc
, x
, y
; /* some binary powers of x then accumulating */
465 acc
= (y
& 1) ? -1 : 1;
476 y
= (unsigned)y
>> 1;
482 f1
= FLOATP (arg1
) ? XFLOAT_DATA (arg1
) : XINT (arg1
);
483 f2
= FLOATP (arg2
) ? XFLOAT_DATA (arg2
) : XINT (arg2
);
484 /* Really should check for overflow, too */
485 if (f1
== 0.0 && f2
== 0.0)
487 #ifdef FLOAT_CHECK_DOMAIN
488 else if ((f1
== 0.0 && f2
< 0.0) || (f1
< 0 && f2
!= floor(f2
)))
489 domain_error2 ("expt", arg1
, arg2
);
491 IN_FLOAT2 (f1
= pow (f1
, f2
), "expt", arg1
, arg2
);
492 return make_float (f1
);
495 DEFUN ("log", Flog
, Slog
, 1, 2, 0,
496 doc
: /* Return the natural logarithm of ARG.
497 If second optional argument BASE is given, return log ARG using that base. */)
499 register Lisp_Object arg
, base
;
501 double d
= extract_float (arg
);
503 #ifdef FLOAT_CHECK_DOMAIN
505 domain_error2 ("log", arg
, base
);
508 IN_FLOAT (d
= log (d
), "log", arg
);
511 double b
= extract_float (base
);
513 #ifdef FLOAT_CHECK_DOMAIN
514 if (b
<= 0.0 || b
== 1.0)
515 domain_error2 ("log", arg
, base
);
518 IN_FLOAT2 (d
= log10 (d
), "log", arg
, base
);
520 IN_FLOAT2 (d
= log (d
) / log (b
), "log", arg
, base
);
522 return make_float (d
);
525 DEFUN ("log10", Flog10
, Slog10
, 1, 1, 0,
526 doc
: /* Return the logarithm base 10 of ARG. */)
528 register Lisp_Object arg
;
530 double d
= extract_float (arg
);
531 #ifdef FLOAT_CHECK_DOMAIN
533 domain_error ("log10", arg
);
535 IN_FLOAT (d
= log10 (d
), "log10", arg
);
536 return make_float (d
);
539 DEFUN ("sqrt", Fsqrt
, Ssqrt
, 1, 1, 0,
540 doc
: /* Return the square root of ARG. */)
542 register Lisp_Object arg
;
544 double d
= extract_float (arg
);
545 #ifdef FLOAT_CHECK_DOMAIN
547 domain_error ("sqrt", arg
);
549 IN_FLOAT (d
= sqrt (d
), "sqrt", arg
);
550 return make_float (d
);
553 #if 0 /* Not clearly worth adding. */
555 DEFUN ("acosh", Facosh
, Sacosh
, 1, 1, 0,
556 doc
: /* Return the inverse hyperbolic cosine of ARG. */)
558 register Lisp_Object arg
;
560 double d
= extract_float (arg
);
561 #ifdef FLOAT_CHECK_DOMAIN
563 domain_error ("acosh", arg
);
565 #ifdef HAVE_INVERSE_HYPERBOLIC
566 IN_FLOAT (d
= acosh (d
), "acosh", arg
);
568 IN_FLOAT (d
= log (d
+ sqrt (d
*d
- 1.0)), "acosh", arg
);
570 return make_float (d
);
573 DEFUN ("asinh", Fasinh
, Sasinh
, 1, 1, 0,
574 doc
: /* Return the inverse hyperbolic sine of ARG. */)
576 register Lisp_Object arg
;
578 double d
= extract_float (arg
);
579 #ifdef HAVE_INVERSE_HYPERBOLIC
580 IN_FLOAT (d
= asinh (d
), "asinh", arg
);
582 IN_FLOAT (d
= log (d
+ sqrt (d
*d
+ 1.0)), "asinh", arg
);
584 return make_float (d
);
587 DEFUN ("atanh", Fatanh
, Satanh
, 1, 1, 0,
588 doc
: /* Return the inverse hyperbolic tangent of ARG. */)
590 register Lisp_Object arg
;
592 double d
= extract_float (arg
);
593 #ifdef FLOAT_CHECK_DOMAIN
594 if (d
>= 1.0 || d
<= -1.0)
595 domain_error ("atanh", arg
);
597 #ifdef HAVE_INVERSE_HYPERBOLIC
598 IN_FLOAT (d
= atanh (d
), "atanh", arg
);
600 IN_FLOAT (d
= 0.5 * log ((1.0 + d
) / (1.0 - d
)), "atanh", arg
);
602 return make_float (d
);
605 DEFUN ("cosh", Fcosh
, Scosh
, 1, 1, 0,
606 doc
: /* Return the hyperbolic cosine of ARG. */)
608 register Lisp_Object arg
;
610 double d
= extract_float (arg
);
611 #ifdef FLOAT_CHECK_DOMAIN
612 if (d
> 710.0 || d
< -710.0)
613 range_error ("cosh", arg
);
615 IN_FLOAT (d
= cosh (d
), "cosh", arg
);
616 return make_float (d
);
619 DEFUN ("sinh", Fsinh
, Ssinh
, 1, 1, 0,
620 doc
: /* Return the hyperbolic sine of ARG. */)
622 register Lisp_Object arg
;
624 double d
= extract_float (arg
);
625 #ifdef FLOAT_CHECK_DOMAIN
626 if (d
> 710.0 || d
< -710.0)
627 range_error ("sinh", arg
);
629 IN_FLOAT (d
= sinh (d
), "sinh", arg
);
630 return make_float (d
);
633 DEFUN ("tanh", Ftanh
, Stanh
, 1, 1, 0,
634 doc
: /* Return the hyperbolic tangent of ARG. */)
636 register Lisp_Object arg
;
638 double d
= extract_float (arg
);
639 IN_FLOAT (d
= tanh (d
), "tanh", arg
);
640 return make_float (d
);
644 DEFUN ("abs", Fabs
, Sabs
, 1, 1, 0,
645 doc
: /* Return the absolute value of ARG. */)
647 register Lisp_Object arg
;
649 CHECK_NUMBER_OR_FLOAT (arg
);
652 IN_FLOAT (arg
= make_float (fabs (XFLOAT_DATA (arg
))), "abs", arg
);
653 else if (XINT (arg
) < 0)
654 XSETINT (arg
, - XINT (arg
));
659 DEFUN ("float", Ffloat
, Sfloat
, 1, 1, 0,
660 doc
: /* Return the floating point number equal to ARG. */)
662 register Lisp_Object arg
;
664 CHECK_NUMBER_OR_FLOAT (arg
);
667 return make_float ((double) XINT (arg
));
668 else /* give 'em the same float back */
672 DEFUN ("logb", Flogb
, Slogb
, 1, 1, 0,
673 doc
: /* Returns largest integer <= the base 2 log of the magnitude of ARG.
674 This is the same as the exponent of a float. */)
680 double f
= extract_float (arg
);
683 value
= -(VALMASK
>> 1);
687 IN_FLOAT (value
= logb (f
), "logb", arg
);
691 IN_FLOAT (frexp (f
, &ivalue
), "logb", arg
);
701 for (i
= 1, d
= 0.5; d
* d
>= f
; i
+= i
)
708 for (i
= 1, d
= 2.0; d
* d
<= f
; i
+= i
)
716 XSETINT (val
, value
);
721 /* the rounding functions */
724 rounding_driver (arg
, divisor
, double_round
, int_round2
, name
)
725 register Lisp_Object arg
, divisor
;
726 double (*double_round
) ();
727 EMACS_INT (*int_round2
) ();
730 CHECK_NUMBER_OR_FLOAT (arg
);
732 if (! NILP (divisor
))
736 CHECK_NUMBER_OR_FLOAT (divisor
);
738 if (FLOATP (arg
) || FLOATP (divisor
))
742 f1
= FLOATP (arg
) ? XFLOAT_DATA (arg
) : XINT (arg
);
743 f2
= (FLOATP (divisor
) ? XFLOAT_DATA (divisor
) : XINT (divisor
));
744 if (! IEEE_FLOATING_POINT
&& f2
== 0)
745 Fsignal (Qarith_error
, Qnil
);
747 IN_FLOAT2 (f1
= (*double_round
) (f1
/ f2
), name
, arg
, divisor
);
748 FLOAT_TO_INT2 (f1
, arg
, name
, arg
, divisor
);
756 Fsignal (Qarith_error
, Qnil
);
758 XSETINT (arg
, (*int_round2
) (i1
, i2
));
766 IN_FLOAT (d
= (*double_round
) (XFLOAT_DATA (arg
)), name
, arg
);
767 FLOAT_TO_INT (d
, arg
, name
, arg
);
773 /* With C's /, the result is implementation-defined if either operand
774 is negative, so take care with negative operands in the following
775 integer functions. */
782 ? (i1
< 0 ? ((-1 - i1
) / -i2
) + 1 : - (i1
/ -i2
))
783 : (i1
<= 0 ? - (-i1
/ i2
) : ((i1
- 1) / i2
) + 1));
791 ? (i1
<= 0 ? -i1
/ -i2
: -1 - ((i1
- 1) / -i2
))
792 : (i1
< 0 ? -1 - ((-1 - i1
) / i2
) : i1
/ i2
));
800 ? (i1
< 0 ? -i1
/ -i2
: - (i1
/ -i2
))
801 : (i1
< 0 ? - (-i1
/ i2
) : i1
/ i2
));
808 /* The C language's division operator gives us one remainder R, but
809 we want the remainder R1 on the other side of 0 if R1 is closer
810 to 0 than R is; because we want to round to even, we also want R1
811 if R and R1 are the same distance from 0 and if C's quotient is
813 EMACS_INT q
= i1
/ i2
;
814 EMACS_INT r
= i1
% i2
;
815 EMACS_INT abs_r
= r
< 0 ? -r
: r
;
816 EMACS_INT abs_r1
= (i2
< 0 ? -i2
: i2
) - abs_r
;
817 return q
+ (abs_r
+ (q
& 1) <= abs_r1
? 0 : (i2
^ r
) < 0 ? -1 : 1);
820 /* The code uses emacs_rint, so that it works to undefine HAVE_RINT
821 if `rint' exists but does not work right. */
823 #define emacs_rint rint
829 return floor (d
+ 0.5);
840 DEFUN ("ceiling", Fceiling
, Sceiling
, 1, 2, 0,
841 doc
: /* Return the smallest integer no less than ARG.
842 This rounds the value towards +inf.
843 With optional DIVISOR, return the smallest integer no less than ARG/DIVISOR. */)
845 Lisp_Object arg
, divisor
;
847 return rounding_driver (arg
, divisor
, ceil
, ceiling2
, "ceiling");
850 DEFUN ("floor", Ffloor
, Sfloor
, 1, 2, 0,
851 doc
: /* Return the largest integer no greater than ARG.
852 This rounds the value towards +inf.
853 With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR. */)
855 Lisp_Object arg
, divisor
;
857 return rounding_driver (arg
, divisor
, floor
, floor2
, "floor");
860 DEFUN ("round", Fround
, Sround
, 1, 2, 0,
861 doc
: /* Return the nearest integer to ARG.
862 With optional DIVISOR, return the nearest integer to ARG/DIVISOR.
864 Rounding a value equidistant between two integers may choose the
865 integer closer to zero, or it may prefer an even integer, depending on
866 your machine. For example, \(round 2.5\) can return 3 on some
867 systems, but 2 on others. */)
869 Lisp_Object arg
, divisor
;
871 return rounding_driver (arg
, divisor
, emacs_rint
, round2
, "round");
874 DEFUN ("truncate", Ftruncate
, Struncate
, 1, 2, 0,
875 doc
: /* Truncate a floating point number to an int.
876 Rounds ARG toward zero.
877 With optional DIVISOR, truncate ARG/DIVISOR. */)
879 Lisp_Object arg
, divisor
;
881 return rounding_driver (arg
, divisor
, double_identity
, truncate2
,
888 register Lisp_Object x
, y
;
892 f1
= FLOATP (x
) ? XFLOAT_DATA (x
) : XINT (x
);
893 f2
= FLOATP (y
) ? XFLOAT_DATA (y
) : XINT (y
);
895 if (! IEEE_FLOATING_POINT
&& f2
== 0)
896 Fsignal (Qarith_error
, Qnil
);
898 /* If the "remainder" comes out with the wrong sign, fix it. */
899 IN_FLOAT2 ((f1
= fmod (f1
, f2
),
900 f1
= (f2
< 0 ? f1
> 0 : f1
< 0) ? f1
+ f2
: f1
),
902 return make_float (f1
);
905 /* It's not clear these are worth adding. */
907 DEFUN ("fceiling", Ffceiling
, Sfceiling
, 1, 1, 0,
908 doc
: /* Return the smallest integer no less than ARG, as a float.
909 \(Round toward +inf.\) */)
911 register Lisp_Object arg
;
913 double d
= extract_float (arg
);
914 IN_FLOAT (d
= ceil (d
), "fceiling", arg
);
915 return make_float (d
);
918 DEFUN ("ffloor", Fffloor
, Sffloor
, 1, 1, 0,
919 doc
: /* Return the largest integer no greater than ARG, as a float.
920 \(Round towards -inf.\) */)
922 register Lisp_Object arg
;
924 double d
= extract_float (arg
);
925 IN_FLOAT (d
= floor (d
), "ffloor", arg
);
926 return make_float (d
);
929 DEFUN ("fround", Ffround
, Sfround
, 1, 1, 0,
930 doc
: /* Return the nearest integer to ARG, as a float. */)
932 register Lisp_Object arg
;
934 double d
= extract_float (arg
);
935 IN_FLOAT (d
= emacs_rint (d
), "fround", arg
);
936 return make_float (d
);
939 DEFUN ("ftruncate", Fftruncate
, Sftruncate
, 1, 1, 0,
940 doc
: /* Truncate a floating point number to an integral float value.
941 Rounds the value toward zero. */)
943 register Lisp_Object arg
;
945 double d
= extract_float (arg
);
947 IN_FLOAT (d
= floor (d
), "ftruncate", arg
);
949 IN_FLOAT (d
= ceil (d
), "ftruncate", arg
);
950 return make_float (d
);
953 #ifdef FLOAT_CATCH_SIGILL
959 fatal_error_signal (signo
);
964 #else /* not BSD4_1 */
965 sigsetmask (SIGEMPTYMASK
);
966 #endif /* not BSD4_1 */
968 /* Must reestablish handler each time it is called. */
969 signal (SIGILL
, float_error
);
970 #endif /* BSD_SYSTEM */
974 Fsignal (Qarith_error
, Fcons (float_error_arg
, Qnil
));
977 /* Another idea was to replace the library function `infnan'
978 where SIGILL is signaled. */
980 #endif /* FLOAT_CATCH_SIGILL */
989 /* Not called from emacs-lisp float routines; do the default thing. */
991 if (!strcmp (x
->name
, "pow"))
995 = Fcons (build_string (x
->name
),
996 Fcons (make_float (x
->arg1
),
997 ((!strcmp (x
->name
, "log") || !strcmp (x
->name
, "pow"))
998 ? Fcons (make_float (x
->arg2
), Qnil
)
1002 case DOMAIN
: Fsignal (Qdomain_error
, args
); break;
1003 case SING
: Fsignal (Qsingularity_error
, args
); break;
1004 case OVERFLOW
: Fsignal (Qoverflow_error
, args
); break;
1005 case UNDERFLOW
: Fsignal (Qunderflow_error
, args
); break;
1006 default: Fsignal (Qarith_error
, args
); break;
1008 return (1); /* don't set errno or print a message */
1010 #endif /* HAVE_MATHERR */
1015 #ifdef FLOAT_CATCH_SIGILL
1016 signal (SIGILL
, float_error
);
1037 defsubr (&Sbessel_y0
);
1038 defsubr (&Sbessel_y1
);
1039 defsubr (&Sbessel_yn
);
1040 defsubr (&Sbessel_j0
);
1041 defsubr (&Sbessel_j1
);
1042 defsubr (&Sbessel_jn
);
1045 defsubr (&Slog_gamma
);
1046 defsubr (&Scube_root
);
1048 defsubr (&Sfceiling
);
1051 defsubr (&Sftruncate
);
1061 defsubr (&Sceiling
);
1064 defsubr (&Struncate
);