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
|
1983
|
122 error using the given arguments. If bignums are available, range errors
|
|
123 are never signaled. */
|
428
|
124 static Lisp_Object
|
442
|
125 float_to_int (double x, const char *name, Lisp_Object num, Lisp_Object num2)
|
428
|
126 {
|
1983
|
127 #ifdef HAVE_BIGNUM
|
|
128 bignum_set_double (scratch_bignum, x);
|
|
129 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
130 #else
|
2039
|
131 REGISTER EMACS_INT result = (EMACS_INT) x;
|
|
132
|
|
133 if (result > EMACS_INT_MAX || result < EMACS_INT_MIN)
|
|
134 {
|
|
135 if (!UNBOUNDP (num2))
|
|
136 range_error2 (name, num, num2);
|
|
137 else
|
|
138 range_error (name, num);
|
|
139 }
|
|
140 return make_int (result);
|
1983
|
141 #endif /* HAVE_BIGNUM */
|
428
|
142 }
|
|
143
|
|
144
|
|
145 static void
|
|
146 in_float_error (void)
|
|
147 {
|
|
148 switch (errno)
|
|
149 {
|
|
150 case 0:
|
|
151 break;
|
|
152 case EDOM:
|
|
153 if (in_float == 2)
|
|
154 domain_error2 (float_error_fn_name, float_error_arg, float_error_arg2);
|
|
155 else
|
|
156 domain_error (float_error_fn_name, float_error_arg);
|
|
157 break;
|
|
158 case ERANGE:
|
|
159 range_error (float_error_fn_name, float_error_arg);
|
|
160 break;
|
|
161 default:
|
|
162 arith_error (float_error_fn_name, float_error_arg);
|
|
163 break;
|
|
164 }
|
|
165 }
|
|
166
|
|
167
|
|
168 static Lisp_Object
|
|
169 mark_float (Lisp_Object obj)
|
|
170 {
|
|
171 return Qnil;
|
|
172 }
|
|
173
|
|
174 static int
|
|
175 float_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
176 {
|
|
177 return (extract_float (obj1) == extract_float (obj2));
|
|
178 }
|
|
179
|
665
|
180 static Hashcode
|
428
|
181 float_hash (Lisp_Object obj, int depth)
|
|
182 {
|
|
183 /* mod the value down to 32-bit range */
|
|
184 /* #### change for 64-bit machines */
|
|
185 return (unsigned long) fmod (extract_float (obj), 4e9);
|
|
186 }
|
|
187
|
1204
|
188 static const struct memory_description float_description[] = {
|
428
|
189 { XD_END }
|
|
190 };
|
|
191
|
934
|
192 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("float", float,
|
|
193 1, /*dumpable-flag*/
|
|
194 mark_float, print_float, 0, float_equal,
|
|
195 float_hash, float_description,
|
|
196 Lisp_Float);
|
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
|
1983
|
209 #ifdef HAVE_BIGNUM
|
|
210 if (BIGNUMP (num))
|
|
211 return bignum_to_double (XBIGNUM_DATA (num));
|
|
212 #endif
|
|
213
|
|
214 #ifdef HAVE_RATIO
|
|
215 if (RATIOP (num))
|
|
216 return ratio_to_double (XRATIO_DATA (num));
|
|
217 #endif
|
|
218
|
|
219 #ifdef HAVE_BIGFLOAT
|
|
220 if (BIGFLOATP (num))
|
|
221 return bigfloat_to_double (XBIGFLOAT_DATA (num));
|
|
222 #endif
|
|
223
|
428
|
224 return extract_float (wrong_type_argument (Qnumberp, num));
|
|
225 }
|
|
226
|
|
227 /* Trig functions. */
|
|
228
|
|
229 DEFUN ("acos", Facos, 1, 1, 0, /*
|
444
|
230 Return the inverse cosine of NUMBER.
|
428
|
231 */
|
444
|
232 (number))
|
428
|
233 {
|
444
|
234 double d = extract_float (number);
|
428
|
235 #ifdef FLOAT_CHECK_DOMAIN
|
|
236 if (d > 1.0 || d < -1.0)
|
444
|
237 domain_error ("acos", number);
|
428
|
238 #endif
|
444
|
239 IN_FLOAT (d = acos (d), "acos", number);
|
428
|
240 return make_float (d);
|
|
241 }
|
|
242
|
|
243 DEFUN ("asin", Fasin, 1, 1, 0, /*
|
444
|
244 Return the inverse sine of NUMBER.
|
428
|
245 */
|
444
|
246 (number))
|
428
|
247 {
|
444
|
248 double d = extract_float (number);
|
428
|
249 #ifdef FLOAT_CHECK_DOMAIN
|
|
250 if (d > 1.0 || d < -1.0)
|
444
|
251 domain_error ("asin", number);
|
428
|
252 #endif
|
444
|
253 IN_FLOAT (d = asin (d), "asin", number);
|
428
|
254 return make_float (d);
|
|
255 }
|
|
256
|
|
257 DEFUN ("atan", Fatan, 1, 2, 0, /*
|
444
|
258 Return the inverse tangent of NUMBER.
|
|
259 If optional second argument NUMBER2 is provided,
|
|
260 return atan2 (NUMBER, NUMBER2).
|
428
|
261 */
|
444
|
262 (number, number2))
|
428
|
263 {
|
444
|
264 double d = extract_float (number);
|
428
|
265
|
444
|
266 if (NILP (number2))
|
|
267 IN_FLOAT (d = atan (d), "atan", number);
|
428
|
268 else
|
|
269 {
|
444
|
270 double d2 = extract_float (number2);
|
428
|
271 #ifdef FLOAT_CHECK_DOMAIN
|
|
272 if (d == 0.0 && d2 == 0.0)
|
444
|
273 domain_error2 ("atan", number, number2);
|
428
|
274 #endif
|
444
|
275 IN_FLOAT2 (d = atan2 (d, d2), "atan", number, number2);
|
428
|
276 }
|
|
277 return make_float (d);
|
|
278 }
|
|
279
|
|
280 DEFUN ("cos", Fcos, 1, 1, 0, /*
|
444
|
281 Return the cosine of NUMBER.
|
428
|
282 */
|
444
|
283 (number))
|
428
|
284 {
|
444
|
285 double d = extract_float (number);
|
|
286 IN_FLOAT (d = cos (d), "cos", number);
|
428
|
287 return make_float (d);
|
|
288 }
|
|
289
|
|
290 DEFUN ("sin", Fsin, 1, 1, 0, /*
|
444
|
291 Return the sine of NUMBER.
|
428
|
292 */
|
444
|
293 (number))
|
428
|
294 {
|
444
|
295 double d = extract_float (number);
|
|
296 IN_FLOAT (d = sin (d), "sin", number);
|
428
|
297 return make_float (d);
|
|
298 }
|
|
299
|
|
300 DEFUN ("tan", Ftan, 1, 1, 0, /*
|
444
|
301 Return the tangent of NUMBER.
|
428
|
302 */
|
444
|
303 (number))
|
428
|
304 {
|
444
|
305 double d = extract_float (number);
|
428
|
306 double c = cos (d);
|
|
307 #ifdef FLOAT_CHECK_DOMAIN
|
|
308 if (c == 0.0)
|
444
|
309 domain_error ("tan", number);
|
428
|
310 #endif
|
444
|
311 IN_FLOAT (d = (sin (d) / c), "tan", number);
|
428
|
312 return make_float (d);
|
|
313 }
|
|
314
|
|
315 /* Bessel functions */
|
|
316 #if 0 /* Leave these out unless we find there's a reason for them. */
|
|
317
|
|
318 DEFUN ("bessel-j0", Fbessel_j0, 1, 1, 0, /*
|
444
|
319 Return the bessel function j0 of NUMBER.
|
428
|
320 */
|
444
|
321 (number))
|
428
|
322 {
|
444
|
323 double d = extract_float (number);
|
|
324 IN_FLOAT (d = j0 (d), "bessel-j0", number);
|
428
|
325 return make_float (d);
|
|
326 }
|
|
327
|
|
328 DEFUN ("bessel-j1", Fbessel_j1, 1, 1, 0, /*
|
444
|
329 Return the bessel function j1 of NUMBER.
|
428
|
330 */
|
444
|
331 (number))
|
428
|
332 {
|
444
|
333 double d = extract_float (number);
|
|
334 IN_FLOAT (d = j1 (d), "bessel-j1", number);
|
428
|
335 return make_float (d);
|
|
336 }
|
|
337
|
|
338 DEFUN ("bessel-jn", Fbessel_jn, 2, 2, 0, /*
|
444
|
339 Return the order N bessel function output jn of NUMBER.
|
|
340 The first number (the order) is truncated to an integer.
|
428
|
341 */
|
444
|
342 (number1, number2))
|
428
|
343 {
|
444
|
344 int i1 = extract_float (number1);
|
|
345 double f2 = extract_float (number2);
|
428
|
346
|
444
|
347 IN_FLOAT (f2 = jn (i1, f2), "bessel-jn", number1);
|
428
|
348 return make_float (f2);
|
|
349 }
|
|
350
|
|
351 DEFUN ("bessel-y0", Fbessel_y0, 1, 1, 0, /*
|
444
|
352 Return the bessel function y0 of NUMBER.
|
428
|
353 */
|
444
|
354 (number))
|
428
|
355 {
|
444
|
356 double d = extract_float (number);
|
|
357 IN_FLOAT (d = y0 (d), "bessel-y0", number);
|
428
|
358 return make_float (d);
|
|
359 }
|
|
360
|
|
361 DEFUN ("bessel-y1", Fbessel_y1, 1, 1, 0, /*
|
444
|
362 Return the bessel function y1 of NUMBER.
|
428
|
363 */
|
444
|
364 (number))
|
428
|
365 {
|
444
|
366 double d = extract_float (number);
|
|
367 IN_FLOAT (d = y1 (d), "bessel-y0", number);
|
428
|
368 return make_float (d);
|
|
369 }
|
|
370
|
|
371 DEFUN ("bessel-yn", Fbessel_yn, 2, 2, 0, /*
|
444
|
372 Return the order N bessel function output yn of NUMBER.
|
|
373 The first number (the order) is truncated to an integer.
|
428
|
374 */
|
444
|
375 (number1, number2))
|
428
|
376 {
|
444
|
377 int i1 = extract_float (number1);
|
|
378 double f2 = extract_float (number2);
|
428
|
379
|
444
|
380 IN_FLOAT (f2 = yn (i1, f2), "bessel-yn", number1);
|
428
|
381 return make_float (f2);
|
|
382 }
|
|
383
|
|
384 #endif /* 0 (bessel functions) */
|
|
385
|
|
386 /* Error functions. */
|
|
387 #if 0 /* Leave these out unless we see they are worth having. */
|
|
388
|
|
389 DEFUN ("erf", Ferf, 1, 1, 0, /*
|
444
|
390 Return the mathematical error function of NUMBER.
|
428
|
391 */
|
444
|
392 (number))
|
428
|
393 {
|
444
|
394 double d = extract_float (number);
|
|
395 IN_FLOAT (d = erf (d), "erf", number);
|
428
|
396 return make_float (d);
|
|
397 }
|
|
398
|
|
399 DEFUN ("erfc", Ferfc, 1, 1, 0, /*
|
444
|
400 Return the complementary error function of NUMBER.
|
428
|
401 */
|
444
|
402 (number))
|
428
|
403 {
|
444
|
404 double d = extract_float (number);
|
|
405 IN_FLOAT (d = erfc (d), "erfc", number);
|
428
|
406 return make_float (d);
|
|
407 }
|
|
408
|
|
409 DEFUN ("log-gamma", Flog_gamma, 1, 1, 0, /*
|
444
|
410 Return the log gamma of NUMBER.
|
428
|
411 */
|
444
|
412 (number))
|
428
|
413 {
|
444
|
414 double d = extract_float (number);
|
|
415 IN_FLOAT (d = lgamma (d), "log-gamma", number);
|
428
|
416 return make_float (d);
|
|
417 }
|
|
418
|
|
419 #endif /* 0 (error functions) */
|
|
420
|
|
421
|
|
422 /* Root and Log functions. */
|
|
423
|
|
424 DEFUN ("exp", Fexp, 1, 1, 0, /*
|
444
|
425 Return the exponential base e of NUMBER.
|
428
|
426 */
|
444
|
427 (number))
|
428
|
428 {
|
444
|
429 double d = extract_float (number);
|
428
|
430 #ifdef FLOAT_CHECK_DOMAIN
|
|
431 if (d > 709.7827) /* Assume IEEE doubles here */
|
444
|
432 range_error ("exp", number);
|
428
|
433 else if (d < -709.0)
|
|
434 return make_float (0.0);
|
|
435 else
|
|
436 #endif
|
444
|
437 IN_FLOAT (d = exp (d), "exp", number);
|
428
|
438 return make_float (d);
|
|
439 }
|
|
440
|
|
441 DEFUN ("expt", Fexpt, 2, 2, 0, /*
|
444
|
442 Return the exponential NUMBER1 ** NUMBER2.
|
428
|
443 */
|
444
|
444 (number1, number2))
|
428
|
445 {
|
1983
|
446 #ifdef HAVE_BIGNUM
|
|
447 if (INTEGERP (number1) && INTP (number2))
|
|
448 {
|
|
449 if (INTP (number1))
|
|
450 {
|
|
451 bignum_set_long (scratch_bignum2, XREALINT (number1));
|
|
452 bignum_pow (scratch_bignum, scratch_bignum2, XREALINT (number2));
|
|
453 }
|
|
454 else
|
|
455 bignum_pow (scratch_bignum, XBIGNUM_DATA (number1),
|
|
456 XREALINT (number2));
|
|
457 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
458 }
|
|
459 #endif
|
|
460
|
444
|
461 if (INTP (number1) && /* common lisp spec */
|
|
462 INTP (number2)) /* don't promote, if both are ints */
|
428
|
463 {
|
|
464 EMACS_INT retval;
|
444
|
465 EMACS_INT x = XINT (number1);
|
|
466 EMACS_INT y = XINT (number2);
|
428
|
467
|
|
468 if (y < 0)
|
|
469 {
|
|
470 if (x == 1)
|
|
471 retval = 1;
|
|
472 else if (x == -1)
|
|
473 retval = (y & 1) ? -1 : 1;
|
|
474 else
|
|
475 retval = 0;
|
|
476 }
|
|
477 else
|
|
478 {
|
|
479 retval = 1;
|
|
480 while (y > 0)
|
|
481 {
|
|
482 if (y & 1)
|
|
483 retval *= x;
|
|
484 x *= x;
|
|
485 y = (EMACS_UINT) y >> 1;
|
|
486 }
|
|
487 }
|
|
488 return make_int (retval);
|
|
489 }
|
|
490
|
1983
|
491 #if defined(HAVE_BIGFLOAT) && defined(bigfloat_pow)
|
|
492 if (BIGFLOATP (number1) && INTEGERP (number2))
|
|
493 {
|
2057
|
494 unsigned long exponent;
|
1983
|
495
|
|
496 #ifdef HAVE_BIGNUM
|
|
497 if (BIGNUMP (number2))
|
2057
|
498 exponent = bignum_to_ulong (XBIGNUM_DATA (number2));
|
1983
|
499 else
|
|
500 #endif
|
2057
|
501 exponent = XUINT (number2);
|
1983
|
502 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number1));
|
2057
|
503 bigfloat_pow (scratch_bigfloat, XBIGFLOAT_DATA (number1), exponent);
|
1983
|
504 return make_bigfloat_bf (scratch_bigfloat);
|
|
505 }
|
|
506 #endif
|
|
507
|
428
|
508 {
|
444
|
509 double f1 = extract_float (number1);
|
|
510 double f2 = extract_float (number2);
|
428
|
511 /* Really should check for overflow, too */
|
|
512 if (f1 == 0.0 && f2 == 0.0)
|
|
513 f1 = 1.0;
|
|
514 # ifdef FLOAT_CHECK_DOMAIN
|
|
515 else if ((f1 == 0.0 && f2 < 0.0) || (f1 < 0 && f2 != floor(f2)))
|
444
|
516 domain_error2 ("expt", number1, number2);
|
428
|
517 # endif /* FLOAT_CHECK_DOMAIN */
|
444
|
518 IN_FLOAT2 (f1 = pow (f1, f2), "expt", number1, number2);
|
428
|
519 return make_float (f1);
|
|
520 }
|
|
521 }
|
|
522
|
|
523 DEFUN ("log", Flog, 1, 2, 0, /*
|
444
|
524 Return the natural logarithm of NUMBER.
|
|
525 If second optional argument BASE is given, return the logarithm of
|
|
526 NUMBER using that base.
|
428
|
527 */
|
444
|
528 (number, base))
|
428
|
529 {
|
444
|
530 double d = extract_float (number);
|
428
|
531 #ifdef FLOAT_CHECK_DOMAIN
|
|
532 if (d <= 0.0)
|
444
|
533 domain_error2 ("log", number, base);
|
428
|
534 #endif
|
|
535 if (NILP (base))
|
444
|
536 IN_FLOAT (d = log (d), "log", number);
|
428
|
537 else
|
|
538 {
|
|
539 double b = extract_float (base);
|
|
540 #ifdef FLOAT_CHECK_DOMAIN
|
|
541 if (b <= 0.0 || b == 1.0)
|
444
|
542 domain_error2 ("log", number, base);
|
428
|
543 #endif
|
|
544 if (b == 10.0)
|
444
|
545 IN_FLOAT2 (d = log10 (d), "log", number, base);
|
428
|
546 else
|
444
|
547 IN_FLOAT2 (d = (log (d) / log (b)), "log", number, base);
|
428
|
548 }
|
|
549 return make_float (d);
|
|
550 }
|
|
551
|
|
552
|
|
553 DEFUN ("log10", Flog10, 1, 1, 0, /*
|
444
|
554 Return the logarithm base 10 of NUMBER.
|
428
|
555 */
|
444
|
556 (number))
|
428
|
557 {
|
444
|
558 double d = extract_float (number);
|
428
|
559 #ifdef FLOAT_CHECK_DOMAIN
|
|
560 if (d <= 0.0)
|
444
|
561 domain_error ("log10", number);
|
428
|
562 #endif
|
444
|
563 IN_FLOAT (d = log10 (d), "log10", number);
|
428
|
564 return make_float (d);
|
|
565 }
|
|
566
|
|
567
|
|
568 DEFUN ("sqrt", Fsqrt, 1, 1, 0, /*
|
444
|
569 Return the square root of NUMBER.
|
428
|
570 */
|
444
|
571 (number))
|
428
|
572 {
|
1983
|
573 double d;
|
|
574
|
|
575 #if defined(HAVE_BIGFLOAT) && defined(bigfloat_sqrt)
|
|
576 if (BIGFLOATP (number))
|
|
577 {
|
|
578 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
579 bigfloat_sqrt (scratch_bigfloat, XBIGFLOAT_DATA (number));
|
|
580 return make_bigfloat_bf (scratch_bigfloat);
|
|
581 }
|
|
582 #endif /* HAVE_BIGFLOAT */
|
|
583 d = extract_float (number);
|
428
|
584 #ifdef FLOAT_CHECK_DOMAIN
|
|
585 if (d < 0.0)
|
444
|
586 domain_error ("sqrt", number);
|
428
|
587 #endif
|
444
|
588 IN_FLOAT (d = sqrt (d), "sqrt", number);
|
428
|
589 return make_float (d);
|
|
590 }
|
|
591
|
|
592
|
|
593 DEFUN ("cube-root", Fcube_root, 1, 1, 0, /*
|
444
|
594 Return the cube root of NUMBER.
|
428
|
595 */
|
444
|
596 (number))
|
428
|
597 {
|
444
|
598 double d = extract_float (number);
|
428
|
599 #ifdef HAVE_CBRT
|
444
|
600 IN_FLOAT (d = cbrt (d), "cube-root", number);
|
428
|
601 #else
|
|
602 if (d >= 0.0)
|
444
|
603 IN_FLOAT (d = pow (d, 1.0/3.0), "cube-root", number);
|
428
|
604 else
|
444
|
605 IN_FLOAT (d = -pow (-d, 1.0/3.0), "cube-root", number);
|
428
|
606 #endif
|
|
607 return make_float (d);
|
|
608 }
|
|
609
|
|
610 /* Inverse trig functions. */
|
|
611
|
|
612 DEFUN ("acosh", Facosh, 1, 1, 0, /*
|
444
|
613 Return the inverse hyperbolic cosine of NUMBER.
|
428
|
614 */
|
444
|
615 (number))
|
428
|
616 {
|
444
|
617 double d = extract_float (number);
|
428
|
618 #ifdef FLOAT_CHECK_DOMAIN
|
|
619 if (d < 1.0)
|
444
|
620 domain_error ("acosh", number);
|
428
|
621 #endif
|
|
622 #ifdef HAVE_INVERSE_HYPERBOLIC
|
444
|
623 IN_FLOAT (d = acosh (d), "acosh", number);
|
428
|
624 #else
|
444
|
625 IN_FLOAT (d = log (d + sqrt (d*d - 1.0)), "acosh", number);
|
428
|
626 #endif
|
|
627 return make_float (d);
|
|
628 }
|
|
629
|
|
630 DEFUN ("asinh", Fasinh, 1, 1, 0, /*
|
444
|
631 Return the inverse hyperbolic sine of NUMBER.
|
428
|
632 */
|
444
|
633 (number))
|
428
|
634 {
|
444
|
635 double d = extract_float (number);
|
428
|
636 #ifdef HAVE_INVERSE_HYPERBOLIC
|
444
|
637 IN_FLOAT (d = asinh (d), "asinh", number);
|
428
|
638 #else
|
444
|
639 IN_FLOAT (d = log (d + sqrt (d*d + 1.0)), "asinh", number);
|
428
|
640 #endif
|
|
641 return make_float (d);
|
|
642 }
|
|
643
|
|
644 DEFUN ("atanh", Fatanh, 1, 1, 0, /*
|
444
|
645 Return the inverse hyperbolic tangent of NUMBER.
|
428
|
646 */
|
444
|
647 (number))
|
428
|
648 {
|
444
|
649 double d = extract_float (number);
|
428
|
650 #ifdef FLOAT_CHECK_DOMAIN
|
|
651 if (d >= 1.0 || d <= -1.0)
|
444
|
652 domain_error ("atanh", number);
|
428
|
653 #endif
|
|
654 #ifdef HAVE_INVERSE_HYPERBOLIC
|
444
|
655 IN_FLOAT (d = atanh (d), "atanh", number);
|
428
|
656 #else
|
444
|
657 IN_FLOAT (d = 0.5 * log ((1.0 + d) / (1.0 - d)), "atanh", number);
|
428
|
658 #endif
|
|
659 return make_float (d);
|
|
660 }
|
|
661
|
|
662 DEFUN ("cosh", Fcosh, 1, 1, 0, /*
|
444
|
663 Return the hyperbolic cosine of NUMBER.
|
428
|
664 */
|
444
|
665 (number))
|
428
|
666 {
|
444
|
667 double d = extract_float (number);
|
428
|
668 #ifdef FLOAT_CHECK_DOMAIN
|
|
669 if (d > 710.0 || d < -710.0)
|
444
|
670 range_error ("cosh", number);
|
428
|
671 #endif
|
444
|
672 IN_FLOAT (d = cosh (d), "cosh", number);
|
428
|
673 return make_float (d);
|
|
674 }
|
|
675
|
|
676 DEFUN ("sinh", Fsinh, 1, 1, 0, /*
|
444
|
677 Return the hyperbolic sine of NUMBER.
|
428
|
678 */
|
444
|
679 (number))
|
428
|
680 {
|
444
|
681 double d = extract_float (number);
|
428
|
682 #ifdef FLOAT_CHECK_DOMAIN
|
|
683 if (d > 710.0 || d < -710.0)
|
444
|
684 range_error ("sinh", number);
|
428
|
685 #endif
|
444
|
686 IN_FLOAT (d = sinh (d), "sinh", number);
|
428
|
687 return make_float (d);
|
|
688 }
|
|
689
|
|
690 DEFUN ("tanh", Ftanh, 1, 1, 0, /*
|
444
|
691 Return the hyperbolic tangent of NUMBER.
|
428
|
692 */
|
444
|
693 (number))
|
428
|
694 {
|
444
|
695 double d = extract_float (number);
|
|
696 IN_FLOAT (d = tanh (d), "tanh", number);
|
428
|
697 return make_float (d);
|
|
698 }
|
|
699
|
|
700 /* Rounding functions */
|
|
701
|
|
702 DEFUN ("abs", Fabs, 1, 1, 0, /*
|
444
|
703 Return the absolute value of NUMBER.
|
428
|
704 */
|
444
|
705 (number))
|
428
|
706 {
|
444
|
707 if (FLOATP (number))
|
428
|
708 {
|
444
|
709 IN_FLOAT (number = make_float (fabs (XFLOAT_DATA (number))),
|
|
710 "abs", number);
|
|
711 return number;
|
428
|
712 }
|
|
713
|
444
|
714 if (INTP (number))
|
1983
|
715 #ifdef HAVE_BIGNUM
|
|
716 /* The most negative Lisp fixnum will overflow */
|
|
717 return (XINT (number) >= 0) ? number : make_integer (- XINT (number));
|
|
718 #else
|
444
|
719 return (XINT (number) >= 0) ? number : make_int (- XINT (number));
|
1983
|
720 #endif
|
|
721
|
|
722 #ifdef HAVE_BIGNUM
|
|
723 if (BIGNUMP (number))
|
|
724 {
|
|
725 if (bignum_sign (XBIGNUM_DATA (number)) >= 0)
|
|
726 return number;
|
|
727 bignum_abs (scratch_bignum, XBIGNUM_DATA (number));
|
|
728 return make_bignum_bg (scratch_bignum);
|
|
729 }
|
|
730 #endif
|
|
731
|
|
732 #ifdef HAVE_RATIO
|
|
733 if (RATIOP (number))
|
|
734 {
|
|
735 if (ratio_sign (XRATIO_DATA (number)) >= 0)
|
|
736 return number;
|
|
737 ratio_abs (scratch_ratio, XRATIO_DATA (number));
|
|
738 return make_ratio_rt (scratch_ratio);
|
|
739 }
|
|
740 #endif
|
|
741
|
|
742 #ifdef HAVE_BIGFLOAT
|
|
743 if (BIGFLOATP (number))
|
|
744 {
|
|
745 if (bigfloat_sign (XBIGFLOAT_DATA (number)) >= 0)
|
|
746 return number;
|
|
747 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
748 bigfloat_abs (scratch_bigfloat, XBIGFLOAT_DATA (number));
|
|
749 return make_bigfloat_bf (scratch_bigfloat);
|
|
750 }
|
|
751 #endif
|
428
|
752
|
444
|
753 return Fabs (wrong_type_argument (Qnumberp, number));
|
428
|
754 }
|
|
755
|
|
756 DEFUN ("float", Ffloat, 1, 1, 0, /*
|
444
|
757 Return the floating point number numerically equal to NUMBER.
|
428
|
758 */
|
444
|
759 (number))
|
428
|
760 {
|
444
|
761 if (INTP (number))
|
|
762 return make_float ((double) XINT (number));
|
428
|
763
|
1983
|
764 #ifdef HAVE_BIGNUM
|
|
765 if (BIGFLOATP (number))
|
|
766 {
|
|
767 #ifdef HAVE_BIGFLOAT
|
|
768 if (ZEROP (Vdefault_float_precision))
|
|
769 #endif
|
|
770 return make_float (bignum_to_double (XBIGNUM_DATA (number)));
|
|
771 #ifdef HAVE_BIGFLOAT
|
|
772 else
|
|
773 {
|
|
774 bigfloat_set_prec (scratch_bigfloat, bigfloat_get_default_prec ());
|
|
775 bigfloat_set_bignum (scratch_bigfloat, XBIGNUM_DATA (number));
|
|
776 return make_bigfloat_bf (scratch_bigfloat);
|
|
777 }
|
|
778 #endif /* HAVE_BIGFLOAT */
|
|
779 }
|
|
780 #endif /* HAVE_BIGNUM */
|
|
781
|
|
782 #ifdef HAVE_RATIO
|
|
783 if (RATIOP (number))
|
2092
|
784 return make_float (ratio_to_double (XRATIO_DATA (number)));
|
1983
|
785 #endif
|
|
786
|
444
|
787 if (FLOATP (number)) /* give 'em the same float back */
|
|
788 return number;
|
428
|
789
|
444
|
790 return Ffloat (wrong_type_argument (Qnumberp, number));
|
428
|
791 }
|
|
792
|
|
793 DEFUN ("logb", Flogb, 1, 1, 0, /*
|
444
|
794 Return largest integer <= the base 2 log of the magnitude of NUMBER.
|
428
|
795 This is the same as the exponent of a float.
|
|
796 */
|
444
|
797 (number))
|
428
|
798 {
|
444
|
799 double f = extract_float (number);
|
428
|
800
|
|
801 if (f == 0.0)
|
2039
|
802 return make_int (EMACS_INT_MIN);
|
428
|
803 #ifdef HAVE_LOGB
|
|
804 {
|
|
805 Lisp_Object val;
|
444
|
806 IN_FLOAT (val = make_int ((EMACS_INT) logb (f)), "logb", number);
|
434
|
807 return val;
|
428
|
808 }
|
|
809 #else
|
|
810 #ifdef HAVE_FREXP
|
|
811 {
|
|
812 int exqp;
|
444
|
813 IN_FLOAT (frexp (f, &exqp), "logb", number);
|
434
|
814 return make_int (exqp - 1);
|
428
|
815 }
|
|
816 #else
|
|
817 {
|
|
818 int i;
|
|
819 double d;
|
|
820 EMACS_INT val;
|
|
821 if (f < 0.0)
|
|
822 f = -f;
|
|
823 val = -1;
|
|
824 while (f < 0.5)
|
|
825 {
|
|
826 for (i = 1, d = 0.5; d * d >= f; i += i)
|
|
827 d *= d;
|
|
828 f /= d;
|
|
829 val -= i;
|
|
830 }
|
|
831 while (f >= 1.0)
|
|
832 {
|
|
833 for (i = 1, d = 2.0; d * d <= f; i += i)
|
|
834 d *= d;
|
|
835 f /= d;
|
|
836 val += i;
|
|
837 }
|
434
|
838 return make_int (val);
|
428
|
839 }
|
|
840 #endif /* ! HAVE_FREXP */
|
|
841 #endif /* ! HAVE_LOGB */
|
|
842 }
|
|
843
|
|
844 DEFUN ("ceiling", Fceiling, 1, 1, 0, /*
|
444
|
845 Return the smallest integer no less than NUMBER. (Round toward +inf.)
|
428
|
846 */
|
444
|
847 (number))
|
428
|
848 {
|
444
|
849 if (FLOATP (number))
|
428
|
850 {
|
|
851 double d;
|
444
|
852 IN_FLOAT ((d = ceil (XFLOAT_DATA (number))), "ceiling", number);
|
|
853 return (float_to_int (d, "ceiling", number, Qunbound));
|
428
|
854 }
|
|
855
|
1983
|
856 #ifdef HAVE_BIGNUM
|
|
857 if (INTEGERP (number))
|
|
858 #else
|
444
|
859 if (INTP (number))
|
1983
|
860 #endif
|
444
|
861 return number;
|
428
|
862
|
1983
|
863 #ifdef HAVE_RATIO
|
|
864 if (RATIOP (number))
|
|
865 {
|
|
866 bignum_ceil (scratch_bignum, XRATIO_NUMERATOR (number),
|
|
867 XRATIO_DENOMINATOR (number));
|
|
868 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
869 }
|
|
870 #endif
|
|
871
|
|
872 #ifdef HAVE_BIGFLOAT
|
|
873 if (BIGFLOATP (number))
|
|
874 {
|
|
875 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
876 bigfloat_ceil (scratch_bigfloat, XBIGFLOAT_DATA (number));
|
|
877 #ifdef HAVE_BIGNUM
|
|
878 bignum_set_bigfloat (scratch_bignum, scratch_bigfloat);
|
|
879 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
880 #else
|
|
881 return make_int ((EMACS_INT) bigfloat_to_long (scratch_bigfloat));
|
|
882 #endif /* HAVE_BIGNUM */
|
|
883 }
|
|
884 #endif /* HAVE_BIGFLOAT */
|
|
885
|
444
|
886 return Fceiling (wrong_type_argument (Qnumberp, number));
|
428
|
887 }
|
|
888
|
|
889
|
|
890 DEFUN ("floor", Ffloor, 1, 2, 0, /*
|
444
|
891 Return the largest integer no greater than NUMBER. (Round towards -inf.)
|
|
892 With optional second argument DIVISOR, return the largest integer no
|
|
893 greater than NUMBER/DIVISOR.
|
428
|
894 */
|
444
|
895 (number, divisor))
|
428
|
896 {
|
1983
|
897 #ifdef WITH_NUMBER_TYPES
|
|
898 CHECK_REAL (number);
|
|
899 if (NILP (divisor))
|
|
900 {
|
|
901 if (FLOATP (number))
|
|
902 {
|
|
903 double d;
|
|
904 IN_FLOAT ((d = floor (XFLOAT_DATA (number))), "floor", number);
|
|
905 return (float_to_int (d, "floor", number, Qunbound));
|
|
906 }
|
|
907 #ifdef HAVE_RATIO
|
|
908 else if (RATIOP (number))
|
|
909 {
|
|
910 bignum_floor (scratch_bignum, XRATIO_NUMERATOR (number),
|
|
911 XRATIO_DENOMINATOR (number));
|
|
912 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
913 }
|
|
914 #endif
|
|
915 #ifdef HAVE_BIGFLOAT
|
|
916 else if (BIGFLOATP (number))
|
|
917 {
|
|
918 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
919 bigfloat_floor (scratch_bigfloat, XBIGFLOAT_DATA (number));
|
|
920 return make_bigfloat_bf (scratch_bigfloat);
|
|
921 }
|
|
922 #endif
|
|
923 return number;
|
|
924 }
|
|
925 else
|
|
926 {
|
|
927 CHECK_REAL (divisor);
|
|
928 switch (promote_args (&number, &divisor))
|
|
929 {
|
|
930 case FIXNUM_T:
|
|
931 {
|
|
932 EMACS_INT i1 = XREALINT (number);
|
|
933 EMACS_INT i2 = XREALINT (divisor);
|
|
934
|
|
935 if (i2 == 0)
|
|
936 Fsignal (Qarith_error, Qnil);
|
|
937
|
|
938 /* With C's /, the result is implementation-defined if either
|
|
939 operand is negative, so use only nonnegative operands. */
|
|
940 i1 = (i2 < 0
|
|
941 ? (i1 <= 0 ? -i1 / -i2 : -1 - ((i1 - 1) / -i2))
|
|
942 : (i1 < 0 ? -1 - ((-1 - i1) / i2) : i1 / i2));
|
|
943
|
|
944 return make_int (i1);
|
|
945 }
|
|
946 #ifdef HAVE_BIGNUM
|
|
947 case BIGNUM_T:
|
|
948 if (bignum_sign (XBIGNUM_DATA (divisor)) == 0)
|
|
949 Fsignal (Qarith_error, Qnil);
|
|
950 bignum_floor (scratch_bignum, XBIGNUM_DATA (number),
|
|
951 XBIGNUM_DATA (divisor));
|
|
952 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
953 #endif
|
|
954 #ifdef HAVE_RATIO
|
|
955 case RATIO_T:
|
|
956 if (ratio_sign (XRATIO_DATA (divisor)) == 0)
|
|
957 Fsignal (Qarith_error, Qnil);
|
|
958 ratio_div (scratch_ratio, XRATIO_DATA (number),
|
|
959 XRATIO_DATA (divisor));
|
|
960 bignum_floor (scratch_bignum, ratio_numerator (scratch_ratio),
|
|
961 ratio_denominator (scratch_ratio));
|
|
962 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
963 #endif
|
|
964 #ifdef HAVE_BIGFLOAT
|
|
965 case BIGFLOAT_T:
|
|
966 if (bigfloat_sign (XBIGFLOAT_DATA (divisor)) == 0)
|
|
967 Fsignal (Qarith_error, Qnil);
|
|
968 bigfloat_set_prec (scratch_bigfloat,
|
|
969 max (XBIGFLOAT_GET_PREC (number),
|
|
970 XBIGFLOAT_GET_PREC (divisor)));
|
|
971 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (number),
|
|
972 XBIGFLOAT_DATA (divisor));
|
|
973 bigfloat_floor (scratch_bigfloat, scratch_bigfloat);
|
|
974 return make_bigfloat_bf (scratch_bigfloat);
|
|
975 #endif
|
1995
|
976 default: /* FLOAT_T */
|
|
977 {
|
|
978 double f1 = extract_float (number);
|
|
979 double f2 = extract_float (divisor);
|
|
980
|
|
981 if (f2 == 0.0)
|
|
982 Fsignal (Qarith_error, Qnil);
|
|
983
|
|
984 IN_FLOAT2 (f1 = floor (f1 / f2), "floor", number, divisor);
|
|
985 return float_to_int (f1, "floor", number, divisor);
|
|
986 }
|
1983
|
987 }
|
|
988 }
|
|
989 #else /* !WITH_NUMBER_TYPES */
|
444
|
990 CHECK_INT_OR_FLOAT (number);
|
428
|
991
|
|
992 if (! NILP (divisor))
|
|
993 {
|
|
994 EMACS_INT i1, i2;
|
|
995
|
|
996 CHECK_INT_OR_FLOAT (divisor);
|
|
997
|
444
|
998 if (FLOATP (number) || FLOATP (divisor))
|
428
|
999 {
|
444
|
1000 double f1 = extract_float (number);
|
428
|
1001 double f2 = extract_float (divisor);
|
|
1002
|
|
1003 if (f2 == 0)
|
|
1004 Fsignal (Qarith_error, Qnil);
|
|
1005
|
444
|
1006 IN_FLOAT2 (f1 = floor (f1 / f2), "floor", number, divisor);
|
|
1007 return float_to_int (f1, "floor", number, divisor);
|
428
|
1008 }
|
|
1009
|
444
|
1010 i1 = XINT (number);
|
428
|
1011 i2 = XINT (divisor);
|
|
1012
|
|
1013 if (i2 == 0)
|
|
1014 Fsignal (Qarith_error, Qnil);
|
|
1015
|
|
1016 /* With C's /, the result is implementation-defined if either operand
|
|
1017 is negative, so use only nonnegative operands. */
|
|
1018 i1 = (i2 < 0
|
|
1019 ? (i1 <= 0 ? -i1 / -i2 : -1 - ((i1 - 1) / -i2))
|
|
1020 : (i1 < 0 ? -1 - ((-1 - i1) / i2) : i1 / i2));
|
|
1021
|
|
1022 return (make_int (i1));
|
|
1023 }
|
|
1024
|
444
|
1025 if (FLOATP (number))
|
428
|
1026 {
|
|
1027 double d;
|
444
|
1028 IN_FLOAT ((d = floor (XFLOAT_DATA (number))), "floor", number);
|
|
1029 return (float_to_int (d, "floor", number, Qunbound));
|
428
|
1030 }
|
|
1031
|
444
|
1032 return number;
|
1983
|
1033 #endif /* WITH_NUMBER_TYPES */
|
428
|
1034 }
|
|
1035
|
|
1036 DEFUN ("round", Fround, 1, 1, 0, /*
|
444
|
1037 Return the nearest integer to NUMBER.
|
428
|
1038 */
|
444
|
1039 (number))
|
428
|
1040 {
|
444
|
1041 if (FLOATP (number))
|
428
|
1042 {
|
|
1043 double d;
|
|
1044 /* Screw the prevailing rounding mode. */
|
444
|
1045 IN_FLOAT ((d = emacs_rint (XFLOAT_DATA (number))), "round", number);
|
|
1046 return (float_to_int (d, "round", number, Qunbound));
|
428
|
1047 }
|
|
1048
|
1983
|
1049 #ifdef HAVE_BIGNUM
|
|
1050 if (INTEGERP (number))
|
|
1051 #else
|
444
|
1052 if (INTP (number))
|
1983
|
1053 #endif
|
444
|
1054 return number;
|
428
|
1055
|
1983
|
1056 #ifdef HAVE_RATIO
|
|
1057 if (RATIOP (number))
|
|
1058 {
|
|
1059 if (bignum_divisible_p (XRATIO_NUMERATOR (number),
|
|
1060 XRATIO_DENOMINATOR (number)))
|
|
1061 {
|
|
1062 bignum_div (scratch_bignum, XRATIO_NUMERATOR (number),
|
|
1063 XRATIO_DENOMINATOR (number));
|
|
1064 }
|
|
1065 else
|
|
1066 {
|
|
1067 bignum_add (scratch_bignum2, XRATIO_NUMERATOR (number),
|
|
1068 XRATIO_DENOMINATOR (number));
|
|
1069 bignum_div (scratch_bignum, scratch_bignum2,
|
|
1070 XRATIO_DENOMINATOR (number));
|
|
1071 }
|
|
1072 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1073 }
|
|
1074 #endif
|
|
1075
|
|
1076 #ifdef HAVE_BIGFLOAT
|
|
1077 if (BIGFLOATP (number))
|
|
1078 {
|
|
1079 unsigned long prec = XBIGFLOAT_GET_PREC (number);
|
|
1080 bigfloat_set_prec (scratch_bigfloat, prec);
|
|
1081 bigfloat_set_prec (scratch_bigfloat2, prec);
|
|
1082 bigfloat_set_double (scratch_bigfloat2,
|
|
1083 bigfloat_sign (XBIGFLOAT_DATA (number)) * 0.5);
|
|
1084 bigfloat_floor (scratch_bigfloat, scratch_bigfloat2);
|
|
1085 #ifdef HAVE_BIGNUM
|
|
1086 bignum_set_bigfloat (scratch_bignum, scratch_bigfloat);
|
|
1087 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1088 #else
|
|
1089 return make_int ((EMACS_INT) bigfloat_to_long (scratch_bigfloat));
|
|
1090 #endif /* HAVE_BIGNUM */
|
|
1091 }
|
|
1092 #endif /* HAVE_BIGFLOAT */
|
|
1093
|
444
|
1094 return Fround (wrong_type_argument (Qnumberp, number));
|
428
|
1095 }
|
|
1096
|
|
1097 DEFUN ("truncate", Ftruncate, 1, 1, 0, /*
|
|
1098 Truncate a floating point number to an integer.
|
|
1099 Rounds the value toward zero.
|
|
1100 */
|
444
|
1101 (number))
|
428
|
1102 {
|
444
|
1103 if (FLOATP (number))
|
|
1104 return float_to_int (XFLOAT_DATA (number), "truncate", number, Qunbound);
|
428
|
1105
|
1983
|
1106 #ifdef HAVE_BIGNUM
|
|
1107 if (INTEGERP (number))
|
|
1108 #else
|
444
|
1109 if (INTP (number))
|
1983
|
1110 #endif
|
444
|
1111 return number;
|
428
|
1112
|
1983
|
1113 #ifdef HAVE_RATIO
|
|
1114 if (RATIOP (number))
|
|
1115 {
|
|
1116 bignum_div (scratch_bignum, XRATIO_NUMERATOR (number),
|
|
1117 XRATIO_DENOMINATOR (number));
|
|
1118 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1119 }
|
|
1120 #endif
|
|
1121
|
|
1122 #ifdef HAVE_BIGFLOAT
|
|
1123 if (BIGFLOATP (number))
|
|
1124 {
|
|
1125 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number));
|
|
1126 bigfloat_trunc (scratch_bigfloat, XBIGFLOAT_DATA (number));
|
|
1127 #ifdef HAVE_BIGNUM
|
|
1128 bignum_set_bigfloat (scratch_bignum, scratch_bigfloat);
|
|
1129 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
1130 #else
|
|
1131 return make_int ((EMACS_INT) bigfloat_to_long (scratch_bigfloat));
|
|
1132 #endif /* HAVE_BIGNUM */
|
|
1133 }
|
|
1134 #endif /* HAVE_BIGFLOAT */
|
|
1135
|
444
|
1136 return Ftruncate (wrong_type_argument (Qnumberp, number));
|
428
|
1137 }
|
|
1138
|
|
1139 /* Float-rounding functions. */
|
|
1140
|
|
1141 DEFUN ("fceiling", Ffceiling, 1, 1, 0, /*
|
444
|
1142 Return the smallest integer no less than NUMBER, as a float.
|
428
|
1143 \(Round toward +inf.\)
|
|
1144 */
|
444
|
1145 (number))
|
428
|
1146 {
|
444
|
1147 double d = extract_float (number);
|
|
1148 IN_FLOAT (d = ceil (d), "fceiling", number);
|
428
|
1149 return make_float (d);
|
|
1150 }
|
|
1151
|
|
1152 DEFUN ("ffloor", Fffloor, 1, 1, 0, /*
|
444
|
1153 Return the largest integer no greater than NUMBER, as a float.
|
428
|
1154 \(Round towards -inf.\)
|
|
1155 */
|
444
|
1156 (number))
|
428
|
1157 {
|
444
|
1158 double d = extract_float (number);
|
|
1159 IN_FLOAT (d = floor (d), "ffloor", number);
|
428
|
1160 return make_float (d);
|
|
1161 }
|
|
1162
|
|
1163 DEFUN ("fround", Ffround, 1, 1, 0, /*
|
444
|
1164 Return the nearest integer to NUMBER, as a float.
|
428
|
1165 */
|
444
|
1166 (number))
|
428
|
1167 {
|
444
|
1168 double d = extract_float (number);
|
|
1169 IN_FLOAT (d = emacs_rint (d), "fround", number);
|
428
|
1170 return make_float (d);
|
|
1171 }
|
|
1172
|
|
1173 DEFUN ("ftruncate", Fftruncate, 1, 1, 0, /*
|
|
1174 Truncate a floating point number to an integral float value.
|
|
1175 Rounds the value toward zero.
|
|
1176 */
|
444
|
1177 (number))
|
428
|
1178 {
|
444
|
1179 double d = extract_float (number);
|
428
|
1180 if (d >= 0.0)
|
444
|
1181 IN_FLOAT (d = floor (d), "ftruncate", number);
|
428
|
1182 else
|
444
|
1183 IN_FLOAT (d = ceil (d), "ftruncate", number);
|
428
|
1184 return make_float (d);
|
|
1185 }
|
|
1186
|
|
1187 #ifdef FLOAT_CATCH_SIGILL
|
|
1188 static SIGTYPE
|
|
1189 float_error (int signo)
|
|
1190 {
|
|
1191 if (! in_float)
|
|
1192 fatal_error_signal (signo);
|
|
1193
|
|
1194 EMACS_REESTABLISH_SIGNAL (signo, arith_error);
|
|
1195 EMACS_UNBLOCK_SIGNAL (signo);
|
|
1196
|
|
1197 in_float = 0;
|
|
1198
|
|
1199 /* Was Fsignal(), but it just doesn't make sense for an error
|
|
1200 occurring inside a signal handler to be restartable, considering
|
|
1201 that anything could happen when the error is signaled and trapped
|
|
1202 and considering the asynchronous nature of signal handlers. */
|
563
|
1203 signal_error (Qarith_error, 0, float_error_arg);
|
428
|
1204 }
|
|
1205
|
|
1206 /* Another idea was to replace the library function `infnan'
|
|
1207 where SIGILL is signaled. */
|
|
1208
|
|
1209 #endif /* FLOAT_CATCH_SIGILL */
|
|
1210
|
|
1211 /* In C++, it is impossible to determine what type matherr expects
|
|
1212 without some more configure magic.
|
|
1213 We shouldn't be using matherr anyways - it's a non-standard SYSVism. */
|
|
1214 #if defined (HAVE_MATHERR) && !defined(__cplusplus)
|
|
1215 int
|
|
1216 matherr (struct exception *x)
|
|
1217 {
|
|
1218 Lisp_Object args;
|
|
1219 if (! in_float)
|
|
1220 /* Not called from emacs-lisp float routines; do the default thing. */
|
|
1221 return 0;
|
|
1222
|
|
1223 /* if (!strcmp (x->name, "pow")) x->name = "expt"; */
|
|
1224
|
|
1225 args = Fcons (build_string (x->name),
|
|
1226 Fcons (make_float (x->arg1),
|
|
1227 ((in_float == 2)
|
|
1228 ? Fcons (make_float (x->arg2), Qnil)
|
|
1229 : Qnil)));
|
|
1230 switch (x->type)
|
|
1231 {
|
|
1232 case DOMAIN: Fsignal (Qdomain_error, args); break;
|
|
1233 case SING: Fsignal (Qsingularity_error, args); break;
|
|
1234 case OVERFLOW: Fsignal (Qoverflow_error, args); break;
|
|
1235 case UNDERFLOW: Fsignal (Qunderflow_error, args); break;
|
|
1236 default: Fsignal (Qarith_error, args); break;
|
|
1237 }
|
|
1238 return 1; /* don't set errno or print a message */
|
|
1239 }
|
|
1240 #endif /* HAVE_MATHERR */
|
|
1241
|
|
1242 void
|
|
1243 init_floatfns_very_early (void)
|
|
1244 {
|
|
1245 # ifdef FLOAT_CATCH_SIGILL
|
613
|
1246 EMACS_SIGNAL (SIGILL, float_error);
|
428
|
1247 # endif
|
|
1248 in_float = 0;
|
|
1249 }
|
|
1250
|
|
1251 void
|
|
1252 syms_of_floatfns (void)
|
|
1253 {
|
442
|
1254 INIT_LRECORD_IMPLEMENTATION (float);
|
428
|
1255
|
|
1256 /* Trig functions. */
|
|
1257
|
|
1258 DEFSUBR (Facos);
|
|
1259 DEFSUBR (Fasin);
|
|
1260 DEFSUBR (Fatan);
|
|
1261 DEFSUBR (Fcos);
|
|
1262 DEFSUBR (Fsin);
|
|
1263 DEFSUBR (Ftan);
|
|
1264
|
|
1265 /* Bessel functions */
|
|
1266
|
|
1267 #if 0
|
|
1268 DEFSUBR (Fbessel_y0);
|
|
1269 DEFSUBR (Fbessel_y1);
|
|
1270 DEFSUBR (Fbessel_yn);
|
|
1271 DEFSUBR (Fbessel_j0);
|
|
1272 DEFSUBR (Fbessel_j1);
|
|
1273 DEFSUBR (Fbessel_jn);
|
|
1274 #endif /* 0 */
|
|
1275
|
|
1276 /* Error functions. */
|
|
1277
|
|
1278 #if 0
|
|
1279 DEFSUBR (Ferf);
|
|
1280 DEFSUBR (Ferfc);
|
|
1281 DEFSUBR (Flog_gamma);
|
|
1282 #endif /* 0 */
|
|
1283
|
|
1284 /* Root and Log functions. */
|
|
1285
|
|
1286 DEFSUBR (Fexp);
|
|
1287 DEFSUBR (Fexpt);
|
|
1288 DEFSUBR (Flog);
|
|
1289 DEFSUBR (Flog10);
|
|
1290 DEFSUBR (Fsqrt);
|
|
1291 DEFSUBR (Fcube_root);
|
|
1292
|
|
1293 /* Inverse trig functions. */
|
|
1294
|
|
1295 DEFSUBR (Facosh);
|
|
1296 DEFSUBR (Fasinh);
|
|
1297 DEFSUBR (Fatanh);
|
|
1298 DEFSUBR (Fcosh);
|
|
1299 DEFSUBR (Fsinh);
|
|
1300 DEFSUBR (Ftanh);
|
|
1301
|
|
1302 /* Rounding functions */
|
|
1303
|
|
1304 DEFSUBR (Fabs);
|
|
1305 DEFSUBR (Ffloat);
|
|
1306 DEFSUBR (Flogb);
|
|
1307 DEFSUBR (Fceiling);
|
|
1308 DEFSUBR (Ffloor);
|
|
1309 DEFSUBR (Fround);
|
|
1310 DEFSUBR (Ftruncate);
|
|
1311
|
|
1312 /* Float-rounding functions. */
|
|
1313
|
|
1314 DEFSUBR (Ffceiling);
|
|
1315 DEFSUBR (Fffloor);
|
|
1316 DEFSUBR (Ffround);
|
|
1317 DEFSUBR (Fftruncate);
|
|
1318 }
|
|
1319
|
|
1320 void
|
|
1321 vars_of_floatfns (void)
|
|
1322 {
|
|
1323 Fprovide (intern ("lisp-float-type"));
|
|
1324 }
|