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