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