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