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