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