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