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