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