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