Update copyright.
[bpt/emacs.git] / src / floatfns.c
CommitLineData
b70021f4 1/* Primitive operations on floating point for GNU Emacs Lisp interpreter.
3a22ee35 2 Copyright (C) 1988, 1993, 1994 Free Software Foundation, Inc.
b70021f4
MR
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
4746118a 8the Free Software Foundation; either version 2, or (at your option)
b70021f4
MR
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
4b6baf5f
RS
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.
24
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.
29
30 Define HAVE_MATHERR if on a system supporting the SysV matherr callback.
31 (This should happen automatically.)
32
33 Define FLOAT_CHECK_ERRNO if the float library routines set errno.
34 This has no effect if HAVE_MATHERR is defined.
35
36 Define FLOAT_CATCH_SIGILL if the float library routines signal SIGILL.
37 (What systems actually do this? Please let us know.)
38
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.)
44 */
45
b70021f4
MR
46#include <signal.h>
47
18160b98 48#include <config.h>
b70021f4 49#include "lisp.h"
e065a56e 50#include "syssignal.h"
b70021f4
MR
51
52Lisp_Object Qarith_error;
53
54#ifdef LISP_FLOAT_TYPE
265a9e55 55
a3fc5236 56#ifdef MSDOS
6ba93f9d 57/* These are redefined (correctly, but differently) in values.h. */
a3fc5236
RS
58#undef INTBITS
59#undef LONGBITS
60#undef SHORTBITS
61#endif
a3fc5236 62
4cd7a373
RS
63/* Work around a problem that happens because math.h on hpux 7
64 defines two static variables--which, in Emacs, are not really static,
65 because `static' is defined as nothing. The problem is that they are
66 defined both here and in lread.c.
67 These macros prevent the name conflict. */
68#if defined (HPUX) && !defined (HPUX8)
69#define _MAXLDBL floatfns_maxldbl
70#define _NMAXLDBL floatfns_nmaxldbl
71#endif
72
b70021f4 73#include <math.h>
4b6baf5f 74
32085e8e 75/* This declaration is omitted on some systems, like Ultrix. */
3c602515 76#if !defined (HPUX) && defined (HAVE_LOGB)
c26406fe 77extern double logb ();
3c602515 78#endif /* not HPUX and HAVE_LOGB */
c26406fe 79
a3fc5236 80#ifndef MSDOS
4b6baf5f
RS
81#if defined(DOMAIN) && defined(SING) && defined(OVERFLOW)
82 /* If those are defined, then this is probably a `matherr' machine. */
83# ifndef HAVE_MATHERR
84# define HAVE_MATHERR
85# endif
86#endif
a3fc5236 87#endif
4b6baf5f 88
c0f0a4a2 89#ifdef NO_MATHERR
f89182a2
RS
90#undef HAVE_MATHERR
91#endif
92
4b6baf5f
RS
93#ifdef HAVE_MATHERR
94# ifdef FLOAT_CHECK_ERRNO
95# undef FLOAT_CHECK_ERRNO
96# endif
97# ifdef FLOAT_CHECK_DOMAIN
98# undef FLOAT_CHECK_DOMAIN
99# endif
100#endif
101
102#ifndef NO_FLOAT_CHECK_ERRNO
103#define FLOAT_CHECK_ERRNO
104#endif
105
106#ifdef FLOAT_CHECK_ERRNO
107# include <errno.h>
265a9e55
JB
108
109extern int errno;
4b6baf5f 110#endif
265a9e55
JB
111
112/* Avoid traps on VMS from sinh and cosh.
113 All the other functions set errno instead. */
114
115#ifdef VMS
116#undef cosh
117#undef sinh
118#define cosh(x) ((exp(x)+exp(-x))*0.5)
119#define sinh(x) ((exp(x)-exp(-x))*0.5)
120#endif /* VMS */
121
4b6baf5f
RS
122#ifndef HAVE_RINT
123#define rint(x) (floor((x)+0.5))
124#endif
125
4746118a 126static SIGTYPE float_error ();
b70021f4
MR
127
128/* Nonzero while executing in floating point.
129 This tells float_error what to do. */
130
131static int in_float;
132
133/* If an argument is out of range for a mathematical function,
265a9e55 134 here is the actual argument value to use in the error message. */
b70021f4 135
4b6baf5f
RS
136static Lisp_Object float_error_arg, float_error_arg2;
137
138static char *float_error_fn_name;
b70021f4 139
265a9e55
JB
140/* Evaluate the floating point expression D, recording NUM
141 as the original argument for error messages.
142 D is normally an assignment expression.
f8d83099
JB
143 Handle errors which may result in signals or may set errno.
144
145 Note that float_error may be declared to return void, so you can't
146 just cast the zero after the colon to (SIGTYPE) to make the types
147 check properly. */
265a9e55 148
4b6baf5f
RS
149#ifdef FLOAT_CHECK_ERRNO
150#define IN_FLOAT(d, name, num) \
151 do { \
152 float_error_arg = num; \
153 float_error_fn_name = name; \
154 in_float = 1; errno = 0; (d); in_float = 0; \
155 switch (errno) { \
156 case 0: break; \
157 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
158 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
159 default: arith_error (float_error_fn_name, float_error_arg); \
160 } \
161 } while (0)
162#define IN_FLOAT2(d, name, num, num2) \
163 do { \
164 float_error_arg = num; \
165 float_error_arg2 = num2; \
166 float_error_fn_name = name; \
167 in_float = 1; errno = 0; (d); in_float = 0; \
168 switch (errno) { \
169 case 0: break; \
170 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
171 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
172 default: arith_error (float_error_fn_name, float_error_arg); \
173 } \
174 } while (0)
175#else
f8131ed2 176#define IN_FLOAT(d, name, num) (in_float = 1, (d), in_float = 0)
4b6baf5f
RS
177#define IN_FLOAT2(d, name, num, num2) (in_float = 1, (d), in_float = 0)
178#endif
179
81a63ccc
KH
180/* Convert float to Lisp_Int if it fits, else signal a range error
181 using the given arguments. */
182#define FLOAT_TO_INT(x, i, name, num) \
183 do \
184 { \
185 if ((x) >= (1 << (VALBITS-1)) || (x) <= - (1 << (VALBITS-1)) - 1) \
186 range_error (name, num); \
187 XSET (i, Lisp_Int, (int)(x)); \
188 } \
189 while (0)
190#define FLOAT_TO_INT2(x, i, name, num1, num2) \
191 do \
192 { \
193 if ((x) >= (1 << (VALBITS-1)) || (x) <= - (1 << (VALBITS-1)) - 1) \
194 range_error2 (name, num1, num2); \
195 XSET (i, Lisp_Int, (int)(x)); \
196 } \
197 while (0)
198
4b6baf5f
RS
199#define arith_error(op,arg) \
200 Fsignal (Qarith_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
201#define range_error(op,arg) \
202 Fsignal (Qrange_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
81a63ccc
KH
203#define range_error2(op,a1,a2) \
204 Fsignal (Qrange_error, Fcons (build_string ((op)), \
205 Fcons ((a1), Fcons ((a2), Qnil))))
4b6baf5f
RS
206#define domain_error(op,arg) \
207 Fsignal (Qdomain_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
208#define domain_error2(op,a1,a2) \
81a63ccc
KH
209 Fsignal (Qdomain_error, Fcons (build_string ((op)), \
210 Fcons ((a1), Fcons ((a2), Qnil))))
b70021f4
MR
211
212/* Extract a Lisp number as a `double', or signal an error. */
213
214double
215extract_float (num)
216 Lisp_Object num;
217{
218 CHECK_NUMBER_OR_FLOAT (num, 0);
219
220 if (XTYPE (num) == Lisp_Float)
221 return XFLOAT (num)->data;
222 return (double) XINT (num);
223}
c2d4ea74
RS
224\f
225/* Trig functions. */
b70021f4
MR
226
227DEFUN ("acos", Facos, Sacos, 1, 1, 0,
228 "Return the inverse cosine of ARG.")
4b6baf5f
RS
229 (arg)
230 register Lisp_Object arg;
b70021f4 231{
4b6baf5f
RS
232 double d = extract_float (arg);
233#ifdef FLOAT_CHECK_DOMAIN
234 if (d > 1.0 || d < -1.0)
235 domain_error ("acos", arg);
236#endif
237 IN_FLOAT (d = acos (d), "acos", arg);
b70021f4
MR
238 return make_float (d);
239}
240
c2d4ea74
RS
241DEFUN ("asin", Fasin, Sasin, 1, 1, 0,
242 "Return the inverse sine of ARG.")
4b6baf5f
RS
243 (arg)
244 register Lisp_Object arg;
b70021f4 245{
4b6baf5f
RS
246 double d = extract_float (arg);
247#ifdef FLOAT_CHECK_DOMAIN
248 if (d > 1.0 || d < -1.0)
249 domain_error ("asin", arg);
250#endif
251 IN_FLOAT (d = asin (d), "asin", arg);
b70021f4
MR
252 return make_float (d);
253}
254
c2d4ea74
RS
255DEFUN ("atan", Fatan, Satan, 1, 1, 0,
256 "Return the inverse tangent of ARG.")
4b6baf5f
RS
257 (arg)
258 register Lisp_Object arg;
b70021f4 259{
4b6baf5f
RS
260 double d = extract_float (arg);
261 IN_FLOAT (d = atan (d), "atan", arg);
b70021f4
MR
262 return make_float (d);
263}
264
c2d4ea74
RS
265DEFUN ("cos", Fcos, Scos, 1, 1, 0,
266 "Return the cosine of ARG.")
4b6baf5f
RS
267 (arg)
268 register Lisp_Object arg;
b70021f4 269{
4b6baf5f
RS
270 double d = extract_float (arg);
271 IN_FLOAT (d = cos (d), "cos", arg);
b70021f4
MR
272 return make_float (d);
273}
274
c2d4ea74
RS
275DEFUN ("sin", Fsin, Ssin, 1, 1, 0,
276 "Return the sine of ARG.")
4b6baf5f
RS
277 (arg)
278 register Lisp_Object arg;
b70021f4 279{
4b6baf5f
RS
280 double d = extract_float (arg);
281 IN_FLOAT (d = sin (d), "sin", arg);
b70021f4
MR
282 return make_float (d);
283}
284
c2d4ea74
RS
285DEFUN ("tan", Ftan, Stan, 1, 1, 0,
286 "Return the tangent of ARG.")
4b6baf5f
RS
287 (arg)
288 register Lisp_Object arg;
289{
290 double d = extract_float (arg);
291 double c = cos (d);
292#ifdef FLOAT_CHECK_DOMAIN
293 if (c == 0.0)
294 domain_error ("tan", arg);
295#endif
296 IN_FLOAT (d = sin (d) / c, "tan", arg);
b70021f4
MR
297 return make_float (d);
298}
299\f
c2d4ea74
RS
300#if 0 /* Leave these out unless we find there's a reason for them. */
301
b70021f4
MR
302DEFUN ("bessel-j0", Fbessel_j0, Sbessel_j0, 1, 1, 0,
303 "Return the bessel function j0 of ARG.")
4b6baf5f
RS
304 (arg)
305 register Lisp_Object arg;
b70021f4 306{
4b6baf5f
RS
307 double d = extract_float (arg);
308 IN_FLOAT (d = j0 (d), "bessel-j0", arg);
b70021f4
MR
309 return make_float (d);
310}
311
312DEFUN ("bessel-j1", Fbessel_j1, Sbessel_j1, 1, 1, 0,
313 "Return the bessel function j1 of ARG.")
4b6baf5f
RS
314 (arg)
315 register Lisp_Object arg;
b70021f4 316{
4b6baf5f
RS
317 double d = extract_float (arg);
318 IN_FLOAT (d = j1 (d), "bessel-j1", arg);
b70021f4
MR
319 return make_float (d);
320}
321
322DEFUN ("bessel-jn", Fbessel_jn, Sbessel_jn, 2, 2, 0,
323 "Return the order N bessel function output jn of ARG.\n\
324The first arg (the order) is truncated to an integer.")
4b6baf5f
RS
325 (arg1, arg2)
326 register Lisp_Object arg1, arg2;
b70021f4 327{
4b6baf5f
RS
328 int i1 = extract_float (arg1);
329 double f2 = extract_float (arg2);
b70021f4 330
4b6baf5f 331 IN_FLOAT (f2 = jn (i1, f2), "bessel-jn", arg1);
b70021f4
MR
332 return make_float (f2);
333}
334
335DEFUN ("bessel-y0", Fbessel_y0, Sbessel_y0, 1, 1, 0,
336 "Return the bessel function y0 of ARG.")
4b6baf5f
RS
337 (arg)
338 register Lisp_Object arg;
b70021f4 339{
4b6baf5f
RS
340 double d = extract_float (arg);
341 IN_FLOAT (d = y0 (d), "bessel-y0", arg);
b70021f4
MR
342 return make_float (d);
343}
344
345DEFUN ("bessel-y1", Fbessel_y1, Sbessel_y1, 1, 1, 0,
346 "Return the bessel function y1 of ARG.")
4b6baf5f
RS
347 (arg)
348 register Lisp_Object arg;
b70021f4 349{
4b6baf5f
RS
350 double d = extract_float (arg);
351 IN_FLOAT (d = y1 (d), "bessel-y0", arg);
b70021f4
MR
352 return make_float (d);
353}
354
355DEFUN ("bessel-yn", Fbessel_yn, Sbessel_yn, 2, 2, 0,
356 "Return the order N bessel function output yn of ARG.\n\
357The first arg (the order) is truncated to an integer.")
4b6baf5f
RS
358 (arg1, arg2)
359 register Lisp_Object arg1, arg2;
b70021f4 360{
4b6baf5f
RS
361 int i1 = extract_float (arg1);
362 double f2 = extract_float (arg2);
b70021f4 363
4b6baf5f 364 IN_FLOAT (f2 = yn (i1, f2), "bessel-yn", arg1);
b70021f4
MR
365 return make_float (f2);
366}
b70021f4 367
c2d4ea74
RS
368#endif
369\f
370#if 0 /* Leave these out unless we see they are worth having. */
b70021f4
MR
371
372DEFUN ("erf", Ferf, Serf, 1, 1, 0,
373 "Return the mathematical error function of ARG.")
4b6baf5f
RS
374 (arg)
375 register Lisp_Object arg;
b70021f4 376{
4b6baf5f
RS
377 double d = extract_float (arg);
378 IN_FLOAT (d = erf (d), "erf", arg);
b70021f4
MR
379 return make_float (d);
380}
381
382DEFUN ("erfc", Ferfc, Serfc, 1, 1, 0,
383 "Return the complementary error function of ARG.")
4b6baf5f
RS
384 (arg)
385 register Lisp_Object arg;
b70021f4 386{
4b6baf5f
RS
387 double d = extract_float (arg);
388 IN_FLOAT (d = erfc (d), "erfc", arg);
b70021f4
MR
389 return make_float (d);
390}
391
b70021f4
MR
392DEFUN ("log-gamma", Flog_gamma, Slog_gamma, 1, 1, 0,
393 "Return the log gamma of ARG.")
4b6baf5f
RS
394 (arg)
395 register Lisp_Object arg;
b70021f4 396{
4b6baf5f
RS
397 double d = extract_float (arg);
398 IN_FLOAT (d = lgamma (d), "log-gamma", arg);
b70021f4
MR
399 return make_float (d);
400}
401
4b6baf5f 402DEFUN ("cube-root", Fcube_root, Scube_root, 1, 1, 0,
c2d4ea74 403 "Return the cube root of ARG.")
4b6baf5f
RS
404 (arg)
405 register Lisp_Object arg;
b70021f4 406{
4b6baf5f
RS
407 double d = extract_float (arg);
408#ifdef HAVE_CBRT
409 IN_FLOAT (d = cbrt (d), "cube-root", arg);
410#else
411 if (d >= 0.0)
412 IN_FLOAT (d = pow (d, 1.0/3.0), "cube-root", arg);
413 else
414 IN_FLOAT (d = -pow (-d, 1.0/3.0), "cube-root", arg);
415#endif
b70021f4
MR
416 return make_float (d);
417}
418
706ac90d
RS
419#endif
420\f
c2d4ea74
RS
421DEFUN ("exp", Fexp, Sexp, 1, 1, 0,
422 "Return the exponential base e of ARG.")
4b6baf5f
RS
423 (arg)
424 register Lisp_Object arg;
425{
426 double d = extract_float (arg);
427#ifdef FLOAT_CHECK_DOMAIN
428 if (d > 709.7827) /* Assume IEEE doubles here */
429 range_error ("exp", arg);
430 else if (d < -709.0)
431 return make_float (0.0);
432 else
433#endif
434 IN_FLOAT (d = exp (d), "exp", arg);
b70021f4
MR
435 return make_float (d);
436}
437
b70021f4 438DEFUN ("expt", Fexpt, Sexpt, 2, 2, 0,
c2d4ea74 439 "Return the exponential X ** Y.")
4b6baf5f
RS
440 (arg1, arg2)
441 register Lisp_Object arg1, arg2;
b70021f4
MR
442{
443 double f1, f2;
444
4b6baf5f
RS
445 CHECK_NUMBER_OR_FLOAT (arg1, 0);
446 CHECK_NUMBER_OR_FLOAT (arg2, 0);
28d849db
RS
447 if (XTYPE (arg1) == Lisp_Int /* common lisp spec */
448 && XTYPE (arg2) == Lisp_Int) /* don't promote, if both are ints */
b70021f4 449 { /* this can be improved by pre-calculating */
eb8c3be9 450 int acc, x, y; /* some binary powers of x then accumulating */
4be1d460
RS
451 Lisp_Object val;
452
4b6baf5f
RS
453 x = XINT (arg1);
454 y = XINT (arg2);
b70021f4
MR
455 acc = 1;
456
457 if (y < 0)
458 {
4b6baf5f
RS
459 if (x == 1)
460 acc = 1;
461 else if (x == -1)
462 acc = (y & 1) ? -1 : 1;
463 else
464 acc = 0;
b70021f4
MR
465 }
466 else
467 {
4b6baf5f
RS
468 while (y > 0)
469 {
470 if (y & 1)
471 acc *= x;
472 x *= x;
473 y = (unsigned)y >> 1;
474 }
b70021f4 475 }
4be1d460
RS
476 XSET (val, Lisp_Int, acc);
477 return val;
b70021f4 478 }
4b6baf5f
RS
479 f1 = (XTYPE (arg1) == Lisp_Float) ? XFLOAT (arg1)->data : XINT (arg1);
480 f2 = (XTYPE (arg2) == Lisp_Float) ? XFLOAT (arg2)->data : XINT (arg2);
481 /* Really should check for overflow, too */
482 if (f1 == 0.0 && f2 == 0.0)
483 f1 = 1.0;
484#ifdef FLOAT_CHECK_DOMAIN
485 else if ((f1 == 0.0 && f2 < 0.0) || (f1 < 0 && f2 != floor(f2)))
486 domain_error2 ("expt", arg1, arg2);
487#endif
28d849db 488 IN_FLOAT2 (f1 = pow (f1, f2), "expt", arg1, arg2);
b70021f4
MR
489 return make_float (f1);
490}
c2d4ea74 491
56abb480 492DEFUN ("log", Flog, Slog, 1, 2, 0,
4b6baf5f
RS
493 "Return the natural logarithm of ARG.\n\
494If second optional argument BASE is given, return log ARG using that base.")
495 (arg, base)
496 register Lisp_Object arg, base;
b70021f4 497{
4b6baf5f 498 double d = extract_float (arg);
56abb480 499
4b6baf5f
RS
500#ifdef FLOAT_CHECK_DOMAIN
501 if (d <= 0.0)
502 domain_error2 ("log", arg, base);
503#endif
56abb480 504 if (NILP (base))
4b6baf5f 505 IN_FLOAT (d = log (d), "log", arg);
56abb480
JB
506 else
507 {
508 double b = extract_float (base);
509
4b6baf5f
RS
510#ifdef FLOAT_CHECK_DOMAIN
511 if (b <= 0.0 || b == 1.0)
512 domain_error2 ("log", arg, base);
513#endif
514 if (b == 10.0)
515 IN_FLOAT2 (d = log10 (d), "log", arg, base);
516 else
f8131ed2 517 IN_FLOAT2 (d = log (d) / log (b), "log", arg, base);
56abb480 518 }
b70021f4
MR
519 return make_float (d);
520}
521
c2d4ea74
RS
522DEFUN ("log10", Flog10, Slog10, 1, 1, 0,
523 "Return the logarithm base 10 of ARG.")
4b6baf5f
RS
524 (arg)
525 register Lisp_Object arg;
b70021f4 526{
4b6baf5f
RS
527 double d = extract_float (arg);
528#ifdef FLOAT_CHECK_DOMAIN
529 if (d <= 0.0)
530 domain_error ("log10", arg);
531#endif
532 IN_FLOAT (d = log10 (d), "log10", arg);
c2d4ea74
RS
533 return make_float (d);
534}
535
b70021f4
MR
536DEFUN ("sqrt", Fsqrt, Ssqrt, 1, 1, 0,
537 "Return the square root of ARG.")
4b6baf5f
RS
538 (arg)
539 register Lisp_Object arg;
b70021f4 540{
4b6baf5f
RS
541 double d = extract_float (arg);
542#ifdef FLOAT_CHECK_DOMAIN
543 if (d < 0.0)
544 domain_error ("sqrt", arg);
545#endif
546 IN_FLOAT (d = sqrt (d), "sqrt", arg);
b70021f4
MR
547 return make_float (d);
548}
c2d4ea74 549\f
706ac90d 550#if 0 /* Not clearly worth adding. */
b70021f4 551
c2d4ea74
RS
552DEFUN ("acosh", Facosh, Sacosh, 1, 1, 0,
553 "Return the inverse hyperbolic cosine of ARG.")
4b6baf5f
RS
554 (arg)
555 register Lisp_Object arg;
b70021f4 556{
4b6baf5f
RS
557 double d = extract_float (arg);
558#ifdef FLOAT_CHECK_DOMAIN
559 if (d < 1.0)
560 domain_error ("acosh", arg);
561#endif
562#ifdef HAVE_INVERSE_HYPERBOLIC
563 IN_FLOAT (d = acosh (d), "acosh", arg);
564#else
565 IN_FLOAT (d = log (d + sqrt (d*d - 1.0)), "acosh", arg);
566#endif
c2d4ea74
RS
567 return make_float (d);
568}
569
570DEFUN ("asinh", Fasinh, Sasinh, 1, 1, 0,
571 "Return the inverse hyperbolic sine of ARG.")
4b6baf5f
RS
572 (arg)
573 register Lisp_Object arg;
c2d4ea74 574{
4b6baf5f
RS
575 double d = extract_float (arg);
576#ifdef HAVE_INVERSE_HYPERBOLIC
577 IN_FLOAT (d = asinh (d), "asinh", arg);
578#else
579 IN_FLOAT (d = log (d + sqrt (d*d + 1.0)), "asinh", arg);
580#endif
c2d4ea74
RS
581 return make_float (d);
582}
583
584DEFUN ("atanh", Fatanh, Satanh, 1, 1, 0,
585 "Return the inverse hyperbolic tangent of ARG.")
4b6baf5f
RS
586 (arg)
587 register Lisp_Object arg;
c2d4ea74 588{
4b6baf5f
RS
589 double d = extract_float (arg);
590#ifdef FLOAT_CHECK_DOMAIN
591 if (d >= 1.0 || d <= -1.0)
592 domain_error ("atanh", arg);
593#endif
594#ifdef HAVE_INVERSE_HYPERBOLIC
595 IN_FLOAT (d = atanh (d), "atanh", arg);
596#else
597 IN_FLOAT (d = 0.5 * log ((1.0 + d) / (1.0 - d)), "atanh", arg);
598#endif
c2d4ea74
RS
599 return make_float (d);
600}
601
602DEFUN ("cosh", Fcosh, Scosh, 1, 1, 0,
603 "Return the hyperbolic cosine of ARG.")
4b6baf5f
RS
604 (arg)
605 register Lisp_Object arg;
c2d4ea74 606{
4b6baf5f
RS
607 double d = extract_float (arg);
608#ifdef FLOAT_CHECK_DOMAIN
609 if (d > 710.0 || d < -710.0)
610 range_error ("cosh", arg);
611#endif
612 IN_FLOAT (d = cosh (d), "cosh", arg);
c2d4ea74
RS
613 return make_float (d);
614}
615
616DEFUN ("sinh", Fsinh, Ssinh, 1, 1, 0,
617 "Return the hyperbolic sine of ARG.")
4b6baf5f
RS
618 (arg)
619 register Lisp_Object arg;
c2d4ea74 620{
4b6baf5f
RS
621 double d = extract_float (arg);
622#ifdef FLOAT_CHECK_DOMAIN
623 if (d > 710.0 || d < -710.0)
624 range_error ("sinh", arg);
625#endif
626 IN_FLOAT (d = sinh (d), "sinh", arg);
b70021f4
MR
627 return make_float (d);
628}
629
630DEFUN ("tanh", Ftanh, Stanh, 1, 1, 0,
631 "Return the hyperbolic tangent of ARG.")
4b6baf5f
RS
632 (arg)
633 register Lisp_Object arg;
b70021f4 634{
4b6baf5f
RS
635 double d = extract_float (arg);
636 IN_FLOAT (d = tanh (d), "tanh", arg);
b70021f4
MR
637 return make_float (d);
638}
c2d4ea74 639#endif
b70021f4
MR
640\f
641DEFUN ("abs", Fabs, Sabs, 1, 1, 0,
642 "Return the absolute value of ARG.")
4b6baf5f
RS
643 (arg)
644 register Lisp_Object arg;
b70021f4 645{
4b6baf5f 646 CHECK_NUMBER_OR_FLOAT (arg, 0);
b70021f4 647
4b6baf5f
RS
648 if (XTYPE (arg) == Lisp_Float)
649 IN_FLOAT (arg = make_float (fabs (XFLOAT (arg)->data)), "abs", arg);
650 else if (XINT (arg) < 0)
651 XSETINT (arg, - XFASTINT (arg));
b70021f4 652
4b6baf5f 653 return arg;
b70021f4
MR
654}
655
656DEFUN ("float", Ffloat, Sfloat, 1, 1, 0,
657 "Return the floating point number equal to ARG.")
4b6baf5f
RS
658 (arg)
659 register Lisp_Object arg;
b70021f4 660{
4b6baf5f 661 CHECK_NUMBER_OR_FLOAT (arg, 0);
b70021f4 662
4b6baf5f
RS
663 if (XTYPE (arg) == Lisp_Int)
664 return make_float ((double) XINT (arg));
b70021f4 665 else /* give 'em the same float back */
4b6baf5f 666 return arg;
b70021f4
MR
667}
668
669DEFUN ("logb", Flogb, Slogb, 1, 1, 0,
1a3ac8b9 670 "Returns largest integer <= the base 2 log of the magnitude of ARG.\n\
b70021f4 671This is the same as the exponent of a float.")
4b6baf5f
RS
672 (arg)
673 Lisp_Object arg;
b70021f4 674{
340176df 675 Lisp_Object val;
5bf54166
RS
676 int value;
677 double f = extract_float (arg);
340176df 678
6694b327
KH
679 if (f == 0.0)
680 value = -(VALMASK >> 1);
681 else
682 {
6d3c6adb 683#ifdef HAVE_LOGB
6694b327 684 IN_FLOAT (value = logb (f), "logb", arg);
6d3c6adb
JB
685#else
686#ifdef HAVE_FREXP
6694b327
KH
687 IN_FLOAT (frexp (f, &value), "logb", arg);
688 value--;
c26406fe 689#else
6694b327
KH
690 int i;
691 double d;
692 if (f < 0.0)
693 f = -f;
694 value = -1;
695 while (f < 0.5)
696 {
697 for (i = 1, d = 0.5; d * d >= f; i += i)
698 d *= d;
699 f /= d;
700 value -= i;
701 }
702 while (f >= 1.0)
703 {
704 for (i = 1, d = 2.0; d * d <= f; i += i)
705 d *= d;
706 f /= d;
707 value += i;
708 }
6d3c6adb 709#endif
340176df 710#endif
6694b327
KH
711 }
712 XSET (val, Lisp_Int, value);
c26406fe 713 return val;
b70021f4
MR
714}
715
716/* the rounding functions */
717
718DEFUN ("ceiling", Fceiling, Sceiling, 1, 1, 0,
719 "Return the smallest integer no less than ARG. (Round toward +inf.)")
4b6baf5f
RS
720 (arg)
721 register Lisp_Object arg;
b70021f4 722{
4b6baf5f 723 CHECK_NUMBER_OR_FLOAT (arg, 0);
b70021f4 724
4b6baf5f 725 if (XTYPE (arg) == Lisp_Float)
81a63ccc
KH
726 {
727 double d;
728
729 IN_FLOAT (d = ceil (XFLOAT (arg)->data), "ceiling", arg);
730 FLOAT_TO_INT (d, arg, "ceiling", arg);
731 }
b70021f4 732
4b6baf5f 733 return arg;
b70021f4
MR
734}
735
fc2157cb
PE
736#endif /* LISP_FLOAT_TYPE */
737
738
739DEFUN ("floor", Ffloor, Sfloor, 1, 2, 0,
740 "Return the largest integer no greater than ARG. (Round towards -inf.)\n\
741With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR.")
742 (arg, divisor)
743 register Lisp_Object arg, divisor;
b70021f4 744{
4b6baf5f 745 CHECK_NUMBER_OR_FLOAT (arg, 0);
b70021f4 746
fc2157cb
PE
747 if (! NILP (divisor))
748 {
749 int i1, i2;
750
751 CHECK_NUMBER_OR_FLOAT (divisor, 1);
752
753#ifdef LISP_FLOAT_TYPE
754 if (XTYPE (arg) == Lisp_Float || XTYPE (divisor) == Lisp_Float)
755 {
756 double f1, f2;
757
758 f1 = XTYPE (arg) == Lisp_Float ? XFLOAT (arg)->data : XINT (arg);
759 f2 = (XTYPE (divisor) == Lisp_Float
760 ? XFLOAT (divisor)->data : XINT (divisor));
761 if (f2 == 0)
762 Fsignal (Qarith_error, Qnil);
763
81a63ccc
KH
764 IN_FLOAT2 (f1 = floor (f1 / f2), "floor", arg, divisor);
765 FLOAT_TO_INT2 (f1, arg, "floor", arg, divisor);
fc2157cb
PE
766 return arg;
767 }
768#endif
769
770 i1 = XINT (arg);
771 i2 = XINT (divisor);
772
773 if (i2 == 0)
774 Fsignal (Qarith_error, Qnil);
775
776 /* With C's /, the result is implementation-defined if either operand
777 is negative, so use only nonnegative operands. */
778 i1 = (i2 < 0
779 ? (i1 <= 0 ? -i1 / -i2 : -1 - ((i1 - 1) / -i2))
780 : (i1 < 0 ? -1 - ((-1 - i1) / i2) : i1 / i2));
781
782 XSET (arg, Lisp_Int, i1);
783 return arg;
784 }
785
786#ifdef LISP_FLOAT_TYPE
4b6baf5f 787 if (XTYPE (arg) == Lisp_Float)
81a63ccc
KH
788 {
789 double d;
790 IN_FLOAT (d = floor (XFLOAT (arg)->data), "floor", arg);
791 FLOAT_TO_INT (d, arg, "floor", arg);
792 }
fc2157cb 793#endif
b70021f4 794
4b6baf5f 795 return arg;
b70021f4
MR
796}
797
fc2157cb
PE
798#ifdef LISP_FLOAT_TYPE
799
b70021f4
MR
800DEFUN ("round", Fround, Sround, 1, 1, 0,
801 "Return the nearest integer to ARG.")
4b6baf5f
RS
802 (arg)
803 register Lisp_Object arg;
b70021f4 804{
4b6baf5f 805 CHECK_NUMBER_OR_FLOAT (arg, 0);
b70021f4 806
4b6baf5f 807 if (XTYPE (arg) == Lisp_Float)
81a63ccc
KH
808 {
809 double d;
810
811 /* Screw the prevailing rounding mode. */
812 IN_FLOAT (d = rint (XFLOAT (arg)->data), "round", arg);
813 FLOAT_TO_INT (d, arg, "round", arg);
814 }
b70021f4 815
4b6baf5f 816 return arg;
b70021f4
MR
817}
818
819DEFUN ("truncate", Ftruncate, Struncate, 1, 1, 0,
820 "Truncate a floating point number to an int.\n\
821Rounds the value toward zero.")
4b6baf5f
RS
822 (arg)
823 register Lisp_Object arg;
b70021f4 824{
4b6baf5f 825 CHECK_NUMBER_OR_FLOAT (arg, 0);
b70021f4 826
4b6baf5f 827 if (XTYPE (arg) == Lisp_Float)
81a63ccc
KH
828 {
829 double d;
830
831 d = XFLOAT (arg)->data;
832 FLOAT_TO_INT (d, arg, "truncate", arg);
833 }
4b6baf5f
RS
834
835 return arg;
836}
837\f
4b6baf5f
RS
838/* It's not clear these are worth adding. */
839
840DEFUN ("fceiling", Ffceiling, Sfceiling, 1, 1, 0,
841 "Return the smallest integer no less than ARG, as a float.\n\
842\(Round toward +inf.\)")
843 (arg)
844 register Lisp_Object arg;
845{
846 double d = extract_float (arg);
847 IN_FLOAT (d = ceil (d), "fceiling", arg);
848 return make_float (d);
849}
850
851DEFUN ("ffloor", Fffloor, Sffloor, 1, 1, 0,
852 "Return the largest integer no greater than ARG, as a float.\n\
853\(Round towards -inf.\)")
854 (arg)
855 register Lisp_Object arg;
856{
857 double d = extract_float (arg);
858 IN_FLOAT (d = floor (d), "ffloor", arg);
859 return make_float (d);
860}
b70021f4 861
4b6baf5f
RS
862DEFUN ("fround", Ffround, Sfround, 1, 1, 0,
863 "Return the nearest integer to ARG, as a float.")
864 (arg)
865 register Lisp_Object arg;
866{
867 double d = extract_float (arg);
892ed7e0 868 IN_FLOAT (d = rint (d), "fround", arg);
4b6baf5f
RS
869 return make_float (d);
870}
871
872DEFUN ("ftruncate", Fftruncate, Sftruncate, 1, 1, 0,
873 "Truncate a floating point number to an integral float value.\n\
874Rounds the value toward zero.")
875 (arg)
876 register Lisp_Object arg;
877{
878 double d = extract_float (arg);
879 if (d >= 0.0)
880 IN_FLOAT (d = floor (d), "ftruncate", arg);
881 else
a3fc5236 882 IN_FLOAT (d = ceil (d), "ftruncate", arg);
4b6baf5f 883 return make_float (d);
b70021f4
MR
884}
885\f
4b6baf5f 886#ifdef FLOAT_CATCH_SIGILL
4746118a 887static SIGTYPE
b70021f4
MR
888float_error (signo)
889 int signo;
890{
891 if (! in_float)
892 fatal_error_signal (signo);
893
265a9e55 894#ifdef BSD
b70021f4
MR
895#ifdef BSD4_1
896 sigrelse (SIGILL);
897#else /* not BSD4_1 */
e065a56e 898 sigsetmask (SIGEMPTYMASK);
b70021f4 899#endif /* not BSD4_1 */
265a9e55
JB
900#else
901 /* Must reestablish handler each time it is called. */
902 signal (SIGILL, float_error);
903#endif /* BSD */
b70021f4
MR
904
905 in_float = 0;
906
907 Fsignal (Qarith_error, Fcons (float_error_arg, Qnil));
908}
909
4b6baf5f
RS
910/* Another idea was to replace the library function `infnan'
911 where SIGILL is signaled. */
912
913#endif /* FLOAT_CATCH_SIGILL */
914
915#ifdef HAVE_MATHERR
916int
917matherr (x)
918 struct exception *x;
919{
920 Lisp_Object args;
921 if (! in_float)
922 /* Not called from emacs-lisp float routines; do the default thing. */
923 return 0;
924 if (!strcmp (x->name, "pow"))
925 x->name = "expt";
926
927 args
928 = Fcons (build_string (x->name),
929 Fcons (make_float (x->arg1),
930 ((!strcmp (x->name, "log") || !strcmp (x->name, "pow"))
931 ? Fcons (make_float (x->arg2), Qnil)
932 : Qnil)));
933 switch (x->type)
934 {
935 case DOMAIN: Fsignal (Qdomain_error, args); break;
936 case SING: Fsignal (Qsingularity_error, args); break;
937 case OVERFLOW: Fsignal (Qoverflow_error, args); break;
938 case UNDERFLOW: Fsignal (Qunderflow_error, args); break;
939 default: Fsignal (Qarith_error, args); break;
940 }
941 return (1); /* don't set errno or print a message */
942}
943#endif /* HAVE_MATHERR */
944
b70021f4
MR
945init_floatfns ()
946{
4b6baf5f 947#ifdef FLOAT_CATCH_SIGILL
b70021f4 948 signal (SIGILL, float_error);
4b6baf5f 949#endif
b70021f4
MR
950 in_float = 0;
951}
952
fc2157cb
PE
953#else /* not LISP_FLOAT_TYPE */
954
955init_floatfns ()
956{}
957
958#endif /* not LISP_FLOAT_TYPE */
959
b70021f4
MR
960syms_of_floatfns ()
961{
fc2157cb 962#ifdef LISP_FLOAT_TYPE
b70021f4 963 defsubr (&Sacos);
b70021f4 964 defsubr (&Sasin);
b70021f4 965 defsubr (&Satan);
c2d4ea74
RS
966 defsubr (&Scos);
967 defsubr (&Ssin);
968 defsubr (&Stan);
969#if 0
970 defsubr (&Sacosh);
971 defsubr (&Sasinh);
b70021f4 972 defsubr (&Satanh);
c2d4ea74
RS
973 defsubr (&Scosh);
974 defsubr (&Ssinh);
975 defsubr (&Stanh);
b70021f4
MR
976 defsubr (&Sbessel_y0);
977 defsubr (&Sbessel_y1);
978 defsubr (&Sbessel_yn);
979 defsubr (&Sbessel_j0);
980 defsubr (&Sbessel_j1);
981 defsubr (&Sbessel_jn);
b70021f4
MR
982 defsubr (&Serf);
983 defsubr (&Serfc);
c2d4ea74 984 defsubr (&Slog_gamma);
4b6baf5f 985 defsubr (&Scube_root);
892ed7e0 986#endif
4b6baf5f
RS
987 defsubr (&Sfceiling);
988 defsubr (&Sffloor);
989 defsubr (&Sfround);
990 defsubr (&Sftruncate);
b70021f4 991 defsubr (&Sexp);
c2d4ea74 992 defsubr (&Sexpt);
b70021f4
MR
993 defsubr (&Slog);
994 defsubr (&Slog10);
b70021f4 995 defsubr (&Ssqrt);
b70021f4
MR
996
997 defsubr (&Sabs);
998 defsubr (&Sfloat);
999 defsubr (&Slogb);
1000 defsubr (&Sceiling);
b70021f4
MR
1001 defsubr (&Sround);
1002 defsubr (&Struncate);
fc2157cb
PE
1003#endif /* LISP_FLOAT_TYPE */
1004 defsubr (&Sfloor);
b70021f4 1005}