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