2965f5205ad9327ed264ef748fcfc1fa8210a8f0
[chise/xemacs-chise.git.1] / src / floatfns.c
1 /* Primitive operations on floating point for XEmacs Lisp interpreter.
2    Copyright (C) 1988, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; 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.  */
20
21 /* Synched up with: FSF 19.30. */
22
23 /* ANSI C requires only these float functions:
24    acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, fmod,
25    frexp, ldexp, log, log10, modf, pow, sin, sinh, sqrt, tan, tanh.
26
27    Define HAVE_INVERSE_HYPERBOLIC if you have acosh, asinh, and atanh.
28    Define HAVE_CBRT if you have cbrt().
29    Define HAVE_RINT if you have rint().
30    If you don't define these, then the appropriate routines will be simulated.
31
32    Define HAVE_MATHERR if on a system supporting the SysV matherr() callback.
33    (This should happen automatically.)
34
35    Define FLOAT_CHECK_ERRNO if the float library routines set errno.
36    This has no effect if HAVE_MATHERR is defined.
37
38    Define FLOAT_CATCH_SIGILL if the float library routines signal SIGILL.
39    (What systems actually do this?  Let me know. -jwz)
40
41    Define FLOAT_CHECK_DOMAIN if the float library doesn't handle errors by
42    either setting errno, or signalling SIGFPE/SIGILL.  Otherwise, domain and
43    range checking will happen before calling the float routines.  This has
44    no effect if HAVE_MATHERR is defined (since matherr will be called when
45    a domain error occurs).
46  */
47
48 #include <config.h>
49 #include "lisp.h"
50 #include "syssignal.h"
51
52 #ifdef LISP_FLOAT_TYPE
53
54 /* Need to define a differentiating symbol -- see sysfloat.h */
55 #define THIS_FILENAME floatfns
56 #include "sysfloat.h"
57
58 #ifndef HAVE_RINT
59 static double
60 rint (double x)
61 {
62   double r = floor (x + 0.5);
63   double diff = fabs (r - x);
64   /* Round to even and correct for any roundoff errors.  */
65   if (diff >= 0.5 && (diff > 0.5 || r != 2.0 * floor (r / 2.0)))
66     r += r < x ? 1.0 : -1.0;
67   return r;
68 }
69 #endif
70
71 /* Nonzero while executing in floating point.
72    This tells float_error what to do.  */
73 static int in_float;
74
75 /* If an argument is out of range for a mathematical function,
76    here is the actual argument value to use in the error message.  */
77 static Lisp_Object float_error_arg, float_error_arg2;
78 static CONST char *float_error_fn_name;
79
80 /* Evaluate the floating point expression D, recording NUM
81    as the original argument for error messages.
82    D is normally an assignment expression.
83    Handle errors which may result in signals or may set errno.
84
85    Note that float_error may be declared to return void, so you can't
86    just cast the zero after the colon to (SIGTYPE) to make the types
87    check properly.  */
88 #ifdef FLOAT_CHECK_ERRNO
89 #define IN_FLOAT(d, name, num)                          \
90   do {                                                  \
91     float_error_arg = num;                              \
92     float_error_fn_name = name;                         \
93     in_float = 1; errno = 0; (d); in_float = 0;         \
94     if (errno != 0) in_float_error ();                  \
95   } while (0)
96 #define IN_FLOAT2(d, name, num, num2)                   \
97   do {                                                  \
98     float_error_arg = num;                              \
99     float_error_arg2 = num2;                            \
100     float_error_fn_name = name;                         \
101     in_float = 2; errno = 0; (d); in_float = 0;         \
102     if (errno != 0) in_float_error ();                  \
103   } while (0)
104 #else
105 #define IN_FLOAT(d, name, num) (in_float = 1, (d), in_float = 0)
106 #define IN_FLOAT2(d, name, num, num2) (in_float = 2, (d), in_float = 0)
107 #endif
108
109
110 #define arith_error(op,arg) \
111   Fsignal (Qarith_error, list2 (build_string ((op)), (arg)))
112 #define range_error(op,arg) \
113   Fsignal (Qrange_error, list2 (build_string ((op)), (arg)))
114 #define range_error2(op,a1,a2) \
115   Fsignal (Qrange_error, list3 (build_string ((op)), (a1), (a2)))
116 #define domain_error(op,arg) \
117   Fsignal (Qdomain_error, list2 (build_string ((op)), (arg)))
118 #define domain_error2(op,a1,a2) \
119   Fsignal (Qdomain_error, list3 (build_string ((op)), (a1), (a2)))
120
121
122 /* Convert float to Lisp Integer if it fits, else signal a range
123    error using the given arguments.  */
124 static Lisp_Object
125 float_to_int (double x, CONST char *name, Lisp_Object num, Lisp_Object num2)
126 {
127   if (x >= ((EMACS_INT) 1 << (VALBITS-1))
128       || x <= - ((EMACS_INT) 1 << (VALBITS-1)) - (EMACS_INT) 1)
129   {
130     if (!UNBOUNDP (num2))
131       range_error2 (name, num, num2);
132     else
133       range_error (name, num);
134   }
135   return (make_int ((EMACS_INT) x));
136 }
137
138
139 static void
140 in_float_error (void)
141 {
142   switch (errno)
143   {
144   case 0:
145     break;
146   case EDOM:
147     if (in_float == 2)
148       domain_error2 (float_error_fn_name, float_error_arg, float_error_arg2);
149     else
150       domain_error (float_error_fn_name, float_error_arg);
151     break;
152   case ERANGE:
153     range_error (float_error_fn_name, float_error_arg);
154     break;
155   default:
156     arith_error (float_error_fn_name, float_error_arg);
157     break;
158   }
159 }
160
161 \f
162 static Lisp_Object
163 mark_float (Lisp_Object obj, void (*markobj) (Lisp_Object))
164 {
165   return Qnil;
166 }
167
168 static int
169 float_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
170 {
171   return (extract_float (obj1) == extract_float (obj2));
172 }
173
174 static unsigned long
175 float_hash (Lisp_Object obj, int depth)
176 {
177   /* mod the value down to 32-bit range */
178   /* #### change for 64-bit machines */
179   return (unsigned long) fmod (extract_float (obj), 4e9);
180 }
181
182 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("float", float,
183                                      mark_float, print_float, 0, float_equal,
184                                      float_hash, struct Lisp_Float);
185 \f
186 /* Extract a Lisp number as a `double', or signal an error.  */
187
188 double
189 extract_float (Lisp_Object num)
190 {
191   if (FLOATP (num))
192     return XFLOAT_DATA (num);
193
194   if (INTP (num))
195     return (double) XINT (num);
196
197   return extract_float (wrong_type_argument (Qnumberp, num));
198 }
199 #endif /* LISP_FLOAT_TYPE */
200
201 \f
202 /* Trig functions.  */
203 #ifdef LISP_FLOAT_TYPE
204
205 DEFUN ("acos", Facos, 1, 1, 0, /*
206 Return the inverse cosine of ARG.
207 */
208        (arg))
209 {
210   double d = extract_float (arg);
211 #ifdef FLOAT_CHECK_DOMAIN
212   if (d > 1.0 || d < -1.0)
213     domain_error ("acos", arg);
214 #endif
215   IN_FLOAT (d = acos (d), "acos", arg);
216   return make_float (d);
217 }
218
219 DEFUN ("asin", Fasin, 1, 1, 0, /*
220 Return the inverse sine of ARG.
221 */
222        (arg))
223 {
224   double d = extract_float (arg);
225 #ifdef FLOAT_CHECK_DOMAIN
226   if (d > 1.0 || d < -1.0)
227     domain_error ("asin", arg);
228 #endif
229   IN_FLOAT (d = asin (d), "asin", arg);
230   return make_float (d);
231 }
232
233 DEFUN ("atan", Fatan, 1, 2, 0, /*
234 Return the inverse tangent of ARG.
235 */
236        (arg1, arg2))
237 {
238   double d = extract_float (arg1);
239
240   if (NILP (arg2))
241     IN_FLOAT (d = atan (d), "atan", arg1);
242   else
243     {
244       double d2 = extract_float (arg2);
245 #ifdef FLOAT_CHECK_DOMAIN
246       if (d == 0.0 && d2 == 0.0)
247         domain_error2 ("atan", arg1, arg2);
248 #endif
249       IN_FLOAT2 (d = atan2 (d, d2), "atan", arg1, arg2);
250     }
251   return make_float (d);
252 }
253
254 DEFUN ("cos", Fcos, 1, 1, 0, /*
255 Return the cosine of ARG.
256 */
257        (arg))
258 {
259   double d = extract_float (arg);
260   IN_FLOAT (d = cos (d), "cos", arg);
261   return make_float (d);
262 }
263
264 DEFUN ("sin", Fsin, 1, 1, 0, /*
265 Return the sine of ARG.
266 */
267        (arg))
268 {
269   double d = extract_float (arg);
270   IN_FLOAT (d = sin (d), "sin", arg);
271   return make_float (d);
272 }
273
274 DEFUN ("tan", Ftan, 1, 1, 0, /*
275 Return the tangent of ARG.
276 */
277        (arg))
278 {
279   double d = extract_float (arg);
280   double c = cos (d);
281 #ifdef FLOAT_CHECK_DOMAIN
282   if (c == 0.0)
283     domain_error ("tan", arg);
284 #endif
285   IN_FLOAT (d = (sin (d) / c), "tan", arg);
286   return make_float (d);
287 }
288 #endif /* LISP_FLOAT_TYPE (trig functions) */
289
290 \f
291 /* Bessel functions */
292 #if 0 /* Leave these out unless we find there's a reason for them.  */
293 /* #ifdef LISP_FLOAT_TYPE */
294
295 DEFUN ("bessel-j0", Fbessel_j0, 1, 1, 0, /*
296 Return the bessel function j0 of ARG.
297 */
298        (arg))
299 {
300   double d = extract_float (arg);
301   IN_FLOAT (d = j0 (d), "bessel-j0", arg);
302   return make_float (d);
303 }
304
305 DEFUN ("bessel-j1", Fbessel_j1, 1, 1, 0, /*
306 Return the bessel function j1 of ARG.
307 */
308        (arg))
309 {
310   double d = extract_float (arg);
311   IN_FLOAT (d = j1 (d), "bessel-j1", arg);
312   return make_float (d);
313 }
314
315 DEFUN ("bessel-jn", Fbessel_jn, 2, 2, 0, /*
316 Return the order N bessel function output jn of ARG.
317 The first arg (the order) is truncated to an integer.
318 */
319        (arg1, arg2))
320 {
321   int i1 = extract_float (arg1);
322   double f2 = extract_float (arg2);
323
324   IN_FLOAT (f2 = jn (i1, f2), "bessel-jn", arg1);
325   return make_float (f2);
326 }
327
328 DEFUN ("bessel-y0", Fbessel_y0, 1, 1, 0, /*
329 Return the bessel function y0 of ARG.
330 */
331        (arg))
332 {
333   double d = extract_float (arg);
334   IN_FLOAT (d = y0 (d), "bessel-y0", arg);
335   return make_float (d);
336 }
337
338 DEFUN ("bessel-y1", Fbessel_y1, 1, 1, 0, /*
339 Return the bessel function y1 of ARG.
340 */
341        (arg))
342 {
343   double d = extract_float (arg);
344   IN_FLOAT (d = y1 (d), "bessel-y0", arg);
345   return make_float (d);
346 }
347
348 DEFUN ("bessel-yn", Fbessel_yn, 2, 2, 0, /*
349 Return the order N bessel function output yn of ARG.
350 The first arg (the order) is truncated to an integer.
351 */
352        (arg1, arg2))
353 {
354   int i1 = extract_float (arg1);
355   double f2 = extract_float (arg2);
356
357   IN_FLOAT (f2 = yn (i1, f2), "bessel-yn", arg1);
358   return make_float (f2);
359 }
360
361 #endif /* 0 (bessel functions) */
362 \f
363 /* Error functions. */
364 #if 0 /* Leave these out unless we see they are worth having.  */
365 /* #ifdef LISP_FLOAT_TYPE */
366
367 DEFUN ("erf", Ferf, 1, 1, 0, /*
368 Return the mathematical error function of ARG.
369 */
370        (arg))
371 {
372   double d = extract_float (arg);
373   IN_FLOAT (d = erf (d), "erf", arg);
374   return make_float (d);
375 }
376
377 DEFUN ("erfc", Ferfc, 1, 1, 0, /*
378 Return the complementary error function of ARG.
379 */
380        (arg))
381 {
382   double d = extract_float (arg);
383   IN_FLOAT (d = erfc (d), "erfc", arg);
384   return make_float (d);
385 }
386
387 DEFUN ("log-gamma", Flog_gamma, 1, 1, 0, /*
388 Return the log gamma of ARG.
389 */
390        (arg))
391 {
392   double d = extract_float (arg);
393   IN_FLOAT (d = lgamma (d), "log-gamma", arg);
394   return make_float (d);
395 }
396
397 #endif /* 0 (error functions) */
398
399 \f
400 /* Root and Log functions. */
401
402 #ifdef LISP_FLOAT_TYPE
403 DEFUN ("exp", Fexp, 1, 1, 0, /*
404 Return the exponential base e of ARG.
405 */
406        (arg))
407 {
408   double d = extract_float (arg);
409 #ifdef FLOAT_CHECK_DOMAIN
410   if (d > 709.7827)   /* Assume IEEE doubles here */
411     range_error ("exp", arg);
412   else if (d < -709.0)
413     return make_float (0.0);
414   else
415 #endif
416     IN_FLOAT (d = exp (d), "exp", arg);
417   return make_float (d);
418 }
419 #endif /* LISP_FLOAT_TYPE */
420
421
422 DEFUN ("expt", Fexpt, 2, 2, 0, /*
423 Return the exponential ARG1 ** ARG2.
424 */
425        (arg1, arg2))
426 {
427   if (INTP (arg1) && /* common lisp spec */
428       INTP (arg2)) /* don't promote, if both are ints */
429     {
430       EMACS_INT retval;
431       EMACS_INT x = XINT (arg1);
432       EMACS_INT y = XINT (arg2);
433
434       if (y < 0)
435         {
436           if (x == 1)
437             retval = 1;
438           else if (x == -1)
439             retval = (y & 1) ? -1 : 1;
440           else
441             retval = 0;
442         }
443       else
444         {
445           retval = 1;
446           while (y > 0)
447             {
448               if (y & 1)
449                 retval *= x;
450               x *= x;
451               y = (EMACS_UINT) y >> 1;
452             }
453         }
454       return make_int (retval);
455     }
456
457 #ifdef LISP_FLOAT_TYPE
458   {
459     double f1 = extract_float (arg1);
460     double f2 = extract_float (arg2);
461     /* Really should check for overflow, too */
462     if (f1 == 0.0 && f2 == 0.0)
463       f1 = 1.0;
464 # ifdef FLOAT_CHECK_DOMAIN
465     else if ((f1 == 0.0 && f2 < 0.0) || (f1 < 0 && f2 != floor(f2)))
466       domain_error2 ("expt", arg1, arg2);
467 # endif /* FLOAT_CHECK_DOMAIN */
468     IN_FLOAT2 (f1 = pow (f1, f2), "expt", arg1, arg2);
469     return make_float (f1);
470   }
471 #else
472   CHECK_INT_OR_FLOAT (arg1);
473   CHECK_INT_OR_FLOAT (arg2);
474   return Fexpt (arg1, arg2);
475 #endif /* LISP_FLOAT_TYPE */
476 }
477
478 #ifdef LISP_FLOAT_TYPE
479 DEFUN ("log", Flog, 1, 2, 0, /*
480 Return the natural logarithm of ARG.
481 If second optional argument BASE is given, return log ARG using that base.
482 */
483        (arg, base))
484 {
485   double d = extract_float (arg);
486 #ifdef FLOAT_CHECK_DOMAIN
487   if (d <= 0.0)
488     domain_error2 ("log", arg, base);
489 #endif
490   if (NILP (base))
491     IN_FLOAT (d = log (d), "log", arg);
492   else
493     {
494       double b = extract_float (base);
495 #ifdef FLOAT_CHECK_DOMAIN
496       if (b <= 0.0 || b == 1.0)
497         domain_error2 ("log", arg, base);
498 #endif
499       if (b == 10.0)
500         IN_FLOAT2 (d = log10 (d), "log", arg, base);
501       else
502         IN_FLOAT2 (d = (log (d) / log (b)), "log", arg, base);
503     }
504   return make_float (d);
505 }
506
507
508 DEFUN ("log10", Flog10, 1, 1, 0, /*
509 Return the logarithm base 10 of ARG.
510 */
511        (arg))
512 {
513   double d = extract_float (arg);
514 #ifdef FLOAT_CHECK_DOMAIN
515   if (d <= 0.0)
516     domain_error ("log10", arg);
517 #endif
518   IN_FLOAT (d = log10 (d), "log10", arg);
519   return make_float (d);
520 }
521
522
523 DEFUN ("sqrt", Fsqrt, 1, 1, 0, /*
524 Return the square root of ARG.
525 */
526        (arg))
527 {
528   double d = extract_float (arg);
529 #ifdef FLOAT_CHECK_DOMAIN
530   if (d < 0.0)
531     domain_error ("sqrt", arg);
532 #endif
533   IN_FLOAT (d = sqrt (d), "sqrt", arg);
534   return make_float (d);
535 }
536
537
538 DEFUN ("cube-root", Fcube_root, 1, 1, 0, /*
539 Return the cube root of ARG.
540 */
541        (arg))
542 {
543   double d = extract_float (arg);
544 #ifdef HAVE_CBRT
545   IN_FLOAT (d = cbrt (d), "cube-root", arg);
546 #else
547   if (d >= 0.0)
548     IN_FLOAT (d = pow (d, 1.0/3.0), "cube-root", arg);
549   else
550     IN_FLOAT (d = -pow (-d, 1.0/3.0), "cube-root", arg);
551 #endif
552   return make_float (d);
553 }
554 #endif /* LISP_FLOAT_TYPE */
555
556 \f
557 /* Inverse trig functions. */
558 #ifdef LISP_FLOAT_TYPE
559 /* #if 0  Not clearly worth adding...  */
560
561 DEFUN ("acosh", Facosh, 1, 1, 0, /*
562 Return the inverse hyperbolic cosine of ARG.
563 */
564        (arg))
565 {
566   double d = extract_float (arg);
567 #ifdef FLOAT_CHECK_DOMAIN
568   if (d < 1.0)
569     domain_error ("acosh", arg);
570 #endif
571 #ifdef HAVE_INVERSE_HYPERBOLIC
572   IN_FLOAT (d = acosh (d), "acosh", arg);
573 #else
574   IN_FLOAT (d = log (d + sqrt (d*d - 1.0)), "acosh", arg);
575 #endif
576   return make_float (d);
577 }
578
579 DEFUN ("asinh", Fasinh, 1, 1, 0, /*
580 Return the inverse hyperbolic sine of ARG.
581 */
582        (arg))
583 {
584   double d = extract_float (arg);
585 #ifdef HAVE_INVERSE_HYPERBOLIC
586   IN_FLOAT (d = asinh (d), "asinh", arg);
587 #else
588   IN_FLOAT (d = log (d + sqrt (d*d + 1.0)), "asinh", arg);
589 #endif
590   return make_float (d);
591 }
592
593 DEFUN ("atanh", Fatanh, 1, 1, 0, /*
594 Return the inverse hyperbolic tangent of ARG.
595 */
596        (arg))
597 {
598   double d = extract_float (arg);
599 #ifdef FLOAT_CHECK_DOMAIN
600   if (d >= 1.0 || d <= -1.0)
601     domain_error ("atanh", arg);
602 #endif
603 #ifdef HAVE_INVERSE_HYPERBOLIC
604   IN_FLOAT (d = atanh (d), "atanh", arg);
605 #else
606   IN_FLOAT (d = 0.5 * log ((1.0 + d) / (1.0 - d)), "atanh", arg);
607 #endif
608   return make_float (d);
609 }
610
611 DEFUN ("cosh", Fcosh, 1, 1, 0, /*
612 Return the hyperbolic cosine of ARG.
613 */
614        (arg))
615 {
616   double d = extract_float (arg);
617 #ifdef FLOAT_CHECK_DOMAIN
618   if (d > 710.0 || d < -710.0)
619     range_error ("cosh", arg);
620 #endif
621   IN_FLOAT (d = cosh (d), "cosh", arg);
622   return make_float (d);
623 }
624
625 DEFUN ("sinh", Fsinh, 1, 1, 0, /*
626 Return the hyperbolic sine of ARG.
627 */
628        (arg))
629 {
630   double d = extract_float (arg);
631 #ifdef FLOAT_CHECK_DOMAIN
632   if (d > 710.0 || d < -710.0)
633     range_error ("sinh", arg);
634 #endif
635   IN_FLOAT (d = sinh (d), "sinh", arg);
636   return make_float (d);
637 }
638
639 DEFUN ("tanh", Ftanh, 1, 1, 0, /*
640 Return the hyperbolic tangent of ARG.
641 */
642        (arg))
643 {
644   double d = extract_float (arg);
645   IN_FLOAT (d = tanh (d), "tanh", arg);
646   return make_float (d);
647 }
648 #endif /* LISP_FLOAT_TYPE (inverse trig functions) */
649 \f
650 /* Rounding functions */
651
652 DEFUN ("abs", Fabs, 1, 1, 0, /*
653 Return the absolute value of ARG.
654 */
655        (arg))
656 {
657 #ifdef LISP_FLOAT_TYPE
658   if (FLOATP (arg))
659     {
660       IN_FLOAT (arg = make_float (fabs (XFLOAT_DATA (arg))),
661                 "abs", arg);
662       return arg;
663     }
664 #endif /* LISP_FLOAT_TYPE */
665
666   if (INTP (arg))
667     return (XINT (arg) >= 0) ? arg : make_int (- XINT (arg));
668
669   return Fabs (wrong_type_argument (Qnumberp, arg));
670 }
671
672 #ifdef LISP_FLOAT_TYPE
673 DEFUN ("float", Ffloat, 1, 1, 0, /*
674 Return the floating point number numerically equal to ARG.
675 */
676        (arg))
677 {
678   if (INTP (arg))
679     return make_float ((double) XINT (arg));
680
681   if (FLOATP (arg))             /* give 'em the same float back */
682     return arg;
683
684   return Ffloat (wrong_type_argument (Qnumberp, arg));
685 }
686 #endif /* LISP_FLOAT_TYPE */
687
688
689 #ifdef LISP_FLOAT_TYPE
690 DEFUN ("logb", Flogb, 1, 1, 0, /*
691 Return largest integer <= the base 2 log of the magnitude of ARG.
692 This is the same as the exponent of a float.
693 */
694        (arg))
695 {
696   double f = extract_float (arg);
697
698   if (f == 0.0)
699     return make_int (- (int)((((EMACS_UINT) 1) << (VALBITS - 1)))); /* most-negative-fixnum */
700 #ifdef HAVE_LOGB
701   {
702     Lisp_Object val;
703     IN_FLOAT (val = make_int ((int) logb (f)), "logb", arg);
704     return (val);
705   }
706 #else
707 #ifdef HAVE_FREXP
708   {
709     int exqp;
710     IN_FLOAT (frexp (f, &exqp), "logb", arg);
711     return (make_int (exqp - 1));
712   }
713 #else
714   {
715     int i;
716     double d;
717     EMACS_INT val;
718     if (f < 0.0)
719       f = -f;
720     val = -1;
721     while (f < 0.5)
722       {
723         for (i = 1, d = 0.5; d * d >= f; i += i)
724           d *= d;
725         f /= d;
726         val -= i;
727       }
728     while (f >= 1.0)
729       {
730         for (i = 1, d = 2.0; d * d <= f; i += i)
731           d *= d;
732         f /= d;
733         val += i;
734       }
735     return (make_int (val));
736   }
737 #endif /* ! HAVE_FREXP */
738 #endif /* ! HAVE_LOGB */
739 }
740 #endif /* LISP_FLOAT_TYPE */
741
742
743 DEFUN ("ceiling", Fceiling, 1, 1, 0, /*
744 Return the smallest integer no less than ARG.  (Round toward +inf.)
745 */
746        (arg))
747 {
748 #ifdef LISP_FLOAT_TYPE
749   if (FLOATP (arg))
750     {
751       double d;
752       IN_FLOAT ((d = ceil (XFLOAT_DATA (arg))), "ceiling", arg);
753       return (float_to_int (d, "ceiling", arg, Qunbound));
754     }
755 #endif /* LISP_FLOAT_TYPE */
756
757   if (INTP (arg))
758     return arg;
759
760   return Fceiling (wrong_type_argument (Qnumberp, arg));
761 }
762
763
764 DEFUN ("floor", Ffloor, 1, 2, 0, /*
765 Return the largest integer no greater than ARG.  (Round towards -inf.)
766 With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR.
767 */
768        (arg, divisor))
769 {
770   CHECK_INT_OR_FLOAT (arg);
771
772   if (! NILP (divisor))
773     {
774       EMACS_INT i1, i2;
775
776       CHECK_INT_OR_FLOAT (divisor);
777
778 #ifdef LISP_FLOAT_TYPE
779       if (FLOATP (arg) || FLOATP (divisor))
780         {
781           double f1 = extract_float (arg);
782           double f2 = extract_float (divisor);
783
784           if (f2 == 0)
785             Fsignal (Qarith_error, Qnil);
786
787           IN_FLOAT2 (f1 = floor (f1 / f2), "floor", arg, divisor);
788           return float_to_int (f1, "floor", arg, divisor);
789         }
790 #endif /* LISP_FLOAT_TYPE */
791
792       i1 = XINT (arg);
793       i2 = XINT (divisor);
794
795       if (i2 == 0)
796         Fsignal (Qarith_error, Qnil);
797
798       /* With C's /, the result is implementation-defined if either operand
799          is negative, so use only nonnegative operands.  */
800       i1 = (i2 < 0
801             ? (i1 <= 0  ?  -i1 / -i2  :  -1 - ((i1 - 1) / -i2))
802             : (i1 < 0  ?  -1 - ((-1 - i1) / i2)  :  i1 / i2));
803
804       return (make_int (i1));
805     }
806
807 #ifdef LISP_FLOAT_TYPE
808   if (FLOATP (arg))
809     {
810       double d;
811       IN_FLOAT ((d = floor (XFLOAT_DATA (arg))), "floor", arg);
812       return (float_to_int (d, "floor", arg, Qunbound));
813     }
814 #endif /* LISP_FLOAT_TYPE */
815
816   return arg;
817 }
818
819 DEFUN ("round", Fround, 1, 1, 0, /*
820 Return the nearest integer to ARG.
821 */
822        (arg))
823 {
824 #ifdef LISP_FLOAT_TYPE
825   if (FLOATP (arg))
826     {
827       double d;
828       /* Screw the prevailing rounding mode.  */
829       IN_FLOAT ((d = rint (XFLOAT_DATA (arg))), "round", arg);
830       return (float_to_int (d, "round", arg, Qunbound));
831     }
832 #endif /* LISP_FLOAT_TYPE */
833
834   if (INTP (arg))
835     return arg;
836
837   return Fround (wrong_type_argument (Qnumberp, arg));
838 }
839
840 DEFUN ("truncate", Ftruncate, 1, 1, 0, /*
841 Truncate a floating point number to an integer.
842 Rounds the value toward zero.
843 */
844        (arg))
845 {
846 #ifdef LISP_FLOAT_TYPE
847   if (FLOATP (arg))
848     return float_to_int (XFLOAT_DATA (arg), "truncate", arg, Qunbound);
849 #endif /* LISP_FLOAT_TYPE */
850
851   if (INTP (arg))
852     return arg;
853
854   return Ftruncate (wrong_type_argument (Qnumberp, arg));
855 }
856 \f
857 /* Float-rounding functions. */
858 #ifdef LISP_FLOAT_TYPE
859 /* #if 1  It's not clear these are worth adding... */
860
861 DEFUN ("fceiling", Ffceiling, 1, 1, 0, /*
862 Return the smallest integer no less than ARG, as a float.
863 \(Round toward +inf.\)
864 */
865        (arg))
866 {
867   double d = extract_float (arg);
868   IN_FLOAT (d = ceil (d), "fceiling", arg);
869   return make_float (d);
870 }
871
872 DEFUN ("ffloor", Fffloor, 1, 1, 0, /*
873 Return the largest integer no greater than ARG, as a float.
874 \(Round towards -inf.\)
875 */
876        (arg))
877 {
878   double d = extract_float (arg);
879   IN_FLOAT (d = floor (d), "ffloor", arg);
880   return make_float (d);
881 }
882
883 DEFUN ("fround", Ffround, 1, 1, 0, /*
884 Return the nearest integer to ARG, as a float.
885 */
886        (arg))
887 {
888   double d = extract_float (arg);
889   IN_FLOAT (d = rint (d), "fround", arg);
890   return make_float (d);
891 }
892
893 DEFUN ("ftruncate", Fftruncate, 1, 1, 0, /*
894 Truncate a floating point number to an integral float value.
895 Rounds the value toward zero.
896 */
897        (arg))
898 {
899   double d = extract_float (arg);
900   if (d >= 0.0)
901     IN_FLOAT (d = floor (d), "ftruncate", arg);
902   else
903     IN_FLOAT (d = ceil (d), "ftruncate", arg);
904   return make_float (d);
905 }
906
907 #endif /* LISP_FLOAT_TYPE (float-rounding functions) */
908
909 \f
910 #ifdef LISP_FLOAT_TYPE
911 #ifdef FLOAT_CATCH_SIGILL
912 static SIGTYPE
913 float_error (int signo)
914 {
915   if (! in_float)
916     fatal_error_signal (signo);
917
918   EMACS_REESTABLISH_SIGNAL (signo, arith_error);
919   EMACS_UNBLOCK_SIGNAL (signo);
920
921   in_float = 0;
922
923   /* Was Fsignal(), but it just doesn't make sense for an error
924      occurring inside a signal handler to be restartable, considering
925      that anything could happen when the error is signaled and trapped
926      and considering the asynchronous nature of signal handlers. */
927   signal_error (Qarith_error, list1 (float_error_arg));
928 }
929
930 /* Another idea was to replace the library function `infnan'
931    where SIGILL is signaled.  */
932
933 #endif /* FLOAT_CATCH_SIGILL */
934
935 /* In C++, it is impossible to determine what type matherr expects
936    without some more configure magic.
937    We shouldn't be using matherr anyways - it's a non-standard SYSVism. */
938 #if defined (HAVE_MATHERR) && !defined(__cplusplus)
939 int
940 matherr (struct exception *x)
941 {
942   Lisp_Object args;
943   if (! in_float)
944     /* Not called from emacs-lisp float routines; do the default thing. */
945     return 0;
946
947   /* if (!strcmp (x->name, "pow")) x->name = "expt"; */
948
949   args = Fcons (build_string (x->name),
950                 Fcons (make_float (x->arg1),
951                        ((in_float == 2)
952                         ? Fcons (make_float (x->arg2), Qnil)
953                         : Qnil)));
954   switch (x->type)
955     {
956     case DOMAIN:    Fsignal (Qdomain_error,      args); break;
957     case SING:      Fsignal (Qsingularity_error, args); break;
958     case OVERFLOW:  Fsignal (Qoverflow_error,    args); break;
959     case UNDERFLOW: Fsignal (Qunderflow_error,   args); break;
960     default:        Fsignal (Qarith_error,       args); break;
961     }
962   return 1;     /* don't set errno or print a message */
963 }
964 #endif /* HAVE_MATHERR */
965 #endif /* LISP_FLOAT_TYPE */
966
967 \f
968 void
969 init_floatfns_very_early (void)
970 {
971 #ifdef LISP_FLOAT_TYPE
972 # ifdef FLOAT_CATCH_SIGILL
973   signal (SIGILL, float_error);
974 # endif
975   in_float = 0;
976 #endif /* LISP_FLOAT_TYPE */
977 }
978
979 void
980 syms_of_floatfns (void)
981 {
982
983   /* Trig functions.  */
984
985 #ifdef LISP_FLOAT_TYPE
986   DEFSUBR (Facos);
987   DEFSUBR (Fasin);
988   DEFSUBR (Fatan);
989   DEFSUBR (Fcos);
990   DEFSUBR (Fsin);
991   DEFSUBR (Ftan);
992 #endif /* LISP_FLOAT_TYPE */
993
994   /* Bessel functions */
995
996 #if 0
997   DEFSUBR (Fbessel_y0);
998   DEFSUBR (Fbessel_y1);
999   DEFSUBR (Fbessel_yn);
1000   DEFSUBR (Fbessel_j0);
1001   DEFSUBR (Fbessel_j1);
1002   DEFSUBR (Fbessel_jn);
1003 #endif /* 0 */
1004
1005   /* Error functions. */
1006
1007 #if 0
1008   DEFSUBR (Ferf);
1009   DEFSUBR (Ferfc);
1010   DEFSUBR (Flog_gamma);
1011 #endif /* 0 */
1012
1013   /* Root and Log functions. */
1014
1015 #ifdef LISP_FLOAT_TYPE
1016   DEFSUBR (Fexp);
1017 #endif /* LISP_FLOAT_TYPE */
1018   DEFSUBR (Fexpt);
1019 #ifdef LISP_FLOAT_TYPE
1020   DEFSUBR (Flog);
1021   DEFSUBR (Flog10);
1022   DEFSUBR (Fsqrt);
1023   DEFSUBR (Fcube_root);
1024 #endif /* LISP_FLOAT_TYPE */
1025
1026   /* Inverse trig functions. */
1027
1028 #ifdef LISP_FLOAT_TYPE
1029   DEFSUBR (Facosh);
1030   DEFSUBR (Fasinh);
1031   DEFSUBR (Fatanh);
1032   DEFSUBR (Fcosh);
1033   DEFSUBR (Fsinh);
1034   DEFSUBR (Ftanh);
1035 #endif /* LISP_FLOAT_TYPE */
1036
1037   /* Rounding functions */
1038
1039   DEFSUBR (Fabs);
1040 #ifdef LISP_FLOAT_TYPE
1041   DEFSUBR (Ffloat);
1042   DEFSUBR (Flogb);
1043 #endif /* LISP_FLOAT_TYPE */
1044   DEFSUBR (Fceiling);
1045   DEFSUBR (Ffloor);
1046   DEFSUBR (Fround);
1047   DEFSUBR (Ftruncate);
1048
1049   /* Float-rounding functions. */
1050
1051 #ifdef LISP_FLOAT_TYPE
1052   DEFSUBR (Ffceiling);
1053   DEFSUBR (Fffloor);
1054   DEFSUBR (Ffround);
1055   DEFSUBR (Fftruncate);
1056 #endif /* LISP_FLOAT_TYPE */
1057 }
1058
1059 void
1060 vars_of_floatfns (void)
1061 {
1062 #ifdef LISP_FLOAT_TYPE
1063   Fprovide (intern ("lisp-float-type"));
1064 #endif
1065 }