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