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