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