1983
|
1 /* Numeric types for XEmacs.
|
|
2 Copyright (C) 2004 Jerry James.
|
|
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: Not in FSF. */
|
|
22
|
|
23 #include <config.h>
|
|
24 #include <limits.h>
|
|
25 #include "lisp.h"
|
|
26
|
2001
|
27 Lisp_Object Qrationalp, Qfloatingp, Qrealp;
|
1983
|
28 Lisp_Object Vdefault_float_precision;
|
|
29 Fixnum Vmost_negative_fixnum, Vmost_positive_fixnum;
|
|
30 static Lisp_Object Qunsupported_type;
|
|
31 static Lisp_Object Vbigfloat_max_prec;
|
|
32 static int number_initialized;
|
|
33
|
|
34 #ifdef HAVE_BIGNUM
|
|
35 bignum scratch_bignum, scratch_bignum2;
|
|
36 #endif
|
|
37 #ifdef HAVE_RATIO
|
|
38 ratio scratch_ratio;
|
|
39 #endif
|
|
40 #ifdef HAVE_BIGFLOAT
|
|
41 bigfloat scratch_bigfloat, scratch_bigfloat2;
|
|
42 #endif
|
|
43
|
|
44 /********************************* Bignums **********************************/
|
|
45 #ifdef HAVE_BIGNUM
|
|
46 static void
|
|
47 bignum_print (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
48 {
|
|
49 CIbyte *bstr = bignum_to_string (XBIGNUM_DATA (obj), 10);
|
|
50 write_c_string (printcharfun, bstr);
|
|
51 xfree (bstr, CIbyte *);
|
|
52 }
|
|
53
|
|
54 static int
|
|
55 bignum_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
56 {
|
|
57 return bignum_eql (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2));
|
|
58 }
|
|
59
|
|
60 static Hashcode
|
|
61 bignum_hash (Lisp_Object obj, int depth)
|
|
62 {
|
|
63 return bignum_hashcode (XBIGNUM_DATA (obj));
|
|
64 }
|
|
65
|
|
66 static const struct memory_description bignum_description[] = {
|
|
67 { XD_OPAQUE_PTR, offsetof (Lisp_Bignum, data) },
|
|
68 { XD_END }
|
|
69 };
|
|
70
|
2061
|
71 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("bignum", bignum, 0, 0, bignum_print,
|
|
72 0, bignum_equal, bignum_hash,
|
|
73 bignum_description, Lisp_Bignum);
|
1983
|
74
|
2092
|
75 #endif /* HAVE_BIGNUM */
|
1983
|
76
|
|
77 Lisp_Object Qbignump;
|
|
78
|
|
79 DEFUN ("bignump", Fbignump, 1, 1, 0, /*
|
|
80 Return t if OBJECT is a bignum, nil otherwise.
|
|
81 */
|
|
82 (object))
|
|
83 {
|
|
84 return BIGNUMP (object) ? Qt : Qnil;
|
|
85 }
|
|
86
|
|
87
|
|
88 /********************************* Integers *********************************/
|
|
89 DEFUN ("integerp", Fintegerp, 1, 1, 0, /*
|
|
90 Return t if OBJECT is an integer, nil otherwise.
|
|
91 */
|
|
92 (object))
|
|
93 {
|
|
94 return INTEGERP (object) ? Qt : Qnil;
|
|
95 }
|
|
96
|
|
97 DEFUN ("evenp", Fevenp, 1, 1, 0, /*
|
|
98 Return t if INTEGER is even, nil otherwise.
|
|
99 */
|
|
100 (integer))
|
|
101 {
|
|
102 CONCHECK_INTEGER (integer);
|
1996
|
103 return (BIGNUMP (integer)
|
|
104 ? bignum_evenp (XBIGNUM_DATA (integer))
|
|
105 : XTYPE (integer) == Lisp_Type_Int_Even) ? Qt : Qnil;
|
1983
|
106 }
|
|
107
|
2019
|
108 DEFUN ("oddp", Foddp, 1, 1, 0, /*
|
1983
|
109 Return t if INTEGER is odd, nil otherwise.
|
|
110 */
|
|
111 (integer))
|
|
112 {
|
|
113 CONCHECK_INTEGER (integer);
|
1996
|
114 return (BIGNUMP (integer)
|
|
115 ? bignum_oddp (XBIGNUM_DATA (integer))
|
|
116 : XTYPE (integer) == Lisp_Type_Int_Odd) ? Qt : Qnil;
|
1983
|
117 }
|
|
118
|
|
119
|
|
120 /********************************** Ratios **********************************/
|
|
121 #ifdef HAVE_RATIO
|
|
122 static void
|
|
123 ratio_print (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
124 {
|
|
125 CIbyte *rstr = ratio_to_string (XRATIO_DATA (obj), 10);
|
|
126 write_c_string (printcharfun, rstr);
|
|
127 xfree (rstr, CIbyte *);
|
|
128 }
|
|
129
|
|
130 static int
|
|
131 ratio_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
132 {
|
|
133 return ratio_eql (XRATIO_DATA (obj1), XRATIO_DATA (obj2));
|
|
134 }
|
|
135
|
|
136 static Hashcode
|
|
137 ratio_hash (Lisp_Object obj, int depth)
|
|
138 {
|
|
139 return ratio_hashcode (XRATIO_DATA (obj));
|
|
140 }
|
|
141
|
|
142 static const struct memory_description ratio_description[] = {
|
|
143 { XD_OPAQUE_PTR, offsetof (Lisp_Ratio, data) },
|
|
144 { XD_END }
|
|
145 };
|
|
146
|
2061
|
147 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("ratio", ratio, 0, 0, ratio_print,
|
|
148 0, ratio_equal, ratio_hash,
|
|
149 ratio_description, Lisp_Ratio);
|
1983
|
150
|
2092
|
151 #endif /* HAVE_RATIO */
|
1983
|
152
|
|
153 Lisp_Object Qratiop;
|
|
154
|
|
155 DEFUN ("ratiop", Fratiop, 1, 1, 0, /*
|
|
156 Return t if OBJECT is a ratio, nil otherwise.
|
|
157 */
|
|
158 (object))
|
|
159 {
|
|
160 return RATIOP (object) ? Qt : Qnil;
|
|
161 }
|
|
162
|
|
163
|
|
164 /******************************** Rationals *********************************/
|
|
165 DEFUN ("rationalp", Frationalp, 1, 1, 0, /*
|
|
166 Return t if OBJECT is a rational, nil otherwise.
|
|
167 */
|
|
168 (object))
|
|
169 {
|
|
170 return RATIONALP (object) ? Qt : Qnil;
|
|
171 }
|
|
172
|
|
173 DEFUN ("numerator", Fnumerator, 1, 1, 0, /*
|
|
174 Return the numerator of the canonical form of RATIONAL.
|
|
175 If RATIONAL is an integer, RATIONAL is returned.
|
|
176 */
|
|
177 (rational))
|
|
178 {
|
|
179 CONCHECK_RATIONAL (rational);
|
|
180 #ifdef HAVE_RATIO
|
|
181 return RATIOP (rational)
|
|
182 ? make_bignum_bg (XRATIO_NUMERATOR (rational))
|
|
183 : rational;
|
|
184 #else
|
|
185 return rational;
|
|
186 #endif
|
|
187 }
|
|
188
|
|
189 DEFUN ("denominator", Fdenominator, 1, 1, 0, /*
|
|
190 Return the denominator of the canonical form of RATIONAL.
|
|
191 If RATIONAL is an integer, 1 is returned.
|
|
192 */
|
|
193 (rational))
|
|
194 {
|
|
195 CONCHECK_RATIONAL (rational);
|
|
196 #ifdef HAVE_RATIO
|
|
197 return RATIOP (rational)
|
|
198 ? make_bignum_bg (XRATIO_DENOMINATOR (rational))
|
|
199 : make_int (1);
|
|
200 #else
|
|
201 return rational;
|
|
202 #endif
|
|
203 }
|
|
204
|
|
205
|
|
206 /******************************** Bigfloats *********************************/
|
|
207 #ifdef HAVE_BIGFLOAT
|
|
208 static void
|
|
209 bigfloat_print (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
210 {
|
|
211 CIbyte *fstr = bigfloat_to_string (XBIGFLOAT_DATA (obj), 10);
|
|
212 write_c_string (printcharfun, fstr);
|
|
213 xfree (fstr, CIbyte *);
|
|
214 }
|
|
215
|
|
216 static int
|
|
217 bigfloat_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
218 {
|
|
219 return bigfloat_eql (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2));
|
|
220 }
|
|
221
|
|
222 static Hashcode
|
|
223 bigfloat_hash (Lisp_Object obj, int depth)
|
|
224 {
|
|
225 return bigfloat_hashcode (XBIGFLOAT_DATA (obj));
|
|
226 }
|
|
227
|
|
228 static const struct memory_description bigfloat_description[] = {
|
|
229 { XD_OPAQUE_PTR, offsetof (Lisp_Bigfloat, bf) },
|
|
230 { XD_END }
|
|
231 };
|
|
232
|
2061
|
233 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("bigfloat", bigfloat, 1, 0,
|
|
234 bigfloat_print, 0,
|
|
235 bigfloat_equal, bigfloat_hash,
|
|
236 bigfloat_description, Lisp_Bigfloat);
|
1983
|
237
|
2092
|
238 #endif /* HAVE_BIGFLOAT */
|
1983
|
239
|
|
240 Lisp_Object Qbigfloatp;
|
|
241
|
|
242 DEFUN ("bigfloatp", Fbigfloatp, 1, 1, 0, /*
|
|
243 Return t if OBJECT is a bigfloat, nil otherwise.
|
|
244 */
|
|
245 (object))
|
|
246 {
|
|
247 return BIGFLOATP (object) ? Qt : Qnil;
|
|
248 }
|
|
249
|
2092
|
250 DEFUN ("bigfloat-get-precision", Fbigfloat_get_precision, 1, 1, 0, /*
|
|
251 Return the precision of bigfloat F as an integer.
|
|
252 */
|
|
253 (f))
|
|
254 {
|
|
255 CHECK_BIGFLOAT (f);
|
|
256 #ifdef HAVE_BIGNUM
|
|
257 bignum_set_ulong (scratch_bignum, XBIGFLOAT_GET_PREC (f));
|
|
258 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
|
|
259 #else
|
|
260 return make_int ((int) XBIGFLOAT_GET_PREC (f));
|
|
261 #endif
|
|
262 }
|
|
263
|
|
264 DEFUN ("bigfloat-set-precision", Fbigfloat_set_precision, 2, 2, 0, /*
|
|
265 Set the precision of F, a bigfloat, to PRECISION, a nonnegative integer.
|
|
266 The new precision of F is returned. Note that the return value may differ
|
|
267 from PRECISION if the underlying library is unable to support exactly
|
|
268 PRECISION bits of precision.
|
|
269 */
|
|
270 (f, precision))
|
|
271 {
|
|
272 unsigned long prec;
|
|
273
|
|
274 CHECK_BIGFLOAT (f);
|
|
275 if (INTP (precision))
|
|
276 {
|
|
277 prec = (XINT (precision) <= 0) ? 1UL : (unsigned long) XINT (precision);
|
|
278 }
|
|
279 #ifdef HAVE_BIGNUM
|
|
280 else if (BIGNUMP (precision))
|
|
281 {
|
|
282 prec = bignum_fits_ulong_p (XBIGNUM_DATA (precision))
|
|
283 ? bignum_to_ulong (XBIGNUM_DATA (precision))
|
|
284 : UINT_MAX;
|
|
285 }
|
|
286 #endif
|
|
287 else
|
|
288 {
|
|
289 dead_wrong_type_argument (Qintegerp, f);
|
|
290 return Qnil;
|
|
291 }
|
|
292
|
|
293 XBIGFLOAT_SET_PREC (f, prec);
|
|
294 return Fbigfloat_get_precision (f);
|
|
295 }
|
|
296
|
1983
|
297 static int
|
|
298 default_float_precision_changed (Lisp_Object sym, Lisp_Object *val,
|
|
299 Lisp_Object in_object, int flags)
|
|
300 {
|
|
301 unsigned long prec;
|
|
302
|
|
303 CONCHECK_INTEGER (*val);
|
|
304 #ifdef HAVE_BIGFLOAT
|
|
305 if (INTP (*val))
|
|
306 prec = XINT (*val);
|
|
307 else
|
|
308 {
|
|
309 if (!bignum_fits_ulong_p (XBIGNUM_DATA (*val)))
|
|
310 args_out_of_range_3 (*val, Qzero, Vbigfloat_max_prec);
|
|
311 prec = bignum_to_ulong (XBIGNUM_DATA (*val));
|
|
312 }
|
|
313 if (prec != 0UL)
|
|
314 bigfloat_set_default_prec (prec);
|
|
315 #endif
|
|
316 return 0;
|
|
317 }
|
|
318
|
|
319
|
|
320 /********************************* Floating *********************************/
|
|
321 Lisp_Object
|
|
322 make_floating (double d)
|
|
323 {
|
|
324 #ifdef HAVE_BIGFLOAT
|
|
325 if (ZEROP (Vdefault_float_precision))
|
|
326 #endif
|
|
327 return make_float (d);
|
|
328 #ifdef HAVE_BIGFLOAT
|
|
329 else
|
|
330 return make_bigfloat (d, 0UL);
|
|
331 #endif
|
|
332 }
|
|
333
|
|
334 DEFUN ("floatingp", Ffloatingp, 1, 1, 0, /*
|
|
335 Return t if OBJECT is a floating point number of any kind, nil otherwise.
|
|
336 */
|
|
337 (object))
|
|
338 {
|
|
339 return FLOATINGP (object) ? Qt : Qnil;
|
|
340 }
|
|
341
|
|
342
|
|
343 /********************************** Reals ***********************************/
|
|
344 DEFUN ("realp", Frealp, 1, 1, 0, /*
|
|
345 Return t if OBJECT is a real, nil otherwise.
|
|
346 */
|
|
347 (object))
|
|
348 {
|
|
349 return REALP (object) ? Qt : Qnil;
|
|
350 }
|
|
351
|
|
352
|
|
353 /********************************* Numbers **********************************/
|
|
354 DEFUN ("canonicalize-number", Fcanonicalize_number, 1, 1, 0, /*
|
|
355 Return the canonical form of NUMBER.
|
|
356 */
|
|
357 (number))
|
|
358 {
|
|
359 /* The tests should go in order from larger, more expressive, or more
|
|
360 complex types to smaller, less expressive, or simpler types so that a
|
|
361 number can cascade all the way down to the simplest type if
|
|
362 appropriate. */
|
|
363 #ifdef HAVE_RATIO
|
|
364 if (RATIOP (number) &&
|
|
365 bignum_fits_long_p (XRATIO_DENOMINATOR (number)) &&
|
|
366 bignum_to_long (XRATIO_DENOMINATOR (number)) == 1L)
|
|
367 number = make_bignum_bg (XRATIO_NUMERATOR (number));
|
|
368 #endif
|
|
369 #ifdef HAVE_BIGNUM
|
|
370 if (BIGNUMP (number) && bignum_fits_int_p (XBIGNUM_DATA (number)))
|
|
371 {
|
|
372 int n = bignum_to_int (XBIGNUM_DATA (number));
|
|
373 if (NUMBER_FITS_IN_AN_EMACS_INT (n))
|
|
374 number = make_int (n);
|
|
375 }
|
|
376 #endif
|
|
377 return number;
|
|
378 }
|
|
379
|
|
380 enum number_type
|
|
381 get_number_type (Lisp_Object arg)
|
|
382 {
|
|
383 if (INTP (arg))
|
|
384 return FIXNUM_T;
|
|
385 #ifdef HAVE_BIGNUM
|
|
386 if (BIGNUMP (arg))
|
|
387 return BIGNUM_T;
|
|
388 #endif
|
|
389 #ifdef HAVE_RATIO
|
|
390 if (RATIOP (arg))
|
|
391 return RATIO_T;
|
|
392 #endif
|
|
393 if (FLOATP (arg))
|
|
394 return FLOAT_T;
|
|
395 #ifdef HAVE_BIGFLOAT
|
|
396 if (BIGFLOATP (arg))
|
|
397 return BIGFLOAT_T;
|
|
398 #endif
|
|
399 /* Catch unintentional bad uses of this function */
|
|
400 abort ();
|
1995
|
401 /* NOTREACHED */
|
|
402 return FIXNUM_T;
|
1983
|
403 }
|
|
404
|
|
405 /* Convert NUMBER to type TYPE. If TYPE is BIGFLOAT_T then use the indicated
|
|
406 PRECISION; otherwise, PRECISION is ignored. */
|
|
407 static Lisp_Object
|
|
408 internal_coerce_number (Lisp_Object number, enum number_type type,
|
|
409 unsigned long precision)
|
|
410 {
|
|
411 enum number_type current_type;
|
|
412
|
|
413 if (CHARP (number))
|
|
414 number = make_int (XCHAR (number));
|
|
415 else if (MARKERP (number))
|
|
416 number = make_int (marker_position (number));
|
|
417
|
|
418 /* Note that CHECK_NUMBER ensures that NUMBER is a supported type. Hence,
|
|
419 we abort() in the #else sections below, because it shouldn't be possible
|
|
420 to arrive there. */
|
|
421 CHECK_NUMBER (number);
|
|
422 current_type = get_number_type (number);
|
|
423 switch (current_type)
|
|
424 {
|
|
425 case FIXNUM_T:
|
|
426 switch (type)
|
|
427 {
|
|
428 case FIXNUM_T:
|
|
429 return number;
|
|
430 case BIGNUM_T:
|
|
431 #ifdef HAVE_BIGNUM
|
|
432 return make_bignum (XREALINT (number));
|
|
433 #else
|
|
434 abort ();
|
|
435 #endif /* HAVE_BIGNUM */
|
|
436 case RATIO_T:
|
|
437 #ifdef HAVE_RATIO
|
|
438 return make_ratio (XREALINT (number), 1UL);
|
|
439 #else
|
|
440 abort ();
|
|
441 #endif /* HAVE_RATIO */
|
|
442 case FLOAT_T:
|
|
443 return make_float (XREALINT (number));
|
|
444 case BIGFLOAT_T:
|
|
445 #ifdef HAVE_BIGFLOAT
|
|
446 return make_bigfloat (XREALINT (number), precision);
|
|
447 #else
|
|
448 abort ();
|
|
449 #endif /* HAVE_BIGFLOAT */
|
|
450 }
|
|
451 case BIGNUM_T:
|
|
452 #ifdef HAVE_BIGNUM
|
|
453 switch (type)
|
|
454 {
|
|
455 case FIXNUM_T:
|
|
456 return make_int (bignum_to_long (XBIGNUM_DATA (number)));
|
|
457 case BIGNUM_T:
|
|
458 return number;
|
|
459 case RATIO_T:
|
|
460 #ifdef HAVE_RATIO
|
|
461 bignum_set_long (scratch_bignum, 1L);
|
|
462 return make_ratio_bg (XBIGNUM_DATA (number), scratch_bignum);
|
|
463 #else
|
|
464 abort ();
|
|
465 #endif /* HAVE_RATIO */
|
|
466 case FLOAT_T:
|
|
467 return make_float (bignum_to_double (XBIGNUM_DATA (number)));
|
|
468 case BIGFLOAT_T:
|
|
469 #ifdef HAVE_BIGFLOAT
|
|
470 {
|
|
471 Lisp_Object temp;
|
|
472 temp = make_bigfloat (0.0, precision);
|
|
473 bigfloat_set_bignum (XBIGFLOAT_DATA (temp), XBIGNUM_DATA (number));
|
|
474 return temp;
|
|
475 }
|
|
476 #else
|
|
477 abort ();
|
|
478 #endif /* HAVE_BIGFLOAT */
|
|
479 }
|
|
480 #else
|
|
481 abort ();
|
|
482 #endif /* HAVE_BIGNUM */
|
|
483 case RATIO_T:
|
|
484 #ifdef HAVE_RATIO
|
|
485 switch (type)
|
|
486 {
|
|
487 case FIXNUM_T:
|
|
488 bignum_div (scratch_bignum, XRATIO_NUMERATOR (number),
|
|
489 XRATIO_DENOMINATOR (number));
|
|
490 return make_int (bignum_to_long (scratch_bignum));
|
|
491 case BIGNUM_T:
|
|
492 bignum_div (scratch_bignum, XRATIO_NUMERATOR (number),
|
|
493 XRATIO_DENOMINATOR (number));
|
|
494 return make_bignum_bg (scratch_bignum);
|
|
495 case RATIO_T:
|
|
496 return number;
|
|
497 case FLOAT_T:
|
|
498 return make_float (ratio_to_double (XRATIO_DATA (number)));
|
|
499 case BIGFLOAT_T:
|
|
500 #ifdef HAVE_BIGFLOAT
|
|
501 {
|
|
502 Lisp_Object temp;
|
|
503 temp = make_bigfloat (0.0, precision);
|
|
504 bigfloat_set_ratio (XBIGFLOAT_DATA (temp), XRATIO_DATA (number));
|
|
505 return temp;
|
|
506 }
|
|
507 #else
|
|
508 abort ();
|
|
509 #endif /* HAVE_BIGFLOAT */
|
|
510 }
|
|
511 #else
|
|
512 abort ();
|
|
513 #endif /* HAVE_RATIO */
|
|
514 case FLOAT_T:
|
|
515 switch (type)
|
|
516 {
|
|
517 case FIXNUM_T:
|
1995
|
518 return Ftruncate (number);
|
1983
|
519 case BIGNUM_T:
|
|
520 #ifdef HAVE_BIGNUM
|
|
521 bignum_set_double (scratch_bignum, XFLOAT_DATA (number));
|
|
522 return make_bignum_bg (scratch_bignum);
|
|
523 #else
|
|
524 abort ();
|
|
525 #endif /* HAVE_BIGNUM */
|
|
526 case RATIO_T:
|
|
527 #ifdef HAVE_RATIO
|
|
528 ratio_set_double (scratch_ratio, XFLOAT_DATA (number));
|
|
529 return make_ratio_rt (scratch_ratio);
|
|
530 #else
|
|
531 abort ();
|
|
532 #endif /* HAVE_RATIO */
|
|
533 case FLOAT_T:
|
|
534 return number;
|
|
535 case BIGFLOAT_T:
|
|
536 #ifdef HAVE_BIGFLOAT
|
|
537 bigfloat_set_prec (scratch_bigfloat, precision);
|
|
538 bigfloat_set_double (scratch_bigfloat, XFLOAT_DATA (number));
|
|
539 return make_bigfloat_bf (scratch_bigfloat);
|
|
540 #else
|
|
541 abort ();
|
|
542 #endif /* HAVE_BIGFLOAT */
|
|
543 }
|
|
544 case BIGFLOAT_T:
|
|
545 #ifdef HAVE_BIGFLOAT
|
|
546 switch (type)
|
|
547 {
|
|
548 case FIXNUM_T:
|
|
549 return make_int (bigfloat_to_long (XBIGFLOAT_DATA (number)));
|
|
550 case BIGNUM_T:
|
|
551 #ifdef HAVE_BIGNUM
|
|
552 bignum_set_bigfloat (scratch_bignum, XBIGFLOAT_DATA (number));
|
|
553 return make_bignum_bg (scratch_bignum);
|
|
554 #else
|
|
555 abort ();
|
|
556 #endif /* HAVE_BIGNUM */
|
|
557 case RATIO_T:
|
|
558 #ifdef HAVE_RATIO
|
|
559 ratio_set_bigfloat (scratch_ratio, XBIGFLOAT_DATA (number));
|
|
560 return make_ratio_rt (scratch_ratio);
|
|
561 #else
|
|
562 abort ();
|
|
563 #endif
|
|
564 case FLOAT_T:
|
|
565 return make_float (bigfloat_to_double (XBIGFLOAT_DATA (number)));
|
|
566 case BIGFLOAT_T:
|
|
567 /* FIXME: Do we need to change the precision? */
|
|
568 return number;
|
|
569 }
|
|
570 #else
|
|
571 abort ();
|
|
572 #endif /* HAVE_BIGFLOAT */
|
|
573 }
|
|
574 abort ();
|
1995
|
575 /* NOTREACHED */
|
|
576 return Qzero;
|
1983
|
577 }
|
|
578
|
|
579 /* This function promotes its arguments as necessary to make them both the
|
|
580 same type. It destructively modifies its arguments to do so. Characters
|
|
581 and markers are ALWAYS converted to integers. */
|
|
582 enum number_type
|
|
583 promote_args (Lisp_Object *arg1, Lisp_Object *arg2)
|
|
584 {
|
|
585 enum number_type type1, type2;
|
|
586
|
|
587 if (CHARP (*arg1))
|
|
588 *arg1 = make_int (XCHAR (*arg1));
|
|
589 else if (MARKERP (*arg1))
|
|
590 *arg1 = make_int (marker_position (*arg1));
|
|
591 if (CHARP (*arg2))
|
|
592 *arg2 = make_int (XCHAR (*arg2));
|
|
593 else if (MARKERP (*arg2))
|
|
594 *arg2 = make_int (marker_position (*arg2));
|
|
595
|
|
596 CHECK_NUMBER (*arg1);
|
|
597 CHECK_NUMBER (*arg2);
|
|
598
|
|
599 type1 = get_number_type (*arg1);
|
|
600 type2 = get_number_type (*arg2);
|
|
601
|
|
602 if (type1 < type2)
|
|
603 {
|
|
604 *arg1 = internal_coerce_number (*arg1, type2,
|
|
605 #ifdef HAVE_BIGFLOAT
|
|
606 type2 == BIGFLOAT_T
|
|
607 ? XBIGFLOAT_GET_PREC (*arg2) :
|
|
608 #endif
|
|
609 0UL);
|
|
610 return type2;
|
|
611 }
|
|
612
|
|
613 if (type2 < type1)
|
|
614 {
|
|
615 *arg2 = internal_coerce_number (*arg2, type1,
|
|
616 #ifdef HAVE_BIGFLOAT
|
|
617 type1 == BIGFLOAT_T
|
|
618 ? XBIGFLOAT_GET_PREC (*arg1) :
|
|
619 #endif
|
|
620 0UL);
|
|
621 return type1;
|
|
622 }
|
|
623
|
|
624 /* No conversion necessary */
|
|
625 return type1;
|
|
626 }
|
|
627
|
|
628 DEFUN ("coerce-number", Fcoerce_number, 2, 3, 0, /*
|
|
629 Convert NUMBER to the indicated type, possibly losing information.
|
|
630 Do not call this function. Use `coerce' instead.
|
|
631
|
|
632 TYPE is one of the symbols 'fixnum, 'integer, 'ratio, 'float, or 'bigfloat.
|
|
633 Not all of these types may be supported.
|
|
634
|
|
635 PRECISION is the number of bits of precision to use when converting to
|
|
636 bigfloat; it is ignored otherwise. If nil, the default precision is used.
|
|
637
|
|
638 Note that some conversions lose information. No error is signaled in such
|
|
639 cases; the information is silently lost.
|
|
640 */
|
|
641 (number, type, precision))
|
|
642 {
|
|
643 CHECK_SYMBOL (type);
|
|
644 if (EQ (type, Qfixnum))
|
|
645 return internal_coerce_number (number, FIXNUM_T, 0UL);
|
|
646 else if (EQ (type, Qinteger))
|
|
647 {
|
|
648 /* If bignums are available, we always convert to one first, then
|
|
649 downgrade to a fixnum if possible. */
|
|
650 #ifdef HAVE_BIGNUM
|
|
651 return Fcanonicalize_number
|
|
652 (internal_coerce_number (number, BIGNUM_T, 0UL));
|
|
653 #else
|
|
654 return internal_coerce_number (number, FIXNUM_T, 0UL);
|
|
655 #endif
|
|
656 }
|
|
657 #ifdef HAVE_RATIO
|
|
658 else if (EQ (type, Qratio))
|
|
659 return internal_coerce_number (number, RATIO_T, 0UL);
|
|
660 #endif
|
|
661 else if (EQ (type, Qfloat))
|
|
662 return internal_coerce_number (number, FLOAT_T, 0UL);
|
|
663 #ifdef HAVE_BIGFLOAT
|
|
664 else if (EQ (type, Qbigfloat))
|
|
665 {
|
|
666 unsigned long prec;
|
|
667
|
|
668 if (NILP (precision))
|
|
669 prec = bigfloat_get_default_prec ();
|
|
670 else
|
|
671 {
|
|
672 CHECK_INTEGER (precision);
|
|
673 #ifdef HAVE_BIGNUM
|
|
674 if (INTP (precision))
|
|
675 #endif /* HAVE_BIGNUM */
|
|
676 prec = (unsigned long) XREALINT (precision);
|
|
677 #ifdef HAVE_BIGNUM
|
|
678 else
|
|
679 {
|
|
680 if (!bignum_fits_ulong_p (XBIGNUM_DATA (precision)))
|
|
681 args_out_of_range (precision, Vbigfloat_max_prec);
|
|
682 prec = bignum_to_ulong (XBIGNUM_DATA (precision));
|
|
683 }
|
|
684 #endif /* HAVE_BIGNUM */
|
|
685 }
|
|
686 return internal_coerce_number (number, BIGFLOAT_T, prec);
|
|
687 }
|
|
688 #endif /* HAVE_BIGFLOAT */
|
|
689
|
|
690 Fsignal (Qunsupported_type, type);
|
|
691 /* NOTREACHED */
|
|
692 return Qnil;
|
|
693 }
|
|
694
|
|
695
|
|
696 void
|
|
697 syms_of_number (void)
|
|
698 {
|
|
699 #ifdef HAVE_BIGNUM
|
|
700 INIT_LRECORD_IMPLEMENTATION (bignum);
|
|
701 #endif
|
|
702 #ifdef HAVE_RATIO
|
|
703 INIT_LRECORD_IMPLEMENTATION (ratio);
|
|
704 #endif
|
|
705 #ifdef HAVE_BIGFLOAT
|
|
706 INIT_LRECORD_IMPLEMENTATION (bigfloat);
|
|
707 #endif
|
|
708
|
|
709 /* Type predicates */
|
|
710 DEFSYMBOL (Qrationalp);
|
|
711 DEFSYMBOL (Qfloatingp);
|
|
712 DEFSYMBOL (Qrealp);
|
|
713 DEFSYMBOL (Qbignump);
|
|
714 DEFSYMBOL (Qratiop);
|
|
715 DEFSYMBOL (Qbigfloatp);
|
|
716
|
|
717 /* Functions */
|
|
718 DEFSUBR (Fbignump);
|
|
719 DEFSUBR (Fintegerp);
|
|
720 DEFSUBR (Fevenp);
|
|
721 DEFSUBR (Foddp);
|
|
722 DEFSUBR (Fratiop);
|
|
723 DEFSUBR (Frationalp);
|
|
724 DEFSUBR (Fnumerator);
|
|
725 DEFSUBR (Fdenominator);
|
|
726 DEFSUBR (Fbigfloatp);
|
2092
|
727 DEFSUBR (Fbigfloat_get_precision);
|
|
728 DEFSUBR (Fbigfloat_set_precision);
|
2001
|
729 DEFSUBR (Ffloatingp);
|
1983
|
730 DEFSUBR (Frealp);
|
|
731 DEFSUBR (Fcanonicalize_number);
|
|
732 DEFSUBR (Fcoerce_number);
|
|
733
|
|
734 /* Errors */
|
|
735 DEFERROR_STANDARD (Qunsupported_type, Qwrong_type_argument);
|
|
736 }
|
|
737
|
|
738 void
|
|
739 vars_of_number (void)
|
|
740 {
|
2051
|
741 /* These variables are Lisp variables rather than number variables so that
|
|
742 we can put bignums in them. */
|
1983
|
743 DEFVAR_LISP_MAGIC ("default-float-precision", &Vdefault_float_precision, /*
|
|
744 The default floating-point precision for newly created floating point values.
|
2092
|
745 This should be 0 to create Lisp float types, or an unsigned integer no greater
|
|
746 than `bigfloat-maximum-precision' to create Lisp bigfloat types with the
|
|
747 indicated precision.
|
1983
|
748 */ default_float_precision_changed);
|
|
749 Vdefault_float_precision = make_int (0);
|
|
750
|
2092
|
751 DEFVAR_CONST_LISP ("bigfloat-maximum-precision", &Vbigfloat_max_prec /*
|
1983
|
752 The maximum number of bits of precision a bigfloat can have.
|
2092
|
753 This is determined by the underlying library used to implement bigfloats.
|
1983
|
754 */);
|
|
755
|
2061
|
756 #ifdef HAVE_BIGFLOAT
|
|
757 #ifdef HAVE_BIGNUM
|
|
758 /* Uncomment the next two lines and remove the line below them when dumping
|
|
759 bignums becomes possible. */
|
|
760 /*
|
|
761 Vbigfloat_max_prec = make_bignum (0L);
|
|
762 bignum_set_ulong (XBIGNUM_DATA (Vbigfloat_max_prec), ULONG_MAX);
|
|
763 */
|
2051
|
764 Vbigfloat_max_prec = make_int (EMACS_INT_MAX);
|
|
765 #else
|
2061
|
766 Vbigfloat_max_prec = make_int (EMACS_INT_MAX);
|
|
767 #endif
|
|
768 #else
|
2051
|
769 Vbigfloat_max_prec = make_int (0);
|
|
770 #endif /* HAVE_BIGFLOAT */
|
|
771
|
1983
|
772 DEFVAR_CONST_INT ("most-negative-fixnum", &Vmost_negative_fixnum /*
|
|
773 The fixnum closest in value to negative infinity.
|
|
774 */);
|
|
775 Vmost_negative_fixnum = EMACS_INT_MIN;
|
|
776
|
|
777 DEFVAR_CONST_INT ("most-positive-fixnum", &Vmost_positive_fixnum /*
|
|
778 The fixnum closest in value to positive infinity.
|
|
779 */);
|
|
780 Vmost_positive_fixnum = EMACS_INT_MAX;
|
|
781
|
|
782 Fprovide (intern ("number-types"));
|
|
783 #ifdef HAVE_BIGNUM
|
|
784 Fprovide (intern ("bignum"));
|
|
785 #endif
|
|
786 #ifdef HAVE_RATIO
|
|
787 Fprovide (intern ("ratio"));
|
|
788 #endif
|
|
789 #ifdef HAVE_BIGFLOAT
|
|
790 Fprovide (intern ("bigfloat"));
|
|
791 #endif
|
|
792 }
|
|
793
|
|
794 void
|
|
795 init_number (void)
|
|
796 {
|
|
797 if (!number_initialized)
|
|
798 {
|
|
799 number_initialized = 1;
|
|
800
|
|
801 #ifdef WITH_GMP
|
|
802 init_number_gmp ();
|
|
803 #endif
|
|
804 #ifdef WITH_MP
|
|
805 init_number_mp ();
|
|
806 #endif
|
|
807
|
|
808 #ifdef HAVE_BIGNUM
|
|
809 bignum_init (scratch_bignum);
|
|
810 bignum_init (scratch_bignum2);
|
|
811 #endif
|
|
812
|
|
813 #ifdef HAVE_RATIO
|
|
814 ratio_init (scratch_ratio);
|
|
815 #endif
|
|
816
|
|
817 #ifdef HAVE_BIGFLOAT
|
|
818 bigfloat_init (scratch_bigfloat);
|
|
819 bigfloat_init (scratch_bigfloat2);
|
|
820 #endif
|
|
821 }
|
|
822 }
|