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