1 /* Primitive operations on floating point for GNU Emacs Lisp interpreter.
2 Copyright (C) 1988, 1993 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. */
21 /* ANSI C requires only these float functions:
22 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, fmod,
23 frexp, ldexp, log, log10, modf, pow, sin, sinh, sqrt, tan, tanh.
25 Define HAVE_INVERSE_HYPERBOLIC if you have acosh, asinh, and atanh.
26 Define HAVE_CBRT if you have cbrt.
27 Define HAVE_RINT if you have rint.
28 If you don't define these, then the appropriate routines will be simulated.
30 Define HAVE_MATHERR if on a system supporting the SysV matherr callback.
31 (This should happen automatically.)
33 Define FLOAT_CHECK_ERRNO if the float library routines set errno.
34 This has no effect if HAVE_MATHERR is defined.
36 Define FLOAT_CATCH_SIGILL if the float library routines signal SIGILL.
37 (What systems actually do this? Please let us know.)
39 Define FLOAT_CHECK_DOMAIN if the float library doesn't handle errors by
40 either setting errno, or signalling SIGFPE/SIGILL. Otherwise, domain and
41 range checking will happen before calling the float routines. This has
42 no effect if HAVE_MATHERR is defined (since matherr will be called when
43 a domain error occurs.)
50 #include "syssignal.h"
52 Lisp_Object Qarith_error
;
54 #ifdef LISP_FLOAT_TYPE
56 /* Work around a problem that happens because math.h on hpux 7
57 defines two static variables--which, in Emacs, are not really static,
58 because `static' is defined as nothing. The problem is that they are
59 defined both here and in lread.c.
60 These macros prevent the name conflict. */
61 #if defined (HPUX) && !defined (HPUX8)
62 #define _MAXLDBL floatfns_maxldbl
63 #define _NMAXLDBL floatfns_nmaxldbl
68 /* This declaration is omitted on some systems, like Ultrix. */
69 #if !defined (hpux) && defined (HAVE_LOGB)
70 extern double logb ();
71 #endif /* !hpux && HAVE_LOGB */
73 #if defined(DOMAIN) && defined(SING) && defined(OVERFLOW)
74 /* If those are defined, then this is probably a `matherr' machine. */
85 # ifdef FLOAT_CHECK_ERRNO
86 # undef FLOAT_CHECK_ERRNO
88 # ifdef FLOAT_CHECK_DOMAIN
89 # undef FLOAT_CHECK_DOMAIN
93 #ifndef NO_FLOAT_CHECK_ERRNO
94 #define FLOAT_CHECK_ERRNO
97 #ifdef FLOAT_CHECK_ERRNO
103 /* Avoid traps on VMS from sinh and cosh.
104 All the other functions set errno instead. */
109 #define cosh(x) ((exp(x)+exp(-x))*0.5)
110 #define sinh(x) ((exp(x)-exp(-x))*0.5)
114 #define rint(x) (floor((x)+0.5))
117 static SIGTYPE
float_error ();
119 /* Nonzero while executing in floating point.
120 This tells float_error what to do. */
124 /* If an argument is out of range for a mathematical function,
125 here is the actual argument value to use in the error message. */
127 static Lisp_Object float_error_arg
, float_error_arg2
;
129 static char *float_error_fn_name
;
131 /* Evaluate the floating point expression D, recording NUM
132 as the original argument for error messages.
133 D is normally an assignment expression.
134 Handle errors which may result in signals or may set errno.
136 Note that float_error may be declared to return void, so you can't
137 just cast the zero after the colon to (SIGTYPE) to make the types
140 #ifdef FLOAT_CHECK_ERRNO
141 #define IN_FLOAT(d, name, num) \
143 float_error_arg = num; \
144 float_error_fn_name = name; \
145 in_float = 1; errno = 0; (d); in_float = 0; \
148 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
149 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
150 default: arith_error (float_error_fn_name, float_error_arg); \
153 #define IN_FLOAT2(d, name, num, num2) \
155 float_error_arg = num; \
156 float_error_arg2 = num2; \
157 float_error_fn_name = name; \
158 in_float = 1; errno = 0; (d); in_float = 0; \
161 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
162 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
163 default: arith_error (float_error_fn_name, float_error_arg); \
167 #define IN_FLOAT(d, name, num) (in_float = 1, (d), in_float = 0)
168 #define IN_FLOAT2(d, name, num, num2) (in_float = 1, (d), in_float = 0)
171 #define arith_error(op,arg) \
172 Fsignal (Qarith_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
173 #define range_error(op,arg) \
174 Fsignal (Qrange_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
175 #define domain_error(op,arg) \
176 Fsignal (Qdomain_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
177 #define domain_error2(op,a1,a2) \
178 Fsignal (Qdomain_error, Fcons (build_string ((op)), Fcons ((a1), Fcons ((a2), Qnil))))
180 /* Extract a Lisp number as a `double', or signal an error. */
186 CHECK_NUMBER_OR_FLOAT (num
, 0);
188 if (XTYPE (num
) == Lisp_Float
)
189 return XFLOAT (num
)->data
;
190 return (double) XINT (num
);
193 /* Trig functions. */
195 DEFUN ("acos", Facos
, Sacos
, 1, 1, 0,
196 "Return the inverse cosine of ARG.")
198 register Lisp_Object arg
;
200 double d
= extract_float (arg
);
201 #ifdef FLOAT_CHECK_DOMAIN
202 if (d
> 1.0 || d
< -1.0)
203 domain_error ("acos", arg
);
205 IN_FLOAT (d
= acos (d
), "acos", arg
);
206 return make_float (d
);
209 DEFUN ("asin", Fasin
, Sasin
, 1, 1, 0,
210 "Return the inverse sine of ARG.")
212 register Lisp_Object arg
;
214 double d
= extract_float (arg
);
215 #ifdef FLOAT_CHECK_DOMAIN
216 if (d
> 1.0 || d
< -1.0)
217 domain_error ("asin", arg
);
219 IN_FLOAT (d
= asin (d
), "asin", arg
);
220 return make_float (d
);
223 DEFUN ("atan", Fatan
, Satan
, 1, 1, 0,
224 "Return the inverse tangent of ARG.")
226 register Lisp_Object arg
;
228 double d
= extract_float (arg
);
229 IN_FLOAT (d
= atan (d
), "atan", arg
);
230 return make_float (d
);
233 DEFUN ("cos", Fcos
, Scos
, 1, 1, 0,
234 "Return the cosine of ARG.")
236 register Lisp_Object arg
;
238 double d
= extract_float (arg
);
239 IN_FLOAT (d
= cos (d
), "cos", arg
);
240 return make_float (d
);
243 DEFUN ("sin", Fsin
, Ssin
, 1, 1, 0,
244 "Return the sine of ARG.")
246 register Lisp_Object arg
;
248 double d
= extract_float (arg
);
249 IN_FLOAT (d
= sin (d
), "sin", arg
);
250 return make_float (d
);
253 DEFUN ("tan", Ftan
, Stan
, 1, 1, 0,
254 "Return the tangent of ARG.")
256 register Lisp_Object arg
;
258 double d
= extract_float (arg
);
260 #ifdef FLOAT_CHECK_DOMAIN
262 domain_error ("tan", arg
);
264 IN_FLOAT (d
= sin (d
) / c
, "tan", arg
);
265 return make_float (d
);
268 #if 0 /* Leave these out unless we find there's a reason for them. */
270 DEFUN ("bessel-j0", Fbessel_j0
, Sbessel_j0
, 1, 1, 0,
271 "Return the bessel function j0 of ARG.")
273 register Lisp_Object arg
;
275 double d
= extract_float (arg
);
276 IN_FLOAT (d
= j0 (d
), "bessel-j0", arg
);
277 return make_float (d
);
280 DEFUN ("bessel-j1", Fbessel_j1
, Sbessel_j1
, 1, 1, 0,
281 "Return the bessel function j1 of ARG.")
283 register Lisp_Object arg
;
285 double d
= extract_float (arg
);
286 IN_FLOAT (d
= j1 (d
), "bessel-j1", arg
);
287 return make_float (d
);
290 DEFUN ("bessel-jn", Fbessel_jn
, Sbessel_jn
, 2, 2, 0,
291 "Return the order N bessel function output jn of ARG.\n\
292 The first arg (the order) is truncated to an integer.")
294 register Lisp_Object arg1
, arg2
;
296 int i1
= extract_float (arg1
);
297 double f2
= extract_float (arg2
);
299 IN_FLOAT (f2
= jn (i1
, f2
), "bessel-jn", arg1
);
300 return make_float (f2
);
303 DEFUN ("bessel-y0", Fbessel_y0
, Sbessel_y0
, 1, 1, 0,
304 "Return the bessel function y0 of ARG.")
306 register Lisp_Object arg
;
308 double d
= extract_float (arg
);
309 IN_FLOAT (d
= y0 (d
), "bessel-y0", arg
);
310 return make_float (d
);
313 DEFUN ("bessel-y1", Fbessel_y1
, Sbessel_y1
, 1, 1, 0,
314 "Return the bessel function y1 of ARG.")
316 register Lisp_Object arg
;
318 double d
= extract_float (arg
);
319 IN_FLOAT (d
= y1 (d
), "bessel-y0", arg
);
320 return make_float (d
);
323 DEFUN ("bessel-yn", Fbessel_yn
, Sbessel_yn
, 2, 2, 0,
324 "Return the order N bessel function output yn of ARG.\n\
325 The first arg (the order) is truncated to an integer.")
327 register Lisp_Object arg1
, arg2
;
329 int i1
= extract_float (arg1
);
330 double f2
= extract_float (arg2
);
332 IN_FLOAT (f2
= yn (i1
, f2
), "bessel-yn", arg1
);
333 return make_float (f2
);
338 #if 0 /* Leave these out unless we see they are worth having. */
340 DEFUN ("erf", Ferf
, Serf
, 1, 1, 0,
341 "Return the mathematical error function of ARG.")
343 register Lisp_Object arg
;
345 double d
= extract_float (arg
);
346 IN_FLOAT (d
= erf (d
), "erf", arg
);
347 return make_float (d
);
350 DEFUN ("erfc", Ferfc
, Serfc
, 1, 1, 0,
351 "Return the complementary error function of ARG.")
353 register Lisp_Object arg
;
355 double d
= extract_float (arg
);
356 IN_FLOAT (d
= erfc (d
), "erfc", arg
);
357 return make_float (d
);
360 DEFUN ("log-gamma", Flog_gamma
, Slog_gamma
, 1, 1, 0,
361 "Return the log gamma of ARG.")
363 register Lisp_Object arg
;
365 double d
= extract_float (arg
);
366 IN_FLOAT (d
= lgamma (d
), "log-gamma", arg
);
367 return make_float (d
);
370 DEFUN ("cube-root", Fcube_root
, Scube_root
, 1, 1, 0,
371 "Return the cube root of ARG.")
373 register Lisp_Object arg
;
375 double d
= extract_float (arg
);
377 IN_FLOAT (d
= cbrt (d
), "cube-root", arg
);
380 IN_FLOAT (d
= pow (d
, 1.0/3.0), "cube-root", arg
);
382 IN_FLOAT (d
= -pow (-d
, 1.0/3.0), "cube-root", arg
);
384 return make_float (d
);
389 DEFUN ("exp", Fexp
, Sexp
, 1, 1, 0,
390 "Return the exponential base e of ARG.")
392 register Lisp_Object arg
;
394 double d
= extract_float (arg
);
395 #ifdef FLOAT_CHECK_DOMAIN
396 if (d
> 709.7827) /* Assume IEEE doubles here */
397 range_error ("exp", arg
);
399 return make_float (0.0);
402 IN_FLOAT (d
= exp (d
), "exp", arg
);
403 return make_float (d
);
406 DEFUN ("expt", Fexpt
, Sexpt
, 2, 2, 0,
407 "Return the exponential X ** Y.")
409 register Lisp_Object arg1
, arg2
;
413 CHECK_NUMBER_OR_FLOAT (arg1
, 0);
414 CHECK_NUMBER_OR_FLOAT (arg2
, 0);
415 if (XTYPE (arg1
) == Lisp_Int
/* common lisp spec */
416 && XTYPE (arg2
) == Lisp_Int
) /* don't promote, if both are ints */
417 { /* this can be improved by pre-calculating */
418 int acc
, x
, y
; /* some binary powers of x then accumulating */
430 acc
= (y
& 1) ? -1 : 1;
442 y
= (unsigned)y
>> 1;
445 XSET (val
, Lisp_Int
, acc
);
448 f1
= (XTYPE (arg1
) == Lisp_Float
) ? XFLOAT (arg1
)->data
: XINT (arg1
);
449 f2
= (XTYPE (arg2
) == Lisp_Float
) ? XFLOAT (arg2
)->data
: XINT (arg2
);
450 /* Really should check for overflow, too */
451 if (f1
== 0.0 && f2
== 0.0)
453 #ifdef FLOAT_CHECK_DOMAIN
454 else if ((f1
== 0.0 && f2
< 0.0) || (f1
< 0 && f2
!= floor(f2
)))
455 domain_error2 ("expt", arg1
, arg2
);
457 IN_FLOAT2 (f1
= pow (f1
, f2
), "expt", arg1
, arg2
);
458 return make_float (f1
);
461 DEFUN ("log", Flog
, Slog
, 1, 2, 0,
462 "Return the natural logarithm of ARG.\n\
463 If second optional argument BASE is given, return log ARG using that base.")
465 register Lisp_Object arg
, base
;
467 double d
= extract_float (arg
);
469 #ifdef FLOAT_CHECK_DOMAIN
471 domain_error2 ("log", arg
, base
);
474 IN_FLOAT (d
= log (d
), "log", arg
);
477 double b
= extract_float (base
);
479 #ifdef FLOAT_CHECK_DOMAIN
480 if (b
<= 0.0 || b
== 1.0)
481 domain_error2 ("log", arg
, base
);
484 IN_FLOAT2 (d
= log10 (d
), "log", arg
, base
);
486 IN_FLOAT2 (d
= log (d
) / log (b
), "log", arg
, base
);
488 return make_float (d
);
491 DEFUN ("log10", Flog10
, Slog10
, 1, 1, 0,
492 "Return the logarithm base 10 of ARG.")
494 register Lisp_Object arg
;
496 double d
= extract_float (arg
);
497 #ifdef FLOAT_CHECK_DOMAIN
499 domain_error ("log10", arg
);
501 IN_FLOAT (d
= log10 (d
), "log10", arg
);
502 return make_float (d
);
505 DEFUN ("sqrt", Fsqrt
, Ssqrt
, 1, 1, 0,
506 "Return the square root of ARG.")
508 register Lisp_Object arg
;
510 double d
= extract_float (arg
);
511 #ifdef FLOAT_CHECK_DOMAIN
513 domain_error ("sqrt", arg
);
515 IN_FLOAT (d
= sqrt (d
), "sqrt", arg
);
516 return make_float (d
);
519 #if 0 /* Not clearly worth adding. */
521 DEFUN ("acosh", Facosh
, Sacosh
, 1, 1, 0,
522 "Return the inverse hyperbolic cosine of ARG.")
524 register Lisp_Object arg
;
526 double d
= extract_float (arg
);
527 #ifdef FLOAT_CHECK_DOMAIN
529 domain_error ("acosh", arg
);
531 #ifdef HAVE_INVERSE_HYPERBOLIC
532 IN_FLOAT (d
= acosh (d
), "acosh", arg
);
534 IN_FLOAT (d
= log (d
+ sqrt (d
*d
- 1.0)), "acosh", arg
);
536 return make_float (d
);
539 DEFUN ("asinh", Fasinh
, Sasinh
, 1, 1, 0,
540 "Return the inverse hyperbolic sine of ARG.")
542 register Lisp_Object arg
;
544 double d
= extract_float (arg
);
545 #ifdef HAVE_INVERSE_HYPERBOLIC
546 IN_FLOAT (d
= asinh (d
), "asinh", arg
);
548 IN_FLOAT (d
= log (d
+ sqrt (d
*d
+ 1.0)), "asinh", arg
);
550 return make_float (d
);
553 DEFUN ("atanh", Fatanh
, Satanh
, 1, 1, 0,
554 "Return the inverse hyperbolic tangent of ARG.")
556 register Lisp_Object arg
;
558 double d
= extract_float (arg
);
559 #ifdef FLOAT_CHECK_DOMAIN
560 if (d
>= 1.0 || d
<= -1.0)
561 domain_error ("atanh", arg
);
563 #ifdef HAVE_INVERSE_HYPERBOLIC
564 IN_FLOAT (d
= atanh (d
), "atanh", arg
);
566 IN_FLOAT (d
= 0.5 * log ((1.0 + d
) / (1.0 - d
)), "atanh", arg
);
568 return make_float (d
);
571 DEFUN ("cosh", Fcosh
, Scosh
, 1, 1, 0,
572 "Return the hyperbolic cosine of ARG.")
574 register Lisp_Object arg
;
576 double d
= extract_float (arg
);
577 #ifdef FLOAT_CHECK_DOMAIN
578 if (d
> 710.0 || d
< -710.0)
579 range_error ("cosh", arg
);
581 IN_FLOAT (d
= cosh (d
), "cosh", arg
);
582 return make_float (d
);
585 DEFUN ("sinh", Fsinh
, Ssinh
, 1, 1, 0,
586 "Return the hyperbolic sine of ARG.")
588 register Lisp_Object arg
;
590 double d
= extract_float (arg
);
591 #ifdef FLOAT_CHECK_DOMAIN
592 if (d
> 710.0 || d
< -710.0)
593 range_error ("sinh", arg
);
595 IN_FLOAT (d
= sinh (d
), "sinh", arg
);
596 return make_float (d
);
599 DEFUN ("tanh", Ftanh
, Stanh
, 1, 1, 0,
600 "Return the hyperbolic tangent of ARG.")
602 register Lisp_Object arg
;
604 double d
= extract_float (arg
);
605 IN_FLOAT (d
= tanh (d
), "tanh", arg
);
606 return make_float (d
);
610 DEFUN ("abs", Fabs
, Sabs
, 1, 1, 0,
611 "Return the absolute value of ARG.")
613 register Lisp_Object arg
;
615 CHECK_NUMBER_OR_FLOAT (arg
, 0);
617 if (XTYPE (arg
) == Lisp_Float
)
618 IN_FLOAT (arg
= make_float (fabs (XFLOAT (arg
)->data
)), "abs", arg
);
619 else if (XINT (arg
) < 0)
620 XSETINT (arg
, - XFASTINT (arg
));
625 DEFUN ("float", Ffloat
, Sfloat
, 1, 1, 0,
626 "Return the floating point number equal to ARG.")
628 register Lisp_Object arg
;
630 CHECK_NUMBER_OR_FLOAT (arg
, 0);
632 if (XTYPE (arg
) == Lisp_Int
)
633 return make_float ((double) XINT (arg
));
634 else /* give 'em the same float back */
638 DEFUN ("logb", Flogb
, Slogb
, 1, 1, 0,
639 "Returns largest integer <= the base 2 log of the magnitude of ARG.\n\
640 This is the same as the exponent of a float.")
646 double f
= extract_float (arg
);
649 IN_FLOAT (value
= logb (f
), "logb", arg
);
650 XSET (val
, Lisp_Int
, value
);
656 IN_FLOAT (frexp (f
, &exp
), "logb", arg
);
657 XSET (val
, Lisp_Int
, exp
-1);
660 /* Would someone like to write code to emulate logb? */
661 error ("`logb' not implemented on this operating system");
668 /* the rounding functions */
670 DEFUN ("ceiling", Fceiling
, Sceiling
, 1, 1, 0,
671 "Return the smallest integer no less than ARG. (Round toward +inf.)")
673 register Lisp_Object arg
;
675 CHECK_NUMBER_OR_FLOAT (arg
, 0);
677 if (XTYPE (arg
) == Lisp_Float
)
678 IN_FLOAT (XSET (arg
, Lisp_Int
, ceil (XFLOAT (arg
)->data
)), "ceiling", arg
);
683 #endif /* LISP_FLOAT_TYPE */
686 DEFUN ("floor", Ffloor
, Sfloor
, 1, 2, 0,
687 "Return the largest integer no greater than ARG. (Round towards -inf.)\n\
688 With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR.")
690 register Lisp_Object arg
, divisor
;
692 CHECK_NUMBER_OR_FLOAT (arg
, 0);
694 if (! NILP (divisor
))
698 CHECK_NUMBER_OR_FLOAT (divisor
, 1);
700 #ifdef LISP_FLOAT_TYPE
701 if (XTYPE (arg
) == Lisp_Float
|| XTYPE (divisor
) == Lisp_Float
)
705 f1
= XTYPE (arg
) == Lisp_Float
? XFLOAT (arg
)->data
: XINT (arg
);
706 f2
= (XTYPE (divisor
) == Lisp_Float
707 ? XFLOAT (divisor
)->data
: XINT (divisor
));
709 Fsignal (Qarith_error
, Qnil
);
711 IN_FLOAT2 (XSET (arg
, Lisp_Int
, floor (f1
/ f2
)),
712 "floor", arg
, divisor
);
721 Fsignal (Qarith_error
, Qnil
);
723 /* With C's /, the result is implementation-defined if either operand
724 is negative, so use only nonnegative operands. */
726 ? (i1
<= 0 ? -i1
/ -i2
: -1 - ((i1
- 1) / -i2
))
727 : (i1
< 0 ? -1 - ((-1 - i1
) / i2
) : i1
/ i2
));
729 XSET (arg
, Lisp_Int
, i1
);
733 #ifdef LISP_FLOAT_TYPE
734 if (XTYPE (arg
) == Lisp_Float
)
735 IN_FLOAT (XSET (arg
, Lisp_Int
, floor (XFLOAT (arg
)->data
)), "floor", arg
);
741 #ifdef LISP_FLOAT_TYPE
743 DEFUN ("round", Fround
, Sround
, 1, 1, 0,
744 "Return the nearest integer to ARG.")
746 register Lisp_Object arg
;
748 CHECK_NUMBER_OR_FLOAT (arg
, 0);
750 if (XTYPE (arg
) == Lisp_Float
)
751 /* Screw the prevailing rounding mode. */
752 IN_FLOAT (XSET (arg
, Lisp_Int
, rint (XFLOAT (arg
)->data
)), "round", arg
);
757 DEFUN ("truncate", Ftruncate
, Struncate
, 1, 1, 0,
758 "Truncate a floating point number to an int.\n\
759 Rounds the value toward zero.")
761 register Lisp_Object arg
;
763 CHECK_NUMBER_OR_FLOAT (arg
, 0);
765 if (XTYPE (arg
) == Lisp_Float
)
766 XSET (arg
, Lisp_Int
, (int) XFLOAT (arg
)->data
);
772 /* It's not clear these are worth adding. */
774 DEFUN ("fceiling", Ffceiling
, Sfceiling
, 1, 1, 0,
775 "Return the smallest integer no less than ARG, as a float.\n\
776 \(Round toward +inf.\)")
778 register Lisp_Object arg
;
780 double d
= extract_float (arg
);
781 IN_FLOAT (d
= ceil (d
), "fceiling", arg
);
782 return make_float (d
);
785 DEFUN ("ffloor", Fffloor
, Sffloor
, 1, 1, 0,
786 "Return the largest integer no greater than ARG, as a float.\n\
787 \(Round towards -inf.\)")
789 register Lisp_Object arg
;
791 double d
= extract_float (arg
);
792 IN_FLOAT (d
= floor (d
), "ffloor", arg
);
793 return make_float (d
);
796 DEFUN ("fround", Ffround
, Sfround
, 1, 1, 0,
797 "Return the nearest integer to ARG, as a float.")
799 register Lisp_Object arg
;
801 double d
= extract_float (arg
);
802 IN_FLOAT (d
= rint (XFLOAT (arg
)->data
), "fround", arg
);
803 return make_float (d
);
806 DEFUN ("ftruncate", Fftruncate
, Sftruncate
, 1, 1, 0,
807 "Truncate a floating point number to an integral float value.\n\
808 Rounds the value toward zero.")
810 register Lisp_Object arg
;
812 double d
= extract_float (arg
);
814 IN_FLOAT (d
= floor (d
), "ftruncate", arg
);
816 IN_FLOAT (d
= ceil (d
), arg
);
817 return make_float (d
);
821 #ifdef FLOAT_CATCH_SIGILL
827 fatal_error_signal (signo
);
832 #else /* not BSD4_1 */
833 sigsetmask (SIGEMPTYMASK
);
834 #endif /* not BSD4_1 */
836 /* Must reestablish handler each time it is called. */
837 signal (SIGILL
, float_error
);
842 Fsignal (Qarith_error
, Fcons (float_error_arg
, Qnil
));
845 /* Another idea was to replace the library function `infnan'
846 where SIGILL is signaled. */
848 #endif /* FLOAT_CATCH_SIGILL */
857 /* Not called from emacs-lisp float routines; do the default thing. */
859 if (!strcmp (x
->name
, "pow"))
863 = Fcons (build_string (x
->name
),
864 Fcons (make_float (x
->arg1
),
865 ((!strcmp (x
->name
, "log") || !strcmp (x
->name
, "pow"))
866 ? Fcons (make_float (x
->arg2
), Qnil
)
870 case DOMAIN
: Fsignal (Qdomain_error
, args
); break;
871 case SING
: Fsignal (Qsingularity_error
, args
); break;
872 case OVERFLOW
: Fsignal (Qoverflow_error
, args
); break;
873 case UNDERFLOW
: Fsignal (Qunderflow_error
, args
); break;
874 default: Fsignal (Qarith_error
, args
); break;
876 return (1); /* don't set errno or print a message */
878 #endif /* HAVE_MATHERR */
882 #ifdef FLOAT_CATCH_SIGILL
883 signal (SIGILL
, float_error
);
888 #else /* not LISP_FLOAT_TYPE */
893 #endif /* not LISP_FLOAT_TYPE */
897 #ifdef LISP_FLOAT_TYPE
911 defsubr (&Sbessel_y0
);
912 defsubr (&Sbessel_y1
);
913 defsubr (&Sbessel_yn
);
914 defsubr (&Sbessel_j0
);
915 defsubr (&Sbessel_j1
);
916 defsubr (&Sbessel_jn
);
919 defsubr (&Slog_gamma
);
920 defsubr (&Scube_root
);
921 defsubr (&Sfceiling
);
924 defsubr (&Sftruncate
);
937 defsubr (&Struncate
);
938 #endif /* LISP_FLOAT_TYPE */