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
|
|
27 Lisp_Object Qintegerp, Qrationalp, Qfloatingp, Qrealp, Qnumberp;
|
|
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
|
|
85 string_to_bignum(const Ibyte *str, Bytecount len, int base)
|
|
86 {
|
|
87 Lisp_Object b = make_bignum (0L);
|
1995
|
88 return (bignum_set_string (XBIGNUM_DATA (b), (const char *) str, base) < 0)
|
1983
|
89 ? Fsignal (Qinvalid_read_syntax,
|
|
90 list3 (build_msg_string
|
|
91 ("Invalid integer constant in reader"),
|
|
92 make_string (str, len),
|
|
93 make_int (10)))
|
|
94 : b;
|
|
95 }
|
|
96
|
|
97 #else /* !HAVE_BIGNUM */
|
|
98
|
|
99 Lisp_Object Qbignump;
|
|
100
|
|
101 #endif /* HAVE_BIGNUM */
|
|
102
|
|
103 DEFUN ("bignump", Fbignump, 1, 1, 0, /*
|
|
104 Return t if OBJECT is a bignum, nil otherwise.
|
|
105 */
|
|
106 (object))
|
|
107 {
|
|
108 return BIGNUMP (object) ? Qt : Qnil;
|
|
109 }
|
|
110
|
|
111
|
|
112 /********************************* Integers *********************************/
|
|
113 DEFUN ("integerp", Fintegerp, 1, 1, 0, /*
|
|
114 Return t if OBJECT is an integer, nil otherwise.
|
|
115 */
|
|
116 (object))
|
|
117 {
|
|
118 return INTEGERP (object) ? Qt : Qnil;
|
|
119 }
|
|
120
|
|
121 DEFUN ("evenp", Fevenp, 1, 1, 0, /*
|
|
122 Return t if INTEGER is even, nil otherwise.
|
|
123 */
|
|
124 (integer))
|
|
125 {
|
|
126 CONCHECK_INTEGER (integer);
|
1996
|
127 return (BIGNUMP (integer)
|
|
128 ? bignum_evenp (XBIGNUM_DATA (integer))
|
|
129 : XTYPE (integer) == Lisp_Type_Int_Even) ? Qt : Qnil;
|
1983
|
130 }
|
|
131
|
|
132 DEFUN ("odd", Foddp, 1, 1, 0, /*
|
|
133 Return t if INTEGER is odd, nil otherwise.
|
|
134 */
|
|
135 (integer))
|
|
136 {
|
|
137 CONCHECK_INTEGER (integer);
|
1996
|
138 return (BIGNUMP (integer)
|
|
139 ? bignum_oddp (XBIGNUM_DATA (integer))
|
|
140 : XTYPE (integer) == Lisp_Type_Int_Odd) ? Qt : Qnil;
|
1983
|
141 }
|
|
142
|
|
143
|
|
144 /********************************** Ratios **********************************/
|
|
145 #ifdef HAVE_RATIO
|
|
146 static void
|
|
147 ratio_print (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
148 {
|
|
149 CIbyte *rstr = ratio_to_string (XRATIO_DATA (obj), 10);
|
|
150 write_c_string (printcharfun, rstr);
|
|
151 xfree (rstr, CIbyte *);
|
|
152 }
|
|
153
|
|
154 static void
|
|
155 ratio_finalize (void *header, int for_disksave)
|
|
156 {
|
|
157 if (for_disksave)
|
|
158 invalid_operation ("Can't dump an XEmacs containing ratio objects",
|
|
159 VOID_TO_LISP (header));
|
|
160 ratio_fini (((Lisp_Ratio *)header)->data);
|
|
161 }
|
|
162 ;
|
|
163
|
|
164 static int
|
|
165 ratio_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
166 {
|
|
167 return ratio_eql (XRATIO_DATA (obj1), XRATIO_DATA (obj2));
|
|
168 }
|
|
169
|
|
170 static Hashcode
|
|
171 ratio_hash (Lisp_Object obj, int depth)
|
|
172 {
|
|
173 return ratio_hashcode (XRATIO_DATA (obj));
|
|
174 }
|
|
175
|
|
176 static const struct memory_description ratio_description[] = {
|
|
177 { XD_OPAQUE_PTR, offsetof (Lisp_Ratio, data) },
|
|
178 { XD_END }
|
|
179 };
|
|
180
|
|
181 DEFINE_LRECORD_IMPLEMENTATION ("ratio", ratio, 0, 0,
|
|
182 ratio_print, ratio_finalize, ratio_equal,
|
|
183 ratio_hash, ratio_description, Lisp_Ratio);
|
|
184
|
|
185 #else /* !HAVE_RATIO */
|
|
186
|
|
187 Lisp_Object Qratiop;
|
|
188
|
|
189 #endif /* HAVE_RATIO */
|
|
190
|
|
191 DEFUN ("ratiop", Fratiop, 1, 1, 0, /*
|
|
192 Return t if OBJECT is a ratio, nil otherwise.
|
|
193 */
|
|
194 (object))
|
|
195 {
|
|
196 return RATIOP (object) ? Qt : Qnil;
|
|
197 }
|
|
198
|
|
199
|
|
200 /******************************** Rationals *********************************/
|
|
201 DEFUN ("rationalp", Frationalp, 1, 1, 0, /*
|
|
202 Return t if OBJECT is a rational, nil otherwise.
|
|
203 */
|
|
204 (object))
|
|
205 {
|
|
206 return RATIONALP (object) ? Qt : Qnil;
|
|
207 }
|
|
208
|
|
209 DEFUN ("numerator", Fnumerator, 1, 1, 0, /*
|
|
210 Return the numerator of the canonical form of RATIONAL.
|
|
211 If RATIONAL is an integer, RATIONAL is returned.
|
|
212 */
|
|
213 (rational))
|
|
214 {
|
|
215 CONCHECK_RATIONAL (rational);
|
|
216 #ifdef HAVE_RATIO
|
|
217 return RATIOP (rational)
|
|
218 ? make_bignum_bg (XRATIO_NUMERATOR (rational))
|
|
219 : rational;
|
|
220 #else
|
|
221 return rational;
|
|
222 #endif
|
|
223 }
|
|
224
|
|
225 DEFUN ("denominator", Fdenominator, 1, 1, 0, /*
|
|
226 Return the denominator of the canonical form of RATIONAL.
|
|
227 If RATIONAL is an integer, 1 is returned.
|
|
228 */
|
|
229 (rational))
|
|
230 {
|
|
231 CONCHECK_RATIONAL (rational);
|
|
232 #ifdef HAVE_RATIO
|
|
233 return RATIOP (rational)
|
|
234 ? make_bignum_bg (XRATIO_DENOMINATOR (rational))
|
|
235 : make_int (1);
|
|
236 #else
|
|
237 return rational;
|
|
238 #endif
|
|
239 }
|
|
240
|
|
241
|
|
242 /******************************** Bigfloats *********************************/
|
|
243 #ifdef HAVE_BIGFLOAT
|
|
244 static void
|
|
245 bigfloat_print (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
246 {
|
|
247 CIbyte *fstr = bigfloat_to_string (XBIGFLOAT_DATA (obj), 10);
|
|
248 write_c_string (printcharfun, fstr);
|
|
249 xfree (fstr, CIbyte *);
|
|
250 }
|
|
251
|
|
252 static void
|
|
253 bigfloat_finalize (void *header, int for_disksave)
|
|
254 {
|
|
255 if (for_disksave)
|
|
256 invalid_operation ("Can't dump an XEmacs containing bigfloat objects",
|
|
257 VOID_TO_LISP (header));
|
|
258 bigfloat_fini (((Lisp_Bigfloat *)header)->bf);
|
|
259 }
|
|
260
|
|
261 static int
|
|
262 bigfloat_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
263 {
|
|
264 return bigfloat_eql (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2));
|
|
265 }
|
|
266
|
|
267 static Hashcode
|
|
268 bigfloat_hash (Lisp_Object obj, int depth)
|
|
269 {
|
|
270 return bigfloat_hashcode (XBIGFLOAT_DATA (obj));
|
|
271 }
|
|
272
|
|
273 static const struct memory_description bigfloat_description[] = {
|
|
274 { XD_OPAQUE_PTR, offsetof (Lisp_Bigfloat, bf) },
|
|
275 { XD_END }
|
|
276 };
|
|
277
|
|
278 DEFINE_LRECORD_IMPLEMENTATION ("bigfloat", bigfloat, 1, 0,
|
|
279 bigfloat_print, bigfloat_finalize,
|
|
280 bigfloat_equal, bigfloat_hash,
|
|
281 bigfloat_description, Lisp_Bigfloat);
|
|
282
|
|
283 #else /* !HAVE_BIGFLOAT */
|
|
284
|
|
285 Lisp_Object Qbigfloatp;
|
|
286
|
|
287 #endif /* HAVE_BIGFLOAT */
|
|
288
|
|
289 DEFUN ("bigfloatp", Fbigfloatp, 1, 1, 0, /*
|
|
290 Return t if OBJECT is a bigfloat, nil otherwise.
|
|
291 */
|
|
292 (object))
|
|
293 {
|
|
294 return BIGFLOATP (object) ? Qt : Qnil;
|
|
295 }
|
|
296
|
|
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 (Qintegerp);
|
|
711 DEFSYMBOL (Qrationalp);
|
|
712 DEFSYMBOL (Qfloatingp);
|
|
713 DEFSYMBOL (Qrealp);
|
|
714 DEFSYMBOL (Qnumberp);
|
|
715 #ifndef HAVE_BIGNUM
|
|
716 DEFSYMBOL (Qbignump);
|
|
717 #endif
|
|
718 #ifndef HAVE_RATIO
|
|
719 DEFSYMBOL (Qratiop);
|
|
720 #endif
|
|
721 #ifndef HAVE_BIGFLOAT
|
|
722 DEFSYMBOL (Qbigfloatp);
|
|
723 #endif
|
|
724
|
|
725 /* Functions */
|
|
726 DEFSUBR (Fbignump);
|
|
727 DEFSUBR (Fintegerp);
|
|
728 DEFSUBR (Fevenp);
|
|
729 DEFSUBR (Foddp);
|
|
730 DEFSUBR (Fratiop);
|
|
731 DEFSUBR (Frationalp);
|
|
732 DEFSUBR (Fnumerator);
|
|
733 DEFSUBR (Fdenominator);
|
|
734 DEFSUBR (Fbigfloatp);
|
|
735 DEFSUBR (Frealp);
|
|
736 DEFSUBR (Fcanonicalize_number);
|
|
737 DEFSUBR (Fcoerce_number);
|
|
738
|
|
739 /* Errors */
|
|
740 DEFERROR_STANDARD (Qunsupported_type, Qwrong_type_argument);
|
|
741 }
|
|
742
|
|
743 void
|
|
744 vars_of_number (void)
|
|
745 {
|
|
746 /* This variable is a Lisp variable rather than a number variable so that we
|
|
747 can put bignums in it. */
|
|
748 DEFVAR_LISP_MAGIC ("default-float-precision", &Vdefault_float_precision, /*
|
|
749 The default floating-point precision for newly created floating point values.
|
|
750 This should be 0 for the precision of the machine-supported floating point
|
|
751 type (the C double type), or an unsigned integer no greater than
|
|
752 bigfloat-max-prec (currently the size of a C unsigned long).
|
|
753 */ default_float_precision_changed);
|
|
754 Vdefault_float_precision = make_int (0);
|
|
755
|
|
756 DEFVAR_CONST_LISP ("bigfloat-max-prec", &Vbigfloat_max_prec /*
|
|
757 The maximum number of bits of precision a bigfloat can have.
|
|
758 This is currently the value of ULONG_MAX on the target machine.
|
|
759 */);
|
|
760
|
|
761 DEFVAR_CONST_INT ("most-negative-fixnum", &Vmost_negative_fixnum /*
|
|
762 The fixnum closest in value to negative infinity.
|
|
763 */);
|
|
764 Vmost_negative_fixnum = EMACS_INT_MIN;
|
|
765
|
|
766 DEFVAR_CONST_INT ("most-positive-fixnum", &Vmost_positive_fixnum /*
|
|
767 The fixnum closest in value to positive infinity.
|
|
768 */);
|
|
769 Vmost_positive_fixnum = EMACS_INT_MAX;
|
|
770
|
|
771 Fprovide (intern ("number-types"));
|
|
772 #ifdef HAVE_BIGNUM
|
|
773 Fprovide (intern ("bignum"));
|
|
774 #endif
|
|
775 #ifdef HAVE_RATIO
|
|
776 Fprovide (intern ("ratio"));
|
|
777 #endif
|
|
778 #ifdef HAVE_BIGFLOAT
|
|
779 Fprovide (intern ("bigfloat"));
|
|
780 #endif
|
|
781 }
|
|
782
|
|
783 void
|
|
784 init_number (void)
|
|
785 {
|
|
786 if (!number_initialized)
|
|
787 {
|
|
788 number_initialized = 1;
|
|
789
|
|
790 #ifdef WITH_GMP
|
|
791 init_number_gmp ();
|
|
792 #endif
|
|
793 #ifdef WITH_MP
|
|
794 init_number_mp ();
|
|
795 #endif
|
|
796
|
|
797 #if defined(BIGNUM) && defined(BIGFLOAT)
|
|
798 Vbigfloat_max_prec = make_bignum (0L);
|
|
799 bignum_set_ulong (XBIGNUM_DATA (Vbigfloat_max_prec), ULONG_MAX);
|
|
800 #endif
|
|
801
|
|
802 #ifdef HAVE_BIGNUM
|
|
803 bignum_init (scratch_bignum);
|
|
804 bignum_init (scratch_bignum2);
|
|
805 #endif
|
|
806
|
|
807 #ifdef HAVE_RATIO
|
|
808 ratio_init (scratch_ratio);
|
|
809 #endif
|
|
810
|
|
811 #ifdef HAVE_BIGFLOAT
|
|
812 bigfloat_init (scratch_bigfloat);
|
|
813 bigfloat_init (scratch_bigfloat2);
|
|
814 #endif
|
|
815 }
|
|
816 }
|