Mercurial > hg > xemacs-beta
annotate src/number.c @ 5911:48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
src/ChangeLog addition:
2015-05-10 Aidan Kehoe <kehoea@parhasard.net>
* floatfns.c (double_to_integer):
Rename this from float_to_int to fit our newer, bignum-compatible
terminology.
GMP can signal SIGFPE when asked to turn NaN or infinity into a
bignum, and we're not prepared to handle that signal if the OS float
library routines don't do that, so check for those values
explicitly.
* floatfns.c (ceiling_two_float):
* floatfns.c (ceiling_one_float):
* floatfns.c (floor_two_float):
* floatfns.c (floor_one_float):
* floatfns.c (round_two_float):
* floatfns.c (round_one_float):
* floatfns.c (truncate_two_float):
* floatfns.c (truncate_one_float):
Call double_to_integer() with its new name.
* number.c:
Don't use the {bignum,ratio,bigfloat}_set_double functions
directly here, with GMP they can choke when handed non-finite C
doubles, call Ftruncate() and the new float_to_bigfloat() from
floatfns.c. Maybe we should extend number-gmp.c with GMP-specific
implementations that check for non-finite values.
tests/ChangeLog addition:
2015-05-10 Aidan Kehoe <kehoea@parhasard.net>
* automated/lisp-tests.el:
Backslash a few parentheses in the first column for the sake of
fontification.
* automated/lisp-tests.el:
Check that the rounding functions signal Lisp errors correctly
when handed positive and negative infinity and NaN.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sun, 10 May 2015 19:07:09 +0100 |
parents | 6174848f3e6c |
children |
rev | line source |
---|---|
1983 | 1 /* Numeric types for XEmacs. |
2 Copyright (C) 2004 Jerry James. | |
5125 | 3 Copyright (C) 2010 Ben Wing. |
1983 | 4 |
5 This file is part of XEmacs. | |
6 | |
5405
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5231
diff
changeset
|
7 XEmacs is free software: you can redistribute it and/or modify it |
1983 | 8 under the terms of the GNU General Public License as published by the |
5405
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5231
diff
changeset
|
9 Free Software Foundation, either version 3 of the License, or (at your |
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5231
diff
changeset
|
10 option) any later version. |
1983 | 11 |
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
5405
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5231
diff
changeset
|
18 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
1983 | 19 |
20 /* Synched up with: Not in FSF. */ | |
21 | |
22 #include <config.h> | |
23 #include <limits.h> | |
24 #include "lisp.h" | |
25 | |
2595 | 26 #ifdef HAVE_BIGFLOAT |
27 #define USED_IF_BIGFLOAT(decl) decl | |
28 #else | |
29 #define USED_IF_BIGFLOAT(decl) UNUSED (decl) | |
30 #endif | |
31 | |
2001 | 32 Lisp_Object Qrationalp, Qfloatingp, Qrealp; |
1983 | 33 Lisp_Object Vdefault_float_precision; |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
34 |
1983 | 35 static Lisp_Object Vbigfloat_max_prec; |
36 static int number_initialized; | |
37 | |
38 #ifdef HAVE_BIGNUM | |
39 bignum scratch_bignum, scratch_bignum2; | |
40 #endif | |
41 #ifdef HAVE_RATIO | |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3391
diff
changeset
|
42 ratio scratch_ratio, scratch_ratio2; |
1983 | 43 #endif |
44 #ifdef HAVE_BIGFLOAT | |
45 bigfloat scratch_bigfloat, scratch_bigfloat2; | |
46 #endif | |
47 | |
48 /********************************* Bignums **********************************/ | |
49 #ifdef HAVE_BIGNUM | |
50 static void | |
2286 | 51 bignum_print (Lisp_Object obj, Lisp_Object printcharfun, |
52 int UNUSED (escapeflag)) | |
1983 | 53 { |
4952
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4883
diff
changeset
|
54 Ascbyte *bstr = bignum_to_string (XBIGNUM_DATA (obj), 10); |
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4883
diff
changeset
|
55 write_ascstring (printcharfun, bstr); |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4962
diff
changeset
|
56 xfree (bstr); |
1983 | 57 } |
58 | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
59 #ifdef NEW_GC |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
60 static void |
5141
0dcd22290039
fix issues with finalizers in number.c, postgresql, ldap
Ben Wing <ben@xemacs.org>
parents:
5125
diff
changeset
|
61 bignum_finalize (Lisp_Object obj) |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
62 { |
5141
0dcd22290039
fix issues with finalizers in number.c, postgresql, ldap
Ben Wing <ben@xemacs.org>
parents:
5125
diff
changeset
|
63 struct Lisp_Bignum *num = XBIGNUM (obj); |
5125 | 64 /* #### WARNING: It would be better to put some sort of check to make |
65 sure this doesn't happen more than once, just in case --- | |
66 e.g. checking if it's zero before finalizing and then setting it to | |
67 zero after finalizing. */ | |
68 bignum_fini (num->data); | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
69 } |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
70 #endif |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
71 |
1983 | 72 static int |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4892
diff
changeset
|
73 bignum_equal (Lisp_Object obj1, Lisp_Object obj2, int UNUSED (depth), |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4892
diff
changeset
|
74 int UNUSED (foldcase)) |
1983 | 75 { |
76 return bignum_eql (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2)); | |
77 } | |
78 | |
79 static Hashcode | |
5191
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
80 bignum_hash (Lisp_Object obj, int UNUSED (depth), Boolint equalp) |
1983 | 81 { |
5191
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
82 if (equalp) |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
83 { |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
84 return FLOAT_HASHCODE_FROM_DOUBLE (bignum_to_double (XBIGNUM_DATA (obj))); |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
85 } |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
86 else |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
87 { |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
88 return bignum_hashcode (XBIGNUM_DATA (obj)); |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
89 } |
1983 | 90 } |
91 | |
2551 | 92 static void |
93 bignum_convert (const void *object, void **data, Bytecount *size) | |
94 { | |
95 CIbyte *bstr = bignum_to_string (*(bignum *)object, 10); | |
96 *data = bstr; | |
97 *size = strlen(bstr)+1; | |
98 } | |
99 | |
100 static void | |
101 bignum_convfree (const void * UNUSED (object), void *data, | |
102 Bytecount UNUSED (size)) | |
103 { | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4962
diff
changeset
|
104 xfree (data); |
2551 | 105 } |
106 | |
107 static void * | |
108 bignum_deconvert (void *object, void *data, Bytecount UNUSED (size)) | |
109 { | |
110 bignum *b = (bignum *) object; | |
111 bignum_init(*b); | |
112 bignum_set_string(*b, (const char *) data, 10); | |
113 return object; | |
114 } | |
115 | |
116 static const struct opaque_convert_functions bignum_opc = { | |
117 bignum_convert, | |
118 bignum_convfree, | |
119 bignum_deconvert | |
120 }; | |
121 | |
1983 | 122 static const struct memory_description bignum_description[] = { |
2551 | 123 { XD_OPAQUE_DATA_CONVERTIBLE, offsetof (Lisp_Bignum, data), |
124 0, { &bignum_opc }, XD_FLAG_NO_KKCC }, | |
1983 | 125 { XD_END } |
126 }; | |
127 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
128 DEFINE_DUMPABLE_FROB_BLOCK_LISP_OBJECT ("bignum", bignum, 0, bignum_print, |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
129 IF_NEW_GC (bignum_finalize), |
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
130 bignum_equal, bignum_hash, |
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
131 bignum_description, Lisp_Bignum); |
2092 | 132 #endif /* HAVE_BIGNUM */ |
1983 | 133 |
134 Lisp_Object Qbignump; | |
135 | |
136 DEFUN ("bignump", Fbignump, 1, 1, 0, /* | |
137 Return t if OBJECT is a bignum, nil otherwise. | |
138 */ | |
139 (object)) | |
140 { | |
141 return BIGNUMP (object) ? Qt : Qnil; | |
142 } | |
143 | |
144 | |
145 /********************************** Ratios **********************************/ | |
146 #ifdef HAVE_RATIO | |
147 static void | |
2286 | 148 ratio_print (Lisp_Object obj, Lisp_Object printcharfun, |
149 int UNUSED (escapeflag)) | |
1983 | 150 { |
151 CIbyte *rstr = ratio_to_string (XRATIO_DATA (obj), 10); | |
4952
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4883
diff
changeset
|
152 write_ascstring (printcharfun, rstr); |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4962
diff
changeset
|
153 xfree (rstr); |
1983 | 154 } |
155 | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
156 #ifdef NEW_GC |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
157 static void |
5141
0dcd22290039
fix issues with finalizers in number.c, postgresql, ldap
Ben Wing <ben@xemacs.org>
parents:
5125
diff
changeset
|
158 ratio_finalize (Lisp_Object obj) |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
159 { |
5141
0dcd22290039
fix issues with finalizers in number.c, postgresql, ldap
Ben Wing <ben@xemacs.org>
parents:
5125
diff
changeset
|
160 struct Lisp_Ratio *num = XRATIO (obj); |
5125 | 161 /* #### WARNING: It would be better to put some sort of check to make |
162 sure this doesn't happen more than once, just in case --- | |
163 e.g. checking if it's zero before finalizing and then setting it to | |
164 zero after finalizing. */ | |
165 ratio_fini (num->data); | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
166 } |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
167 #endif /* not NEW_GC */ |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
168 |
1983 | 169 static int |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4892
diff
changeset
|
170 ratio_equal (Lisp_Object obj1, Lisp_Object obj2, int UNUSED (depth), |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4892
diff
changeset
|
171 int UNUSED (foldcase)) |
1983 | 172 { |
173 return ratio_eql (XRATIO_DATA (obj1), XRATIO_DATA (obj2)); | |
174 } | |
175 | |
176 static Hashcode | |
5191
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
177 ratio_hash (Lisp_Object obj, int UNUSED (depth), Boolint equalp) |
1983 | 178 { |
5191
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
179 if (equalp) |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
180 { |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
181 return FLOAT_HASHCODE_FROM_DOUBLE (ratio_to_double (XRATIO_DATA (obj))); |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
182 } |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
183 else |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
184 { |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
185 return ratio_hashcode (XRATIO_DATA (obj)); |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
186 } |
1983 | 187 } |
188 | |
189 static const struct memory_description ratio_description[] = { | |
190 { XD_OPAQUE_PTR, offsetof (Lisp_Ratio, data) }, | |
191 { XD_END } | |
192 }; | |
193 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
194 DEFINE_NODUMP_FROB_BLOCK_LISP_OBJECT ("ratio", ratio, 0, ratio_print, |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
195 IF_NEW_GC (ratio_finalize), |
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
196 ratio_equal, ratio_hash, |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
197 ratio_description, Lisp_Ratio); |
1983 | 198 |
2092 | 199 #endif /* HAVE_RATIO */ |
1983 | 200 |
201 Lisp_Object Qratiop; | |
202 | |
203 DEFUN ("ratiop", Fratiop, 1, 1, 0, /* | |
204 Return t if OBJECT is a ratio, nil otherwise. | |
205 */ | |
206 (object)) | |
207 { | |
208 return RATIOP (object) ? Qt : Qnil; | |
209 } | |
210 | |
211 | |
212 /******************************** Rationals *********************************/ | |
213 DEFUN ("rationalp", Frationalp, 1, 1, 0, /* | |
214 Return t if OBJECT is a rational, nil otherwise. | |
215 */ | |
216 (object)) | |
217 { | |
218 return RATIONALP (object) ? Qt : Qnil; | |
219 } | |
220 | |
221 DEFUN ("numerator", Fnumerator, 1, 1, 0, /* | |
222 Return the numerator of the canonical form of RATIONAL. | |
223 If RATIONAL is an integer, RATIONAL is returned. | |
224 */ | |
225 (rational)) | |
226 { | |
227 CONCHECK_RATIONAL (rational); | |
228 #ifdef HAVE_RATIO | |
4883
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
229 if (RATIOP (rational)) |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
230 { |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
231 return |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
232 Fcanonicalize_number (make_bignum_bg (XRATIO_NUMERATOR (rational))); |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
233 } |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
234 #endif |
1983 | 235 return rational; |
236 } | |
237 | |
238 DEFUN ("denominator", Fdenominator, 1, 1, 0, /* | |
239 Return the denominator of the canonical form of RATIONAL. | |
240 If RATIONAL is an integer, 1 is returned. | |
241 */ | |
242 (rational)) | |
243 { | |
244 CONCHECK_RATIONAL (rational); | |
245 #ifdef HAVE_RATIO | |
4883
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
246 if (RATIOP (rational)) |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
247 { |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
248 return Fcanonicalize_number (make_bignum_bg |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
249 (XRATIO_DENOMINATOR (rational))); |
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
250 } |
4892
d1d4ce10c7b4
Fix the build problem in number.c
Aidan Kehoe <kehoea@parhasard.net>
parents:
4886
diff
changeset
|
251 #endif |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
252 return make_fixnum (1); |
1983 | 253 } |
254 | |
255 | |
256 /******************************** Bigfloats *********************************/ | |
257 #ifdef HAVE_BIGFLOAT | |
258 static void | |
2286 | 259 bigfloat_print (Lisp_Object obj, Lisp_Object printcharfun, |
260 int UNUSED (escapeflag)) | |
1983 | 261 { |
4952
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4883
diff
changeset
|
262 Ascbyte *fstr = bigfloat_to_string (XBIGFLOAT_DATA (obj), 10); |
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4883
diff
changeset
|
263 write_ascstring (printcharfun, fstr); |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4962
diff
changeset
|
264 xfree (fstr); |
1983 | 265 } |
266 | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
267 #ifdef NEW_GC |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
268 static void |
5141
0dcd22290039
fix issues with finalizers in number.c, postgresql, ldap
Ben Wing <ben@xemacs.org>
parents:
5125
diff
changeset
|
269 bigfloat_finalize (Lisp_Object obj) |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
270 { |
5141
0dcd22290039
fix issues with finalizers in number.c, postgresql, ldap
Ben Wing <ben@xemacs.org>
parents:
5125
diff
changeset
|
271 struct Lisp_Bigfloat *num = XBIGFLOAT (obj); |
5125 | 272 /* #### WARNING: It would be better to put some sort of check to make |
273 sure this doesn't happen more than once, just in case --- | |
274 e.g. checking if it's zero before finalizing and then setting it to | |
275 zero after finalizing. */ | |
276 bigfloat_fini (num->bf); | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
277 } |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
278 #endif /* not NEW_GC */ |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
279 |
1983 | 280 static int |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4892
diff
changeset
|
281 bigfloat_equal (Lisp_Object obj1, Lisp_Object obj2, int UNUSED (depth), |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4892
diff
changeset
|
282 int UNUSED (foldcase)) |
1983 | 283 { |
284 return bigfloat_eql (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2)); | |
285 } | |
286 | |
287 static Hashcode | |
5191
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
288 bigfloat_hash (Lisp_Object obj, int UNUSED (depth), Boolint equalp) |
1983 | 289 { |
5191
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
290 if (equalp) |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
291 { |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
292 return |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
293 FLOAT_HASHCODE_FROM_DOUBLE (bigfloat_to_double (XBIGFLOAT_DATA (obj))); |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
294 } |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
295 else |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
296 { |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
297 return bigfloat_hashcode (XBIGFLOAT_DATA (obj)); |
71ee43b8a74d
Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API
Aidan Kehoe <kehoea@parhasard.net>
parents:
5169
diff
changeset
|
298 } |
1983 | 299 } |
300 | |
301 static const struct memory_description bigfloat_description[] = { | |
302 { XD_OPAQUE_PTR, offsetof (Lisp_Bigfloat, bf) }, | |
303 { XD_END } | |
304 }; | |
305 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
306 DEFINE_DUMPABLE_FROB_BLOCK_LISP_OBJECT ("bigfloat", bigfloat, 0, |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
307 bigfloat_print, |
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5141
diff
changeset
|
308 IF_NEW_GC (bigfloat_finalize), |
5124
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
309 bigfloat_equal, bigfloat_hash, |
623d57b7fbe8
separate regular and disksave finalization, print method fixes.
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
310 bigfloat_description, Lisp_Bigfloat); |
1983 | 311 |
5911
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
312 extern Lisp_Object float_to_bigfloat (const Ascbyte *, Lisp_Object, |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
313 unsigned long); |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
314 |
2092 | 315 #endif /* HAVE_BIGFLOAT */ |
1983 | 316 |
317 Lisp_Object Qbigfloatp; | |
318 | |
319 DEFUN ("bigfloatp", Fbigfloatp, 1, 1, 0, /* | |
320 Return t if OBJECT is a bigfloat, nil otherwise. | |
321 */ | |
322 (object)) | |
323 { | |
324 return BIGFLOATP (object) ? Qt : Qnil; | |
325 } | |
326 | |
2092 | 327 DEFUN ("bigfloat-get-precision", Fbigfloat_get_precision, 1, 1, 0, /* |
328 Return the precision of bigfloat F as an integer. | |
329 */ | |
330 (f)) | |
331 { | |
332 CHECK_BIGFLOAT (f); | |
5602
c9e5612f5424
Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5581
diff
changeset
|
333 #ifdef HAVE_BIGFLOAT |
2092 | 334 #ifdef HAVE_BIGNUM |
335 bignum_set_ulong (scratch_bignum, XBIGFLOAT_GET_PREC (f)); | |
336 return Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
337 #else | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
338 return make_fixnum ((int) XBIGFLOAT_GET_PREC (f)); |
2092 | 339 #endif |
5602
c9e5612f5424
Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5581
diff
changeset
|
340 #endif |
2092 | 341 } |
342 | |
343 DEFUN ("bigfloat-set-precision", Fbigfloat_set_precision, 2, 2, 0, /* | |
344 Set the precision of F, a bigfloat, to PRECISION, a nonnegative integer. | |
345 The new precision of F is returned. Note that the return value may differ | |
346 from PRECISION if the underlying library is unable to support exactly | |
347 PRECISION bits of precision. | |
348 */ | |
349 (f, precision)) | |
350 { | |
351 unsigned long prec; | |
352 | |
353 CHECK_BIGFLOAT (f); | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
354 if (FIXNUMP (precision)) |
2092 | 355 { |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
356 prec = (XFIXNUM (precision) <= 0) ? 1UL : (unsigned long) XFIXNUM (precision); |
2092 | 357 } |
358 #ifdef HAVE_BIGNUM | |
359 else if (BIGNUMP (precision)) | |
360 { | |
361 prec = bignum_fits_ulong_p (XBIGNUM_DATA (precision)) | |
362 ? bignum_to_ulong (XBIGNUM_DATA (precision)) | |
363 : UINT_MAX; | |
364 } | |
365 #endif | |
366 else | |
367 { | |
368 dead_wrong_type_argument (Qintegerp, f); | |
369 return Qnil; | |
370 } | |
5602
c9e5612f5424
Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5581
diff
changeset
|
371 #ifdef HAVE_BIGFLOAT |
2092 | 372 XBIGFLOAT_SET_PREC (f, prec); |
5602
c9e5612f5424
Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5581
diff
changeset
|
373 #endif |
2092 | 374 return Fbigfloat_get_precision (f); |
375 } | |
376 | |
1983 | 377 static int |
2286 | 378 default_float_precision_changed (Lisp_Object UNUSED (sym), Lisp_Object *val, |
379 Lisp_Object UNUSED (in_object), | |
380 int UNUSED (flags)) | |
1983 | 381 { |
382 unsigned long prec; | |
383 | |
384 CONCHECK_INTEGER (*val); | |
385 #ifdef HAVE_BIGFLOAT | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
386 if (FIXNUMP (*val)) |
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
387 prec = XFIXNUM (*val); |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
388 else |
1983 | 389 { |
390 if (!bignum_fits_ulong_p (XBIGNUM_DATA (*val))) | |
391 args_out_of_range_3 (*val, Qzero, Vbigfloat_max_prec); | |
392 prec = bignum_to_ulong (XBIGNUM_DATA (*val)); | |
393 } | |
394 if (prec != 0UL) | |
395 bigfloat_set_default_prec (prec); | |
396 #endif | |
397 return 0; | |
398 } | |
399 | |
400 | |
401 /********************************* Floating *********************************/ | |
402 Lisp_Object | |
403 make_floating (double d) | |
404 { | |
405 #ifdef HAVE_BIGFLOAT | |
406 if (ZEROP (Vdefault_float_precision)) | |
407 #endif | |
408 return make_float (d); | |
409 #ifdef HAVE_BIGFLOAT | |
410 else | |
411 return make_bigfloat (d, 0UL); | |
412 #endif | |
413 } | |
414 | |
415 DEFUN ("floatingp", Ffloatingp, 1, 1, 0, /* | |
416 Return t if OBJECT is a floating point number of any kind, nil otherwise. | |
417 */ | |
418 (object)) | |
419 { | |
420 return FLOATINGP (object) ? Qt : Qnil; | |
421 } | |
422 | |
423 | |
424 /********************************** Reals ***********************************/ | |
425 DEFUN ("realp", Frealp, 1, 1, 0, /* | |
426 Return t if OBJECT is a real, nil otherwise. | |
427 */ | |
428 (object)) | |
429 { | |
430 return REALP (object) ? Qt : Qnil; | |
431 } | |
432 | |
433 | |
434 /********************************* Numbers **********************************/ | |
435 DEFUN ("canonicalize-number", Fcanonicalize_number, 1, 1, 0, /* | |
436 Return the canonical form of NUMBER. | |
437 */ | |
438 (number)) | |
439 { | |
440 /* The tests should go in order from larger, more expressive, or more | |
441 complex types to smaller, less expressive, or simpler types so that a | |
442 number can cascade all the way down to the simplest type if | |
443 appropriate. */ | |
444 #ifdef HAVE_RATIO | |
445 if (RATIOP (number) && | |
446 bignum_fits_long_p (XRATIO_DENOMINATOR (number)) && | |
447 bignum_to_long (XRATIO_DENOMINATOR (number)) == 1L) | |
4883
f730384b8ddf
Be more careful about canonical integer forms when dealing with ratios.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4802
diff
changeset
|
448 number = Fcanonicalize_number (make_bignum_bg (XRATIO_NUMERATOR (number))); |
1983 | 449 #endif |
450 #ifdef HAVE_BIGNUM | |
3391 | 451 if (BIGNUMP (number) && bignum_fits_emacs_int_p (XBIGNUM_DATA (number))) |
1983 | 452 { |
3391 | 453 EMACS_INT n = bignum_to_emacs_int (XBIGNUM_DATA (number)); |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
454 if (NUMBER_FITS_IN_A_FIXNUM (n)) |
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
455 number = make_fixnum (n); |
1983 | 456 } |
457 #endif | |
458 return number; | |
459 } | |
460 | |
461 enum number_type | |
462 get_number_type (Lisp_Object arg) | |
463 { | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
464 if (FIXNUMP (arg)) |
1983 | 465 return FIXNUM_T; |
466 #ifdef HAVE_BIGNUM | |
467 if (BIGNUMP (arg)) | |
468 return BIGNUM_T; | |
469 #endif | |
470 #ifdef HAVE_RATIO | |
471 if (RATIOP (arg)) | |
472 return RATIO_T; | |
473 #endif | |
474 if (FLOATP (arg)) | |
475 return FLOAT_T; | |
476 #ifdef HAVE_BIGFLOAT | |
477 if (BIGFLOATP (arg)) | |
478 return BIGFLOAT_T; | |
479 #endif | |
480 /* Catch unintentional bad uses of this function */ | |
2500 | 481 ABORT (); |
1995 | 482 /* NOTREACHED */ |
483 return FIXNUM_T; | |
1983 | 484 } |
485 | |
486 /* Convert NUMBER to type TYPE. If TYPE is BIGFLOAT_T then use the indicated | |
487 PRECISION; otherwise, PRECISION is ignored. */ | |
488 static Lisp_Object | |
489 internal_coerce_number (Lisp_Object number, enum number_type type, | |
2286 | 490 #ifdef HAVE_BIGFLOAT |
491 unsigned long precision | |
492 #else | |
493 unsigned long UNUSED (precision) | |
494 #endif | |
495 ) | |
1983 | 496 { |
497 enum number_type current_type; | |
498 | |
499 if (CHARP (number)) | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
500 number = make_fixnum (XCHAR (number)); |
1983 | 501 else if (MARKERP (number)) |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
502 number = make_fixnum (marker_position (number)); |
1983 | 503 |
504 /* Note that CHECK_NUMBER ensures that NUMBER is a supported type. Hence, | |
2500 | 505 we ABORT() in the #else sections below, because it shouldn't be possible |
1983 | 506 to arrive there. */ |
507 CHECK_NUMBER (number); | |
508 current_type = get_number_type (number); | |
509 switch (current_type) | |
510 { | |
511 case FIXNUM_T: | |
512 switch (type) | |
513 { | |
514 case FIXNUM_T: | |
515 return number; | |
516 case BIGNUM_T: | |
517 #ifdef HAVE_BIGNUM | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
518 return make_bignum (XREALFIXNUM (number)); |
1983 | 519 #else |
2500 | 520 ABORT (); |
1983 | 521 #endif /* HAVE_BIGNUM */ |
522 case RATIO_T: | |
523 #ifdef HAVE_RATIO | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
524 return make_ratio (XREALFIXNUM (number), 1UL); |
1983 | 525 #else |
2500 | 526 ABORT (); |
1983 | 527 #endif /* HAVE_RATIO */ |
528 case FLOAT_T: | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
529 return make_float (XREALFIXNUM (number)); |
1983 | 530 case BIGFLOAT_T: |
531 #ifdef HAVE_BIGFLOAT | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
532 return make_bigfloat (XREALFIXNUM (number), precision); |
1983 | 533 #else |
2500 | 534 ABORT (); |
1983 | 535 #endif /* HAVE_BIGFLOAT */ |
536 } | |
537 case BIGNUM_T: | |
538 #ifdef HAVE_BIGNUM | |
539 switch (type) | |
540 { | |
541 case FIXNUM_T: | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
542 return make_fixnum (bignum_to_long (XBIGNUM_DATA (number))); |
1983 | 543 case BIGNUM_T: |
544 return number; | |
545 case RATIO_T: | |
546 #ifdef HAVE_RATIO | |
547 bignum_set_long (scratch_bignum, 1L); | |
548 return make_ratio_bg (XBIGNUM_DATA (number), scratch_bignum); | |
549 #else | |
2500 | 550 ABORT (); |
1983 | 551 #endif /* HAVE_RATIO */ |
552 case FLOAT_T: | |
553 return make_float (bignum_to_double (XBIGNUM_DATA (number))); | |
554 case BIGFLOAT_T: | |
555 #ifdef HAVE_BIGFLOAT | |
556 { | |
557 Lisp_Object temp; | |
558 temp = make_bigfloat (0.0, precision); | |
559 bigfloat_set_bignum (XBIGFLOAT_DATA (temp), XBIGNUM_DATA (number)); | |
560 return temp; | |
561 } | |
562 #else | |
2500 | 563 ABORT (); |
1983 | 564 #endif /* HAVE_BIGFLOAT */ |
565 } | |
566 #else | |
2500 | 567 ABORT (); |
1983 | 568 #endif /* HAVE_BIGNUM */ |
569 case RATIO_T: | |
570 #ifdef HAVE_RATIO | |
571 switch (type) | |
572 { | |
573 case FIXNUM_T: | |
574 bignum_div (scratch_bignum, XRATIO_NUMERATOR (number), | |
575 XRATIO_DENOMINATOR (number)); | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
576 return make_fixnum (bignum_to_long (scratch_bignum)); |
1983 | 577 case BIGNUM_T: |
578 bignum_div (scratch_bignum, XRATIO_NUMERATOR (number), | |
579 XRATIO_DENOMINATOR (number)); | |
580 return make_bignum_bg (scratch_bignum); | |
581 case RATIO_T: | |
582 return number; | |
583 case FLOAT_T: | |
584 return make_float (ratio_to_double (XRATIO_DATA (number))); | |
585 case BIGFLOAT_T: | |
586 #ifdef HAVE_BIGFLOAT | |
587 { | |
588 Lisp_Object temp; | |
589 temp = make_bigfloat (0.0, precision); | |
590 bigfloat_set_ratio (XBIGFLOAT_DATA (temp), XRATIO_DATA (number)); | |
591 return temp; | |
592 } | |
593 #else | |
2500 | 594 ABORT (); |
1983 | 595 #endif /* HAVE_BIGFLOAT */ |
596 } | |
597 #else | |
2500 | 598 ABORT (); |
1983 | 599 #endif /* HAVE_RATIO */ |
600 case FLOAT_T: | |
601 switch (type) | |
602 { | |
603 case FIXNUM_T: | |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3391
diff
changeset
|
604 return Ftruncate (number, Qnil); |
1983 | 605 case BIGNUM_T: |
606 #ifdef HAVE_BIGNUM | |
5911
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
607 { |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
608 Lisp_Object truncate = Ftruncate (number, Qnil); |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
609 return FIXNUMP (truncate) ? |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
610 make_bignum (XREALFIXNUM (truncate)) : truncate; |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
611 } |
1983 | 612 #else |
2500 | 613 ABORT (); |
1983 | 614 #endif /* HAVE_BIGNUM */ |
615 case RATIO_T: | |
616 #ifdef HAVE_RATIO | |
5911
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
617 { |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
618 Lisp_Object truncate = Ftruncate (number, Qnil); |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
619 if (FIXNUMP (truncate)) |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
620 { |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
621 return make_ratio (XREALFIXNUM (truncate), 1UL); |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
622 } |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
623 |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
624 bignum_set_long (scratch_bignum, 1L); |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
625 return make_ratio_bg (XBIGNUM_DATA (truncate), scratch_bignum); |
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
626 } |
1983 | 627 #else |
2500 | 628 ABORT (); |
1983 | 629 #endif /* HAVE_RATIO */ |
630 case FLOAT_T: | |
631 return number; | |
632 case BIGFLOAT_T: | |
633 #ifdef HAVE_BIGFLOAT | |
5911
48386fd60fd0
GMP functions that take doubles choke on non-finite values, avoid that.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5908
diff
changeset
|
634 return float_to_bigfloat ("coerce-number", number, precision); |
1983 | 635 #else |
2500 | 636 ABORT (); |
1983 | 637 #endif /* HAVE_BIGFLOAT */ |
638 } | |
639 case BIGFLOAT_T: | |
640 #ifdef HAVE_BIGFLOAT | |
641 switch (type) | |
642 { | |
643 case FIXNUM_T: | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
644 return make_fixnum (bigfloat_to_long (XBIGFLOAT_DATA (number))); |
1983 | 645 case BIGNUM_T: |
646 #ifdef HAVE_BIGNUM | |
647 bignum_set_bigfloat (scratch_bignum, XBIGFLOAT_DATA (number)); | |
648 return make_bignum_bg (scratch_bignum); | |
649 #else | |
2500 | 650 ABORT (); |
1983 | 651 #endif /* HAVE_BIGNUM */ |
652 case RATIO_T: | |
653 #ifdef HAVE_RATIO | |
654 ratio_set_bigfloat (scratch_ratio, XBIGFLOAT_DATA (number)); | |
655 return make_ratio_rt (scratch_ratio); | |
656 #else | |
2500 | 657 ABORT (); |
1983 | 658 #endif |
659 case FLOAT_T: | |
660 return make_float (bigfloat_to_double (XBIGFLOAT_DATA (number))); | |
661 case BIGFLOAT_T: | |
662 /* FIXME: Do we need to change the precision? */ | |
663 return number; | |
664 } | |
665 #else | |
2500 | 666 ABORT (); |
1983 | 667 #endif /* HAVE_BIGFLOAT */ |
668 } | |
2500 | 669 ABORT (); |
1995 | 670 /* NOTREACHED */ |
671 return Qzero; | |
1983 | 672 } |
673 | |
674 /* This function promotes its arguments as necessary to make them both the | |
675 same type. It destructively modifies its arguments to do so. Characters | |
676 and markers are ALWAYS converted to integers. */ | |
677 enum number_type | |
678 promote_args (Lisp_Object *arg1, Lisp_Object *arg2) | |
679 { | |
680 enum number_type type1, type2; | |
681 | |
682 if (CHARP (*arg1)) | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
683 *arg1 = make_fixnum (XCHAR (*arg1)); |
1983 | 684 else if (MARKERP (*arg1)) |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
685 *arg1 = make_fixnum (marker_position (*arg1)); |
1983 | 686 if (CHARP (*arg2)) |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
687 *arg2 = make_fixnum (XCHAR (*arg2)); |
1983 | 688 else if (MARKERP (*arg2)) |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
689 *arg2 = make_fixnum (marker_position (*arg2)); |
1983 | 690 |
691 CHECK_NUMBER (*arg1); | |
692 CHECK_NUMBER (*arg2); | |
693 | |
694 type1 = get_number_type (*arg1); | |
695 type2 = get_number_type (*arg2); | |
696 | |
697 if (type1 < type2) | |
698 { | |
699 *arg1 = internal_coerce_number (*arg1, type2, | |
700 #ifdef HAVE_BIGFLOAT | |
701 type2 == BIGFLOAT_T | |
702 ? XBIGFLOAT_GET_PREC (*arg2) : | |
703 #endif | |
704 0UL); | |
705 return type2; | |
706 } | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
707 |
1983 | 708 if (type2 < type1) |
709 { | |
710 *arg2 = internal_coerce_number (*arg2, type1, | |
711 #ifdef HAVE_BIGFLOAT | |
712 type1 == BIGFLOAT_T | |
713 ? XBIGFLOAT_GET_PREC (*arg1) : | |
714 #endif | |
715 0UL); | |
716 return type1; | |
717 } | |
718 | |
719 /* No conversion necessary */ | |
720 return type1; | |
721 } | |
722 | |
723 DEFUN ("coerce-number", Fcoerce_number, 2, 3, 0, /* | |
724 Convert NUMBER to the indicated type, possibly losing information. | |
725 Do not call this function. Use `coerce' instead. | |
726 | |
3025 | 727 TYPE is one of the symbols `fixnum', `integer', `ratio', `float', or |
728 `bigfloat'. Not all of these types may be supported. | |
1983 | 729 |
730 PRECISION is the number of bits of precision to use when converting to | |
731 bigfloat; it is ignored otherwise. If nil, the default precision is used. | |
732 | |
733 Note that some conversions lose information. No error is signaled in such | |
734 cases; the information is silently lost. | |
735 */ | |
2595 | 736 (number, type, USED_IF_BIGFLOAT (precision))) |
1983 | 737 { |
738 CHECK_SYMBOL (type); | |
739 if (EQ (type, Qfixnum)) | |
740 return internal_coerce_number (number, FIXNUM_T, 0UL); | |
741 else if (EQ (type, Qinteger)) | |
742 { | |
743 /* If bignums are available, we always convert to one first, then | |
744 downgrade to a fixnum if possible. */ | |
745 #ifdef HAVE_BIGNUM | |
746 return Fcanonicalize_number | |
747 (internal_coerce_number (number, BIGNUM_T, 0UL)); | |
748 #else | |
749 return internal_coerce_number (number, FIXNUM_T, 0UL); | |
750 #endif | |
751 } | |
752 #ifdef HAVE_RATIO | |
753 else if (EQ (type, Qratio)) | |
754 return internal_coerce_number (number, RATIO_T, 0UL); | |
755 #endif | |
756 else if (EQ (type, Qfloat)) | |
757 return internal_coerce_number (number, FLOAT_T, 0UL); | |
758 #ifdef HAVE_BIGFLOAT | |
759 else if (EQ (type, Qbigfloat)) | |
760 { | |
761 unsigned long prec; | |
762 | |
763 if (NILP (precision)) | |
764 prec = bigfloat_get_default_prec (); | |
765 else | |
766 { | |
767 CHECK_INTEGER (precision); | |
768 #ifdef HAVE_BIGNUM | |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
769 if (FIXNUMP (precision)) |
1983 | 770 #endif /* HAVE_BIGNUM */ |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
771 prec = (unsigned long) XREALFIXNUM (precision); |
1983 | 772 #ifdef HAVE_BIGNUM |
773 else | |
774 { | |
775 if (!bignum_fits_ulong_p (XBIGNUM_DATA (precision))) | |
776 args_out_of_range (precision, Vbigfloat_max_prec); | |
777 prec = bignum_to_ulong (XBIGNUM_DATA (precision)); | |
778 } | |
779 #endif /* HAVE_BIGNUM */ | |
780 } | |
781 return internal_coerce_number (number, BIGFLOAT_T, prec); | |
782 } | |
783 #endif /* HAVE_BIGFLOAT */ | |
784 | |
785 Fsignal (Qunsupported_type, type); | |
786 /* NOTREACHED */ | |
787 return Qnil; | |
788 } | |
789 | |
790 | |
791 void | |
792 syms_of_number (void) | |
793 { | |
794 #ifdef HAVE_BIGNUM | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
795 INIT_LISP_OBJECT (bignum); |
1983 | 796 #endif |
797 #ifdef HAVE_RATIO | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
798 INIT_LISP_OBJECT (ratio); |
1983 | 799 #endif |
800 #ifdef HAVE_BIGFLOAT | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
801 INIT_LISP_OBJECT (bigfloat); |
1983 | 802 #endif |
803 | |
804 /* Type predicates */ | |
805 DEFSYMBOL (Qrationalp); | |
806 DEFSYMBOL (Qfloatingp); | |
807 DEFSYMBOL (Qrealp); | |
808 DEFSYMBOL (Qbignump); | |
809 DEFSYMBOL (Qratiop); | |
810 DEFSYMBOL (Qbigfloatp); | |
811 | |
812 /* Functions */ | |
813 DEFSUBR (Fbignump); | |
814 DEFSUBR (Fratiop); | |
815 DEFSUBR (Frationalp); | |
816 DEFSUBR (Fnumerator); | |
817 DEFSUBR (Fdenominator); | |
818 DEFSUBR (Fbigfloatp); | |
2092 | 819 DEFSUBR (Fbigfloat_get_precision); |
820 DEFSUBR (Fbigfloat_set_precision); | |
2001 | 821 DEFSUBR (Ffloatingp); |
1983 | 822 DEFSUBR (Frealp); |
823 DEFSUBR (Fcanonicalize_number); | |
824 DEFSUBR (Fcoerce_number); | |
825 } | |
826 | |
827 void | |
828 vars_of_number (void) | |
829 { | |
2051 | 830 /* These variables are Lisp variables rather than number variables so that |
831 we can put bignums in them. */ | |
1983 | 832 DEFVAR_LISP_MAGIC ("default-float-precision", &Vdefault_float_precision, /* |
833 The default floating-point precision for newly created floating point values. | |
2092 | 834 This should be 0 to create Lisp float types, or an unsigned integer no greater |
835 than `bigfloat-maximum-precision' to create Lisp bigfloat types with the | |
836 indicated precision. | |
1983 | 837 */ default_float_precision_changed); |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
838 Vdefault_float_precision = make_fixnum (0); |
1983 | 839 |
2092 | 840 DEFVAR_CONST_LISP ("bigfloat-maximum-precision", &Vbigfloat_max_prec /* |
1983 | 841 The maximum number of bits of precision a bigfloat can have. |
2092 | 842 This is determined by the underlying library used to implement bigfloats. |
1983 | 843 */); |
844 | |
2061 | 845 #ifdef HAVE_BIGFLOAT |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
846 /* Don't create a bignum here. Otherwise, we lose with NEW_GC + pdump. |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
847 See reinit_vars_of_number(). */ |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
848 Vbigfloat_max_prec = make_fixnum (MOST_POSITIVE_FIXNUM); |
2061 | 849 #else |
5581
56144c8593a8
Mechanically change INT to FIXNUM in our sources.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5405
diff
changeset
|
850 Vbigfloat_max_prec = make_fixnum (0); |
2051 | 851 #endif /* HAVE_BIGFLOAT */ |
852 | |
1983 | 853 Fprovide (intern ("number-types")); |
854 #ifdef HAVE_BIGNUM | |
855 Fprovide (intern ("bignum")); | |
856 #endif | |
857 #ifdef HAVE_RATIO | |
858 Fprovide (intern ("ratio")); | |
859 #endif | |
860 #ifdef HAVE_BIGFLOAT | |
861 Fprovide (intern ("bigfloat")); | |
862 #endif | |
863 } | |
864 | |
865 void | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
866 reinit_vars_of_number (void) |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
867 { |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
868 #if defined(HAVE_BIGFLOAT) && defined(HAVE_BIGNUM) |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
869 Vbigfloat_max_prec = make_bignum (0L); |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
870 bignum_set_ulong (XBIGNUM_DATA (Vbigfloat_max_prec), ULONG_MAX); |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
871 #endif |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
872 } |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
873 |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
874 void |
1983 | 875 init_number (void) |
876 { | |
877 if (!number_initialized) | |
878 { | |
879 number_initialized = 1; | |
880 | |
5739
a2912073be85
Support bignums with MPIR. Add documentation on the bignum, ratio,
Jerry James <james@xemacs.org>
parents:
5602
diff
changeset
|
881 #if defined(WITH_GMP) || defined(WITH_MPIR) |
1983 | 882 init_number_gmp (); |
883 #endif | |
884 #ifdef WITH_MP | |
885 init_number_mp (); | |
886 #endif | |
887 | |
888 #ifdef HAVE_BIGNUM | |
889 bignum_init (scratch_bignum); | |
890 bignum_init (scratch_bignum2); | |
891 #endif | |
892 | |
893 #ifdef HAVE_RATIO | |
894 ratio_init (scratch_ratio); | |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3391
diff
changeset
|
895 ratio_init (scratch_ratio2); |
1983 | 896 #endif |
897 | |
898 #ifdef HAVE_BIGFLOAT | |
899 bigfloat_init (scratch_bigfloat); | |
900 bigfloat_init (scratch_bigfloat2); | |
901 #endif | |
4802
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
902 |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
903 #ifndef PDUMP |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
904 reinit_vars_of_number (); |
2fc0e2f18322
Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents:
4678
diff
changeset
|
905 #endif |
1983 | 906 } |
907 } |