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