Commit | Line | Data |
---|---|---|
238ebcef | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. |
ba74ef4e MV |
2 | * |
3 | * Portions Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories | |
4 | * and Bellcore. See scm_divide. | |
5 | * | |
f81e080b | 6 | * |
73be1d9e MV |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.1 of the License, or (at your option) any later version. | |
0f2d19dd | 11 | * |
73be1d9e MV |
12 | * This library is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
0f2d19dd | 16 | * |
73be1d9e MV |
17 | * You should have received a copy of the GNU Lesser General Public |
18 | * License along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
1bbd0b84 | 21 | |
0f2d19dd | 22 | \f |
ca46fb90 RB |
23 | /* General assumptions: |
24 | * All objects satisfying SCM_COMPLEXP() have a non-zero complex component. | |
25 | * All objects satisfying SCM_BIGP() are too large to fit in a fixnum. | |
26 | * If an object satisfies integer?, it's either an inum, a bignum, or a real. | |
27 | * If floor (r) == r, r is an int, and mpz_set_d will DTRT. | |
f92e85f7 | 28 | * All objects satisfying SCM_FRACTIONP are never an integer. |
ca46fb90 RB |
29 | */ |
30 | ||
31 | /* TODO: | |
32 | ||
33 | - see if special casing bignums and reals in integer-exponent when | |
34 | possible (to use mpz_pow and mpf_pow_ui) is faster. | |
35 | ||
36 | - look in to better short-circuiting of common cases in | |
37 | integer-expt and elsewhere. | |
38 | ||
39 | - see if direct mpz operations can help in ash and elsewhere. | |
40 | ||
41 | */ | |
0f2d19dd | 42 | |
fa605590 KR |
43 | /* tell glibc (2.3) to give prototype for C99 trunc() */ |
44 | #define _GNU_SOURCE | |
45 | ||
ee33d62a RB |
46 | #if HAVE_CONFIG_H |
47 | # include <config.h> | |
48 | #endif | |
49 | ||
0f2d19dd | 50 | #include <math.h> |
3c9a524f | 51 | #include <ctype.h> |
fc194577 | 52 | #include <string.h> |
ca46fb90 | 53 | #include <gmp.h> |
f92e85f7 | 54 | |
a0599745 | 55 | #include "libguile/_scm.h" |
a0599745 MD |
56 | #include "libguile/feature.h" |
57 | #include "libguile/ports.h" | |
58 | #include "libguile/root.h" | |
59 | #include "libguile/smob.h" | |
60 | #include "libguile/strings.h" | |
a0599745 MD |
61 | |
62 | #include "libguile/validate.h" | |
63 | #include "libguile/numbers.h" | |
1be6b49c | 64 | #include "libguile/deprecation.h" |
f4c627b3 | 65 | |
f92e85f7 MV |
66 | #include "libguile/eq.h" |
67 | ||
55f26379 MV |
68 | #include "libguile/discouraged.h" |
69 | ||
0f2d19dd | 70 | \f |
f4c627b3 | 71 | |
ca46fb90 RB |
72 | /* |
73 | Wonder if this might be faster for some of our code? A switch on | |
74 | the numtag would jump directly to the right case, and the | |
75 | SCM_I_NUMTAG code might be faster than repeated SCM_FOOP tests... | |
76 | ||
77 | #define SCM_I_NUMTAG_NOTNUM 0 | |
78 | #define SCM_I_NUMTAG_INUM 1 | |
79 | #define SCM_I_NUMTAG_BIG scm_tc16_big | |
80 | #define SCM_I_NUMTAG_REAL scm_tc16_real | |
81 | #define SCM_I_NUMTAG_COMPLEX scm_tc16_complex | |
82 | #define SCM_I_NUMTAG(x) \ | |
e11e83f3 | 83 | (SCM_I_INUMP(x) ? SCM_I_NUMTAG_INUM \ |
ca46fb90 | 84 | : (SCM_IMP(x) ? SCM_I_NUMTAG_NOTNUM \ |
534c55a9 | 85 | : (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) ? SCM_TYP16(x) \ |
ca46fb90 RB |
86 | : SCM_I_NUMTAG_NOTNUM))) |
87 | */ | |
f92e85f7 | 88 | /* the macro above will not work as is with fractions */ |
f4c627b3 DH |
89 | |
90 | ||
34d19ef6 | 91 | #define SCM_SWAP(x, y) do { SCM __t = x; x = y; y = __t; } while (0) |
09fb7599 | 92 | |
56e55ac7 | 93 | /* FLOBUFLEN is the maximum number of characters neccessary for the |
3a9809df DH |
94 | * printed or scm_string representation of an inexact number. |
95 | */ | |
0b799eea | 96 | #define FLOBUFLEN (40+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10) |
3a9809df | 97 | |
7351e207 MV |
98 | #if defined (SCO) |
99 | #if ! defined (HAVE_ISNAN) | |
100 | #define HAVE_ISNAN | |
101 | static int | |
102 | isnan (double x) | |
103 | { | |
104 | return (IsNANorINF (x) && NaN (x) && ! IsINF (x)) ? 1 : 0; | |
105 | } | |
0f2d19dd | 106 | #endif |
7351e207 MV |
107 | #if ! defined (HAVE_ISINF) |
108 | #define HAVE_ISINF | |
109 | static int | |
110 | isinf (double x) | |
111 | { | |
112 | return (IsNANorINF (x) && IsINF (x)) ? 1 : 0; | |
113 | } | |
0f2d19dd | 114 | |
7351e207 | 115 | #endif |
e6f3ef58 MD |
116 | #endif |
117 | ||
b127c712 KR |
118 | |
119 | /* mpz_cmp_d only recognises infinities in gmp 4.2 and up. | |
120 | For prior versions use an explicit check here. */ | |
121 | #if __GNU_MP_VERSION < 4 \ | |
122 | || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR < 2) | |
123 | #define xmpz_cmp_d(z, d) \ | |
124 | (xisinf (d) ? (d < 0.0 ? 1 : -1) : mpz_cmp_d (z, d)) | |
125 | #else | |
126 | #define xmpz_cmp_d(z, d) mpz_cmp_d (z, d) | |
127 | #endif | |
128 | ||
a98ce907 KR |
129 | /* For reference, sparc solaris 7 has infinities (IEEE) but doesn't have |
130 | isinf. It does have finite and isnan though, hence the use of those. | |
131 | fpclass would be a possibility on that system too. */ | |
f92e85f7 MV |
132 | static int |
133 | xisinf (double x) | |
134 | { | |
135 | #if defined (HAVE_ISINF) | |
136 | return isinf (x); | |
137 | #elif defined (HAVE_FINITE) && defined (HAVE_ISNAN) | |
138 | return (! (finite (x) || isnan (x))); | |
139 | #else | |
140 | return 0; | |
141 | #endif | |
142 | } | |
143 | ||
144 | static int | |
145 | xisnan (double x) | |
146 | { | |
147 | #if defined (HAVE_ISNAN) | |
148 | return isnan (x); | |
149 | #else | |
150 | return 0; | |
151 | #endif | |
152 | } | |
153 | ||
0f2d19dd JB |
154 | \f |
155 | ||
713a4259 | 156 | static mpz_t z_negative_one; |
ac0c002c DH |
157 | |
158 | \f | |
159 | ||
570b6821 | 160 | SCM_C_INLINE_KEYWORD SCM |
ca46fb90 RB |
161 | scm_i_mkbig () |
162 | { | |
163 | /* Return a newly created bignum. */ | |
164 | SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0); | |
165 | mpz_init (SCM_I_BIG_MPZ (z)); | |
166 | return z; | |
167 | } | |
168 | ||
c71b0706 MV |
169 | SCM_C_INLINE_KEYWORD SCM |
170 | scm_i_long2big (long x) | |
171 | { | |
172 | /* Return a newly created bignum initialized to X. */ | |
173 | SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0); | |
174 | mpz_init_set_si (SCM_I_BIG_MPZ (z), x); | |
175 | return z; | |
176 | } | |
177 | ||
178 | SCM_C_INLINE_KEYWORD SCM | |
179 | scm_i_ulong2big (unsigned long x) | |
180 | { | |
181 | /* Return a newly created bignum initialized to X. */ | |
182 | SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0); | |
183 | mpz_init_set_ui (SCM_I_BIG_MPZ (z), x); | |
184 | return z; | |
185 | } | |
186 | ||
570b6821 | 187 | SCM_C_INLINE_KEYWORD static SCM |
ca46fb90 RB |
188 | scm_i_clonebig (SCM src_big, int same_sign_p) |
189 | { | |
190 | /* Copy src_big's value, negate it if same_sign_p is false, and return. */ | |
191 | SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0); | |
192 | mpz_init_set (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (src_big)); | |
0aacf84e MD |
193 | if (!same_sign_p) |
194 | mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z)); | |
ca46fb90 RB |
195 | return z; |
196 | } | |
197 | ||
570b6821 | 198 | SCM_C_INLINE_KEYWORD int |
ca46fb90 RB |
199 | scm_i_bigcmp (SCM x, SCM y) |
200 | { | |
201 | /* Return neg if x < y, pos if x > y, and 0 if x == y */ | |
202 | /* presume we already know x and y are bignums */ | |
203 | int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
204 | scm_remember_upto_here_2 (x, y); | |
205 | return result; | |
206 | } | |
207 | ||
570b6821 | 208 | SCM_C_INLINE_KEYWORD SCM |
ca46fb90 RB |
209 | scm_i_dbl2big (double d) |
210 | { | |
211 | /* results are only defined if d is an integer */ | |
212 | SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0); | |
213 | mpz_init_set_d (SCM_I_BIG_MPZ (z), d); | |
214 | return z; | |
215 | } | |
216 | ||
f92e85f7 MV |
217 | /* Convert a integer in double representation to a SCM number. */ |
218 | ||
219 | SCM_C_INLINE_KEYWORD SCM | |
220 | scm_i_dbl2num (double u) | |
221 | { | |
222 | /* SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM are both | |
223 | powers of 2, so there's no rounding when making "double" values | |
224 | from them. If plain SCM_MOST_POSITIVE_FIXNUM was used it could | |
225 | get rounded on a 64-bit machine, hence the "+1". | |
226 | ||
227 | The use of floor() to force to an integer value ensures we get a | |
228 | "numerically closest" value without depending on how a | |
229 | double->long cast or how mpz_set_d will round. For reference, | |
230 | double->long probably follows the hardware rounding mode, | |
231 | mpz_set_d truncates towards zero. */ | |
232 | ||
233 | /* XXX - what happens when SCM_MOST_POSITIVE_FIXNUM etc is not | |
234 | representable as a double? */ | |
235 | ||
236 | if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1) | |
237 | && u >= (double) SCM_MOST_NEGATIVE_FIXNUM) | |
d956fa6f | 238 | return SCM_I_MAKINUM ((long) u); |
f92e85f7 MV |
239 | else |
240 | return scm_i_dbl2big (u); | |
241 | } | |
242 | ||
089c9a59 KR |
243 | /* scm_i_big2dbl() rounds to the closest representable double, in accordance |
244 | with R5RS exact->inexact. | |
245 | ||
246 | The approach is to use mpz_get_d to pick out the high DBL_MANT_DIG bits | |
247 | (ie. it truncates towards zero), then adjust to get the closest double by | |
248 | examining the next lower bit and adding 1 if necessary. | |
249 | ||
250 | Note that bignums exactly half way between representable doubles are | |
251 | rounded to the next higher absolute value (ie. away from zero). This | |
252 | seems like an adequate interpretation of R5RS "numerically closest", and | |
253 | it's easier and faster than a full "nearest-even" style. | |
254 | ||
255 | The bit test is done on the absolute value of the mpz_t, which means we | |
256 | must use mpz_getlimbn. mpz_tstbit is not right, it treats negatives as | |
257 | twos complement. | |
258 | ||
259 | Prior to GMP 4.2, the rounding done by mpz_get_d was unspecified. It | |
260 | happened to follow the hardware rounding mode, but on the absolute value | |
261 | of its operand. This is not what we want, so we put the high | |
262 | DBL_MANT_DIG bits into a temporary. This extra init/clear is a slowdown, | |
263 | but doesn't matter too much since it's only for older GMP. */ | |
264 | ||
265 | double | |
ca46fb90 RB |
266 | scm_i_big2dbl (SCM b) |
267 | { | |
089c9a59 KR |
268 | double result; |
269 | size_t bits; | |
270 | ||
271 | bits = mpz_sizeinbase (SCM_I_BIG_MPZ (b), 2); | |
272 | ||
273 | #if __GNU_MP_VERSION < 4 \ | |
274 | || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR < 2) | |
275 | { | |
276 | /* GMP prior to 4.2, force truncate towards zero */ | |
277 | mpz_t tmp; | |
278 | if (bits > DBL_MANT_DIG) | |
279 | { | |
280 | size_t shift = bits - DBL_MANT_DIG; | |
281 | mpz_init2 (tmp, DBL_MANT_DIG); | |
282 | mpz_tdiv_q_2exp (tmp, SCM_I_BIG_MPZ (b), shift); | |
283 | result = ldexp (mpz_get_d (tmp), shift); | |
284 | mpz_clear (tmp); | |
285 | } | |
286 | else | |
287 | { | |
288 | result = mpz_get_d (SCM_I_BIG_MPZ (b)); | |
289 | } | |
290 | } | |
291 | #else | |
292 | /* GMP 4.2 and up */ | |
293 | result = mpz_get_d (SCM_I_BIG_MPZ (b)); | |
294 | #endif | |
295 | ||
296 | if (bits > DBL_MANT_DIG) | |
297 | { | |
298 | unsigned long pos = bits - DBL_MANT_DIG - 1; | |
299 | /* test bit number "pos" in absolute value */ | |
300 | if (mpz_getlimbn (SCM_I_BIG_MPZ (b), pos / GMP_NUMB_BITS) | |
301 | & ((mp_limb_t) 1 << (pos % GMP_NUMB_BITS))) | |
302 | { | |
303 | result += ldexp ((double) mpz_sgn (SCM_I_BIG_MPZ (b)), pos + 1); | |
304 | } | |
305 | } | |
306 | ||
ca46fb90 RB |
307 | scm_remember_upto_here_1 (b); |
308 | return result; | |
309 | } | |
310 | ||
570b6821 | 311 | SCM_C_INLINE_KEYWORD SCM |
ca46fb90 RB |
312 | scm_i_normbig (SCM b) |
313 | { | |
314 | /* convert a big back to a fixnum if it'll fit */ | |
315 | /* presume b is a bignum */ | |
316 | if (mpz_fits_slong_p (SCM_I_BIG_MPZ (b))) | |
317 | { | |
318 | long val = mpz_get_si (SCM_I_BIG_MPZ (b)); | |
319 | if (SCM_FIXABLE (val)) | |
d956fa6f | 320 | b = SCM_I_MAKINUM (val); |
ca46fb90 RB |
321 | } |
322 | return b; | |
323 | } | |
f872b822 | 324 | |
f92e85f7 MV |
325 | static SCM_C_INLINE_KEYWORD SCM |
326 | scm_i_mpz2num (mpz_t b) | |
327 | { | |
328 | /* convert a mpz number to a SCM number. */ | |
329 | if (mpz_fits_slong_p (b)) | |
330 | { | |
331 | long val = mpz_get_si (b); | |
332 | if (SCM_FIXABLE (val)) | |
d956fa6f | 333 | return SCM_I_MAKINUM (val); |
f92e85f7 MV |
334 | } |
335 | ||
336 | { | |
337 | SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0); | |
338 | mpz_init_set (SCM_I_BIG_MPZ (z), b); | |
339 | return z; | |
340 | } | |
341 | } | |
342 | ||
343 | /* this is needed when we want scm_divide to make a float, not a ratio, even if passed two ints */ | |
344 | static SCM scm_divide2real (SCM x, SCM y); | |
345 | ||
cba42c93 MV |
346 | static SCM |
347 | scm_i_make_ratio (SCM numerator, SCM denominator) | |
c60e130c | 348 | #define FUNC_NAME "make-ratio" |
f92e85f7 | 349 | { |
c60e130c MV |
350 | /* First make sure the arguments are proper. |
351 | */ | |
e11e83f3 | 352 | if (SCM_I_INUMP (denominator)) |
f92e85f7 | 353 | { |
bc36d050 | 354 | if (scm_is_eq (denominator, SCM_INUM0)) |
f92e85f7 | 355 | scm_num_overflow ("make-ratio"); |
bc36d050 | 356 | if (scm_is_eq (denominator, SCM_I_MAKINUM(1))) |
f92e85f7 MV |
357 | return numerator; |
358 | } | |
359 | else | |
360 | { | |
361 | if (!(SCM_BIGP(denominator))) | |
362 | SCM_WRONG_TYPE_ARG (2, denominator); | |
363 | } | |
e11e83f3 | 364 | if (!SCM_I_INUMP (numerator) && !SCM_BIGP (numerator)) |
c60e130c MV |
365 | SCM_WRONG_TYPE_ARG (1, numerator); |
366 | ||
367 | /* Then flip signs so that the denominator is positive. | |
368 | */ | |
73e4de09 | 369 | if (scm_is_true (scm_negative_p (denominator))) |
c60e130c MV |
370 | { |
371 | numerator = scm_difference (numerator, SCM_UNDEFINED); | |
372 | denominator = scm_difference (denominator, SCM_UNDEFINED); | |
373 | } | |
374 | ||
375 | /* Now consider for each of the four fixnum/bignum combinations | |
376 | whether the rational number is really an integer. | |
377 | */ | |
e11e83f3 | 378 | if (SCM_I_INUMP (numerator)) |
f92e85f7 | 379 | { |
e11e83f3 | 380 | long x = SCM_I_INUM (numerator); |
bc36d050 | 381 | if (scm_is_eq (numerator, SCM_INUM0)) |
f92e85f7 | 382 | return SCM_INUM0; |
e11e83f3 | 383 | if (SCM_I_INUMP (denominator)) |
f92e85f7 | 384 | { |
dd5130ca | 385 | long y; |
e11e83f3 | 386 | y = SCM_I_INUM (denominator); |
f92e85f7 | 387 | if (x == y) |
d956fa6f | 388 | return SCM_I_MAKINUM(1); |
f92e85f7 | 389 | if ((x % y) == 0) |
d956fa6f | 390 | return SCM_I_MAKINUM (x / y); |
f92e85f7 | 391 | } |
dd5130ca KR |
392 | else |
393 | { | |
394 | /* When x == SCM_MOST_NEGATIVE_FIXNUM we could have the negative | |
3271a325 KR |
395 | of that value for the denominator, as a bignum. Apart from |
396 | that case, abs(bignum) > abs(inum) so inum/bignum is not an | |
397 | integer. */ | |
398 | if (x == SCM_MOST_NEGATIVE_FIXNUM | |
399 | && mpz_cmp_ui (SCM_I_BIG_MPZ (denominator), | |
400 | - SCM_MOST_NEGATIVE_FIXNUM) == 0) | |
d956fa6f | 401 | return SCM_I_MAKINUM(-1); |
dd5130ca | 402 | } |
f92e85f7 | 403 | } |
c60e130c | 404 | else if (SCM_BIGP (numerator)) |
f92e85f7 | 405 | { |
e11e83f3 | 406 | if (SCM_I_INUMP (denominator)) |
c60e130c | 407 | { |
e11e83f3 | 408 | long yy = SCM_I_INUM (denominator); |
c60e130c MV |
409 | if (mpz_divisible_ui_p (SCM_I_BIG_MPZ (numerator), yy)) |
410 | return scm_divide (numerator, denominator); | |
411 | } | |
412 | else | |
f92e85f7 | 413 | { |
bc36d050 | 414 | if (scm_is_eq (numerator, denominator)) |
d956fa6f | 415 | return SCM_I_MAKINUM(1); |
c60e130c MV |
416 | if (mpz_divisible_p (SCM_I_BIG_MPZ (numerator), |
417 | SCM_I_BIG_MPZ (denominator))) | |
418 | return scm_divide(numerator, denominator); | |
f92e85f7 | 419 | } |
f92e85f7 | 420 | } |
c60e130c MV |
421 | |
422 | /* No, it's a proper fraction. | |
423 | */ | |
424 | return scm_double_cell (scm_tc16_fraction, | |
425 | SCM_UNPACK (numerator), | |
426 | SCM_UNPACK (denominator), 0); | |
f92e85f7 | 427 | } |
c60e130c | 428 | #undef FUNC_NAME |
f92e85f7 MV |
429 | |
430 | static void scm_i_fraction_reduce (SCM z) | |
431 | { | |
432 | if (!(SCM_FRACTION_REDUCED (z))) | |
433 | { | |
434 | SCM divisor; | |
435 | divisor = scm_gcd (SCM_FRACTION_NUMERATOR (z), SCM_FRACTION_DENOMINATOR (z)); | |
bc36d050 | 436 | if (!(scm_is_eq (divisor, SCM_I_MAKINUM(1)))) |
f92e85f7 MV |
437 | { |
438 | /* is this safe? */ | |
439 | SCM_FRACTION_SET_NUMERATOR (z, scm_divide (SCM_FRACTION_NUMERATOR (z), divisor)); | |
440 | SCM_FRACTION_SET_DENOMINATOR (z, scm_divide (SCM_FRACTION_DENOMINATOR (z), divisor)); | |
441 | } | |
442 | SCM_FRACTION_REDUCED_SET (z); | |
443 | } | |
444 | } | |
445 | ||
446 | double | |
447 | scm_i_fraction2double (SCM z) | |
448 | { | |
55f26379 MV |
449 | return scm_to_double (scm_divide2real (SCM_FRACTION_NUMERATOR (z), |
450 | SCM_FRACTION_DENOMINATOR (z))); | |
f92e85f7 MV |
451 | } |
452 | ||
a1ec6916 | 453 | SCM_DEFINE (scm_exact_p, "exact?", 1, 0, 0, |
1bbd0b84 | 454 | (SCM x), |
942e5b91 MG |
455 | "Return @code{#t} if @var{x} is an exact number, @code{#f}\n" |
456 | "otherwise.") | |
1bbd0b84 | 457 | #define FUNC_NAME s_scm_exact_p |
0f2d19dd | 458 | { |
e11e83f3 | 459 | if (SCM_I_INUMP (x)) |
0aacf84e MD |
460 | return SCM_BOOL_T; |
461 | if (SCM_BIGP (x)) | |
462 | return SCM_BOOL_T; | |
f92e85f7 MV |
463 | if (SCM_FRACTIONP (x)) |
464 | return SCM_BOOL_T; | |
eb927cb9 MV |
465 | if (SCM_NUMBERP (x)) |
466 | return SCM_BOOL_F; | |
467 | SCM_WRONG_TYPE_ARG (1, x); | |
0f2d19dd | 468 | } |
1bbd0b84 | 469 | #undef FUNC_NAME |
0f2d19dd | 470 | |
4219f20d | 471 | |
a1ec6916 | 472 | SCM_DEFINE (scm_odd_p, "odd?", 1, 0, 0, |
1bbd0b84 | 473 | (SCM n), |
942e5b91 MG |
474 | "Return @code{#t} if @var{n} is an odd number, @code{#f}\n" |
475 | "otherwise.") | |
1bbd0b84 | 476 | #define FUNC_NAME s_scm_odd_p |
0f2d19dd | 477 | { |
e11e83f3 | 478 | if (SCM_I_INUMP (n)) |
0aacf84e | 479 | { |
e11e83f3 | 480 | long val = SCM_I_INUM (n); |
73e4de09 | 481 | return scm_from_bool ((val & 1L) != 0); |
0aacf84e MD |
482 | } |
483 | else if (SCM_BIGP (n)) | |
484 | { | |
485 | int odd_p = mpz_odd_p (SCM_I_BIG_MPZ (n)); | |
486 | scm_remember_upto_here_1 (n); | |
73e4de09 | 487 | return scm_from_bool (odd_p); |
0aacf84e | 488 | } |
73e4de09 | 489 | else if (scm_is_true (scm_inf_p (n))) |
7351e207 | 490 | return SCM_BOOL_T; |
f92e85f7 MV |
491 | else if (SCM_REALP (n)) |
492 | { | |
493 | double rem = fabs (fmod (SCM_REAL_VALUE(n), 2.0)); | |
494 | if (rem == 1.0) | |
495 | return SCM_BOOL_T; | |
496 | else if (rem == 0.0) | |
497 | return SCM_BOOL_F; | |
498 | else | |
499 | SCM_WRONG_TYPE_ARG (1, n); | |
500 | } | |
0aacf84e | 501 | else |
a1a33b0f | 502 | SCM_WRONG_TYPE_ARG (1, n); |
0f2d19dd | 503 | } |
1bbd0b84 | 504 | #undef FUNC_NAME |
0f2d19dd | 505 | |
4219f20d | 506 | |
a1ec6916 | 507 | SCM_DEFINE (scm_even_p, "even?", 1, 0, 0, |
1bbd0b84 | 508 | (SCM n), |
942e5b91 MG |
509 | "Return @code{#t} if @var{n} is an even number, @code{#f}\n" |
510 | "otherwise.") | |
1bbd0b84 | 511 | #define FUNC_NAME s_scm_even_p |
0f2d19dd | 512 | { |
e11e83f3 | 513 | if (SCM_I_INUMP (n)) |
0aacf84e | 514 | { |
e11e83f3 | 515 | long val = SCM_I_INUM (n); |
73e4de09 | 516 | return scm_from_bool ((val & 1L) == 0); |
0aacf84e MD |
517 | } |
518 | else if (SCM_BIGP (n)) | |
519 | { | |
520 | int even_p = mpz_even_p (SCM_I_BIG_MPZ (n)); | |
521 | scm_remember_upto_here_1 (n); | |
73e4de09 | 522 | return scm_from_bool (even_p); |
0aacf84e | 523 | } |
73e4de09 | 524 | else if (scm_is_true (scm_inf_p (n))) |
7351e207 | 525 | return SCM_BOOL_T; |
f92e85f7 MV |
526 | else if (SCM_REALP (n)) |
527 | { | |
528 | double rem = fabs (fmod (SCM_REAL_VALUE(n), 2.0)); | |
529 | if (rem == 1.0) | |
530 | return SCM_BOOL_F; | |
531 | else if (rem == 0.0) | |
532 | return SCM_BOOL_T; | |
533 | else | |
534 | SCM_WRONG_TYPE_ARG (1, n); | |
535 | } | |
0aacf84e | 536 | else |
a1a33b0f | 537 | SCM_WRONG_TYPE_ARG (1, n); |
0f2d19dd | 538 | } |
1bbd0b84 | 539 | #undef FUNC_NAME |
0f2d19dd | 540 | |
7351e207 MV |
541 | SCM_DEFINE (scm_inf_p, "inf?", 1, 0, 0, |
542 | (SCM n), | |
543 | "Return @code{#t} if @var{n} is infinite, @code{#f}\n" | |
544 | "otherwise.") | |
545 | #define FUNC_NAME s_scm_inf_p | |
546 | { | |
0aacf84e | 547 | if (SCM_REALP (n)) |
73e4de09 | 548 | return scm_from_bool (xisinf (SCM_REAL_VALUE (n))); |
0aacf84e | 549 | else if (SCM_COMPLEXP (n)) |
73e4de09 | 550 | return scm_from_bool (xisinf (SCM_COMPLEX_REAL (n)) |
7351e207 | 551 | || xisinf (SCM_COMPLEX_IMAG (n))); |
0aacf84e | 552 | else |
7351e207 | 553 | return SCM_BOOL_F; |
7351e207 MV |
554 | } |
555 | #undef FUNC_NAME | |
556 | ||
557 | SCM_DEFINE (scm_nan_p, "nan?", 1, 0, 0, | |
558 | (SCM n), | |
559 | "Return @code{#t} if @var{n} is a NaN, @code{#f}\n" | |
560 | "otherwise.") | |
561 | #define FUNC_NAME s_scm_nan_p | |
562 | { | |
0aacf84e | 563 | if (SCM_REALP (n)) |
73e4de09 | 564 | return scm_from_bool (xisnan (SCM_REAL_VALUE (n))); |
0aacf84e | 565 | else if (SCM_COMPLEXP (n)) |
73e4de09 | 566 | return scm_from_bool (xisnan (SCM_COMPLEX_REAL (n)) |
7351e207 | 567 | || xisnan (SCM_COMPLEX_IMAG (n))); |
0aacf84e | 568 | else |
7351e207 | 569 | return SCM_BOOL_F; |
7351e207 MV |
570 | } |
571 | #undef FUNC_NAME | |
572 | ||
573 | /* Guile's idea of infinity. */ | |
574 | static double guile_Inf; | |
575 | ||
576 | /* Guile's idea of not a number. */ | |
577 | static double guile_NaN; | |
578 | ||
579 | static void | |
580 | guile_ieee_init (void) | |
581 | { | |
582 | #if defined (HAVE_ISINF) || defined (HAVE_FINITE) | |
583 | ||
584 | /* Some version of gcc on some old version of Linux used to crash when | |
585 | trying to make Inf and NaN. */ | |
586 | ||
240a27d2 KR |
587 | #ifdef INFINITY |
588 | /* C99 INFINITY, when available. | |
589 | FIXME: The standard allows for INFINITY to be something that overflows | |
590 | at compile time. We ought to have a configure test to check for that | |
591 | before trying to use it. (But in practice we believe this is not a | |
592 | problem on any system guile is likely to target.) */ | |
593 | guile_Inf = INFINITY; | |
594 | #elif HAVE_DINFINITY | |
595 | /* OSF */ | |
7351e207 MV |
596 | extern unsigned int DINFINITY[2]; |
597 | guile_Inf = (*(X_CAST(double *, DINFINITY))); | |
598 | #else | |
599 | double tmp = 1e+10; | |
600 | guile_Inf = tmp; | |
601 | for (;;) | |
602 | { | |
603 | guile_Inf *= 1e+10; | |
604 | if (guile_Inf == tmp) | |
605 | break; | |
606 | tmp = guile_Inf; | |
607 | } | |
608 | #endif | |
609 | ||
610 | #endif | |
611 | ||
612 | #if defined (HAVE_ISNAN) | |
613 | ||
240a27d2 KR |
614 | #ifdef NAN |
615 | /* C99 NAN, when available */ | |
616 | guile_NaN = NAN; | |
617 | #elif HAVE_DQNAN | |
618 | /* OSF */ | |
7351e207 MV |
619 | extern unsigned int DQNAN[2]; |
620 | guile_NaN = (*(X_CAST(double *, DQNAN))); | |
621 | #else | |
622 | guile_NaN = guile_Inf / guile_Inf; | |
623 | #endif | |
624 | ||
625 | #endif | |
626 | } | |
627 | ||
628 | SCM_DEFINE (scm_inf, "inf", 0, 0, 0, | |
629 | (void), | |
630 | "Return Inf.") | |
631 | #define FUNC_NAME s_scm_inf | |
632 | { | |
633 | static int initialized = 0; | |
634 | if (! initialized) | |
635 | { | |
636 | guile_ieee_init (); | |
637 | initialized = 1; | |
638 | } | |
55f26379 | 639 | return scm_from_double (guile_Inf); |
7351e207 MV |
640 | } |
641 | #undef FUNC_NAME | |
642 | ||
643 | SCM_DEFINE (scm_nan, "nan", 0, 0, 0, | |
644 | (void), | |
645 | "Return NaN.") | |
646 | #define FUNC_NAME s_scm_nan | |
647 | { | |
648 | static int initialized = 0; | |
0aacf84e | 649 | if (!initialized) |
7351e207 MV |
650 | { |
651 | guile_ieee_init (); | |
652 | initialized = 1; | |
653 | } | |
55f26379 | 654 | return scm_from_double (guile_NaN); |
7351e207 MV |
655 | } |
656 | #undef FUNC_NAME | |
657 | ||
4219f20d | 658 | |
a48d60b1 MD |
659 | SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0, |
660 | (SCM x), | |
661 | "Return the absolute value of @var{x}.") | |
662 | #define FUNC_NAME | |
0f2d19dd | 663 | { |
e11e83f3 | 664 | if (SCM_I_INUMP (x)) |
0aacf84e | 665 | { |
e11e83f3 | 666 | long int xx = SCM_I_INUM (x); |
0aacf84e MD |
667 | if (xx >= 0) |
668 | return x; | |
669 | else if (SCM_POSFIXABLE (-xx)) | |
d956fa6f | 670 | return SCM_I_MAKINUM (-xx); |
0aacf84e MD |
671 | else |
672 | return scm_i_long2big (-xx); | |
4219f20d | 673 | } |
0aacf84e MD |
674 | else if (SCM_BIGP (x)) |
675 | { | |
676 | const int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
677 | if (sgn < 0) | |
678 | return scm_i_clonebig (x, 0); | |
679 | else | |
680 | return x; | |
4219f20d | 681 | } |
0aacf84e | 682 | else if (SCM_REALP (x)) |
ae38324d KR |
683 | { |
684 | /* note that if x is a NaN then xx<0 is false so we return x unchanged */ | |
685 | double xx = SCM_REAL_VALUE (x); | |
686 | if (xx < 0.0) | |
55f26379 | 687 | return scm_from_double (-xx); |
ae38324d KR |
688 | else |
689 | return x; | |
690 | } | |
f92e85f7 MV |
691 | else if (SCM_FRACTIONP (x)) |
692 | { | |
73e4de09 | 693 | if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x)))) |
f92e85f7 | 694 | return x; |
cba42c93 | 695 | return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED), |
f92e85f7 MV |
696 | SCM_FRACTION_DENOMINATOR (x)); |
697 | } | |
0aacf84e | 698 | else |
a48d60b1 | 699 | SCM_WTA_DISPATCH_1 (g_scm_abs, x, 1, s_scm_abs); |
0f2d19dd | 700 | } |
a48d60b1 | 701 | #undef FUNC_NAME |
0f2d19dd | 702 | |
4219f20d | 703 | |
9de33deb | 704 | SCM_GPROC (s_quotient, "quotient", 2, 0, 0, scm_quotient, g_quotient); |
942e5b91 MG |
705 | /* "Return the quotient of the numbers @var{x} and @var{y}." |
706 | */ | |
0f2d19dd | 707 | SCM |
6e8d25a6 | 708 | scm_quotient (SCM x, SCM y) |
0f2d19dd | 709 | { |
e11e83f3 | 710 | if (SCM_I_INUMP (x)) |
0aacf84e | 711 | { |
e11e83f3 MV |
712 | long xx = SCM_I_INUM (x); |
713 | if (SCM_I_INUMP (y)) | |
0aacf84e | 714 | { |
e11e83f3 | 715 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
716 | if (yy == 0) |
717 | scm_num_overflow (s_quotient); | |
718 | else | |
719 | { | |
720 | long z = xx / yy; | |
721 | if (SCM_FIXABLE (z)) | |
d956fa6f | 722 | return SCM_I_MAKINUM (z); |
0aacf84e MD |
723 | else |
724 | return scm_i_long2big (z); | |
725 | } | |
828865c3 | 726 | } |
0aacf84e | 727 | else if (SCM_BIGP (y)) |
ac0c002c | 728 | { |
e11e83f3 | 729 | if ((SCM_I_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM) |
4dc09ee4 KR |
730 | && (mpz_cmp_ui (SCM_I_BIG_MPZ (y), |
731 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
732 | { | |
733 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
734 | scm_remember_upto_here_1 (y); | |
d956fa6f | 735 | return SCM_I_MAKINUM (-1); |
4dc09ee4 | 736 | } |
0aacf84e | 737 | else |
d956fa6f | 738 | return SCM_I_MAKINUM (0); |
ac0c002c DH |
739 | } |
740 | else | |
0aacf84e | 741 | SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient); |
828865c3 | 742 | } |
0aacf84e MD |
743 | else if (SCM_BIGP (x)) |
744 | { | |
e11e83f3 | 745 | if (SCM_I_INUMP (y)) |
0aacf84e | 746 | { |
e11e83f3 | 747 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
748 | if (yy == 0) |
749 | scm_num_overflow (s_quotient); | |
750 | else if (yy == 1) | |
751 | return x; | |
752 | else | |
753 | { | |
754 | SCM result = scm_i_mkbig (); | |
755 | if (yy < 0) | |
756 | { | |
757 | mpz_tdiv_q_ui (SCM_I_BIG_MPZ (result), | |
758 | SCM_I_BIG_MPZ (x), | |
759 | - yy); | |
760 | mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result)); | |
761 | } | |
762 | else | |
763 | mpz_tdiv_q_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy); | |
764 | scm_remember_upto_here_1 (x); | |
765 | return scm_i_normbig (result); | |
766 | } | |
767 | } | |
768 | else if (SCM_BIGP (y)) | |
769 | { | |
770 | SCM result = scm_i_mkbig (); | |
771 | mpz_tdiv_q (SCM_I_BIG_MPZ (result), | |
772 | SCM_I_BIG_MPZ (x), | |
773 | SCM_I_BIG_MPZ (y)); | |
774 | scm_remember_upto_here_2 (x, y); | |
775 | return scm_i_normbig (result); | |
776 | } | |
777 | else | |
778 | SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient); | |
f872b822 | 779 | } |
0aacf84e | 780 | else |
89a7e495 | 781 | SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG1, s_quotient); |
0f2d19dd JB |
782 | } |
783 | ||
9de33deb | 784 | SCM_GPROC (s_remainder, "remainder", 2, 0, 0, scm_remainder, g_remainder); |
942e5b91 MG |
785 | /* "Return the remainder of the numbers @var{x} and @var{y}.\n" |
786 | * "@lisp\n" | |
787 | * "(remainder 13 4) @result{} 1\n" | |
788 | * "(remainder -13 4) @result{} -1\n" | |
789 | * "@end lisp" | |
790 | */ | |
0f2d19dd | 791 | SCM |
6e8d25a6 | 792 | scm_remainder (SCM x, SCM y) |
0f2d19dd | 793 | { |
e11e83f3 | 794 | if (SCM_I_INUMP (x)) |
0aacf84e | 795 | { |
e11e83f3 | 796 | if (SCM_I_INUMP (y)) |
0aacf84e | 797 | { |
e11e83f3 | 798 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
799 | if (yy == 0) |
800 | scm_num_overflow (s_remainder); | |
801 | else | |
802 | { | |
e11e83f3 | 803 | long z = SCM_I_INUM (x) % yy; |
d956fa6f | 804 | return SCM_I_MAKINUM (z); |
0aacf84e MD |
805 | } |
806 | } | |
807 | else if (SCM_BIGP (y)) | |
ac0c002c | 808 | { |
e11e83f3 | 809 | if ((SCM_I_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM) |
4dc09ee4 KR |
810 | && (mpz_cmp_ui (SCM_I_BIG_MPZ (y), |
811 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
812 | { | |
813 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
814 | scm_remember_upto_here_1 (y); | |
d956fa6f | 815 | return SCM_I_MAKINUM (0); |
4dc09ee4 | 816 | } |
0aacf84e MD |
817 | else |
818 | return x; | |
ac0c002c DH |
819 | } |
820 | else | |
0aacf84e | 821 | SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder); |
89a7e495 | 822 | } |
0aacf84e MD |
823 | else if (SCM_BIGP (x)) |
824 | { | |
e11e83f3 | 825 | if (SCM_I_INUMP (y)) |
0aacf84e | 826 | { |
e11e83f3 | 827 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
828 | if (yy == 0) |
829 | scm_num_overflow (s_remainder); | |
830 | else | |
831 | { | |
832 | SCM result = scm_i_mkbig (); | |
833 | if (yy < 0) | |
834 | yy = - yy; | |
835 | mpz_tdiv_r_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ(x), yy); | |
836 | scm_remember_upto_here_1 (x); | |
837 | return scm_i_normbig (result); | |
838 | } | |
839 | } | |
840 | else if (SCM_BIGP (y)) | |
841 | { | |
842 | SCM result = scm_i_mkbig (); | |
843 | mpz_tdiv_r (SCM_I_BIG_MPZ (result), | |
844 | SCM_I_BIG_MPZ (x), | |
845 | SCM_I_BIG_MPZ (y)); | |
846 | scm_remember_upto_here_2 (x, y); | |
847 | return scm_i_normbig (result); | |
848 | } | |
849 | else | |
850 | SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder); | |
f872b822 | 851 | } |
0aacf84e | 852 | else |
89a7e495 | 853 | SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG1, s_remainder); |
0f2d19dd JB |
854 | } |
855 | ||
89a7e495 | 856 | |
9de33deb | 857 | SCM_GPROC (s_modulo, "modulo", 2, 0, 0, scm_modulo, g_modulo); |
942e5b91 MG |
858 | /* "Return the modulo of the numbers @var{x} and @var{y}.\n" |
859 | * "@lisp\n" | |
860 | * "(modulo 13 4) @result{} 1\n" | |
861 | * "(modulo -13 4) @result{} 3\n" | |
862 | * "@end lisp" | |
863 | */ | |
0f2d19dd | 864 | SCM |
6e8d25a6 | 865 | scm_modulo (SCM x, SCM y) |
0f2d19dd | 866 | { |
e11e83f3 | 867 | if (SCM_I_INUMP (x)) |
0aacf84e | 868 | { |
e11e83f3 MV |
869 | long xx = SCM_I_INUM (x); |
870 | if (SCM_I_INUMP (y)) | |
0aacf84e | 871 | { |
e11e83f3 | 872 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
873 | if (yy == 0) |
874 | scm_num_overflow (s_modulo); | |
875 | else | |
876 | { | |
877 | /* FIXME: I think this may be a bug on some arches -- results | |
878 | of % with negative second arg are undefined... */ | |
879 | long z = xx % yy; | |
880 | long result; | |
881 | ||
882 | if (yy < 0) | |
883 | { | |
884 | if (z > 0) | |
885 | result = z + yy; | |
886 | else | |
887 | result = z; | |
888 | } | |
889 | else | |
890 | { | |
891 | if (z < 0) | |
892 | result = z + yy; | |
893 | else | |
894 | result = z; | |
895 | } | |
d956fa6f | 896 | return SCM_I_MAKINUM (result); |
0aacf84e MD |
897 | } |
898 | } | |
899 | else if (SCM_BIGP (y)) | |
900 | { | |
901 | int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
0aacf84e MD |
902 | { |
903 | mpz_t z_x; | |
904 | SCM result; | |
905 | ||
906 | if (sgn_y < 0) | |
907 | { | |
908 | SCM pos_y = scm_i_clonebig (y, 0); | |
909 | /* do this after the last scm_op */ | |
910 | mpz_init_set_si (z_x, xx); | |
911 | result = pos_y; /* re-use this bignum */ | |
912 | mpz_mod (SCM_I_BIG_MPZ (result), | |
913 | z_x, | |
914 | SCM_I_BIG_MPZ (pos_y)); | |
915 | scm_remember_upto_here_1 (pos_y); | |
916 | } | |
917 | else | |
918 | { | |
919 | result = scm_i_mkbig (); | |
920 | /* do this after the last scm_op */ | |
921 | mpz_init_set_si (z_x, xx); | |
922 | mpz_mod (SCM_I_BIG_MPZ (result), | |
923 | z_x, | |
924 | SCM_I_BIG_MPZ (y)); | |
925 | scm_remember_upto_here_1 (y); | |
926 | } | |
ca46fb90 | 927 | |
0aacf84e MD |
928 | if ((sgn_y < 0) && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0) |
929 | mpz_add (SCM_I_BIG_MPZ (result), | |
930 | SCM_I_BIG_MPZ (y), | |
931 | SCM_I_BIG_MPZ (result)); | |
932 | scm_remember_upto_here_1 (y); | |
933 | /* and do this before the next one */ | |
934 | mpz_clear (z_x); | |
935 | return scm_i_normbig (result); | |
936 | } | |
937 | } | |
938 | else | |
939 | SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo); | |
f872b822 | 940 | } |
0aacf84e MD |
941 | else if (SCM_BIGP (x)) |
942 | { | |
e11e83f3 | 943 | if (SCM_I_INUMP (y)) |
0aacf84e | 944 | { |
e11e83f3 | 945 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
946 | if (yy == 0) |
947 | scm_num_overflow (s_modulo); | |
948 | else | |
949 | { | |
950 | SCM result = scm_i_mkbig (); | |
951 | mpz_mod_ui (SCM_I_BIG_MPZ (result), | |
952 | SCM_I_BIG_MPZ (x), | |
953 | (yy < 0) ? - yy : yy); | |
954 | scm_remember_upto_here_1 (x); | |
955 | if ((yy < 0) && (mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)) | |
956 | mpz_sub_ui (SCM_I_BIG_MPZ (result), | |
957 | SCM_I_BIG_MPZ (result), | |
958 | - yy); | |
959 | return scm_i_normbig (result); | |
960 | } | |
961 | } | |
962 | else if (SCM_BIGP (y)) | |
963 | { | |
0aacf84e MD |
964 | { |
965 | SCM result = scm_i_mkbig (); | |
966 | int y_sgn = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
967 | SCM pos_y = scm_i_clonebig (y, y_sgn >= 0); | |
968 | mpz_mod (SCM_I_BIG_MPZ (result), | |
969 | SCM_I_BIG_MPZ (x), | |
970 | SCM_I_BIG_MPZ (pos_y)); | |
ca46fb90 | 971 | |
0aacf84e MD |
972 | scm_remember_upto_here_1 (x); |
973 | if ((y_sgn < 0) && (mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)) | |
974 | mpz_add (SCM_I_BIG_MPZ (result), | |
975 | SCM_I_BIG_MPZ (y), | |
976 | SCM_I_BIG_MPZ (result)); | |
977 | scm_remember_upto_here_2 (y, pos_y); | |
978 | return scm_i_normbig (result); | |
979 | } | |
980 | } | |
981 | else | |
982 | SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo); | |
828865c3 | 983 | } |
0aacf84e | 984 | else |
09fb7599 | 985 | SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG1, s_modulo); |
0f2d19dd JB |
986 | } |
987 | ||
9de33deb | 988 | SCM_GPROC1 (s_gcd, "gcd", scm_tc7_asubr, scm_gcd, g_gcd); |
942e5b91 MG |
989 | /* "Return the greatest common divisor of all arguments.\n" |
990 | * "If called without arguments, 0 is returned." | |
991 | */ | |
0f2d19dd | 992 | SCM |
6e8d25a6 | 993 | scm_gcd (SCM x, SCM y) |
0f2d19dd | 994 | { |
ca46fb90 | 995 | if (SCM_UNBNDP (y)) |
0aacf84e | 996 | return SCM_UNBNDP (x) ? SCM_INUM0 : x; |
ca46fb90 | 997 | |
e11e83f3 | 998 | if (SCM_I_INUMP (x)) |
ca46fb90 | 999 | { |
e11e83f3 | 1000 | if (SCM_I_INUMP (y)) |
ca46fb90 | 1001 | { |
e11e83f3 MV |
1002 | long xx = SCM_I_INUM (x); |
1003 | long yy = SCM_I_INUM (y); | |
ca46fb90 RB |
1004 | long u = xx < 0 ? -xx : xx; |
1005 | long v = yy < 0 ? -yy : yy; | |
1006 | long result; | |
0aacf84e MD |
1007 | if (xx == 0) |
1008 | result = v; | |
1009 | else if (yy == 0) | |
1010 | result = u; | |
1011 | else | |
1012 | { | |
1013 | long k = 1; | |
1014 | long t; | |
1015 | /* Determine a common factor 2^k */ | |
1016 | while (!(1 & (u | v))) | |
1017 | { | |
1018 | k <<= 1; | |
1019 | u >>= 1; | |
1020 | v >>= 1; | |
1021 | } | |
1022 | /* Now, any factor 2^n can be eliminated */ | |
1023 | if (u & 1) | |
1024 | t = -v; | |
1025 | else | |
1026 | { | |
1027 | t = u; | |
1028 | b3: | |
1029 | t = SCM_SRS (t, 1); | |
1030 | } | |
1031 | if (!(1 & t)) | |
1032 | goto b3; | |
1033 | if (t > 0) | |
1034 | u = t; | |
1035 | else | |
1036 | v = -t; | |
1037 | t = u - v; | |
1038 | if (t != 0) | |
1039 | goto b3; | |
1040 | result = u * k; | |
1041 | } | |
1042 | return (SCM_POSFIXABLE (result) | |
d956fa6f | 1043 | ? SCM_I_MAKINUM (result) |
0aacf84e | 1044 | : scm_i_long2big (result)); |
ca46fb90 RB |
1045 | } |
1046 | else if (SCM_BIGP (y)) | |
1047 | { | |
0bff4dce KR |
1048 | SCM_SWAP (x, y); |
1049 | goto big_inum; | |
ca46fb90 RB |
1050 | } |
1051 | else | |
1052 | SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd); | |
f872b822 | 1053 | } |
ca46fb90 RB |
1054 | else if (SCM_BIGP (x)) |
1055 | { | |
e11e83f3 | 1056 | if (SCM_I_INUMP (y)) |
ca46fb90 RB |
1057 | { |
1058 | unsigned long result; | |
0bff4dce KR |
1059 | long yy; |
1060 | big_inum: | |
e11e83f3 | 1061 | yy = SCM_I_INUM (y); |
8c5b0afc KR |
1062 | if (yy == 0) |
1063 | return scm_abs (x); | |
0aacf84e MD |
1064 | if (yy < 0) |
1065 | yy = -yy; | |
ca46fb90 RB |
1066 | result = mpz_gcd_ui (NULL, SCM_I_BIG_MPZ (x), yy); |
1067 | scm_remember_upto_here_1 (x); | |
0aacf84e | 1068 | return (SCM_POSFIXABLE (result) |
d956fa6f | 1069 | ? SCM_I_MAKINUM (result) |
c71b0706 | 1070 | : scm_from_ulong (result)); |
ca46fb90 RB |
1071 | } |
1072 | else if (SCM_BIGP (y)) | |
1073 | { | |
1074 | SCM result = scm_i_mkbig (); | |
0aacf84e MD |
1075 | mpz_gcd (SCM_I_BIG_MPZ (result), |
1076 | SCM_I_BIG_MPZ (x), | |
1077 | SCM_I_BIG_MPZ (y)); | |
1078 | scm_remember_upto_here_2 (x, y); | |
ca46fb90 RB |
1079 | return scm_i_normbig (result); |
1080 | } | |
1081 | else | |
1082 | SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd); | |
09fb7599 | 1083 | } |
ca46fb90 | 1084 | else |
09fb7599 | 1085 | SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG1, s_gcd); |
0f2d19dd JB |
1086 | } |
1087 | ||
9de33deb | 1088 | SCM_GPROC1 (s_lcm, "lcm", scm_tc7_asubr, scm_lcm, g_lcm); |
942e5b91 MG |
1089 | /* "Return the least common multiple of the arguments.\n" |
1090 | * "If called without arguments, 1 is returned." | |
1091 | */ | |
0f2d19dd | 1092 | SCM |
6e8d25a6 | 1093 | scm_lcm (SCM n1, SCM n2) |
0f2d19dd | 1094 | { |
ca46fb90 RB |
1095 | if (SCM_UNBNDP (n2)) |
1096 | { | |
1097 | if (SCM_UNBNDP (n1)) | |
d956fa6f MV |
1098 | return SCM_I_MAKINUM (1L); |
1099 | n2 = SCM_I_MAKINUM (1L); | |
09fb7599 | 1100 | } |
09fb7599 | 1101 | |
e11e83f3 | 1102 | SCM_GASSERT2 (SCM_I_INUMP (n1) || SCM_BIGP (n1), |
ca46fb90 | 1103 | g_lcm, n1, n2, SCM_ARG1, s_lcm); |
e11e83f3 | 1104 | SCM_GASSERT2 (SCM_I_INUMP (n2) || SCM_BIGP (n2), |
ca46fb90 | 1105 | g_lcm, n1, n2, SCM_ARGn, s_lcm); |
09fb7599 | 1106 | |
e11e83f3 | 1107 | if (SCM_I_INUMP (n1)) |
ca46fb90 | 1108 | { |
e11e83f3 | 1109 | if (SCM_I_INUMP (n2)) |
ca46fb90 RB |
1110 | { |
1111 | SCM d = scm_gcd (n1, n2); | |
bc36d050 | 1112 | if (scm_is_eq (d, SCM_INUM0)) |
ca46fb90 RB |
1113 | return d; |
1114 | else | |
1115 | return scm_abs (scm_product (n1, scm_quotient (n2, d))); | |
1116 | } | |
1117 | else | |
1118 | { | |
1119 | /* inum n1, big n2 */ | |
1120 | inumbig: | |
1121 | { | |
1122 | SCM result = scm_i_mkbig (); | |
e11e83f3 | 1123 | long nn1 = SCM_I_INUM (n1); |
ca46fb90 RB |
1124 | if (nn1 == 0) return SCM_INUM0; |
1125 | if (nn1 < 0) nn1 = - nn1; | |
1126 | mpz_lcm_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n2), nn1); | |
1127 | scm_remember_upto_here_1 (n2); | |
1128 | return result; | |
1129 | } | |
1130 | } | |
1131 | } | |
1132 | else | |
1133 | { | |
1134 | /* big n1 */ | |
e11e83f3 | 1135 | if (SCM_I_INUMP (n2)) |
ca46fb90 RB |
1136 | { |
1137 | SCM_SWAP (n1, n2); | |
1138 | goto inumbig; | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | SCM result = scm_i_mkbig (); | |
1143 | mpz_lcm(SCM_I_BIG_MPZ (result), | |
1144 | SCM_I_BIG_MPZ (n1), | |
1145 | SCM_I_BIG_MPZ (n2)); | |
1146 | scm_remember_upto_here_2(n1, n2); | |
1147 | /* shouldn't need to normalize b/c lcm of 2 bigs should be big */ | |
1148 | return result; | |
1149 | } | |
f872b822 | 1150 | } |
0f2d19dd JB |
1151 | } |
1152 | ||
8a525303 GB |
1153 | /* Emulating 2's complement bignums with sign magnitude arithmetic: |
1154 | ||
1155 | Logand: | |
1156 | X Y Result Method: | |
1157 | (len) | |
1158 | + + + x (map digit:logand X Y) | |
1159 | + - + x (map digit:logand X (lognot (+ -1 Y))) | |
1160 | - + + y (map digit:logand (lognot (+ -1 X)) Y) | |
1161 | - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y))) | |
1162 | ||
1163 | Logior: | |
1164 | X Y Result Method: | |
1165 | ||
1166 | + + + (map digit:logior X Y) | |
1167 | + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y))) | |
1168 | - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y))) | |
1169 | - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y))) | |
1170 | ||
1171 | Logxor: | |
1172 | X Y Result Method: | |
1173 | ||
1174 | + + + (map digit:logxor X Y) | |
1175 | + - - (+ 1 (map digit:logxor X (+ -1 Y))) | |
1176 | - + - (+ 1 (map digit:logxor (+ -1 X) Y)) | |
1177 | - - + (map digit:logxor (+ -1 X) (+ -1 Y)) | |
1178 | ||
1179 | Logtest: | |
1180 | X Y Result | |
1181 | ||
1182 | + + (any digit:logand X Y) | |
1183 | + - (any digit:logand X (lognot (+ -1 Y))) | |
1184 | - + (any digit:logand (lognot (+ -1 X)) Y) | |
1185 | - - #t | |
1186 | ||
1187 | */ | |
1188 | ||
c3ee7520 | 1189 | SCM_DEFINE1 (scm_logand, "logand", scm_tc7_asubr, |
1bbd0b84 | 1190 | (SCM n1, SCM n2), |
3c3db128 GH |
1191 | "Return the bitwise AND of the integer arguments.\n\n" |
1192 | "@lisp\n" | |
1193 | "(logand) @result{} -1\n" | |
1194 | "(logand 7) @result{} 7\n" | |
535f2a51 | 1195 | "(logand #b111 #b011 #b001) @result{} 1\n" |
3c3db128 | 1196 | "@end lisp") |
1bbd0b84 | 1197 | #define FUNC_NAME s_scm_logand |
0f2d19dd | 1198 | { |
9a00c9fc DH |
1199 | long int nn1; |
1200 | ||
0aacf84e MD |
1201 | if (SCM_UNBNDP (n2)) |
1202 | { | |
1203 | if (SCM_UNBNDP (n1)) | |
d956fa6f | 1204 | return SCM_I_MAKINUM (-1); |
0aacf84e MD |
1205 | else if (!SCM_NUMBERP (n1)) |
1206 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
1207 | else if (SCM_NUMBERP (n1)) | |
1208 | return n1; | |
1209 | else | |
1210 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
d28da049 | 1211 | } |
09fb7599 | 1212 | |
e11e83f3 | 1213 | if (SCM_I_INUMP (n1)) |
0aacf84e | 1214 | { |
e11e83f3 MV |
1215 | nn1 = SCM_I_INUM (n1); |
1216 | if (SCM_I_INUMP (n2)) | |
0aacf84e | 1217 | { |
e11e83f3 | 1218 | long nn2 = SCM_I_INUM (n2); |
d956fa6f | 1219 | return SCM_I_MAKINUM (nn1 & nn2); |
0aacf84e MD |
1220 | } |
1221 | else if SCM_BIGP (n2) | |
1222 | { | |
1223 | intbig: | |
1224 | if (n1 == 0) | |
1225 | return SCM_INUM0; | |
1226 | { | |
1227 | SCM result_z = scm_i_mkbig (); | |
1228 | mpz_t nn1_z; | |
1229 | mpz_init_set_si (nn1_z, nn1); | |
1230 | mpz_and (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2)); | |
1231 | scm_remember_upto_here_1 (n2); | |
1232 | mpz_clear (nn1_z); | |
1233 | return scm_i_normbig (result_z); | |
1234 | } | |
1235 | } | |
1236 | else | |
1237 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
1238 | } | |
1239 | else if (SCM_BIGP (n1)) | |
1240 | { | |
e11e83f3 | 1241 | if (SCM_I_INUMP (n2)) |
0aacf84e MD |
1242 | { |
1243 | SCM_SWAP (n1, n2); | |
e11e83f3 | 1244 | nn1 = SCM_I_INUM (n1); |
0aacf84e MD |
1245 | goto intbig; |
1246 | } | |
1247 | else if (SCM_BIGP (n2)) | |
1248 | { | |
1249 | SCM result_z = scm_i_mkbig (); | |
1250 | mpz_and (SCM_I_BIG_MPZ (result_z), | |
1251 | SCM_I_BIG_MPZ (n1), | |
1252 | SCM_I_BIG_MPZ (n2)); | |
1253 | scm_remember_upto_here_2 (n1, n2); | |
1254 | return scm_i_normbig (result_z); | |
1255 | } | |
1256 | else | |
1257 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
09fb7599 | 1258 | } |
0aacf84e | 1259 | else |
09fb7599 | 1260 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); |
0f2d19dd | 1261 | } |
1bbd0b84 | 1262 | #undef FUNC_NAME |
0f2d19dd | 1263 | |
09fb7599 | 1264 | |
c3ee7520 | 1265 | SCM_DEFINE1 (scm_logior, "logior", scm_tc7_asubr, |
1bbd0b84 | 1266 | (SCM n1, SCM n2), |
3c3db128 GH |
1267 | "Return the bitwise OR of the integer arguments.\n\n" |
1268 | "@lisp\n" | |
1269 | "(logior) @result{} 0\n" | |
1270 | "(logior 7) @result{} 7\n" | |
1271 | "(logior #b000 #b001 #b011) @result{} 3\n" | |
1e6808ea | 1272 | "@end lisp") |
1bbd0b84 | 1273 | #define FUNC_NAME s_scm_logior |
0f2d19dd | 1274 | { |
9a00c9fc DH |
1275 | long int nn1; |
1276 | ||
0aacf84e MD |
1277 | if (SCM_UNBNDP (n2)) |
1278 | { | |
1279 | if (SCM_UNBNDP (n1)) | |
1280 | return SCM_INUM0; | |
1281 | else if (SCM_NUMBERP (n1)) | |
1282 | return n1; | |
1283 | else | |
1284 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
d28da049 | 1285 | } |
09fb7599 | 1286 | |
e11e83f3 | 1287 | if (SCM_I_INUMP (n1)) |
0aacf84e | 1288 | { |
e11e83f3 MV |
1289 | nn1 = SCM_I_INUM (n1); |
1290 | if (SCM_I_INUMP (n2)) | |
0aacf84e | 1291 | { |
e11e83f3 | 1292 | long nn2 = SCM_I_INUM (n2); |
d956fa6f | 1293 | return SCM_I_MAKINUM (nn1 | nn2); |
0aacf84e MD |
1294 | } |
1295 | else if (SCM_BIGP (n2)) | |
1296 | { | |
1297 | intbig: | |
1298 | if (nn1 == 0) | |
1299 | return n2; | |
1300 | { | |
1301 | SCM result_z = scm_i_mkbig (); | |
1302 | mpz_t nn1_z; | |
1303 | mpz_init_set_si (nn1_z, nn1); | |
1304 | mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2)); | |
1305 | scm_remember_upto_here_1 (n2); | |
1306 | mpz_clear (nn1_z); | |
1307 | return result_z; | |
1308 | } | |
1309 | } | |
1310 | else | |
1311 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
1312 | } | |
1313 | else if (SCM_BIGP (n1)) | |
1314 | { | |
e11e83f3 | 1315 | if (SCM_I_INUMP (n2)) |
0aacf84e MD |
1316 | { |
1317 | SCM_SWAP (n1, n2); | |
e11e83f3 | 1318 | nn1 = SCM_I_INUM (n1); |
0aacf84e MD |
1319 | goto intbig; |
1320 | } | |
1321 | else if (SCM_BIGP (n2)) | |
1322 | { | |
1323 | SCM result_z = scm_i_mkbig (); | |
1324 | mpz_ior (SCM_I_BIG_MPZ (result_z), | |
1325 | SCM_I_BIG_MPZ (n1), | |
1326 | SCM_I_BIG_MPZ (n2)); | |
1327 | scm_remember_upto_here_2 (n1, n2); | |
1328 | return result_z; | |
1329 | } | |
1330 | else | |
1331 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
09fb7599 | 1332 | } |
0aacf84e | 1333 | else |
09fb7599 | 1334 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); |
0f2d19dd | 1335 | } |
1bbd0b84 | 1336 | #undef FUNC_NAME |
0f2d19dd | 1337 | |
09fb7599 | 1338 | |
c3ee7520 | 1339 | SCM_DEFINE1 (scm_logxor, "logxor", scm_tc7_asubr, |
1bbd0b84 | 1340 | (SCM n1, SCM n2), |
3c3db128 GH |
1341 | "Return the bitwise XOR of the integer arguments. A bit is\n" |
1342 | "set in the result if it is set in an odd number of arguments.\n" | |
1343 | "@lisp\n" | |
1344 | "(logxor) @result{} 0\n" | |
1345 | "(logxor 7) @result{} 7\n" | |
1346 | "(logxor #b000 #b001 #b011) @result{} 2\n" | |
1347 | "(logxor #b000 #b001 #b011 #b011) @result{} 1\n" | |
1e6808ea | 1348 | "@end lisp") |
1bbd0b84 | 1349 | #define FUNC_NAME s_scm_logxor |
0f2d19dd | 1350 | { |
9a00c9fc DH |
1351 | long int nn1; |
1352 | ||
0aacf84e MD |
1353 | if (SCM_UNBNDP (n2)) |
1354 | { | |
1355 | if (SCM_UNBNDP (n1)) | |
1356 | return SCM_INUM0; | |
1357 | else if (SCM_NUMBERP (n1)) | |
1358 | return n1; | |
1359 | else | |
1360 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
d28da049 | 1361 | } |
09fb7599 | 1362 | |
e11e83f3 | 1363 | if (SCM_I_INUMP (n1)) |
0aacf84e | 1364 | { |
e11e83f3 MV |
1365 | nn1 = SCM_I_INUM (n1); |
1366 | if (SCM_I_INUMP (n2)) | |
0aacf84e | 1367 | { |
e11e83f3 | 1368 | long nn2 = SCM_I_INUM (n2); |
d956fa6f | 1369 | return SCM_I_MAKINUM (nn1 ^ nn2); |
0aacf84e MD |
1370 | } |
1371 | else if (SCM_BIGP (n2)) | |
1372 | { | |
1373 | intbig: | |
1374 | { | |
1375 | SCM result_z = scm_i_mkbig (); | |
1376 | mpz_t nn1_z; | |
1377 | mpz_init_set_si (nn1_z, nn1); | |
1378 | mpz_xor (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2)); | |
1379 | scm_remember_upto_here_1 (n2); | |
1380 | mpz_clear (nn1_z); | |
1381 | return scm_i_normbig (result_z); | |
1382 | } | |
1383 | } | |
1384 | else | |
1385 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
1386 | } | |
1387 | else if (SCM_BIGP (n1)) | |
1388 | { | |
e11e83f3 | 1389 | if (SCM_I_INUMP (n2)) |
0aacf84e MD |
1390 | { |
1391 | SCM_SWAP (n1, n2); | |
e11e83f3 | 1392 | nn1 = SCM_I_INUM (n1); |
0aacf84e MD |
1393 | goto intbig; |
1394 | } | |
1395 | else if (SCM_BIGP (n2)) | |
1396 | { | |
1397 | SCM result_z = scm_i_mkbig (); | |
1398 | mpz_xor (SCM_I_BIG_MPZ (result_z), | |
1399 | SCM_I_BIG_MPZ (n1), | |
1400 | SCM_I_BIG_MPZ (n2)); | |
1401 | scm_remember_upto_here_2 (n1, n2); | |
1402 | return scm_i_normbig (result_z); | |
1403 | } | |
1404 | else | |
1405 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
09fb7599 | 1406 | } |
0aacf84e | 1407 | else |
09fb7599 | 1408 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); |
0f2d19dd | 1409 | } |
1bbd0b84 | 1410 | #undef FUNC_NAME |
0f2d19dd | 1411 | |
09fb7599 | 1412 | |
a1ec6916 | 1413 | SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0, |
1e6808ea MG |
1414 | (SCM j, SCM k), |
1415 | "@lisp\n" | |
b380b885 MD |
1416 | "(logtest j k) @equiv{} (not (zero? (logand j k)))\n\n" |
1417 | "(logtest #b0100 #b1011) @result{} #f\n" | |
1418 | "(logtest #b0100 #b0111) @result{} #t\n" | |
1e6808ea | 1419 | "@end lisp") |
1bbd0b84 | 1420 | #define FUNC_NAME s_scm_logtest |
0f2d19dd | 1421 | { |
1e6808ea | 1422 | long int nj; |
9a00c9fc | 1423 | |
e11e83f3 | 1424 | if (SCM_I_INUMP (j)) |
0aacf84e | 1425 | { |
e11e83f3 MV |
1426 | nj = SCM_I_INUM (j); |
1427 | if (SCM_I_INUMP (k)) | |
0aacf84e | 1428 | { |
e11e83f3 | 1429 | long nk = SCM_I_INUM (k); |
73e4de09 | 1430 | return scm_from_bool (nj & nk); |
0aacf84e MD |
1431 | } |
1432 | else if (SCM_BIGP (k)) | |
1433 | { | |
1434 | intbig: | |
1435 | if (nj == 0) | |
1436 | return SCM_BOOL_F; | |
1437 | { | |
1438 | SCM result; | |
1439 | mpz_t nj_z; | |
1440 | mpz_init_set_si (nj_z, nj); | |
1441 | mpz_and (nj_z, nj_z, SCM_I_BIG_MPZ (k)); | |
1442 | scm_remember_upto_here_1 (k); | |
73e4de09 | 1443 | result = scm_from_bool (mpz_sgn (nj_z) != 0); |
0aacf84e MD |
1444 | mpz_clear (nj_z); |
1445 | return result; | |
1446 | } | |
1447 | } | |
1448 | else | |
1449 | SCM_WRONG_TYPE_ARG (SCM_ARG2, k); | |
1450 | } | |
1451 | else if (SCM_BIGP (j)) | |
1452 | { | |
e11e83f3 | 1453 | if (SCM_I_INUMP (k)) |
0aacf84e MD |
1454 | { |
1455 | SCM_SWAP (j, k); | |
e11e83f3 | 1456 | nj = SCM_I_INUM (j); |
0aacf84e MD |
1457 | goto intbig; |
1458 | } | |
1459 | else if (SCM_BIGP (k)) | |
1460 | { | |
1461 | SCM result; | |
1462 | mpz_t result_z; | |
1463 | mpz_init (result_z); | |
1464 | mpz_and (result_z, | |
1465 | SCM_I_BIG_MPZ (j), | |
1466 | SCM_I_BIG_MPZ (k)); | |
1467 | scm_remember_upto_here_2 (j, k); | |
73e4de09 | 1468 | result = scm_from_bool (mpz_sgn (result_z) != 0); |
0aacf84e MD |
1469 | mpz_clear (result_z); |
1470 | return result; | |
1471 | } | |
1472 | else | |
1473 | SCM_WRONG_TYPE_ARG (SCM_ARG2, k); | |
1474 | } | |
1475 | else | |
1476 | SCM_WRONG_TYPE_ARG (SCM_ARG1, j); | |
0f2d19dd | 1477 | } |
1bbd0b84 | 1478 | #undef FUNC_NAME |
0f2d19dd | 1479 | |
c1bfcf60 | 1480 | |
a1ec6916 | 1481 | SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0, |
2cd04b42 | 1482 | (SCM index, SCM j), |
1e6808ea | 1483 | "@lisp\n" |
b380b885 MD |
1484 | "(logbit? index j) @equiv{} (logtest (integer-expt 2 index) j)\n\n" |
1485 | "(logbit? 0 #b1101) @result{} #t\n" | |
1486 | "(logbit? 1 #b1101) @result{} #f\n" | |
1487 | "(logbit? 2 #b1101) @result{} #t\n" | |
1488 | "(logbit? 3 #b1101) @result{} #t\n" | |
1489 | "(logbit? 4 #b1101) @result{} #f\n" | |
1e6808ea | 1490 | "@end lisp") |
1bbd0b84 | 1491 | #define FUNC_NAME s_scm_logbit_p |
0f2d19dd | 1492 | { |
78166ad5 | 1493 | unsigned long int iindex; |
5efd3c7d | 1494 | iindex = scm_to_ulong (index); |
78166ad5 | 1495 | |
e11e83f3 | 1496 | if (SCM_I_INUMP (j)) |
0d75f6d8 KR |
1497 | { |
1498 | /* bits above what's in an inum follow the sign bit */ | |
20fcc8ed | 1499 | iindex = min (iindex, SCM_LONG_BIT - 1); |
e11e83f3 | 1500 | return scm_from_bool ((1L << iindex) & SCM_I_INUM (j)); |
0d75f6d8 | 1501 | } |
0aacf84e MD |
1502 | else if (SCM_BIGP (j)) |
1503 | { | |
1504 | int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex); | |
1505 | scm_remember_upto_here_1 (j); | |
73e4de09 | 1506 | return scm_from_bool (val); |
0aacf84e MD |
1507 | } |
1508 | else | |
78166ad5 | 1509 | SCM_WRONG_TYPE_ARG (SCM_ARG2, j); |
0f2d19dd | 1510 | } |
1bbd0b84 | 1511 | #undef FUNC_NAME |
0f2d19dd | 1512 | |
78166ad5 | 1513 | |
a1ec6916 | 1514 | SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0, |
1bbd0b84 | 1515 | (SCM n), |
4d814788 | 1516 | "Return the integer which is the ones-complement of the integer\n" |
1e6808ea MG |
1517 | "argument.\n" |
1518 | "\n" | |
b380b885 MD |
1519 | "@lisp\n" |
1520 | "(number->string (lognot #b10000000) 2)\n" | |
1521 | " @result{} \"-10000001\"\n" | |
1522 | "(number->string (lognot #b0) 2)\n" | |
1523 | " @result{} \"-1\"\n" | |
1e6808ea | 1524 | "@end lisp") |
1bbd0b84 | 1525 | #define FUNC_NAME s_scm_lognot |
0f2d19dd | 1526 | { |
e11e83f3 | 1527 | if (SCM_I_INUMP (n)) { |
f9811f9f KR |
1528 | /* No overflow here, just need to toggle all the bits making up the inum. |
1529 | Enhancement: No need to strip the tag and add it back, could just xor | |
1530 | a block of 1 bits, if that worked with the various debug versions of | |
1531 | the SCM typedef. */ | |
e11e83f3 | 1532 | return SCM_I_MAKINUM (~ SCM_I_INUM (n)); |
f9811f9f KR |
1533 | |
1534 | } else if (SCM_BIGP (n)) { | |
1535 | SCM result = scm_i_mkbig (); | |
1536 | mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n)); | |
1537 | scm_remember_upto_here_1 (n); | |
1538 | return result; | |
1539 | ||
1540 | } else { | |
1541 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); | |
1542 | } | |
0f2d19dd | 1543 | } |
1bbd0b84 | 1544 | #undef FUNC_NAME |
0f2d19dd | 1545 | |
518b7508 KR |
1546 | /* returns 0 if IN is not an integer. OUT must already be |
1547 | initialized. */ | |
1548 | static int | |
1549 | coerce_to_big (SCM in, mpz_t out) | |
1550 | { | |
1551 | if (SCM_BIGP (in)) | |
1552 | mpz_set (out, SCM_I_BIG_MPZ (in)); | |
e11e83f3 MV |
1553 | else if (SCM_I_INUMP (in)) |
1554 | mpz_set_si (out, SCM_I_INUM (in)); | |
518b7508 KR |
1555 | else |
1556 | return 0; | |
1557 | ||
1558 | return 1; | |
1559 | } | |
1560 | ||
d885e204 | 1561 | SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0, |
518b7508 KR |
1562 | (SCM n, SCM k, SCM m), |
1563 | "Return @var{n} raised to the integer exponent\n" | |
1564 | "@var{k}, modulo @var{m}.\n" | |
1565 | "\n" | |
1566 | "@lisp\n" | |
1567 | "(modulo-expt 2 3 5)\n" | |
1568 | " @result{} 3\n" | |
1569 | "@end lisp") | |
d885e204 | 1570 | #define FUNC_NAME s_scm_modulo_expt |
518b7508 KR |
1571 | { |
1572 | mpz_t n_tmp; | |
1573 | mpz_t k_tmp; | |
1574 | mpz_t m_tmp; | |
1575 | ||
1576 | /* There are two classes of error we might encounter -- | |
1577 | 1) Math errors, which we'll report by calling scm_num_overflow, | |
1578 | and | |
1579 | 2) wrong-type errors, which of course we'll report by calling | |
1580 | SCM_WRONG_TYPE_ARG. | |
1581 | We don't report those errors immediately, however; instead we do | |
1582 | some cleanup first. These variables tell us which error (if | |
1583 | any) we should report after cleaning up. | |
1584 | */ | |
1585 | int report_overflow = 0; | |
1586 | ||
1587 | int position_of_wrong_type = 0; | |
1588 | SCM value_of_wrong_type = SCM_INUM0; | |
1589 | ||
1590 | SCM result = SCM_UNDEFINED; | |
1591 | ||
1592 | mpz_init (n_tmp); | |
1593 | mpz_init (k_tmp); | |
1594 | mpz_init (m_tmp); | |
1595 | ||
bc36d050 | 1596 | if (scm_is_eq (m, SCM_INUM0)) |
518b7508 KR |
1597 | { |
1598 | report_overflow = 1; | |
1599 | goto cleanup; | |
1600 | } | |
1601 | ||
1602 | if (!coerce_to_big (n, n_tmp)) | |
1603 | { | |
1604 | value_of_wrong_type = n; | |
1605 | position_of_wrong_type = 1; | |
1606 | goto cleanup; | |
1607 | } | |
1608 | ||
1609 | if (!coerce_to_big (k, k_tmp)) | |
1610 | { | |
1611 | value_of_wrong_type = k; | |
1612 | position_of_wrong_type = 2; | |
1613 | goto cleanup; | |
1614 | } | |
1615 | ||
1616 | if (!coerce_to_big (m, m_tmp)) | |
1617 | { | |
1618 | value_of_wrong_type = m; | |
1619 | position_of_wrong_type = 3; | |
1620 | goto cleanup; | |
1621 | } | |
1622 | ||
1623 | /* if the exponent K is negative, and we simply call mpz_powm, we | |
1624 | will get a divide-by-zero exception when an inverse 1/n mod m | |
1625 | doesn't exist (or is not unique). Since exceptions are hard to | |
1626 | handle, we'll attempt the inversion "by hand" -- that way, we get | |
1627 | a simple failure code, which is easy to handle. */ | |
1628 | ||
1629 | if (-1 == mpz_sgn (k_tmp)) | |
1630 | { | |
1631 | if (!mpz_invert (n_tmp, n_tmp, m_tmp)) | |
1632 | { | |
1633 | report_overflow = 1; | |
1634 | goto cleanup; | |
1635 | } | |
1636 | mpz_neg (k_tmp, k_tmp); | |
1637 | } | |
1638 | ||
1639 | result = scm_i_mkbig (); | |
1640 | mpz_powm (SCM_I_BIG_MPZ (result), | |
1641 | n_tmp, | |
1642 | k_tmp, | |
1643 | m_tmp); | |
b7b8c575 KR |
1644 | |
1645 | if (mpz_sgn (m_tmp) < 0 && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0) | |
1646 | mpz_add (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), m_tmp); | |
1647 | ||
518b7508 KR |
1648 | cleanup: |
1649 | mpz_clear (m_tmp); | |
1650 | mpz_clear (k_tmp); | |
1651 | mpz_clear (n_tmp); | |
1652 | ||
1653 | if (report_overflow) | |
1654 | scm_num_overflow (FUNC_NAME); | |
1655 | ||
1656 | if (position_of_wrong_type) | |
1657 | SCM_WRONG_TYPE_ARG (position_of_wrong_type, | |
1658 | value_of_wrong_type); | |
1659 | ||
1660 | return scm_i_normbig (result); | |
1661 | } | |
1662 | #undef FUNC_NAME | |
1663 | ||
a1ec6916 | 1664 | SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0, |
2cd04b42 | 1665 | (SCM n, SCM k), |
1e6808ea MG |
1666 | "Return @var{n} raised to the non-negative integer exponent\n" |
1667 | "@var{k}.\n" | |
1668 | "\n" | |
b380b885 MD |
1669 | "@lisp\n" |
1670 | "(integer-expt 2 5)\n" | |
1671 | " @result{} 32\n" | |
1672 | "(integer-expt -3 3)\n" | |
1673 | " @result{} -27\n" | |
1674 | "@end lisp") | |
1bbd0b84 | 1675 | #define FUNC_NAME s_scm_integer_expt |
0f2d19dd | 1676 | { |
1c35cb19 RB |
1677 | long i2 = 0; |
1678 | SCM z_i2 = SCM_BOOL_F; | |
1679 | int i2_is_big = 0; | |
d956fa6f | 1680 | SCM acc = SCM_I_MAKINUM (1L); |
ca46fb90 | 1681 | |
d57ed702 | 1682 | /* 0^0 == 1 according to R5RS */ |
bc36d050 | 1683 | if (scm_is_eq (n, SCM_INUM0) || scm_is_eq (n, acc)) |
73e4de09 | 1684 | return scm_is_false (scm_zero_p(k)) ? n : acc; |
bc36d050 | 1685 | else if (scm_is_eq (n, SCM_I_MAKINUM (-1L))) |
73e4de09 | 1686 | return scm_is_false (scm_even_p (k)) ? n : acc; |
ca46fb90 | 1687 | |
e11e83f3 MV |
1688 | if (SCM_I_INUMP (k)) |
1689 | i2 = SCM_I_INUM (k); | |
ca46fb90 RB |
1690 | else if (SCM_BIGP (k)) |
1691 | { | |
1692 | z_i2 = scm_i_clonebig (k, 1); | |
ca46fb90 RB |
1693 | scm_remember_upto_here_1 (k); |
1694 | i2_is_big = 1; | |
1695 | } | |
1696 | else if (SCM_REALP (k)) | |
2830fd91 MD |
1697 | { |
1698 | double r = SCM_REAL_VALUE (k); | |
ca46fb90 RB |
1699 | if (floor (r) != r) |
1700 | SCM_WRONG_TYPE_ARG (2, k); | |
1701 | if ((r > SCM_MOST_POSITIVE_FIXNUM) || (r < SCM_MOST_NEGATIVE_FIXNUM)) | |
1702 | { | |
1703 | z_i2 = scm_i_mkbig (); | |
753ac1e7 | 1704 | mpz_set_d (SCM_I_BIG_MPZ (z_i2), r); |
ca46fb90 RB |
1705 | i2_is_big = 1; |
1706 | } | |
1707 | else | |
1708 | { | |
1709 | i2 = r; | |
1710 | } | |
2830fd91 MD |
1711 | } |
1712 | else | |
ca46fb90 RB |
1713 | SCM_WRONG_TYPE_ARG (2, k); |
1714 | ||
1715 | if (i2_is_big) | |
f872b822 | 1716 | { |
ca46fb90 RB |
1717 | if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == -1) |
1718 | { | |
1719 | mpz_neg (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2)); | |
1720 | n = scm_divide (n, SCM_UNDEFINED); | |
1721 | } | |
1722 | while (1) | |
1723 | { | |
1724 | if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == 0) | |
1725 | { | |
ca46fb90 RB |
1726 | return acc; |
1727 | } | |
1728 | if (mpz_cmp_ui(SCM_I_BIG_MPZ (z_i2), 1) == 0) | |
1729 | { | |
ca46fb90 RB |
1730 | return scm_product (acc, n); |
1731 | } | |
1732 | if (mpz_tstbit(SCM_I_BIG_MPZ (z_i2), 0)) | |
1733 | acc = scm_product (acc, n); | |
1734 | n = scm_product (n, n); | |
1735 | mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2), 1); | |
1736 | } | |
f872b822 | 1737 | } |
ca46fb90 | 1738 | else |
f872b822 | 1739 | { |
ca46fb90 RB |
1740 | if (i2 < 0) |
1741 | { | |
1742 | i2 = -i2; | |
1743 | n = scm_divide (n, SCM_UNDEFINED); | |
1744 | } | |
1745 | while (1) | |
1746 | { | |
1747 | if (0 == i2) | |
1748 | return acc; | |
1749 | if (1 == i2) | |
1750 | return scm_product (acc, n); | |
1751 | if (i2 & 1) | |
1752 | acc = scm_product (acc, n); | |
1753 | n = scm_product (n, n); | |
1754 | i2 >>= 1; | |
1755 | } | |
f872b822 | 1756 | } |
0f2d19dd | 1757 | } |
1bbd0b84 | 1758 | #undef FUNC_NAME |
0f2d19dd | 1759 | |
a1ec6916 | 1760 | SCM_DEFINE (scm_ash, "ash", 2, 0, 0, |
1bbd0b84 | 1761 | (SCM n, SCM cnt), |
32f19569 KR |
1762 | "Return @var{n} shifted left by @var{cnt} bits, or shifted right\n" |
1763 | "if @var{cnt} is negative. This is an ``arithmetic'' shift.\n" | |
1e6808ea | 1764 | "\n" |
e7644cb2 | 1765 | "This is effectively a multiplication by 2^@var{cnt}, and when\n" |
32f19569 KR |
1766 | "@var{cnt} is negative it's a division, rounded towards negative\n" |
1767 | "infinity. (Note that this is not the same rounding as\n" | |
1768 | "@code{quotient} does.)\n" | |
1769 | "\n" | |
1770 | "With @var{n} viewed as an infinite precision twos complement,\n" | |
1771 | "@code{ash} means a left shift introducing zero bits, or a right\n" | |
1772 | "shift dropping bits.\n" | |
1e6808ea | 1773 | "\n" |
b380b885 | 1774 | "@lisp\n" |
1e6808ea MG |
1775 | "(number->string (ash #b1 3) 2) @result{} \"1000\"\n" |
1776 | "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n" | |
32f19569 KR |
1777 | "\n" |
1778 | ";; -23 is bits ...11101001, -6 is bits ...111010\n" | |
1779 | "(ash -23 -2) @result{} -6\n" | |
a3c8b9fc | 1780 | "@end lisp") |
1bbd0b84 | 1781 | #define FUNC_NAME s_scm_ash |
0f2d19dd | 1782 | { |
3ab9f56e | 1783 | long bits_to_shift; |
5efd3c7d | 1784 | bits_to_shift = scm_to_long (cnt); |
ca46fb90 RB |
1785 | |
1786 | if (bits_to_shift < 0) | |
1787 | { | |
1788 | /* Shift right by abs(cnt) bits. This is realized as a division | |
1789 | by div:=2^abs(cnt). However, to guarantee the floor | |
1790 | rounding, negative values require some special treatment. | |
1791 | */ | |
d956fa6f | 1792 | SCM div = scm_integer_expt (SCM_I_MAKINUM (2), |
5efd3c7d | 1793 | scm_from_long (-bits_to_shift)); |
f92e85f7 MV |
1794 | |
1795 | /* scm_quotient assumes its arguments are integers, but it's legal to (ash 1/2 -1) */ | |
73e4de09 | 1796 | if (scm_is_false (scm_negative_p (n))) |
ca46fb90 RB |
1797 | return scm_quotient (n, div); |
1798 | else | |
d956fa6f MV |
1799 | return scm_sum (SCM_I_MAKINUM (-1L), |
1800 | scm_quotient (scm_sum (SCM_I_MAKINUM (1L), n), div)); | |
ca46fb90 RB |
1801 | } |
1802 | else | |
3ab9f56e | 1803 | /* Shift left is done by multiplication with 2^CNT */ |
d956fa6f | 1804 | return scm_product (n, scm_integer_expt (SCM_I_MAKINUM (2), cnt)); |
0f2d19dd | 1805 | } |
1bbd0b84 | 1806 | #undef FUNC_NAME |
0f2d19dd | 1807 | |
3c9f20f8 | 1808 | |
a1ec6916 | 1809 | SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0, |
1bbd0b84 | 1810 | (SCM n, SCM start, SCM end), |
1e6808ea MG |
1811 | "Return the integer composed of the @var{start} (inclusive)\n" |
1812 | "through @var{end} (exclusive) bits of @var{n}. The\n" | |
1813 | "@var{start}th bit becomes the 0-th bit in the result.\n" | |
1814 | "\n" | |
b380b885 MD |
1815 | "@lisp\n" |
1816 | "(number->string (bit-extract #b1101101010 0 4) 2)\n" | |
1817 | " @result{} \"1010\"\n" | |
1818 | "(number->string (bit-extract #b1101101010 4 9) 2)\n" | |
1819 | " @result{} \"10110\"\n" | |
1820 | "@end lisp") | |
1bbd0b84 | 1821 | #define FUNC_NAME s_scm_bit_extract |
0f2d19dd | 1822 | { |
7f848242 | 1823 | unsigned long int istart, iend, bits; |
5efd3c7d MV |
1824 | istart = scm_to_ulong (start); |
1825 | iend = scm_to_ulong (end); | |
c1bfcf60 | 1826 | SCM_ASSERT_RANGE (3, end, (iend >= istart)); |
78166ad5 | 1827 | |
7f848242 KR |
1828 | /* how many bits to keep */ |
1829 | bits = iend - istart; | |
1830 | ||
e11e83f3 | 1831 | if (SCM_I_INUMP (n)) |
0aacf84e | 1832 | { |
e11e83f3 | 1833 | long int in = SCM_I_INUM (n); |
7f848242 KR |
1834 | |
1835 | /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to | |
d77ad560 | 1836 | SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */ |
857ae6af | 1837 | in = SCM_SRS (in, min (istart, SCM_I_FIXNUM_BIT-1)); |
ac0c002c | 1838 | |
0aacf84e MD |
1839 | if (in < 0 && bits >= SCM_I_FIXNUM_BIT) |
1840 | { | |
1841 | /* Since we emulate two's complement encoded numbers, this | |
1842 | * special case requires us to produce a result that has | |
7f848242 | 1843 | * more bits than can be stored in a fixnum. |
0aacf84e | 1844 | */ |
7f848242 KR |
1845 | SCM result = scm_i_long2big (in); |
1846 | mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), | |
1847 | bits); | |
1848 | return result; | |
0aacf84e | 1849 | } |
ac0c002c | 1850 | |
7f848242 | 1851 | /* mask down to requisite bits */ |
857ae6af | 1852 | bits = min (bits, SCM_I_FIXNUM_BIT); |
d956fa6f | 1853 | return SCM_I_MAKINUM (in & ((1L << bits) - 1)); |
0aacf84e MD |
1854 | } |
1855 | else if (SCM_BIGP (n)) | |
ac0c002c | 1856 | { |
7f848242 KR |
1857 | SCM result; |
1858 | if (bits == 1) | |
1859 | { | |
d956fa6f | 1860 | result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart)); |
7f848242 KR |
1861 | } |
1862 | else | |
1863 | { | |
1864 | /* ENHANCE-ME: It'd be nice not to allocate a new bignum when | |
1865 | bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get | |
1866 | such bits into a ulong. */ | |
1867 | result = scm_i_mkbig (); | |
1868 | mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart); | |
1869 | mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits); | |
1870 | result = scm_i_normbig (result); | |
1871 | } | |
1872 | scm_remember_upto_here_1 (n); | |
1873 | return result; | |
ac0c002c | 1874 | } |
0aacf84e | 1875 | else |
78166ad5 | 1876 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); |
0f2d19dd | 1877 | } |
1bbd0b84 | 1878 | #undef FUNC_NAME |
0f2d19dd | 1879 | |
7f848242 | 1880 | |
e4755e5c JB |
1881 | static const char scm_logtab[] = { |
1882 | 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 | |
1883 | }; | |
1cc91f1b | 1884 | |
a1ec6916 | 1885 | SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0, |
1bbd0b84 | 1886 | (SCM n), |
1e6808ea MG |
1887 | "Return the number of bits in integer @var{n}. If integer is\n" |
1888 | "positive, the 1-bits in its binary representation are counted.\n" | |
1889 | "If negative, the 0-bits in its two's-complement binary\n" | |
1890 | "representation are counted. If 0, 0 is returned.\n" | |
1891 | "\n" | |
b380b885 MD |
1892 | "@lisp\n" |
1893 | "(logcount #b10101010)\n" | |
ca46fb90 RB |
1894 | " @result{} 4\n" |
1895 | "(logcount 0)\n" | |
1896 | " @result{} 0\n" | |
1897 | "(logcount -2)\n" | |
1898 | " @result{} 1\n" | |
1899 | "@end lisp") | |
1900 | #define FUNC_NAME s_scm_logcount | |
1901 | { | |
e11e83f3 | 1902 | if (SCM_I_INUMP (n)) |
f872b822 | 1903 | { |
ca46fb90 | 1904 | unsigned long int c = 0; |
e11e83f3 | 1905 | long int nn = SCM_I_INUM (n); |
ca46fb90 RB |
1906 | if (nn < 0) |
1907 | nn = -1 - nn; | |
1908 | while (nn) | |
1909 | { | |
1910 | c += scm_logtab[15 & nn]; | |
1911 | nn >>= 4; | |
1912 | } | |
d956fa6f | 1913 | return SCM_I_MAKINUM (c); |
f872b822 | 1914 | } |
ca46fb90 | 1915 | else if (SCM_BIGP (n)) |
f872b822 | 1916 | { |
ca46fb90 | 1917 | unsigned long count; |
713a4259 KR |
1918 | if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0) |
1919 | count = mpz_popcount (SCM_I_BIG_MPZ (n)); | |
ca46fb90 | 1920 | else |
713a4259 KR |
1921 | count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one); |
1922 | scm_remember_upto_here_1 (n); | |
d956fa6f | 1923 | return SCM_I_MAKINUM (count); |
f872b822 | 1924 | } |
ca46fb90 RB |
1925 | else |
1926 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); | |
0f2d19dd | 1927 | } |
ca46fb90 | 1928 | #undef FUNC_NAME |
0f2d19dd JB |
1929 | |
1930 | ||
ca46fb90 RB |
1931 | static const char scm_ilentab[] = { |
1932 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 | |
1933 | }; | |
1934 | ||
0f2d19dd | 1935 | |
ca46fb90 RB |
1936 | SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0, |
1937 | (SCM n), | |
1938 | "Return the number of bits necessary to represent @var{n}.\n" | |
1939 | "\n" | |
1940 | "@lisp\n" | |
1941 | "(integer-length #b10101010)\n" | |
1942 | " @result{} 8\n" | |
1943 | "(integer-length 0)\n" | |
1944 | " @result{} 0\n" | |
1945 | "(integer-length #b1111)\n" | |
1946 | " @result{} 4\n" | |
1947 | "@end lisp") | |
1948 | #define FUNC_NAME s_scm_integer_length | |
1949 | { | |
e11e83f3 | 1950 | if (SCM_I_INUMP (n)) |
0aacf84e MD |
1951 | { |
1952 | unsigned long int c = 0; | |
1953 | unsigned int l = 4; | |
e11e83f3 | 1954 | long int nn = SCM_I_INUM (n); |
0aacf84e MD |
1955 | if (nn < 0) |
1956 | nn = -1 - nn; | |
1957 | while (nn) | |
1958 | { | |
1959 | c += 4; | |
1960 | l = scm_ilentab [15 & nn]; | |
1961 | nn >>= 4; | |
1962 | } | |
d956fa6f | 1963 | return SCM_I_MAKINUM (c - 4 + l); |
0aacf84e MD |
1964 | } |
1965 | else if (SCM_BIGP (n)) | |
1966 | { | |
1967 | /* mpz_sizeinbase looks at the absolute value of negatives, whereas we | |
1968 | want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is | |
1969 | 1 too big, so check for that and adjust. */ | |
1970 | size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2); | |
1971 | if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0 | |
1972 | && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */ | |
1973 | mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX) | |
1974 | size--; | |
1975 | scm_remember_upto_here_1 (n); | |
d956fa6f | 1976 | return SCM_I_MAKINUM (size); |
0aacf84e MD |
1977 | } |
1978 | else | |
ca46fb90 | 1979 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); |
ca46fb90 RB |
1980 | } |
1981 | #undef FUNC_NAME | |
0f2d19dd JB |
1982 | |
1983 | /*** NUMBERS -> STRINGS ***/ | |
0b799eea MV |
1984 | #define SCM_MAX_DBL_PREC 60 |
1985 | #define SCM_MAX_DBL_RADIX 36 | |
1986 | ||
1987 | /* this is an array starting with radix 2, and ending with radix SCM_MAX_DBL_RADIX */ | |
1988 | static int scm_dblprec[SCM_MAX_DBL_RADIX - 1]; | |
1989 | static double fx_per_radix[SCM_MAX_DBL_RADIX - 1][SCM_MAX_DBL_PREC]; | |
1990 | ||
1991 | static | |
1992 | void init_dblprec(int *prec, int radix) { | |
1993 | /* determine floating point precision by adding successively | |
1994 | smaller increments to 1.0 until it is considered == 1.0 */ | |
1995 | double f = ((double)1.0)/radix; | |
1996 | double fsum = 1.0 + f; | |
1997 | ||
1998 | *prec = 0; | |
1999 | while (fsum != 1.0) | |
2000 | { | |
2001 | if (++(*prec) > SCM_MAX_DBL_PREC) | |
2002 | fsum = 1.0; | |
2003 | else | |
2004 | { | |
2005 | f /= radix; | |
2006 | fsum = f + 1.0; | |
2007 | } | |
2008 | } | |
2009 | (*prec) -= 1; | |
2010 | } | |
2011 | ||
2012 | static | |
2013 | void init_fx_radix(double *fx_list, int radix) | |
2014 | { | |
2015 | /* initialize a per-radix list of tolerances. When added | |
2016 | to a number < 1.0, we can determine if we should raund | |
2017 | up and quit converting a number to a string. */ | |
2018 | int i; | |
2019 | fx_list[0] = 0.0; | |
2020 | fx_list[1] = 0.5; | |
2021 | for( i = 2 ; i < SCM_MAX_DBL_PREC; ++i ) | |
2022 | fx_list[i] = (fx_list[i-1] / radix); | |
2023 | } | |
2024 | ||
2025 | /* use this array as a way to generate a single digit */ | |
2026 | static const char*number_chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
0f2d19dd | 2027 | |
1be6b49c | 2028 | static size_t |
0b799eea | 2029 | idbl2str (double f, char *a, int radix) |
0f2d19dd | 2030 | { |
0b799eea MV |
2031 | int efmt, dpt, d, i, wp; |
2032 | double *fx; | |
2033 | #ifdef DBL_MIN_10_EXP | |
2034 | double f_cpy; | |
2035 | int exp_cpy; | |
2036 | #endif /* DBL_MIN_10_EXP */ | |
2037 | size_t ch = 0; | |
2038 | int exp = 0; | |
2039 | ||
2040 | if(radix < 2 || | |
2041 | radix > SCM_MAX_DBL_RADIX) | |
2042 | { | |
2043 | /* revert to existing behavior */ | |
2044 | radix = 10; | |
2045 | } | |
2046 | ||
2047 | wp = scm_dblprec[radix-2]; | |
2048 | fx = fx_per_radix[radix-2]; | |
0f2d19dd | 2049 | |
f872b822 | 2050 | if (f == 0.0) |
abb7e44d MV |
2051 | { |
2052 | #ifdef HAVE_COPYSIGN | |
2053 | double sgn = copysign (1.0, f); | |
2054 | ||
2055 | if (sgn < 0.0) | |
2056 | a[ch++] = '-'; | |
2057 | #endif | |
abb7e44d MV |
2058 | goto zero; /*{a[0]='0'; a[1]='.'; a[2]='0'; return 3;} */ |
2059 | } | |
7351e207 MV |
2060 | |
2061 | if (xisinf (f)) | |
2062 | { | |
2063 | if (f < 0) | |
2064 | strcpy (a, "-inf.0"); | |
2065 | else | |
2066 | strcpy (a, "+inf.0"); | |
2067 | return ch+6; | |
2068 | } | |
2069 | else if (xisnan (f)) | |
2070 | { | |
2071 | strcpy (a, "+nan.0"); | |
2072 | return ch+6; | |
2073 | } | |
2074 | ||
f872b822 MD |
2075 | if (f < 0.0) |
2076 | { | |
2077 | f = -f; | |
2078 | a[ch++] = '-'; | |
2079 | } | |
7351e207 | 2080 | |
f872b822 MD |
2081 | #ifdef DBL_MIN_10_EXP /* Prevent unnormalized values, as from |
2082 | make-uniform-vector, from causing infinite loops. */ | |
0b799eea MV |
2083 | /* just do the checking...if it passes, we do the conversion for our |
2084 | radix again below */ | |
2085 | f_cpy = f; | |
2086 | exp_cpy = exp; | |
2087 | ||
2088 | while (f_cpy < 1.0) | |
f872b822 | 2089 | { |
0b799eea MV |
2090 | f_cpy *= 10.0; |
2091 | if (exp_cpy-- < DBL_MIN_10_EXP) | |
7351e207 MV |
2092 | { |
2093 | a[ch++] = '#'; | |
2094 | a[ch++] = '.'; | |
2095 | a[ch++] = '#'; | |
2096 | return ch; | |
2097 | } | |
f872b822 | 2098 | } |
0b799eea | 2099 | while (f_cpy > 10.0) |
f872b822 | 2100 | { |
0b799eea MV |
2101 | f_cpy *= 0.10; |
2102 | if (exp_cpy++ > DBL_MAX_10_EXP) | |
7351e207 MV |
2103 | { |
2104 | a[ch++] = '#'; | |
2105 | a[ch++] = '.'; | |
2106 | a[ch++] = '#'; | |
2107 | return ch; | |
2108 | } | |
f872b822 | 2109 | } |
0b799eea MV |
2110 | #endif |
2111 | ||
f872b822 MD |
2112 | while (f < 1.0) |
2113 | { | |
0b799eea | 2114 | f *= radix; |
f872b822 MD |
2115 | exp--; |
2116 | } | |
0b799eea | 2117 | while (f > radix) |
f872b822 | 2118 | { |
0b799eea | 2119 | f /= radix; |
f872b822 MD |
2120 | exp++; |
2121 | } | |
0b799eea MV |
2122 | |
2123 | if (f + fx[wp] >= radix) | |
f872b822 MD |
2124 | { |
2125 | f = 1.0; | |
2126 | exp++; | |
2127 | } | |
0f2d19dd | 2128 | zero: |
0b799eea MV |
2129 | #ifdef ENGNOT |
2130 | /* adding 9999 makes this equivalent to abs(x) % 3 */ | |
f872b822 | 2131 | dpt = (exp + 9999) % 3; |
0f2d19dd JB |
2132 | exp -= dpt++; |
2133 | efmt = 1; | |
f872b822 MD |
2134 | #else |
2135 | efmt = (exp < -3) || (exp > wp + 2); | |
0f2d19dd | 2136 | if (!efmt) |
cda139a7 MD |
2137 | { |
2138 | if (exp < 0) | |
2139 | { | |
2140 | a[ch++] = '0'; | |
2141 | a[ch++] = '.'; | |
2142 | dpt = exp; | |
f872b822 MD |
2143 | while (++dpt) |
2144 | a[ch++] = '0'; | |
cda139a7 MD |
2145 | } |
2146 | else | |
f872b822 | 2147 | dpt = exp + 1; |
cda139a7 | 2148 | } |
0f2d19dd JB |
2149 | else |
2150 | dpt = 1; | |
f872b822 MD |
2151 | #endif |
2152 | ||
2153 | do | |
2154 | { | |
2155 | d = f; | |
2156 | f -= d; | |
0b799eea | 2157 | a[ch++] = number_chars[d]; |
f872b822 MD |
2158 | if (f < fx[wp]) |
2159 | break; | |
2160 | if (f + fx[wp] >= 1.0) | |
2161 | { | |
0b799eea | 2162 | a[ch - 1] = number_chars[d+1]; |
f872b822 MD |
2163 | break; |
2164 | } | |
0b799eea | 2165 | f *= radix; |
f872b822 MD |
2166 | if (!(--dpt)) |
2167 | a[ch++] = '.'; | |
0f2d19dd | 2168 | } |
f872b822 | 2169 | while (wp--); |
0f2d19dd JB |
2170 | |
2171 | if (dpt > 0) | |
cda139a7 | 2172 | { |
f872b822 | 2173 | #ifndef ENGNOT |
cda139a7 MD |
2174 | if ((dpt > 4) && (exp > 6)) |
2175 | { | |
f872b822 | 2176 | d = (a[0] == '-' ? 2 : 1); |
cda139a7 | 2177 | for (i = ch++; i > d; i--) |
f872b822 | 2178 | a[i] = a[i - 1]; |
cda139a7 MD |
2179 | a[d] = '.'; |
2180 | efmt = 1; | |
2181 | } | |
2182 | else | |
f872b822 | 2183 | #endif |
cda139a7 | 2184 | { |
f872b822 MD |
2185 | while (--dpt) |
2186 | a[ch++] = '0'; | |
cda139a7 MD |
2187 | a[ch++] = '.'; |
2188 | } | |
2189 | } | |
f872b822 MD |
2190 | if (a[ch - 1] == '.') |
2191 | a[ch++] = '0'; /* trailing zero */ | |
2192 | if (efmt && exp) | |
2193 | { | |
2194 | a[ch++] = 'e'; | |
2195 | if (exp < 0) | |
2196 | { | |
2197 | exp = -exp; | |
2198 | a[ch++] = '-'; | |
2199 | } | |
0b799eea MV |
2200 | for (i = radix; i <= exp; i *= radix); |
2201 | for (i /= radix; i; i /= radix) | |
f872b822 | 2202 | { |
0b799eea | 2203 | a[ch++] = number_chars[exp / i]; |
f872b822 MD |
2204 | exp %= i; |
2205 | } | |
0f2d19dd | 2206 | } |
0f2d19dd JB |
2207 | return ch; |
2208 | } | |
2209 | ||
1be6b49c | 2210 | static size_t |
0b799eea | 2211 | iflo2str (SCM flt, char *str, int radix) |
0f2d19dd | 2212 | { |
1be6b49c | 2213 | size_t i; |
3c9a524f | 2214 | if (SCM_REALP (flt)) |
0b799eea | 2215 | i = idbl2str (SCM_REAL_VALUE (flt), str, radix); |
0f2d19dd | 2216 | else |
f872b822 | 2217 | { |
0b799eea | 2218 | i = idbl2str (SCM_COMPLEX_REAL (flt), str, radix); |
f3ae5d60 MD |
2219 | if (SCM_COMPLEX_IMAG (flt) != 0.0) |
2220 | { | |
7351e207 MV |
2221 | double imag = SCM_COMPLEX_IMAG (flt); |
2222 | /* Don't output a '+' for negative numbers or for Inf and | |
2223 | NaN. They will provide their own sign. */ | |
2224 | if (0 <= imag && !xisinf (imag) && !xisnan (imag)) | |
f3ae5d60 | 2225 | str[i++] = '+'; |
0b799eea | 2226 | i += idbl2str (imag, &str[i], radix); |
f3ae5d60 MD |
2227 | str[i++] = 'i'; |
2228 | } | |
f872b822 | 2229 | } |
0f2d19dd JB |
2230 | return i; |
2231 | } | |
0f2d19dd | 2232 | |
5c11cc9d | 2233 | /* convert a long to a string (unterminated). returns the number of |
1bbd0b84 GB |
2234 | characters in the result. |
2235 | rad is output base | |
2236 | p is destination: worst case (base 2) is SCM_INTBUFLEN */ | |
1be6b49c | 2237 | size_t |
1bbd0b84 | 2238 | scm_iint2str (long num, int rad, char *p) |
0f2d19dd | 2239 | { |
1be6b49c ML |
2240 | size_t j = 1; |
2241 | size_t i; | |
5c11cc9d GH |
2242 | unsigned long n = (num < 0) ? -num : num; |
2243 | ||
f872b822 | 2244 | for (n /= rad; n > 0; n /= rad) |
5c11cc9d GH |
2245 | j++; |
2246 | ||
2247 | i = j; | |
2248 | if (num < 0) | |
f872b822 | 2249 | { |
f872b822 | 2250 | *p++ = '-'; |
5c11cc9d GH |
2251 | j++; |
2252 | n = -num; | |
f872b822 | 2253 | } |
5c11cc9d GH |
2254 | else |
2255 | n = num; | |
f872b822 MD |
2256 | while (i--) |
2257 | { | |
5c11cc9d GH |
2258 | int d = n % rad; |
2259 | ||
f872b822 MD |
2260 | n /= rad; |
2261 | p[i] = d + ((d < 10) ? '0' : 'a' - 10); | |
2262 | } | |
0f2d19dd JB |
2263 | return j; |
2264 | } | |
2265 | ||
a1ec6916 | 2266 | SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0, |
bb628794 DH |
2267 | (SCM n, SCM radix), |
2268 | "Return a string holding the external representation of the\n" | |
942e5b91 MG |
2269 | "number @var{n} in the given @var{radix}. If @var{n} is\n" |
2270 | "inexact, a radix of 10 will be used.") | |
1bbd0b84 | 2271 | #define FUNC_NAME s_scm_number_to_string |
0f2d19dd | 2272 | { |
1bbd0b84 | 2273 | int base; |
98cb6e75 | 2274 | |
0aacf84e | 2275 | if (SCM_UNBNDP (radix)) |
98cb6e75 | 2276 | base = 10; |
0aacf84e | 2277 | else |
5efd3c7d | 2278 | base = scm_to_signed_integer (radix, 2, 36); |
98cb6e75 | 2279 | |
e11e83f3 | 2280 | if (SCM_I_INUMP (n)) |
0aacf84e MD |
2281 | { |
2282 | char num_buf [SCM_INTBUFLEN]; | |
e11e83f3 | 2283 | size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf); |
0aacf84e MD |
2284 | return scm_mem2string (num_buf, length); |
2285 | } | |
2286 | else if (SCM_BIGP (n)) | |
2287 | { | |
2288 | char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n)); | |
2289 | scm_remember_upto_here_1 (n); | |
2290 | return scm_take0str (str); | |
2291 | } | |
f92e85f7 MV |
2292 | else if (SCM_FRACTIONP (n)) |
2293 | { | |
2294 | scm_i_fraction_reduce (n); | |
2295 | return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix), | |
2296 | scm_mem2string ("/", 1), | |
2297 | scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix))); | |
2298 | } | |
0aacf84e MD |
2299 | else if (SCM_INEXACTP (n)) |
2300 | { | |
2301 | char num_buf [FLOBUFLEN]; | |
0b799eea | 2302 | return scm_mem2string (num_buf, iflo2str (n, num_buf, base)); |
0aacf84e MD |
2303 | } |
2304 | else | |
bb628794 | 2305 | SCM_WRONG_TYPE_ARG (1, n); |
0f2d19dd | 2306 | } |
1bbd0b84 | 2307 | #undef FUNC_NAME |
0f2d19dd JB |
2308 | |
2309 | ||
ca46fb90 RB |
2310 | /* These print routines used to be stubbed here so that scm_repl.c |
2311 | wouldn't need SCM_BIGDIG conditionals (pre GMP) */ | |
1cc91f1b | 2312 | |
0f2d19dd | 2313 | int |
e81d98ec | 2314 | scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED) |
0f2d19dd | 2315 | { |
56e55ac7 | 2316 | char num_buf[FLOBUFLEN]; |
0b799eea | 2317 | scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port); |
0f2d19dd JB |
2318 | return !0; |
2319 | } | |
2320 | ||
f3ae5d60 | 2321 | int |
e81d98ec | 2322 | scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED) |
f92e85f7 | 2323 | |
f3ae5d60 | 2324 | { |
56e55ac7 | 2325 | char num_buf[FLOBUFLEN]; |
0b799eea | 2326 | scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port); |
f3ae5d60 MD |
2327 | return !0; |
2328 | } | |
1cc91f1b | 2329 | |
f92e85f7 MV |
2330 | int |
2331 | scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED) | |
2332 | { | |
2333 | SCM str; | |
2334 | scm_i_fraction_reduce (sexp); | |
2335 | str = scm_number_to_string (sexp, SCM_UNDEFINED); | |
2336 | scm_lfwrite (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port); | |
2337 | scm_remember_upto_here_1 (str); | |
2338 | return !0; | |
2339 | } | |
2340 | ||
0f2d19dd | 2341 | int |
e81d98ec | 2342 | scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) |
0f2d19dd | 2343 | { |
ca46fb90 RB |
2344 | char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp)); |
2345 | scm_remember_upto_here_1 (exp); | |
2346 | scm_lfwrite (str, (size_t) strlen (str), port); | |
2347 | free (str); | |
0f2d19dd JB |
2348 | return !0; |
2349 | } | |
2350 | /*** END nums->strs ***/ | |
2351 | ||
3c9a524f | 2352 | |
0f2d19dd | 2353 | /*** STRINGS -> NUMBERS ***/ |
2a8fecee | 2354 | |
3c9a524f DH |
2355 | /* The following functions implement the conversion from strings to numbers. |
2356 | * The implementation somehow follows the grammar for numbers as it is given | |
2357 | * in R5RS. Thus, the functions resemble syntactic units (<ureal R>, | |
2358 | * <uinteger R>, ...) that are used to build up numbers in the grammar. Some | |
2359 | * points should be noted about the implementation: | |
2360 | * * Each function keeps a local index variable 'idx' that points at the | |
2361 | * current position within the parsed string. The global index is only | |
2362 | * updated if the function could parse the corresponding syntactic unit | |
2363 | * successfully. | |
2364 | * * Similarly, the functions keep track of indicators of inexactness ('#', | |
2365 | * '.' or exponents) using local variables ('hash_seen', 'x'). Again, the | |
2366 | * global exactness information is only updated after each part has been | |
2367 | * successfully parsed. | |
2368 | * * Sequences of digits are parsed into temporary variables holding fixnums. | |
2369 | * Only if these fixnums would overflow, the result variables are updated | |
2370 | * using the standard functions scm_add, scm_product, scm_divide etc. Then, | |
2371 | * the temporary variables holding the fixnums are cleared, and the process | |
2372 | * starts over again. If for example fixnums were able to store five decimal | |
2373 | * digits, a number 1234567890 would be parsed in two parts 12345 and 67890, | |
2374 | * and the result was computed as 12345 * 100000 + 67890. In other words, | |
2375 | * only every five digits two bignum operations were performed. | |
2376 | */ | |
2377 | ||
2378 | enum t_exactness {NO_EXACTNESS, INEXACT, EXACT}; | |
2379 | ||
2380 | /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */ | |
2381 | ||
2382 | /* In non ASCII-style encodings the following macro might not work. */ | |
71df73ac KR |
2383 | #define XDIGIT2UINT(d) \ |
2384 | (isdigit ((int) (unsigned char) d) \ | |
2385 | ? (d) - '0' \ | |
2386 | : tolower ((int) (unsigned char) d) - 'a' + 10) | |
3c9a524f | 2387 | |
2a8fecee | 2388 | static SCM |
3c9a524f DH |
2389 | mem2uinteger (const char* mem, size_t len, unsigned int *p_idx, |
2390 | unsigned int radix, enum t_exactness *p_exactness) | |
2a8fecee | 2391 | { |
3c9a524f DH |
2392 | unsigned int idx = *p_idx; |
2393 | unsigned int hash_seen = 0; | |
2394 | scm_t_bits shift = 1; | |
2395 | scm_t_bits add = 0; | |
2396 | unsigned int digit_value; | |
2397 | SCM result; | |
2398 | char c; | |
2399 | ||
2400 | if (idx == len) | |
2401 | return SCM_BOOL_F; | |
2a8fecee | 2402 | |
3c9a524f | 2403 | c = mem[idx]; |
71df73ac | 2404 | if (!isxdigit ((int) (unsigned char) c)) |
3c9a524f DH |
2405 | return SCM_BOOL_F; |
2406 | digit_value = XDIGIT2UINT (c); | |
2407 | if (digit_value >= radix) | |
2408 | return SCM_BOOL_F; | |
2409 | ||
2410 | idx++; | |
d956fa6f | 2411 | result = SCM_I_MAKINUM (digit_value); |
3c9a524f | 2412 | while (idx != len) |
f872b822 | 2413 | { |
3c9a524f | 2414 | char c = mem[idx]; |
71df73ac | 2415 | if (isxdigit ((int) (unsigned char) c)) |
f872b822 | 2416 | { |
3c9a524f | 2417 | if (hash_seen) |
1fe5e088 | 2418 | break; |
3c9a524f DH |
2419 | digit_value = XDIGIT2UINT (c); |
2420 | if (digit_value >= radix) | |
1fe5e088 | 2421 | break; |
f872b822 | 2422 | } |
3c9a524f DH |
2423 | else if (c == '#') |
2424 | { | |
2425 | hash_seen = 1; | |
2426 | digit_value = 0; | |
2427 | } | |
2428 | else | |
2429 | break; | |
2430 | ||
2431 | idx++; | |
2432 | if (SCM_MOST_POSITIVE_FIXNUM / radix < shift) | |
2433 | { | |
d956fa6f | 2434 | result = scm_product (result, SCM_I_MAKINUM (shift)); |
3c9a524f | 2435 | if (add > 0) |
d956fa6f | 2436 | result = scm_sum (result, SCM_I_MAKINUM (add)); |
3c9a524f DH |
2437 | |
2438 | shift = radix; | |
2439 | add = digit_value; | |
2440 | } | |
2441 | else | |
2442 | { | |
2443 | shift = shift * radix; | |
2444 | add = add * radix + digit_value; | |
2445 | } | |
2446 | }; | |
2447 | ||
2448 | if (shift > 1) | |
d956fa6f | 2449 | result = scm_product (result, SCM_I_MAKINUM (shift)); |
3c9a524f | 2450 | if (add > 0) |
d956fa6f | 2451 | result = scm_sum (result, SCM_I_MAKINUM (add)); |
3c9a524f DH |
2452 | |
2453 | *p_idx = idx; | |
2454 | if (hash_seen) | |
2455 | *p_exactness = INEXACT; | |
2456 | ||
2457 | return result; | |
2a8fecee JB |
2458 | } |
2459 | ||
2460 | ||
3c9a524f DH |
2461 | /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only |
2462 | * covers the parts of the rules that start at a potential point. The value | |
2463 | * of the digits up to the point have been parsed by the caller and are given | |
79d34f68 DH |
2464 | * in variable result. The content of *p_exactness indicates, whether a hash |
2465 | * has already been seen in the digits before the point. | |
3c9a524f | 2466 | */ |
1cc91f1b | 2467 | |
3c9a524f DH |
2468 | /* In non ASCII-style encodings the following macro might not work. */ |
2469 | #define DIGIT2UINT(d) ((d) - '0') | |
2470 | ||
2471 | static SCM | |
79d34f68 | 2472 | mem2decimal_from_point (SCM result, const char* mem, size_t len, |
3c9a524f | 2473 | unsigned int *p_idx, enum t_exactness *p_exactness) |
0f2d19dd | 2474 | { |
3c9a524f DH |
2475 | unsigned int idx = *p_idx; |
2476 | enum t_exactness x = *p_exactness; | |
3c9a524f DH |
2477 | |
2478 | if (idx == len) | |
79d34f68 | 2479 | return result; |
3c9a524f DH |
2480 | |
2481 | if (mem[idx] == '.') | |
2482 | { | |
2483 | scm_t_bits shift = 1; | |
2484 | scm_t_bits add = 0; | |
2485 | unsigned int digit_value; | |
d956fa6f | 2486 | SCM big_shift = SCM_I_MAKINUM (1); |
3c9a524f DH |
2487 | |
2488 | idx++; | |
2489 | while (idx != len) | |
2490 | { | |
2491 | char c = mem[idx]; | |
71df73ac | 2492 | if (isdigit ((int) (unsigned char) c)) |
3c9a524f DH |
2493 | { |
2494 | if (x == INEXACT) | |
2495 | return SCM_BOOL_F; | |
2496 | else | |
2497 | digit_value = DIGIT2UINT (c); | |
2498 | } | |
2499 | else if (c == '#') | |
2500 | { | |
2501 | x = INEXACT; | |
2502 | digit_value = 0; | |
2503 | } | |
2504 | else | |
2505 | break; | |
2506 | ||
2507 | idx++; | |
2508 | if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift) | |
2509 | { | |
d956fa6f MV |
2510 | big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift)); |
2511 | result = scm_product (result, SCM_I_MAKINUM (shift)); | |
3c9a524f | 2512 | if (add > 0) |
d956fa6f | 2513 | result = scm_sum (result, SCM_I_MAKINUM (add)); |
3c9a524f DH |
2514 | |
2515 | shift = 10; | |
2516 | add = digit_value; | |
2517 | } | |
2518 | else | |
2519 | { | |
2520 | shift = shift * 10; | |
2521 | add = add * 10 + digit_value; | |
2522 | } | |
2523 | }; | |
2524 | ||
2525 | if (add > 0) | |
2526 | { | |
d956fa6f MV |
2527 | big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift)); |
2528 | result = scm_product (result, SCM_I_MAKINUM (shift)); | |
2529 | result = scm_sum (result, SCM_I_MAKINUM (add)); | |
3c9a524f DH |
2530 | } |
2531 | ||
d8592269 | 2532 | result = scm_divide (result, big_shift); |
79d34f68 | 2533 | |
3c9a524f DH |
2534 | /* We've seen a decimal point, thus the value is implicitly inexact. */ |
2535 | x = INEXACT; | |
f872b822 | 2536 | } |
3c9a524f | 2537 | |
3c9a524f | 2538 | if (idx != len) |
f872b822 | 2539 | { |
3c9a524f DH |
2540 | int sign = 1; |
2541 | unsigned int start; | |
2542 | char c; | |
2543 | int exponent; | |
2544 | SCM e; | |
2545 | ||
2546 | /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */ | |
2547 | ||
2548 | switch (mem[idx]) | |
f872b822 | 2549 | { |
3c9a524f DH |
2550 | case 'd': case 'D': |
2551 | case 'e': case 'E': | |
2552 | case 'f': case 'F': | |
2553 | case 'l': case 'L': | |
2554 | case 's': case 'S': | |
2555 | idx++; | |
2556 | start = idx; | |
2557 | c = mem[idx]; | |
2558 | if (c == '-') | |
2559 | { | |
2560 | idx++; | |
2561 | sign = -1; | |
2562 | c = mem[idx]; | |
2563 | } | |
2564 | else if (c == '+') | |
2565 | { | |
2566 | idx++; | |
2567 | sign = 1; | |
2568 | c = mem[idx]; | |
2569 | } | |
2570 | else | |
2571 | sign = 1; | |
2572 | ||
71df73ac | 2573 | if (!isdigit ((int) (unsigned char) c)) |
3c9a524f DH |
2574 | return SCM_BOOL_F; |
2575 | ||
2576 | idx++; | |
2577 | exponent = DIGIT2UINT (c); | |
2578 | while (idx != len) | |
f872b822 | 2579 | { |
3c9a524f | 2580 | char c = mem[idx]; |
71df73ac | 2581 | if (isdigit ((int) (unsigned char) c)) |
3c9a524f DH |
2582 | { |
2583 | idx++; | |
2584 | if (exponent <= SCM_MAXEXP) | |
2585 | exponent = exponent * 10 + DIGIT2UINT (c); | |
2586 | } | |
2587 | else | |
2588 | break; | |
f872b822 | 2589 | } |
3c9a524f DH |
2590 | |
2591 | if (exponent > SCM_MAXEXP) | |
f872b822 | 2592 | { |
3c9a524f DH |
2593 | size_t exp_len = idx - start; |
2594 | SCM exp_string = scm_mem2string (&mem[start], exp_len); | |
2595 | SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED); | |
2596 | scm_out_of_range ("string->number", exp_num); | |
f872b822 | 2597 | } |
3c9a524f | 2598 | |
d956fa6f | 2599 | e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent)); |
3c9a524f DH |
2600 | if (sign == 1) |
2601 | result = scm_product (result, e); | |
2602 | else | |
f92e85f7 | 2603 | result = scm_divide2real (result, e); |
3c9a524f DH |
2604 | |
2605 | /* We've seen an exponent, thus the value is implicitly inexact. */ | |
2606 | x = INEXACT; | |
2607 | ||
f872b822 | 2608 | break; |
3c9a524f | 2609 | |
f872b822 | 2610 | default: |
3c9a524f | 2611 | break; |
f872b822 | 2612 | } |
0f2d19dd | 2613 | } |
3c9a524f DH |
2614 | |
2615 | *p_idx = idx; | |
2616 | if (x == INEXACT) | |
2617 | *p_exactness = x; | |
2618 | ||
2619 | return result; | |
0f2d19dd | 2620 | } |
0f2d19dd | 2621 | |
3c9a524f DH |
2622 | |
2623 | /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */ | |
2624 | ||
2625 | static SCM | |
2626 | mem2ureal (const char* mem, size_t len, unsigned int *p_idx, | |
2627 | unsigned int radix, enum t_exactness *p_exactness) | |
0f2d19dd | 2628 | { |
3c9a524f | 2629 | unsigned int idx = *p_idx; |
164d2481 | 2630 | SCM result; |
3c9a524f DH |
2631 | |
2632 | if (idx == len) | |
2633 | return SCM_BOOL_F; | |
2634 | ||
7351e207 MV |
2635 | if (idx+5 <= len && !strncmp (mem+idx, "inf.0", 5)) |
2636 | { | |
2637 | *p_idx = idx+5; | |
2638 | return scm_inf (); | |
2639 | } | |
2640 | ||
2641 | if (idx+4 < len && !strncmp (mem+idx, "nan.", 4)) | |
2642 | { | |
2643 | enum t_exactness x = EXACT; | |
2644 | ||
d8592269 MV |
2645 | /* Cobble up the fractional part. We might want to set the |
2646 | NaN's mantissa from it. */ | |
7351e207 MV |
2647 | idx += 4; |
2648 | mem2uinteger (mem, len, &idx, 10, &x); | |
2649 | *p_idx = idx; | |
2650 | return scm_nan (); | |
2651 | } | |
2652 | ||
3c9a524f DH |
2653 | if (mem[idx] == '.') |
2654 | { | |
2655 | if (radix != 10) | |
2656 | return SCM_BOOL_F; | |
2657 | else if (idx + 1 == len) | |
2658 | return SCM_BOOL_F; | |
71df73ac | 2659 | else if (!isdigit ((int) (unsigned char) mem[idx + 1])) |
3c9a524f DH |
2660 | return SCM_BOOL_F; |
2661 | else | |
d956fa6f | 2662 | result = mem2decimal_from_point (SCM_I_MAKINUM (0), mem, len, |
164d2481 | 2663 | p_idx, p_exactness); |
f872b822 | 2664 | } |
3c9a524f DH |
2665 | else |
2666 | { | |
2667 | enum t_exactness x = EXACT; | |
2668 | SCM uinteger; | |
3c9a524f DH |
2669 | |
2670 | uinteger = mem2uinteger (mem, len, &idx, radix, &x); | |
73e4de09 | 2671 | if (scm_is_false (uinteger)) |
3c9a524f DH |
2672 | return SCM_BOOL_F; |
2673 | ||
2674 | if (idx == len) | |
2675 | result = uinteger; | |
2676 | else if (mem[idx] == '/') | |
f872b822 | 2677 | { |
3c9a524f DH |
2678 | SCM divisor; |
2679 | ||
2680 | idx++; | |
2681 | ||
2682 | divisor = mem2uinteger (mem, len, &idx, radix, &x); | |
73e4de09 | 2683 | if (scm_is_false (divisor)) |
3c9a524f DH |
2684 | return SCM_BOOL_F; |
2685 | ||
f92e85f7 | 2686 | /* both are int/big here, I assume */ |
cba42c93 | 2687 | result = scm_i_make_ratio (uinteger, divisor); |
f872b822 | 2688 | } |
3c9a524f DH |
2689 | else if (radix == 10) |
2690 | { | |
2691 | result = mem2decimal_from_point (uinteger, mem, len, &idx, &x); | |
73e4de09 | 2692 | if (scm_is_false (result)) |
3c9a524f DH |
2693 | return SCM_BOOL_F; |
2694 | } | |
2695 | else | |
2696 | result = uinteger; | |
2697 | ||
2698 | *p_idx = idx; | |
2699 | if (x == INEXACT) | |
2700 | *p_exactness = x; | |
f872b822 | 2701 | } |
164d2481 MV |
2702 | |
2703 | /* When returning an inexact zero, make sure it is represented as a | |
2704 | floating point value so that we can change its sign. | |
2705 | */ | |
bc36d050 | 2706 | if (scm_is_eq (result, SCM_I_MAKINUM(0)) && *p_exactness == INEXACT) |
55f26379 | 2707 | result = scm_from_double (0.0); |
164d2481 MV |
2708 | |
2709 | return result; | |
3c9a524f | 2710 | } |
0f2d19dd | 2711 | |
0f2d19dd | 2712 | |
3c9a524f | 2713 | /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */ |
0f2d19dd | 2714 | |
3c9a524f DH |
2715 | static SCM |
2716 | mem2complex (const char* mem, size_t len, unsigned int idx, | |
2717 | unsigned int radix, enum t_exactness *p_exactness) | |
2718 | { | |
2719 | char c; | |
2720 | int sign = 0; | |
2721 | SCM ureal; | |
2722 | ||
2723 | if (idx == len) | |
2724 | return SCM_BOOL_F; | |
2725 | ||
2726 | c = mem[idx]; | |
2727 | if (c == '+') | |
2728 | { | |
2729 | idx++; | |
2730 | sign = 1; | |
2731 | } | |
2732 | else if (c == '-') | |
2733 | { | |
2734 | idx++; | |
2735 | sign = -1; | |
0f2d19dd | 2736 | } |
0f2d19dd | 2737 | |
3c9a524f DH |
2738 | if (idx == len) |
2739 | return SCM_BOOL_F; | |
2740 | ||
2741 | ureal = mem2ureal (mem, len, &idx, radix, p_exactness); | |
73e4de09 | 2742 | if (scm_is_false (ureal)) |
f872b822 | 2743 | { |
3c9a524f DH |
2744 | /* input must be either +i or -i */ |
2745 | ||
2746 | if (sign == 0) | |
2747 | return SCM_BOOL_F; | |
2748 | ||
2749 | if (mem[idx] == 'i' || mem[idx] == 'I') | |
f872b822 | 2750 | { |
3c9a524f DH |
2751 | idx++; |
2752 | if (idx != len) | |
2753 | return SCM_BOOL_F; | |
2754 | ||
d956fa6f | 2755 | return scm_make_rectangular (SCM_I_MAKINUM (0), SCM_I_MAKINUM (sign)); |
f872b822 | 2756 | } |
3c9a524f DH |
2757 | else |
2758 | return SCM_BOOL_F; | |
0f2d19dd | 2759 | } |
3c9a524f DH |
2760 | else |
2761 | { | |
73e4de09 | 2762 | if (sign == -1 && scm_is_false (scm_nan_p (ureal))) |
3c9a524f | 2763 | ureal = scm_difference (ureal, SCM_UNDEFINED); |
f872b822 | 2764 | |
3c9a524f DH |
2765 | if (idx == len) |
2766 | return ureal; | |
2767 | ||
2768 | c = mem[idx]; | |
2769 | switch (c) | |
f872b822 | 2770 | { |
3c9a524f DH |
2771 | case 'i': case 'I': |
2772 | /* either +<ureal>i or -<ureal>i */ | |
2773 | ||
2774 | idx++; | |
2775 | if (sign == 0) | |
2776 | return SCM_BOOL_F; | |
2777 | if (idx != len) | |
2778 | return SCM_BOOL_F; | |
d956fa6f | 2779 | return scm_make_rectangular (SCM_I_MAKINUM (0), ureal); |
3c9a524f DH |
2780 | |
2781 | case '@': | |
2782 | /* polar input: <real>@<real>. */ | |
2783 | ||
2784 | idx++; | |
2785 | if (idx == len) | |
2786 | return SCM_BOOL_F; | |
2787 | else | |
f872b822 | 2788 | { |
3c9a524f DH |
2789 | int sign; |
2790 | SCM angle; | |
2791 | SCM result; | |
2792 | ||
2793 | c = mem[idx]; | |
2794 | if (c == '+') | |
2795 | { | |
2796 | idx++; | |
2797 | sign = 1; | |
2798 | } | |
2799 | else if (c == '-') | |
2800 | { | |
2801 | idx++; | |
2802 | sign = -1; | |
2803 | } | |
2804 | else | |
2805 | sign = 1; | |
2806 | ||
2807 | angle = mem2ureal (mem, len, &idx, radix, p_exactness); | |
73e4de09 | 2808 | if (scm_is_false (angle)) |
3c9a524f DH |
2809 | return SCM_BOOL_F; |
2810 | if (idx != len) | |
2811 | return SCM_BOOL_F; | |
2812 | ||
73e4de09 | 2813 | if (sign == -1 && scm_is_false (scm_nan_p (ureal))) |
3c9a524f DH |
2814 | angle = scm_difference (angle, SCM_UNDEFINED); |
2815 | ||
2816 | result = scm_make_polar (ureal, angle); | |
2817 | return result; | |
f872b822 | 2818 | } |
3c9a524f DH |
2819 | case '+': |
2820 | case '-': | |
2821 | /* expecting input matching <real>[+-]<ureal>?i */ | |
0f2d19dd | 2822 | |
3c9a524f DH |
2823 | idx++; |
2824 | if (idx == len) | |
2825 | return SCM_BOOL_F; | |
2826 | else | |
2827 | { | |
2828 | int sign = (c == '+') ? 1 : -1; | |
2829 | SCM imag = mem2ureal (mem, len, &idx, radix, p_exactness); | |
0f2d19dd | 2830 | |
73e4de09 | 2831 | if (scm_is_false (imag)) |
d956fa6f | 2832 | imag = SCM_I_MAKINUM (sign); |
73e4de09 | 2833 | else if (sign == -1 && scm_is_false (scm_nan_p (ureal))) |
1fe5e088 | 2834 | imag = scm_difference (imag, SCM_UNDEFINED); |
0f2d19dd | 2835 | |
3c9a524f DH |
2836 | if (idx == len) |
2837 | return SCM_BOOL_F; | |
2838 | if (mem[idx] != 'i' && mem[idx] != 'I') | |
2839 | return SCM_BOOL_F; | |
0f2d19dd | 2840 | |
3c9a524f DH |
2841 | idx++; |
2842 | if (idx != len) | |
2843 | return SCM_BOOL_F; | |
0f2d19dd | 2844 | |
1fe5e088 | 2845 | return scm_make_rectangular (ureal, imag); |
3c9a524f DH |
2846 | } |
2847 | default: | |
2848 | return SCM_BOOL_F; | |
2849 | } | |
2850 | } | |
0f2d19dd | 2851 | } |
0f2d19dd JB |
2852 | |
2853 | ||
3c9a524f DH |
2854 | /* R5RS, section 7.1.1, lexical structure of numbers: <number> */ |
2855 | ||
2856 | enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16}; | |
1cc91f1b | 2857 | |
0f2d19dd | 2858 | SCM |
3c9a524f | 2859 | scm_i_mem2number (const char* mem, size_t len, unsigned int default_radix) |
0f2d19dd | 2860 | { |
3c9a524f DH |
2861 | unsigned int idx = 0; |
2862 | unsigned int radix = NO_RADIX; | |
2863 | enum t_exactness forced_x = NO_EXACTNESS; | |
2864 | enum t_exactness implicit_x = EXACT; | |
2865 | SCM result; | |
2866 | ||
2867 | /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */ | |
2868 | while (idx + 2 < len && mem[idx] == '#') | |
2869 | { | |
2870 | switch (mem[idx + 1]) | |
2871 | { | |
2872 | case 'b': case 'B': | |
2873 | if (radix != NO_RADIX) | |
2874 | return SCM_BOOL_F; | |
2875 | radix = DUAL; | |
2876 | break; | |
2877 | case 'd': case 'D': | |
2878 | if (radix != NO_RADIX) | |
2879 | return SCM_BOOL_F; | |
2880 | radix = DEC; | |
2881 | break; | |
2882 | case 'i': case 'I': | |
2883 | if (forced_x != NO_EXACTNESS) | |
2884 | return SCM_BOOL_F; | |
2885 | forced_x = INEXACT; | |
2886 | break; | |
2887 | case 'e': case 'E': | |
2888 | if (forced_x != NO_EXACTNESS) | |
2889 | return SCM_BOOL_F; | |
2890 | forced_x = EXACT; | |
2891 | break; | |
2892 | case 'o': case 'O': | |
2893 | if (radix != NO_RADIX) | |
2894 | return SCM_BOOL_F; | |
2895 | radix = OCT; | |
2896 | break; | |
2897 | case 'x': case 'X': | |
2898 | if (radix != NO_RADIX) | |
2899 | return SCM_BOOL_F; | |
2900 | radix = HEX; | |
2901 | break; | |
2902 | default: | |
f872b822 | 2903 | return SCM_BOOL_F; |
3c9a524f DH |
2904 | } |
2905 | idx += 2; | |
2906 | } | |
2907 | ||
2908 | /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */ | |
2909 | if (radix == NO_RADIX) | |
2910 | result = mem2complex (mem, len, idx, default_radix, &implicit_x); | |
2911 | else | |
2912 | result = mem2complex (mem, len, idx, (unsigned int) radix, &implicit_x); | |
2913 | ||
73e4de09 | 2914 | if (scm_is_false (result)) |
3c9a524f | 2915 | return SCM_BOOL_F; |
f872b822 | 2916 | |
3c9a524f | 2917 | switch (forced_x) |
f872b822 | 2918 | { |
3c9a524f DH |
2919 | case EXACT: |
2920 | if (SCM_INEXACTP (result)) | |
3c9a524f DH |
2921 | return scm_inexact_to_exact (result); |
2922 | else | |
2923 | return result; | |
2924 | case INEXACT: | |
2925 | if (SCM_INEXACTP (result)) | |
2926 | return result; | |
2927 | else | |
2928 | return scm_exact_to_inexact (result); | |
2929 | case NO_EXACTNESS: | |
2930 | default: | |
2931 | if (implicit_x == INEXACT) | |
2932 | { | |
2933 | if (SCM_INEXACTP (result)) | |
2934 | return result; | |
2935 | else | |
2936 | return scm_exact_to_inexact (result); | |
2937 | } | |
2938 | else | |
2939 | return result; | |
f872b822 | 2940 | } |
0f2d19dd JB |
2941 | } |
2942 | ||
2943 | ||
a1ec6916 | 2944 | SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0, |
bb628794 | 2945 | (SCM string, SCM radix), |
1e6808ea | 2946 | "Return a number of the maximally precise representation\n" |
942e5b91 | 2947 | "expressed by the given @var{string}. @var{radix} must be an\n" |
5352393c MG |
2948 | "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n" |
2949 | "is a default radix that may be overridden by an explicit radix\n" | |
2950 | "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n" | |
2951 | "supplied, then the default radix is 10. If string is not a\n" | |
2952 | "syntactically valid notation for a number, then\n" | |
2953 | "@code{string->number} returns @code{#f}.") | |
1bbd0b84 | 2954 | #define FUNC_NAME s_scm_string_to_number |
0f2d19dd JB |
2955 | { |
2956 | SCM answer; | |
5efd3c7d | 2957 | unsigned int base; |
a6d9e5ab | 2958 | SCM_VALIDATE_STRING (1, string); |
5efd3c7d MV |
2959 | |
2960 | if (SCM_UNBNDP (radix)) | |
2961 | base = 10; | |
2962 | else | |
2963 | base = scm_to_unsigned_integer (radix, 2, INT_MAX); | |
2964 | ||
3c9a524f | 2965 | answer = scm_i_mem2number (SCM_STRING_CHARS (string), |
d8592269 MV |
2966 | SCM_STRING_LENGTH (string), |
2967 | base); | |
bb628794 | 2968 | return scm_return_first (answer, string); |
0f2d19dd | 2969 | } |
1bbd0b84 | 2970 | #undef FUNC_NAME |
3c9a524f DH |
2971 | |
2972 | ||
0f2d19dd JB |
2973 | /*** END strs->nums ***/ |
2974 | ||
5986c47d | 2975 | |
0f2d19dd | 2976 | SCM |
1bbd0b84 | 2977 | scm_bigequal (SCM x, SCM y) |
0f2d19dd | 2978 | { |
47ae1f0e | 2979 | int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); |
ca46fb90 | 2980 | scm_remember_upto_here_2 (x, y); |
73e4de09 | 2981 | return scm_from_bool (0 == result); |
0f2d19dd JB |
2982 | } |
2983 | ||
0f2d19dd | 2984 | SCM |
f3ae5d60 | 2985 | scm_real_equalp (SCM x, SCM y) |
0f2d19dd | 2986 | { |
73e4de09 | 2987 | return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y)); |
0f2d19dd JB |
2988 | } |
2989 | ||
f3ae5d60 MD |
2990 | SCM |
2991 | scm_complex_equalp (SCM x, SCM y) | |
2992 | { | |
73e4de09 | 2993 | return scm_from_bool (SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y) |
f3ae5d60 MD |
2994 | && SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)); |
2995 | } | |
0f2d19dd | 2996 | |
f92e85f7 MV |
2997 | SCM |
2998 | scm_i_fraction_equalp (SCM x, SCM y) | |
2999 | { | |
3000 | scm_i_fraction_reduce (x); | |
3001 | scm_i_fraction_reduce (y); | |
73e4de09 | 3002 | if (scm_is_false (scm_equal_p (SCM_FRACTION_NUMERATOR (x), |
02164269 | 3003 | SCM_FRACTION_NUMERATOR (y))) |
73e4de09 | 3004 | || scm_is_false (scm_equal_p (SCM_FRACTION_DENOMINATOR (x), |
02164269 MV |
3005 | SCM_FRACTION_DENOMINATOR (y)))) |
3006 | return SCM_BOOL_F; | |
3007 | else | |
3008 | return SCM_BOOL_T; | |
f92e85f7 | 3009 | } |
0f2d19dd JB |
3010 | |
3011 | ||
8507ec80 MV |
3012 | SCM_DEFINE (scm_number_p, "number?", 1, 0, 0, |
3013 | (SCM x), | |
3014 | "Return @code{#t} if @var{x} is a number, @code{#f}\n" | |
3015 | "otherwise.") | |
3016 | #define FUNC_NAME s_scm_number_p | |
3017 | { | |
3018 | return scm_from_bool (SCM_NUMBERP (x)); | |
3019 | } | |
3020 | #undef FUNC_NAME | |
3021 | ||
3022 | SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0, | |
1bbd0b84 | 3023 | (SCM x), |
942e5b91 | 3024 | "Return @code{#t} if @var{x} is a complex number, @code{#f}\n" |
bb2c02f2 | 3025 | "otherwise. Note that the sets of real, rational and integer\n" |
942e5b91 MG |
3026 | "values form subsets of the set of complex numbers, i. e. the\n" |
3027 | "predicate will also be fulfilled if @var{x} is a real,\n" | |
3028 | "rational or integer number.") | |
8507ec80 | 3029 | #define FUNC_NAME s_scm_complex_p |
0f2d19dd | 3030 | { |
8507ec80 MV |
3031 | /* all numbers are complex. */ |
3032 | return scm_number_p (x); | |
0f2d19dd | 3033 | } |
1bbd0b84 | 3034 | #undef FUNC_NAME |
0f2d19dd | 3035 | |
f92e85f7 MV |
3036 | SCM_DEFINE (scm_real_p, "real?", 1, 0, 0, |
3037 | (SCM x), | |
3038 | "Return @code{#t} if @var{x} is a real number, @code{#f}\n" | |
3039 | "otherwise. Note that the set of integer values forms a subset of\n" | |
3040 | "the set of real numbers, i. e. the predicate will also be\n" | |
3041 | "fulfilled if @var{x} is an integer number.") | |
3042 | #define FUNC_NAME s_scm_real_p | |
3043 | { | |
3044 | /* we can't represent irrational numbers. */ | |
3045 | return scm_rational_p (x); | |
3046 | } | |
3047 | #undef FUNC_NAME | |
3048 | ||
3049 | SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0, | |
1bbd0b84 | 3050 | (SCM x), |
942e5b91 | 3051 | "Return @code{#t} if @var{x} is a rational number, @code{#f}\n" |
bb2c02f2 | 3052 | "otherwise. Note that the set of integer values forms a subset of\n" |
942e5b91 | 3053 | "the set of rational numbers, i. e. the predicate will also be\n" |
f92e85f7 MV |
3054 | "fulfilled if @var{x} is an integer number.") |
3055 | #define FUNC_NAME s_scm_rational_p | |
0f2d19dd | 3056 | { |
e11e83f3 | 3057 | if (SCM_I_INUMP (x)) |
0f2d19dd | 3058 | return SCM_BOOL_T; |
0aacf84e | 3059 | else if (SCM_IMP (x)) |
0f2d19dd | 3060 | return SCM_BOOL_F; |
0aacf84e | 3061 | else if (SCM_BIGP (x)) |
0f2d19dd | 3062 | return SCM_BOOL_T; |
f92e85f7 MV |
3063 | else if (SCM_FRACTIONP (x)) |
3064 | return SCM_BOOL_T; | |
3065 | else if (SCM_REALP (x)) | |
3066 | /* due to their limited precision, all floating point numbers are | |
3067 | rational as well. */ | |
3068 | return SCM_BOOL_T; | |
0aacf84e | 3069 | else |
bb628794 | 3070 | return SCM_BOOL_F; |
0f2d19dd | 3071 | } |
1bbd0b84 | 3072 | #undef FUNC_NAME |
0f2d19dd | 3073 | |
a1ec6916 | 3074 | SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0, |
1bbd0b84 | 3075 | (SCM x), |
942e5b91 MG |
3076 | "Return @code{#t} if @var{x} is an integer number, @code{#f}\n" |
3077 | "else.") | |
1bbd0b84 | 3078 | #define FUNC_NAME s_scm_integer_p |
0f2d19dd JB |
3079 | { |
3080 | double r; | |
e11e83f3 | 3081 | if (SCM_I_INUMP (x)) |
f872b822 MD |
3082 | return SCM_BOOL_T; |
3083 | if (SCM_IMP (x)) | |
3084 | return SCM_BOOL_F; | |
f872b822 MD |
3085 | if (SCM_BIGP (x)) |
3086 | return SCM_BOOL_T; | |
3c9a524f | 3087 | if (!SCM_INEXACTP (x)) |
f872b822 | 3088 | return SCM_BOOL_F; |
3c9a524f | 3089 | if (SCM_COMPLEXP (x)) |
f872b822 | 3090 | return SCM_BOOL_F; |
5986c47d | 3091 | r = SCM_REAL_VALUE (x); |
f872b822 MD |
3092 | if (r == floor (r)) |
3093 | return SCM_BOOL_T; | |
0f2d19dd JB |
3094 | return SCM_BOOL_F; |
3095 | } | |
1bbd0b84 | 3096 | #undef FUNC_NAME |
0f2d19dd JB |
3097 | |
3098 | ||
a1ec6916 | 3099 | SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0, |
1bbd0b84 | 3100 | (SCM x), |
942e5b91 MG |
3101 | "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n" |
3102 | "else.") | |
1bbd0b84 | 3103 | #define FUNC_NAME s_scm_inexact_p |
0f2d19dd | 3104 | { |
eb927cb9 MV |
3105 | if (SCM_INEXACTP (x)) |
3106 | return SCM_BOOL_T; | |
3107 | if (SCM_NUMBERP (x)) | |
3108 | return SCM_BOOL_F; | |
3109 | SCM_WRONG_TYPE_ARG (1, x); | |
0f2d19dd | 3110 | } |
1bbd0b84 | 3111 | #undef FUNC_NAME |
0f2d19dd JB |
3112 | |
3113 | ||
152f82bf | 3114 | SCM_GPROC1 (s_eq_p, "=", scm_tc7_rpsubr, scm_num_eq_p, g_eq_p); |
942e5b91 | 3115 | /* "Return @code{#t} if all parameters are numerically equal." */ |
0f2d19dd | 3116 | SCM |
6e8d25a6 | 3117 | scm_num_eq_p (SCM x, SCM y) |
0f2d19dd | 3118 | { |
d8b95e27 | 3119 | again: |
e11e83f3 | 3120 | if (SCM_I_INUMP (x)) |
0aacf84e | 3121 | { |
e11e83f3 MV |
3122 | long xx = SCM_I_INUM (x); |
3123 | if (SCM_I_INUMP (y)) | |
0aacf84e | 3124 | { |
e11e83f3 | 3125 | long yy = SCM_I_INUM (y); |
73e4de09 | 3126 | return scm_from_bool (xx == yy); |
0aacf84e MD |
3127 | } |
3128 | else if (SCM_BIGP (y)) | |
3129 | return SCM_BOOL_F; | |
3130 | else if (SCM_REALP (y)) | |
73e4de09 | 3131 | return scm_from_bool ((double) xx == SCM_REAL_VALUE (y)); |
0aacf84e | 3132 | else if (SCM_COMPLEXP (y)) |
73e4de09 | 3133 | return scm_from_bool (((double) xx == SCM_COMPLEX_REAL (y)) |
0aacf84e | 3134 | && (0.0 == SCM_COMPLEX_IMAG (y))); |
f92e85f7 MV |
3135 | else if (SCM_FRACTIONP (y)) |
3136 | return SCM_BOOL_F; | |
0aacf84e MD |
3137 | else |
3138 | SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p); | |
f872b822 | 3139 | } |
0aacf84e MD |
3140 | else if (SCM_BIGP (x)) |
3141 | { | |
e11e83f3 | 3142 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
3143 | return SCM_BOOL_F; |
3144 | else if (SCM_BIGP (y)) | |
3145 | { | |
3146 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3147 | scm_remember_upto_here_2 (x, y); | |
73e4de09 | 3148 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
3149 | } |
3150 | else if (SCM_REALP (y)) | |
3151 | { | |
3152 | int cmp; | |
3153 | if (xisnan (SCM_REAL_VALUE (y))) | |
3154 | return SCM_BOOL_F; | |
3155 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y)); | |
3156 | scm_remember_upto_here_1 (x); | |
73e4de09 | 3157 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
3158 | } |
3159 | else if (SCM_COMPLEXP (y)) | |
3160 | { | |
3161 | int cmp; | |
3162 | if (0.0 != SCM_COMPLEX_IMAG (y)) | |
3163 | return SCM_BOOL_F; | |
3164 | if (xisnan (SCM_COMPLEX_REAL (y))) | |
3165 | return SCM_BOOL_F; | |
3166 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y)); | |
3167 | scm_remember_upto_here_1 (x); | |
73e4de09 | 3168 | return scm_from_bool (0 == cmp); |
0aacf84e | 3169 | } |
f92e85f7 MV |
3170 | else if (SCM_FRACTIONP (y)) |
3171 | return SCM_BOOL_F; | |
0aacf84e MD |
3172 | else |
3173 | SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p); | |
f4c627b3 | 3174 | } |
0aacf84e MD |
3175 | else if (SCM_REALP (x)) |
3176 | { | |
e11e83f3 MV |
3177 | if (SCM_I_INUMP (y)) |
3178 | return scm_from_bool (SCM_REAL_VALUE (x) == (double) SCM_I_INUM (y)); | |
0aacf84e MD |
3179 | else if (SCM_BIGP (y)) |
3180 | { | |
3181 | int cmp; | |
3182 | if (xisnan (SCM_REAL_VALUE (x))) | |
3183 | return SCM_BOOL_F; | |
3184 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x)); | |
3185 | scm_remember_upto_here_1 (y); | |
73e4de09 | 3186 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
3187 | } |
3188 | else if (SCM_REALP (y)) | |
73e4de09 | 3189 | return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y)); |
0aacf84e | 3190 | else if (SCM_COMPLEXP (y)) |
73e4de09 | 3191 | return scm_from_bool ((SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y)) |
0aacf84e | 3192 | && (0.0 == SCM_COMPLEX_IMAG (y))); |
f92e85f7 | 3193 | else if (SCM_FRACTIONP (y)) |
d8b95e27 KR |
3194 | { |
3195 | double xx = SCM_REAL_VALUE (x); | |
3196 | if (xisnan (xx)) | |
3197 | return SCM_BOOL_F; | |
3198 | if (xisinf (xx)) | |
73e4de09 | 3199 | return scm_from_bool (xx < 0.0); |
d8b95e27 KR |
3200 | x = scm_inexact_to_exact (x); /* with x as frac or int */ |
3201 | goto again; | |
3202 | } | |
0aacf84e MD |
3203 | else |
3204 | SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p); | |
f872b822 | 3205 | } |
0aacf84e MD |
3206 | else if (SCM_COMPLEXP (x)) |
3207 | { | |
e11e83f3 MV |
3208 | if (SCM_I_INUMP (y)) |
3209 | return scm_from_bool ((SCM_COMPLEX_REAL (x) == (double) SCM_I_INUM (y)) | |
0aacf84e MD |
3210 | && (SCM_COMPLEX_IMAG (x) == 0.0)); |
3211 | else if (SCM_BIGP (y)) | |
3212 | { | |
3213 | int cmp; | |
3214 | if (0.0 != SCM_COMPLEX_IMAG (x)) | |
3215 | return SCM_BOOL_F; | |
3216 | if (xisnan (SCM_COMPLEX_REAL (x))) | |
3217 | return SCM_BOOL_F; | |
3218 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x)); | |
3219 | scm_remember_upto_here_1 (y); | |
73e4de09 | 3220 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
3221 | } |
3222 | else if (SCM_REALP (y)) | |
73e4de09 | 3223 | return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y)) |
0aacf84e MD |
3224 | && (SCM_COMPLEX_IMAG (x) == 0.0)); |
3225 | else if (SCM_COMPLEXP (y)) | |
73e4de09 | 3226 | return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)) |
0aacf84e | 3227 | && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y))); |
f92e85f7 | 3228 | else if (SCM_FRACTIONP (y)) |
d8b95e27 KR |
3229 | { |
3230 | double xx; | |
3231 | if (SCM_COMPLEX_IMAG (x) != 0.0) | |
3232 | return SCM_BOOL_F; | |
3233 | xx = SCM_COMPLEX_REAL (x); | |
3234 | if (xisnan (xx)) | |
3235 | return SCM_BOOL_F; | |
3236 | if (xisinf (xx)) | |
73e4de09 | 3237 | return scm_from_bool (xx < 0.0); |
d8b95e27 KR |
3238 | x = scm_inexact_to_exact (x); /* with x as frac or int */ |
3239 | goto again; | |
3240 | } | |
f92e85f7 MV |
3241 | else |
3242 | SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p); | |
3243 | } | |
3244 | else if (SCM_FRACTIONP (x)) | |
3245 | { | |
e11e83f3 | 3246 | if (SCM_I_INUMP (y)) |
f92e85f7 MV |
3247 | return SCM_BOOL_F; |
3248 | else if (SCM_BIGP (y)) | |
3249 | return SCM_BOOL_F; | |
3250 | else if (SCM_REALP (y)) | |
d8b95e27 KR |
3251 | { |
3252 | double yy = SCM_REAL_VALUE (y); | |
3253 | if (xisnan (yy)) | |
3254 | return SCM_BOOL_F; | |
3255 | if (xisinf (yy)) | |
73e4de09 | 3256 | return scm_from_bool (0.0 < yy); |
d8b95e27 KR |
3257 | y = scm_inexact_to_exact (y); /* with y as frac or int */ |
3258 | goto again; | |
3259 | } | |
f92e85f7 | 3260 | else if (SCM_COMPLEXP (y)) |
d8b95e27 KR |
3261 | { |
3262 | double yy; | |
3263 | if (SCM_COMPLEX_IMAG (y) != 0.0) | |
3264 | return SCM_BOOL_F; | |
3265 | yy = SCM_COMPLEX_REAL (y); | |
3266 | if (xisnan (yy)) | |
3267 | return SCM_BOOL_F; | |
3268 | if (xisinf (yy)) | |
73e4de09 | 3269 | return scm_from_bool (0.0 < yy); |
d8b95e27 KR |
3270 | y = scm_inexact_to_exact (y); /* with y as frac or int */ |
3271 | goto again; | |
3272 | } | |
f92e85f7 MV |
3273 | else if (SCM_FRACTIONP (y)) |
3274 | return scm_i_fraction_equalp (x, y); | |
0aacf84e MD |
3275 | else |
3276 | SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p); | |
f4c627b3 | 3277 | } |
0aacf84e | 3278 | else |
f4c627b3 | 3279 | SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARG1, s_eq_p); |
0f2d19dd JB |
3280 | } |
3281 | ||
3282 | ||
a5f0b599 KR |
3283 | /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications |
3284 | done are good for inums, but for bignums an answer can almost always be | |
3285 | had by just examining a few high bits of the operands, as done by GMP in | |
3286 | mpq_cmp. flonum/frac compares likewise, but with the slight complication | |
3287 | of the float exponent to take into account. */ | |
3288 | ||
152f82bf | 3289 | SCM_GPROC1 (s_less_p, "<", scm_tc7_rpsubr, scm_less_p, g_less_p); |
942e5b91 MG |
3290 | /* "Return @code{#t} if the list of parameters is monotonically\n" |
3291 | * "increasing." | |
3292 | */ | |
0f2d19dd | 3293 | SCM |
6e8d25a6 | 3294 | scm_less_p (SCM x, SCM y) |
0f2d19dd | 3295 | { |
a5f0b599 | 3296 | again: |
e11e83f3 | 3297 | if (SCM_I_INUMP (x)) |
0aacf84e | 3298 | { |
e11e83f3 MV |
3299 | long xx = SCM_I_INUM (x); |
3300 | if (SCM_I_INUMP (y)) | |
0aacf84e | 3301 | { |
e11e83f3 | 3302 | long yy = SCM_I_INUM (y); |
73e4de09 | 3303 | return scm_from_bool (xx < yy); |
0aacf84e MD |
3304 | } |
3305 | else if (SCM_BIGP (y)) | |
3306 | { | |
3307 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
3308 | scm_remember_upto_here_1 (y); | |
73e4de09 | 3309 | return scm_from_bool (sgn > 0); |
0aacf84e MD |
3310 | } |
3311 | else if (SCM_REALP (y)) | |
73e4de09 | 3312 | return scm_from_bool ((double) xx < SCM_REAL_VALUE (y)); |
f92e85f7 | 3313 | else if (SCM_FRACTIONP (y)) |
a5f0b599 KR |
3314 | { |
3315 | /* "x < a/b" becomes "x*b < a" */ | |
3316 | int_frac: | |
3317 | x = scm_product (x, SCM_FRACTION_DENOMINATOR (y)); | |
3318 | y = SCM_FRACTION_NUMERATOR (y); | |
3319 | goto again; | |
3320 | } | |
0aacf84e MD |
3321 | else |
3322 | SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p); | |
f872b822 | 3323 | } |
0aacf84e MD |
3324 | else if (SCM_BIGP (x)) |
3325 | { | |
e11e83f3 | 3326 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
3327 | { |
3328 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
3329 | scm_remember_upto_here_1 (x); | |
73e4de09 | 3330 | return scm_from_bool (sgn < 0); |
0aacf84e MD |
3331 | } |
3332 | else if (SCM_BIGP (y)) | |
3333 | { | |
3334 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3335 | scm_remember_upto_here_2 (x, y); | |
73e4de09 | 3336 | return scm_from_bool (cmp < 0); |
0aacf84e MD |
3337 | } |
3338 | else if (SCM_REALP (y)) | |
3339 | { | |
3340 | int cmp; | |
3341 | if (xisnan (SCM_REAL_VALUE (y))) | |
3342 | return SCM_BOOL_F; | |
3343 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y)); | |
3344 | scm_remember_upto_here_1 (x); | |
73e4de09 | 3345 | return scm_from_bool (cmp < 0); |
0aacf84e | 3346 | } |
f92e85f7 | 3347 | else if (SCM_FRACTIONP (y)) |
a5f0b599 | 3348 | goto int_frac; |
0aacf84e MD |
3349 | else |
3350 | SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p); | |
f4c627b3 | 3351 | } |
0aacf84e MD |
3352 | else if (SCM_REALP (x)) |
3353 | { | |
e11e83f3 MV |
3354 | if (SCM_I_INUMP (y)) |
3355 | return scm_from_bool (SCM_REAL_VALUE (x) < (double) SCM_I_INUM (y)); | |
0aacf84e MD |
3356 | else if (SCM_BIGP (y)) |
3357 | { | |
3358 | int cmp; | |
3359 | if (xisnan (SCM_REAL_VALUE (x))) | |
3360 | return SCM_BOOL_F; | |
3361 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x)); | |
3362 | scm_remember_upto_here_1 (y); | |
73e4de09 | 3363 | return scm_from_bool (cmp > 0); |
0aacf84e MD |
3364 | } |
3365 | else if (SCM_REALP (y)) | |
73e4de09 | 3366 | return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y)); |
f92e85f7 | 3367 | else if (SCM_FRACTIONP (y)) |
a5f0b599 KR |
3368 | { |
3369 | double xx = SCM_REAL_VALUE (x); | |
3370 | if (xisnan (xx)) | |
3371 | return SCM_BOOL_F; | |
3372 | if (xisinf (xx)) | |
73e4de09 | 3373 | return scm_from_bool (xx < 0.0); |
a5f0b599 KR |
3374 | x = scm_inexact_to_exact (x); /* with x as frac or int */ |
3375 | goto again; | |
3376 | } | |
f92e85f7 MV |
3377 | else |
3378 | SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p); | |
3379 | } | |
3380 | else if (SCM_FRACTIONP (x)) | |
3381 | { | |
e11e83f3 | 3382 | if (SCM_I_INUMP (y) || SCM_BIGP (y)) |
a5f0b599 KR |
3383 | { |
3384 | /* "a/b < y" becomes "a < y*b" */ | |
3385 | y = scm_product (y, SCM_FRACTION_DENOMINATOR (x)); | |
3386 | x = SCM_FRACTION_NUMERATOR (x); | |
3387 | goto again; | |
3388 | } | |
f92e85f7 | 3389 | else if (SCM_REALP (y)) |
a5f0b599 KR |
3390 | { |
3391 | double yy = SCM_REAL_VALUE (y); | |
3392 | if (xisnan (yy)) | |
3393 | return SCM_BOOL_F; | |
3394 | if (xisinf (yy)) | |
73e4de09 | 3395 | return scm_from_bool (0.0 < yy); |
a5f0b599 KR |
3396 | y = scm_inexact_to_exact (y); /* with y as frac or int */ |
3397 | goto again; | |
3398 | } | |
f92e85f7 | 3399 | else if (SCM_FRACTIONP (y)) |
a5f0b599 KR |
3400 | { |
3401 | /* "a/b < c/d" becomes "a*d < c*b" */ | |
3402 | SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x), | |
3403 | SCM_FRACTION_DENOMINATOR (y)); | |
3404 | SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y), | |
3405 | SCM_FRACTION_DENOMINATOR (x)); | |
3406 | x = new_x; | |
3407 | y = new_y; | |
3408 | goto again; | |
3409 | } | |
0aacf84e MD |
3410 | else |
3411 | SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p); | |
f872b822 | 3412 | } |
0aacf84e | 3413 | else |
f4c627b3 | 3414 | SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARG1, s_less_p); |
0f2d19dd JB |
3415 | } |
3416 | ||
3417 | ||
c76b1eaf | 3418 | SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p); |
942e5b91 MG |
3419 | /* "Return @code{#t} if the list of parameters is monotonically\n" |
3420 | * "decreasing." | |
c76b1eaf | 3421 | */ |
1bbd0b84 | 3422 | #define FUNC_NAME s_scm_gr_p |
c76b1eaf MD |
3423 | SCM |
3424 | scm_gr_p (SCM x, SCM y) | |
0f2d19dd | 3425 | { |
c76b1eaf MD |
3426 | if (!SCM_NUMBERP (x)) |
3427 | SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG1, FUNC_NAME); | |
3428 | else if (!SCM_NUMBERP (y)) | |
3429 | SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG2, FUNC_NAME); | |
3430 | else | |
3431 | return scm_less_p (y, x); | |
0f2d19dd | 3432 | } |
1bbd0b84 | 3433 | #undef FUNC_NAME |
0f2d19dd JB |
3434 | |
3435 | ||
c76b1eaf | 3436 | SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p); |
942e5b91 | 3437 | /* "Return @code{#t} if the list of parameters is monotonically\n" |
c76b1eaf MD |
3438 | * "non-decreasing." |
3439 | */ | |
1bbd0b84 | 3440 | #define FUNC_NAME s_scm_leq_p |
c76b1eaf MD |
3441 | SCM |
3442 | scm_leq_p (SCM x, SCM y) | |
0f2d19dd | 3443 | { |
c76b1eaf MD |
3444 | if (!SCM_NUMBERP (x)) |
3445 | SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG1, FUNC_NAME); | |
3446 | else if (!SCM_NUMBERP (y)) | |
3447 | SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG2, FUNC_NAME); | |
73e4de09 | 3448 | else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y))) |
fc194577 | 3449 | return SCM_BOOL_F; |
c76b1eaf | 3450 | else |
73e4de09 | 3451 | return scm_not (scm_less_p (y, x)); |
0f2d19dd | 3452 | } |
1bbd0b84 | 3453 | #undef FUNC_NAME |
0f2d19dd JB |
3454 | |
3455 | ||
c76b1eaf | 3456 | SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p); |
942e5b91 | 3457 | /* "Return @code{#t} if the list of parameters is monotonically\n" |
c76b1eaf MD |
3458 | * "non-increasing." |
3459 | */ | |
1bbd0b84 | 3460 | #define FUNC_NAME s_scm_geq_p |
c76b1eaf MD |
3461 | SCM |
3462 | scm_geq_p (SCM x, SCM y) | |
0f2d19dd | 3463 | { |
c76b1eaf MD |
3464 | if (!SCM_NUMBERP (x)) |
3465 | SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG1, FUNC_NAME); | |
3466 | else if (!SCM_NUMBERP (y)) | |
3467 | SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG2, FUNC_NAME); | |
73e4de09 | 3468 | else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y))) |
fc194577 | 3469 | return SCM_BOOL_F; |
c76b1eaf | 3470 | else |
73e4de09 | 3471 | return scm_not (scm_less_p (x, y)); |
0f2d19dd | 3472 | } |
1bbd0b84 | 3473 | #undef FUNC_NAME |
0f2d19dd JB |
3474 | |
3475 | ||
152f82bf | 3476 | SCM_GPROC (s_zero_p, "zero?", 1, 0, 0, scm_zero_p, g_zero_p); |
942e5b91 MG |
3477 | /* "Return @code{#t} if @var{z} is an exact or inexact number equal to\n" |
3478 | * "zero." | |
3479 | */ | |
0f2d19dd | 3480 | SCM |
6e8d25a6 | 3481 | scm_zero_p (SCM z) |
0f2d19dd | 3482 | { |
e11e83f3 | 3483 | if (SCM_I_INUMP (z)) |
bc36d050 | 3484 | return scm_from_bool (scm_is_eq (z, SCM_INUM0)); |
0aacf84e | 3485 | else if (SCM_BIGP (z)) |
c2ff8ab0 | 3486 | return SCM_BOOL_F; |
0aacf84e | 3487 | else if (SCM_REALP (z)) |
73e4de09 | 3488 | return scm_from_bool (SCM_REAL_VALUE (z) == 0.0); |
0aacf84e | 3489 | else if (SCM_COMPLEXP (z)) |
73e4de09 | 3490 | return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0 |
c2ff8ab0 | 3491 | && SCM_COMPLEX_IMAG (z) == 0.0); |
f92e85f7 MV |
3492 | else if (SCM_FRACTIONP (z)) |
3493 | return SCM_BOOL_F; | |
0aacf84e | 3494 | else |
c2ff8ab0 | 3495 | SCM_WTA_DISPATCH_1 (g_zero_p, z, SCM_ARG1, s_zero_p); |
0f2d19dd JB |
3496 | } |
3497 | ||
3498 | ||
152f82bf | 3499 | SCM_GPROC (s_positive_p, "positive?", 1, 0, 0, scm_positive_p, g_positive_p); |
942e5b91 MG |
3500 | /* "Return @code{#t} if @var{x} is an exact or inexact number greater than\n" |
3501 | * "zero." | |
3502 | */ | |
0f2d19dd | 3503 | SCM |
6e8d25a6 | 3504 | scm_positive_p (SCM x) |
0f2d19dd | 3505 | { |
e11e83f3 MV |
3506 | if (SCM_I_INUMP (x)) |
3507 | return scm_from_bool (SCM_I_INUM (x) > 0); | |
0aacf84e MD |
3508 | else if (SCM_BIGP (x)) |
3509 | { | |
3510 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
3511 | scm_remember_upto_here_1 (x); | |
73e4de09 | 3512 | return scm_from_bool (sgn > 0); |
0aacf84e MD |
3513 | } |
3514 | else if (SCM_REALP (x)) | |
73e4de09 | 3515 | return scm_from_bool(SCM_REAL_VALUE (x) > 0.0); |
f92e85f7 MV |
3516 | else if (SCM_FRACTIONP (x)) |
3517 | return scm_positive_p (SCM_FRACTION_NUMERATOR (x)); | |
0aacf84e | 3518 | else |
c2ff8ab0 | 3519 | SCM_WTA_DISPATCH_1 (g_positive_p, x, SCM_ARG1, s_positive_p); |
0f2d19dd JB |
3520 | } |
3521 | ||
3522 | ||
152f82bf | 3523 | SCM_GPROC (s_negative_p, "negative?", 1, 0, 0, scm_negative_p, g_negative_p); |
942e5b91 MG |
3524 | /* "Return @code{#t} if @var{x} is an exact or inexact number less than\n" |
3525 | * "zero." | |
3526 | */ | |
0f2d19dd | 3527 | SCM |
6e8d25a6 | 3528 | scm_negative_p (SCM x) |
0f2d19dd | 3529 | { |
e11e83f3 MV |
3530 | if (SCM_I_INUMP (x)) |
3531 | return scm_from_bool (SCM_I_INUM (x) < 0); | |
0aacf84e MD |
3532 | else if (SCM_BIGP (x)) |
3533 | { | |
3534 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
3535 | scm_remember_upto_here_1 (x); | |
73e4de09 | 3536 | return scm_from_bool (sgn < 0); |
0aacf84e MD |
3537 | } |
3538 | else if (SCM_REALP (x)) | |
73e4de09 | 3539 | return scm_from_bool(SCM_REAL_VALUE (x) < 0.0); |
f92e85f7 MV |
3540 | else if (SCM_FRACTIONP (x)) |
3541 | return scm_negative_p (SCM_FRACTION_NUMERATOR (x)); | |
0aacf84e | 3542 | else |
c2ff8ab0 | 3543 | SCM_WTA_DISPATCH_1 (g_negative_p, x, SCM_ARG1, s_negative_p); |
0f2d19dd JB |
3544 | } |
3545 | ||
3546 | ||
2a06f791 KR |
3547 | /* scm_min and scm_max return an inexact when either argument is inexact, as |
3548 | required by r5rs. On that basis, for exact/inexact combinations the | |
3549 | exact is converted to inexact to compare and possibly return. This is | |
3550 | unlike scm_less_p above which takes some trouble to preserve all bits in | |
3551 | its test, such trouble is not required for min and max. */ | |
3552 | ||
9de33deb | 3553 | SCM_GPROC1 (s_max, "max", scm_tc7_asubr, scm_max, g_max); |
942e5b91 MG |
3554 | /* "Return the maximum of all parameter values." |
3555 | */ | |
0f2d19dd | 3556 | SCM |
6e8d25a6 | 3557 | scm_max (SCM x, SCM y) |
0f2d19dd | 3558 | { |
0aacf84e MD |
3559 | if (SCM_UNBNDP (y)) |
3560 | { | |
3561 | if (SCM_UNBNDP (x)) | |
3562 | SCM_WTA_DISPATCH_0 (g_max, s_max); | |
e11e83f3 | 3563 | else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x)) |
0aacf84e MD |
3564 | return x; |
3565 | else | |
3566 | SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max); | |
f872b822 | 3567 | } |
f4c627b3 | 3568 | |
e11e83f3 | 3569 | if (SCM_I_INUMP (x)) |
0aacf84e | 3570 | { |
e11e83f3 MV |
3571 | long xx = SCM_I_INUM (x); |
3572 | if (SCM_I_INUMP (y)) | |
0aacf84e | 3573 | { |
e11e83f3 | 3574 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
3575 | return (xx < yy) ? y : x; |
3576 | } | |
3577 | else if (SCM_BIGP (y)) | |
3578 | { | |
3579 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
3580 | scm_remember_upto_here_1 (y); | |
3581 | return (sgn < 0) ? x : y; | |
3582 | } | |
3583 | else if (SCM_REALP (y)) | |
3584 | { | |
3585 | double z = xx; | |
3586 | /* if y==NaN then ">" is false and we return NaN */ | |
55f26379 | 3587 | return (z > SCM_REAL_VALUE (y)) ? scm_from_double (z) : y; |
0aacf84e | 3588 | } |
f92e85f7 MV |
3589 | else if (SCM_FRACTIONP (y)) |
3590 | { | |
e4bc5d6c | 3591 | use_less: |
73e4de09 | 3592 | return (scm_is_false (scm_less_p (x, y)) ? x : y); |
f92e85f7 | 3593 | } |
0aacf84e MD |
3594 | else |
3595 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
f872b822 | 3596 | } |
0aacf84e MD |
3597 | else if (SCM_BIGP (x)) |
3598 | { | |
e11e83f3 | 3599 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
3600 | { |
3601 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
3602 | scm_remember_upto_here_1 (x); | |
3603 | return (sgn < 0) ? y : x; | |
3604 | } | |
3605 | else if (SCM_BIGP (y)) | |
3606 | { | |
3607 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3608 | scm_remember_upto_here_2 (x, y); | |
3609 | return (cmp > 0) ? x : y; | |
3610 | } | |
3611 | else if (SCM_REALP (y)) | |
3612 | { | |
2a06f791 KR |
3613 | /* if y==NaN then xx>yy is false, so we return the NaN y */ |
3614 | double xx, yy; | |
3615 | big_real: | |
3616 | xx = scm_i_big2dbl (x); | |
3617 | yy = SCM_REAL_VALUE (y); | |
55f26379 | 3618 | return (xx > yy ? scm_from_double (xx) : y); |
0aacf84e | 3619 | } |
f92e85f7 MV |
3620 | else if (SCM_FRACTIONP (y)) |
3621 | { | |
e4bc5d6c | 3622 | goto use_less; |
f92e85f7 | 3623 | } |
0aacf84e MD |
3624 | else |
3625 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
f4c627b3 | 3626 | } |
0aacf84e MD |
3627 | else if (SCM_REALP (x)) |
3628 | { | |
e11e83f3 | 3629 | if (SCM_I_INUMP (y)) |
0aacf84e | 3630 | { |
e11e83f3 | 3631 | double z = SCM_I_INUM (y); |
0aacf84e | 3632 | /* if x==NaN then "<" is false and we return NaN */ |
55f26379 | 3633 | return (SCM_REAL_VALUE (x) < z) ? scm_from_double (z) : x; |
0aacf84e MD |
3634 | } |
3635 | else if (SCM_BIGP (y)) | |
3636 | { | |
b6f8f763 | 3637 | SCM_SWAP (x, y); |
2a06f791 | 3638 | goto big_real; |
0aacf84e MD |
3639 | } |
3640 | else if (SCM_REALP (y)) | |
3641 | { | |
3642 | /* if x==NaN then our explicit check means we return NaN | |
3643 | if y==NaN then ">" is false and we return NaN | |
3644 | calling isnan is unavoidable, since it's the only way to know | |
3645 | which of x or y causes any compares to be false */ | |
3646 | double xx = SCM_REAL_VALUE (x); | |
3647 | return (xisnan (xx) || xx > SCM_REAL_VALUE (y)) ? x : y; | |
3648 | } | |
f92e85f7 MV |
3649 | else if (SCM_FRACTIONP (y)) |
3650 | { | |
3651 | double yy = scm_i_fraction2double (y); | |
3652 | double xx = SCM_REAL_VALUE (x); | |
55f26379 | 3653 | return (xx < yy) ? scm_from_double (yy) : x; |
f92e85f7 MV |
3654 | } |
3655 | else | |
3656 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
3657 | } | |
3658 | else if (SCM_FRACTIONP (x)) | |
3659 | { | |
e11e83f3 | 3660 | if (SCM_I_INUMP (y)) |
f92e85f7 | 3661 | { |
e4bc5d6c | 3662 | goto use_less; |
f92e85f7 MV |
3663 | } |
3664 | else if (SCM_BIGP (y)) | |
3665 | { | |
e4bc5d6c | 3666 | goto use_less; |
f92e85f7 MV |
3667 | } |
3668 | else if (SCM_REALP (y)) | |
3669 | { | |
3670 | double xx = scm_i_fraction2double (x); | |
55f26379 | 3671 | return (xx < SCM_REAL_VALUE (y)) ? y : scm_from_double (xx); |
f92e85f7 MV |
3672 | } |
3673 | else if (SCM_FRACTIONP (y)) | |
3674 | { | |
e4bc5d6c | 3675 | goto use_less; |
f92e85f7 | 3676 | } |
0aacf84e MD |
3677 | else |
3678 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
f872b822 | 3679 | } |
0aacf84e | 3680 | else |
f4c627b3 | 3681 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max); |
0f2d19dd JB |
3682 | } |
3683 | ||
3684 | ||
9de33deb | 3685 | SCM_GPROC1 (s_min, "min", scm_tc7_asubr, scm_min, g_min); |
942e5b91 MG |
3686 | /* "Return the minium of all parameter values." |
3687 | */ | |
0f2d19dd | 3688 | SCM |
6e8d25a6 | 3689 | scm_min (SCM x, SCM y) |
0f2d19dd | 3690 | { |
0aacf84e MD |
3691 | if (SCM_UNBNDP (y)) |
3692 | { | |
3693 | if (SCM_UNBNDP (x)) | |
3694 | SCM_WTA_DISPATCH_0 (g_min, s_min); | |
e11e83f3 | 3695 | else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x)) |
0aacf84e MD |
3696 | return x; |
3697 | else | |
3698 | SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min); | |
f872b822 | 3699 | } |
f4c627b3 | 3700 | |
e11e83f3 | 3701 | if (SCM_I_INUMP (x)) |
0aacf84e | 3702 | { |
e11e83f3 MV |
3703 | long xx = SCM_I_INUM (x); |
3704 | if (SCM_I_INUMP (y)) | |
0aacf84e | 3705 | { |
e11e83f3 | 3706 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
3707 | return (xx < yy) ? x : y; |
3708 | } | |
3709 | else if (SCM_BIGP (y)) | |
3710 | { | |
3711 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
3712 | scm_remember_upto_here_1 (y); | |
3713 | return (sgn < 0) ? y : x; | |
3714 | } | |
3715 | else if (SCM_REALP (y)) | |
3716 | { | |
3717 | double z = xx; | |
3718 | /* if y==NaN then "<" is false and we return NaN */ | |
55f26379 | 3719 | return (z < SCM_REAL_VALUE (y)) ? scm_from_double (z) : y; |
0aacf84e | 3720 | } |
f92e85f7 MV |
3721 | else if (SCM_FRACTIONP (y)) |
3722 | { | |
e4bc5d6c | 3723 | use_less: |
73e4de09 | 3724 | return (scm_is_false (scm_less_p (x, y)) ? y : x); |
f92e85f7 | 3725 | } |
0aacf84e MD |
3726 | else |
3727 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min); | |
f872b822 | 3728 | } |
0aacf84e MD |
3729 | else if (SCM_BIGP (x)) |
3730 | { | |
e11e83f3 | 3731 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
3732 | { |
3733 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
3734 | scm_remember_upto_here_1 (x); | |
3735 | return (sgn < 0) ? x : y; | |
3736 | } | |
3737 | else if (SCM_BIGP (y)) | |
3738 | { | |
3739 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3740 | scm_remember_upto_here_2 (x, y); | |
3741 | return (cmp > 0) ? y : x; | |
3742 | } | |
3743 | else if (SCM_REALP (y)) | |
3744 | { | |
2a06f791 KR |
3745 | /* if y==NaN then xx<yy is false, so we return the NaN y */ |
3746 | double xx, yy; | |
3747 | big_real: | |
3748 | xx = scm_i_big2dbl (x); | |
3749 | yy = SCM_REAL_VALUE (y); | |
55f26379 | 3750 | return (xx < yy ? scm_from_double (xx) : y); |
0aacf84e | 3751 | } |
f92e85f7 MV |
3752 | else if (SCM_FRACTIONP (y)) |
3753 | { | |
e4bc5d6c | 3754 | goto use_less; |
f92e85f7 | 3755 | } |
0aacf84e MD |
3756 | else |
3757 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min); | |
f4c627b3 | 3758 | } |
0aacf84e MD |
3759 | else if (SCM_REALP (x)) |
3760 | { | |
e11e83f3 | 3761 | if (SCM_I_INUMP (y)) |
0aacf84e | 3762 | { |
e11e83f3 | 3763 | double z = SCM_I_INUM (y); |
0aacf84e | 3764 | /* if x==NaN then "<" is false and we return NaN */ |
55f26379 | 3765 | return (z < SCM_REAL_VALUE (x)) ? scm_from_double (z) : x; |
0aacf84e MD |
3766 | } |
3767 | else if (SCM_BIGP (y)) | |
3768 | { | |
b6f8f763 | 3769 | SCM_SWAP (x, y); |
2a06f791 | 3770 | goto big_real; |
0aacf84e MD |
3771 | } |
3772 | else if (SCM_REALP (y)) | |
3773 | { | |
3774 | /* if x==NaN then our explicit check means we return NaN | |
3775 | if y==NaN then "<" is false and we return NaN | |
3776 | calling isnan is unavoidable, since it's the only way to know | |
3777 | which of x or y causes any compares to be false */ | |
3778 | double xx = SCM_REAL_VALUE (x); | |
3779 | return (xisnan (xx) || xx < SCM_REAL_VALUE (y)) ? x : y; | |
3780 | } | |
f92e85f7 MV |
3781 | else if (SCM_FRACTIONP (y)) |
3782 | { | |
3783 | double yy = scm_i_fraction2double (y); | |
3784 | double xx = SCM_REAL_VALUE (x); | |
55f26379 | 3785 | return (yy < xx) ? scm_from_double (yy) : x; |
f92e85f7 | 3786 | } |
0aacf84e MD |
3787 | else |
3788 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min); | |
f872b822 | 3789 | } |
f92e85f7 MV |
3790 | else if (SCM_FRACTIONP (x)) |
3791 | { | |
e11e83f3 | 3792 | if (SCM_I_INUMP (y)) |
f92e85f7 | 3793 | { |
e4bc5d6c | 3794 | goto use_less; |
f92e85f7 MV |
3795 | } |
3796 | else if (SCM_BIGP (y)) | |
3797 | { | |
e4bc5d6c | 3798 | goto use_less; |
f92e85f7 MV |
3799 | } |
3800 | else if (SCM_REALP (y)) | |
3801 | { | |
3802 | double xx = scm_i_fraction2double (x); | |
55f26379 | 3803 | return (SCM_REAL_VALUE (y) < xx) ? y : scm_from_double (xx); |
f92e85f7 MV |
3804 | } |
3805 | else if (SCM_FRACTIONP (y)) | |
3806 | { | |
e4bc5d6c | 3807 | goto use_less; |
f92e85f7 MV |
3808 | } |
3809 | else | |
3810 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
3811 | } | |
0aacf84e | 3812 | else |
f4c627b3 | 3813 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min); |
0f2d19dd JB |
3814 | } |
3815 | ||
3816 | ||
9de33deb | 3817 | SCM_GPROC1 (s_sum, "+", scm_tc7_asubr, scm_sum, g_sum); |
942e5b91 MG |
3818 | /* "Return the sum of all parameter values. Return 0 if called without\n" |
3819 | * "any parameters." | |
3820 | */ | |
0f2d19dd | 3821 | SCM |
6e8d25a6 | 3822 | scm_sum (SCM x, SCM y) |
0f2d19dd | 3823 | { |
ca46fb90 RB |
3824 | if (SCM_UNBNDP (y)) |
3825 | { | |
3826 | if (SCM_NUMBERP (x)) return x; | |
3827 | if (SCM_UNBNDP (x)) return SCM_INUM0; | |
98cb6e75 | 3828 | SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum); |
f872b822 | 3829 | } |
c209c88e | 3830 | |
e11e83f3 | 3831 | if (SCM_I_INUMP (x)) |
ca46fb90 | 3832 | { |
e11e83f3 | 3833 | if (SCM_I_INUMP (y)) |
ca46fb90 | 3834 | { |
e11e83f3 MV |
3835 | long xx = SCM_I_INUM (x); |
3836 | long yy = SCM_I_INUM (y); | |
ca46fb90 | 3837 | long int z = xx + yy; |
d956fa6f | 3838 | return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_long2big (z); |
ca46fb90 RB |
3839 | } |
3840 | else if (SCM_BIGP (y)) | |
3841 | { | |
3842 | SCM_SWAP (x, y); | |
3843 | goto add_big_inum; | |
3844 | } | |
3845 | else if (SCM_REALP (y)) | |
3846 | { | |
e11e83f3 | 3847 | long int xx = SCM_I_INUM (x); |
55f26379 | 3848 | return scm_from_double (xx + SCM_REAL_VALUE (y)); |
ca46fb90 RB |
3849 | } |
3850 | else if (SCM_COMPLEXP (y)) | |
3851 | { | |
e11e83f3 | 3852 | long int xx = SCM_I_INUM (x); |
8507ec80 | 3853 | return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y), |
ca46fb90 RB |
3854 | SCM_COMPLEX_IMAG (y)); |
3855 | } | |
f92e85f7 | 3856 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 3857 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y), |
f92e85f7 MV |
3858 | scm_product (x, SCM_FRACTION_DENOMINATOR (y))), |
3859 | SCM_FRACTION_DENOMINATOR (y)); | |
ca46fb90 RB |
3860 | else |
3861 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
0aacf84e MD |
3862 | } else if (SCM_BIGP (x)) |
3863 | { | |
e11e83f3 | 3864 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
3865 | { |
3866 | long int inum; | |
3867 | int bigsgn; | |
3868 | add_big_inum: | |
e11e83f3 | 3869 | inum = SCM_I_INUM (y); |
0aacf84e MD |
3870 | if (inum == 0) |
3871 | return x; | |
3872 | bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
3873 | if (inum < 0) | |
3874 | { | |
3875 | SCM result = scm_i_mkbig (); | |
3876 | mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum); | |
3877 | scm_remember_upto_here_1 (x); | |
3878 | /* we know the result will have to be a bignum */ | |
3879 | if (bigsgn == -1) | |
3880 | return result; | |
3881 | return scm_i_normbig (result); | |
3882 | } | |
3883 | else | |
3884 | { | |
3885 | SCM result = scm_i_mkbig (); | |
3886 | mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum); | |
3887 | scm_remember_upto_here_1 (x); | |
3888 | /* we know the result will have to be a bignum */ | |
3889 | if (bigsgn == 1) | |
3890 | return result; | |
3891 | return scm_i_normbig (result); | |
3892 | } | |
3893 | } | |
3894 | else if (SCM_BIGP (y)) | |
3895 | { | |
3896 | SCM result = scm_i_mkbig (); | |
3897 | int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
3898 | int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
3899 | mpz_add (SCM_I_BIG_MPZ (result), | |
3900 | SCM_I_BIG_MPZ (x), | |
3901 | SCM_I_BIG_MPZ (y)); | |
3902 | scm_remember_upto_here_2 (x, y); | |
3903 | /* we know the result will have to be a bignum */ | |
3904 | if (sgn_x == sgn_y) | |
3905 | return result; | |
3906 | return scm_i_normbig (result); | |
3907 | } | |
3908 | else if (SCM_REALP (y)) | |
3909 | { | |
3910 | double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y); | |
3911 | scm_remember_upto_here_1 (x); | |
55f26379 | 3912 | return scm_from_double (result); |
0aacf84e MD |
3913 | } |
3914 | else if (SCM_COMPLEXP (y)) | |
3915 | { | |
3916 | double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x)) | |
3917 | + SCM_COMPLEX_REAL (y)); | |
3918 | scm_remember_upto_here_1 (x); | |
8507ec80 | 3919 | return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y)); |
0aacf84e | 3920 | } |
f92e85f7 | 3921 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 3922 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y), |
f92e85f7 MV |
3923 | scm_product (x, SCM_FRACTION_DENOMINATOR (y))), |
3924 | SCM_FRACTION_DENOMINATOR (y)); | |
0aacf84e MD |
3925 | else |
3926 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
0f2d19dd | 3927 | } |
0aacf84e MD |
3928 | else if (SCM_REALP (x)) |
3929 | { | |
e11e83f3 | 3930 | if (SCM_I_INUMP (y)) |
55f26379 | 3931 | return scm_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y)); |
0aacf84e MD |
3932 | else if (SCM_BIGP (y)) |
3933 | { | |
3934 | double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x); | |
3935 | scm_remember_upto_here_1 (y); | |
55f26379 | 3936 | return scm_from_double (result); |
0aacf84e MD |
3937 | } |
3938 | else if (SCM_REALP (y)) | |
55f26379 | 3939 | return scm_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y)); |
0aacf84e | 3940 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 3941 | return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y), |
0aacf84e | 3942 | SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 3943 | else if (SCM_FRACTIONP (y)) |
55f26379 | 3944 | return scm_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y)); |
0aacf84e MD |
3945 | else |
3946 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
f872b822 | 3947 | } |
0aacf84e MD |
3948 | else if (SCM_COMPLEXP (x)) |
3949 | { | |
e11e83f3 | 3950 | if (SCM_I_INUMP (y)) |
8507ec80 | 3951 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y), |
0aacf84e MD |
3952 | SCM_COMPLEX_IMAG (x)); |
3953 | else if (SCM_BIGP (y)) | |
3954 | { | |
3955 | double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y)) | |
3956 | + SCM_COMPLEX_REAL (x)); | |
3957 | scm_remember_upto_here_1 (y); | |
8507ec80 | 3958 | return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x)); |
0aacf84e MD |
3959 | } |
3960 | else if (SCM_REALP (y)) | |
8507ec80 | 3961 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y), |
0aacf84e MD |
3962 | SCM_COMPLEX_IMAG (x)); |
3963 | else if (SCM_COMPLEXP (y)) | |
8507ec80 | 3964 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y), |
0aacf84e | 3965 | SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 3966 | else if (SCM_FRACTIONP (y)) |
8507ec80 | 3967 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y), |
f92e85f7 MV |
3968 | SCM_COMPLEX_IMAG (x)); |
3969 | else | |
3970 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
3971 | } | |
3972 | else if (SCM_FRACTIONP (x)) | |
3973 | { | |
e11e83f3 | 3974 | if (SCM_I_INUMP (y)) |
cba42c93 | 3975 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
3976 | scm_product (y, SCM_FRACTION_DENOMINATOR (x))), |
3977 | SCM_FRACTION_DENOMINATOR (x)); | |
3978 | else if (SCM_BIGP (y)) | |
cba42c93 | 3979 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
3980 | scm_product (y, SCM_FRACTION_DENOMINATOR (x))), |
3981 | SCM_FRACTION_DENOMINATOR (x)); | |
3982 | else if (SCM_REALP (y)) | |
55f26379 | 3983 | return scm_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x)); |
f92e85f7 | 3984 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 3985 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x), |
f92e85f7 MV |
3986 | SCM_COMPLEX_IMAG (y)); |
3987 | else if (SCM_FRACTIONP (y)) | |
3988 | /* a/b + c/d = (ad + bc) / bd */ | |
cba42c93 | 3989 | return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
3990 | scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))), |
3991 | scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y))); | |
0aacf84e MD |
3992 | else |
3993 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
98cb6e75 | 3994 | } |
0aacf84e | 3995 | else |
98cb6e75 | 3996 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum); |
0f2d19dd JB |
3997 | } |
3998 | ||
3999 | ||
9de33deb | 4000 | SCM_GPROC1 (s_difference, "-", scm_tc7_asubr, scm_difference, g_difference); |
609c3d30 MG |
4001 | /* If called with one argument @var{z1}, -@var{z1} returned. Otherwise |
4002 | * the sum of all but the first argument are subtracted from the first | |
4003 | * argument. */ | |
c05e97b7 | 4004 | #define FUNC_NAME s_difference |
0f2d19dd | 4005 | SCM |
6e8d25a6 | 4006 | scm_difference (SCM x, SCM y) |
0f2d19dd | 4007 | { |
ca46fb90 RB |
4008 | if (SCM_UNBNDP (y)) |
4009 | { | |
4010 | if (SCM_UNBNDP (x)) | |
4011 | SCM_WTA_DISPATCH_0 (g_difference, s_difference); | |
4012 | else | |
e11e83f3 | 4013 | if (SCM_I_INUMP (x)) |
ca46fb90 | 4014 | { |
e11e83f3 | 4015 | long xx = -SCM_I_INUM (x); |
ca46fb90 | 4016 | if (SCM_FIXABLE (xx)) |
d956fa6f | 4017 | return SCM_I_MAKINUM (xx); |
ca46fb90 RB |
4018 | else |
4019 | return scm_i_long2big (xx); | |
4020 | } | |
4021 | else if (SCM_BIGP (x)) | |
4022 | /* FIXME: do we really need to normalize here? */ | |
4023 | return scm_i_normbig (scm_i_clonebig (x, 0)); | |
4024 | else if (SCM_REALP (x)) | |
55f26379 | 4025 | return scm_from_double (-SCM_REAL_VALUE (x)); |
ca46fb90 | 4026 | else if (SCM_COMPLEXP (x)) |
8507ec80 | 4027 | return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x), |
ca46fb90 | 4028 | -SCM_COMPLEX_IMAG (x)); |
f92e85f7 | 4029 | else if (SCM_FRACTIONP (x)) |
cba42c93 | 4030 | return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED), |
f92e85f7 | 4031 | SCM_FRACTION_DENOMINATOR (x)); |
ca46fb90 RB |
4032 | else |
4033 | SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference); | |
f872b822 | 4034 | } |
ca46fb90 | 4035 | |
e11e83f3 | 4036 | if (SCM_I_INUMP (x)) |
0aacf84e | 4037 | { |
e11e83f3 | 4038 | if (SCM_I_INUMP (y)) |
0aacf84e | 4039 | { |
e11e83f3 MV |
4040 | long int xx = SCM_I_INUM (x); |
4041 | long int yy = SCM_I_INUM (y); | |
0aacf84e MD |
4042 | long int z = xx - yy; |
4043 | if (SCM_FIXABLE (z)) | |
d956fa6f | 4044 | return SCM_I_MAKINUM (z); |
0aacf84e MD |
4045 | else |
4046 | return scm_i_long2big (z); | |
4047 | } | |
4048 | else if (SCM_BIGP (y)) | |
4049 | { | |
4050 | /* inum-x - big-y */ | |
e11e83f3 | 4051 | long xx = SCM_I_INUM (x); |
ca46fb90 | 4052 | |
0aacf84e MD |
4053 | if (xx == 0) |
4054 | return scm_i_clonebig (y, 0); | |
4055 | else | |
4056 | { | |
4057 | int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
4058 | SCM result = scm_i_mkbig (); | |
ca46fb90 | 4059 | |
0aacf84e MD |
4060 | if (xx >= 0) |
4061 | mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y)); | |
4062 | else | |
4063 | { | |
4064 | /* x - y == -(y + -x) */ | |
4065 | mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx); | |
4066 | mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result)); | |
4067 | } | |
4068 | scm_remember_upto_here_1 (y); | |
ca46fb90 | 4069 | |
0aacf84e MD |
4070 | if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0)) |
4071 | /* we know the result will have to be a bignum */ | |
4072 | return result; | |
4073 | else | |
4074 | return scm_i_normbig (result); | |
4075 | } | |
4076 | } | |
4077 | else if (SCM_REALP (y)) | |
4078 | { | |
e11e83f3 | 4079 | long int xx = SCM_I_INUM (x); |
55f26379 | 4080 | return scm_from_double (xx - SCM_REAL_VALUE (y)); |
0aacf84e MD |
4081 | } |
4082 | else if (SCM_COMPLEXP (y)) | |
4083 | { | |
e11e83f3 | 4084 | long int xx = SCM_I_INUM (x); |
8507ec80 | 4085 | return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y), |
0aacf84e MD |
4086 | - SCM_COMPLEX_IMAG (y)); |
4087 | } | |
f92e85f7 MV |
4088 | else if (SCM_FRACTIONP (y)) |
4089 | /* a - b/c = (ac - b) / c */ | |
cba42c93 | 4090 | return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
4091 | SCM_FRACTION_NUMERATOR (y)), |
4092 | SCM_FRACTION_DENOMINATOR (y)); | |
0aacf84e MD |
4093 | else |
4094 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
f872b822 | 4095 | } |
0aacf84e MD |
4096 | else if (SCM_BIGP (x)) |
4097 | { | |
e11e83f3 | 4098 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
4099 | { |
4100 | /* big-x - inum-y */ | |
e11e83f3 | 4101 | long yy = SCM_I_INUM (y); |
0aacf84e | 4102 | int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x)); |
ca46fb90 | 4103 | |
0aacf84e MD |
4104 | scm_remember_upto_here_1 (x); |
4105 | if (sgn_x == 0) | |
c71b0706 MV |
4106 | return (SCM_FIXABLE (-yy) ? |
4107 | SCM_I_MAKINUM (-yy) : scm_from_long (-yy)); | |
0aacf84e MD |
4108 | else |
4109 | { | |
4110 | SCM result = scm_i_mkbig (); | |
ca46fb90 | 4111 | |
708f22c6 KR |
4112 | if (yy >= 0) |
4113 | mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy); | |
4114 | else | |
4115 | mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy); | |
0aacf84e | 4116 | scm_remember_upto_here_1 (x); |
ca46fb90 | 4117 | |
0aacf84e MD |
4118 | if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0)) |
4119 | /* we know the result will have to be a bignum */ | |
4120 | return result; | |
4121 | else | |
4122 | return scm_i_normbig (result); | |
4123 | } | |
4124 | } | |
4125 | else if (SCM_BIGP (y)) | |
4126 | { | |
4127 | int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
4128 | int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
4129 | SCM result = scm_i_mkbig (); | |
4130 | mpz_sub (SCM_I_BIG_MPZ (result), | |
4131 | SCM_I_BIG_MPZ (x), | |
4132 | SCM_I_BIG_MPZ (y)); | |
4133 | scm_remember_upto_here_2 (x, y); | |
4134 | /* we know the result will have to be a bignum */ | |
4135 | if ((sgn_x == 1) && (sgn_y == -1)) | |
4136 | return result; | |
4137 | if ((sgn_x == -1) && (sgn_y == 1)) | |
4138 | return result; | |
4139 | return scm_i_normbig (result); | |
4140 | } | |
4141 | else if (SCM_REALP (y)) | |
4142 | { | |
4143 | double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y); | |
4144 | scm_remember_upto_here_1 (x); | |
55f26379 | 4145 | return scm_from_double (result); |
0aacf84e MD |
4146 | } |
4147 | else if (SCM_COMPLEXP (y)) | |
4148 | { | |
4149 | double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x)) | |
4150 | - SCM_COMPLEX_REAL (y)); | |
4151 | scm_remember_upto_here_1 (x); | |
8507ec80 | 4152 | return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y)); |
0aacf84e | 4153 | } |
f92e85f7 | 4154 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 4155 | return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
4156 | SCM_FRACTION_NUMERATOR (y)), |
4157 | SCM_FRACTION_DENOMINATOR (y)); | |
0aacf84e | 4158 | else SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); |
ca46fb90 | 4159 | } |
0aacf84e MD |
4160 | else if (SCM_REALP (x)) |
4161 | { | |
e11e83f3 | 4162 | if (SCM_I_INUMP (y)) |
55f26379 | 4163 | return scm_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y)); |
0aacf84e MD |
4164 | else if (SCM_BIGP (y)) |
4165 | { | |
4166 | double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y)); | |
4167 | scm_remember_upto_here_1 (x); | |
55f26379 | 4168 | return scm_from_double (result); |
0aacf84e MD |
4169 | } |
4170 | else if (SCM_REALP (y)) | |
55f26379 | 4171 | return scm_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y)); |
0aacf84e | 4172 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 4173 | return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y), |
0aacf84e | 4174 | -SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 4175 | else if (SCM_FRACTIONP (y)) |
55f26379 | 4176 | return scm_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y)); |
0aacf84e MD |
4177 | else |
4178 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
98cb6e75 | 4179 | } |
0aacf84e MD |
4180 | else if (SCM_COMPLEXP (x)) |
4181 | { | |
e11e83f3 | 4182 | if (SCM_I_INUMP (y)) |
8507ec80 | 4183 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y), |
0aacf84e MD |
4184 | SCM_COMPLEX_IMAG (x)); |
4185 | else if (SCM_BIGP (y)) | |
4186 | { | |
4187 | double real_part = (SCM_COMPLEX_REAL (x) | |
4188 | - mpz_get_d (SCM_I_BIG_MPZ (y))); | |
4189 | scm_remember_upto_here_1 (x); | |
8507ec80 | 4190 | return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y)); |
0aacf84e MD |
4191 | } |
4192 | else if (SCM_REALP (y)) | |
8507ec80 | 4193 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y), |
0aacf84e MD |
4194 | SCM_COMPLEX_IMAG (x)); |
4195 | else if (SCM_COMPLEXP (y)) | |
8507ec80 | 4196 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y), |
0aacf84e | 4197 | SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 4198 | else if (SCM_FRACTIONP (y)) |
8507ec80 | 4199 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y), |
f92e85f7 MV |
4200 | SCM_COMPLEX_IMAG (x)); |
4201 | else | |
4202 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
4203 | } | |
4204 | else if (SCM_FRACTIONP (x)) | |
4205 | { | |
e11e83f3 | 4206 | if (SCM_I_INUMP (y)) |
f92e85f7 | 4207 | /* a/b - c = (a - cb) / b */ |
cba42c93 | 4208 | return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
4209 | scm_product(y, SCM_FRACTION_DENOMINATOR (x))), |
4210 | SCM_FRACTION_DENOMINATOR (x)); | |
4211 | else if (SCM_BIGP (y)) | |
cba42c93 | 4212 | return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
4213 | scm_product(y, SCM_FRACTION_DENOMINATOR (x))), |
4214 | SCM_FRACTION_DENOMINATOR (x)); | |
4215 | else if (SCM_REALP (y)) | |
55f26379 | 4216 | return scm_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y)); |
f92e85f7 | 4217 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 4218 | return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y), |
f92e85f7 MV |
4219 | -SCM_COMPLEX_IMAG (y)); |
4220 | else if (SCM_FRACTIONP (y)) | |
4221 | /* a/b - c/d = (ad - bc) / bd */ | |
cba42c93 | 4222 | return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
4223 | scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))), |
4224 | scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y))); | |
0aacf84e MD |
4225 | else |
4226 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
98cb6e75 | 4227 | } |
0aacf84e | 4228 | else |
98cb6e75 | 4229 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference); |
0f2d19dd | 4230 | } |
c05e97b7 | 4231 | #undef FUNC_NAME |
0f2d19dd | 4232 | |
ca46fb90 | 4233 | |
9de33deb | 4234 | SCM_GPROC1 (s_product, "*", scm_tc7_asubr, scm_product, g_product); |
942e5b91 MG |
4235 | /* "Return the product of all arguments. If called without arguments,\n" |
4236 | * "1 is returned." | |
4237 | */ | |
0f2d19dd | 4238 | SCM |
6e8d25a6 | 4239 | scm_product (SCM x, SCM y) |
0f2d19dd | 4240 | { |
0aacf84e MD |
4241 | if (SCM_UNBNDP (y)) |
4242 | { | |
4243 | if (SCM_UNBNDP (x)) | |
d956fa6f | 4244 | return SCM_I_MAKINUM (1L); |
0aacf84e MD |
4245 | else if (SCM_NUMBERP (x)) |
4246 | return x; | |
4247 | else | |
4248 | SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product); | |
f872b822 | 4249 | } |
ca46fb90 | 4250 | |
e11e83f3 | 4251 | if (SCM_I_INUMP (x)) |
0aacf84e MD |
4252 | { |
4253 | long xx; | |
f4c627b3 | 4254 | |
0aacf84e | 4255 | intbig: |
e11e83f3 | 4256 | xx = SCM_I_INUM (x); |
f4c627b3 | 4257 | |
0aacf84e MD |
4258 | switch (xx) |
4259 | { | |
ca46fb90 RB |
4260 | case 0: return x; break; |
4261 | case 1: return y; break; | |
0aacf84e | 4262 | } |
f4c627b3 | 4263 | |
e11e83f3 | 4264 | if (SCM_I_INUMP (y)) |
0aacf84e | 4265 | { |
e11e83f3 | 4266 | long yy = SCM_I_INUM (y); |
0aacf84e | 4267 | long kk = xx * yy; |
d956fa6f | 4268 | SCM k = SCM_I_MAKINUM (kk); |
e11e83f3 | 4269 | if ((kk == SCM_I_INUM (k)) && (kk / xx == yy)) |
0aacf84e MD |
4270 | return k; |
4271 | else | |
4272 | { | |
4273 | SCM result = scm_i_long2big (xx); | |
4274 | mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy); | |
4275 | return scm_i_normbig (result); | |
4276 | } | |
4277 | } | |
4278 | else if (SCM_BIGP (y)) | |
4279 | { | |
4280 | SCM result = scm_i_mkbig (); | |
4281 | mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx); | |
4282 | scm_remember_upto_here_1 (y); | |
4283 | return result; | |
4284 | } | |
4285 | else if (SCM_REALP (y)) | |
55f26379 | 4286 | return scm_from_double (xx * SCM_REAL_VALUE (y)); |
0aacf84e | 4287 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 4288 | return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y), |
0aacf84e | 4289 | xx * SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 4290 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 4291 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)), |
f92e85f7 | 4292 | SCM_FRACTION_DENOMINATOR (y)); |
0aacf84e MD |
4293 | else |
4294 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 4295 | } |
0aacf84e MD |
4296 | else if (SCM_BIGP (x)) |
4297 | { | |
e11e83f3 | 4298 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
4299 | { |
4300 | SCM_SWAP (x, y); | |
4301 | goto intbig; | |
4302 | } | |
4303 | else if (SCM_BIGP (y)) | |
4304 | { | |
4305 | SCM result = scm_i_mkbig (); | |
4306 | mpz_mul (SCM_I_BIG_MPZ (result), | |
4307 | SCM_I_BIG_MPZ (x), | |
4308 | SCM_I_BIG_MPZ (y)); | |
4309 | scm_remember_upto_here_2 (x, y); | |
4310 | return result; | |
4311 | } | |
4312 | else if (SCM_REALP (y)) | |
4313 | { | |
4314 | double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y); | |
4315 | scm_remember_upto_here_1 (x); | |
55f26379 | 4316 | return scm_from_double (result); |
0aacf84e MD |
4317 | } |
4318 | else if (SCM_COMPLEXP (y)) | |
4319 | { | |
4320 | double z = mpz_get_d (SCM_I_BIG_MPZ (x)); | |
4321 | scm_remember_upto_here_1 (x); | |
8507ec80 | 4322 | return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y), |
0aacf84e MD |
4323 | z * SCM_COMPLEX_IMAG (y)); |
4324 | } | |
f92e85f7 | 4325 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 4326 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)), |
f92e85f7 | 4327 | SCM_FRACTION_DENOMINATOR (y)); |
0aacf84e MD |
4328 | else |
4329 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 4330 | } |
0aacf84e MD |
4331 | else if (SCM_REALP (x)) |
4332 | { | |
e11e83f3 | 4333 | if (SCM_I_INUMP (y)) |
55f26379 | 4334 | return scm_from_double (SCM_I_INUM (y) * SCM_REAL_VALUE (x)); |
0aacf84e MD |
4335 | else if (SCM_BIGP (y)) |
4336 | { | |
4337 | double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x); | |
4338 | scm_remember_upto_here_1 (y); | |
55f26379 | 4339 | return scm_from_double (result); |
0aacf84e MD |
4340 | } |
4341 | else if (SCM_REALP (y)) | |
55f26379 | 4342 | return scm_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y)); |
0aacf84e | 4343 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 4344 | return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y), |
0aacf84e | 4345 | SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 4346 | else if (SCM_FRACTIONP (y)) |
55f26379 | 4347 | return scm_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y)); |
0aacf84e MD |
4348 | else |
4349 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 4350 | } |
0aacf84e MD |
4351 | else if (SCM_COMPLEXP (x)) |
4352 | { | |
e11e83f3 | 4353 | if (SCM_I_INUMP (y)) |
8507ec80 | 4354 | return scm_c_make_rectangular (SCM_I_INUM (y) * SCM_COMPLEX_REAL (x), |
e11e83f3 | 4355 | SCM_I_INUM (y) * SCM_COMPLEX_IMAG (x)); |
0aacf84e MD |
4356 | else if (SCM_BIGP (y)) |
4357 | { | |
4358 | double z = mpz_get_d (SCM_I_BIG_MPZ (y)); | |
4359 | scm_remember_upto_here_1 (y); | |
8507ec80 | 4360 | return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x), |
76506335 | 4361 | z * SCM_COMPLEX_IMAG (x)); |
0aacf84e MD |
4362 | } |
4363 | else if (SCM_REALP (y)) | |
8507ec80 | 4364 | return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x), |
0aacf84e MD |
4365 | SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x)); |
4366 | else if (SCM_COMPLEXP (y)) | |
4367 | { | |
8507ec80 | 4368 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y) |
0aacf84e MD |
4369 | - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y), |
4370 | SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y) | |
4371 | + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y)); | |
4372 | } | |
f92e85f7 MV |
4373 | else if (SCM_FRACTIONP (y)) |
4374 | { | |
4375 | double yy = scm_i_fraction2double (y); | |
8507ec80 | 4376 | return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x), |
f92e85f7 MV |
4377 | yy * SCM_COMPLEX_IMAG (x)); |
4378 | } | |
4379 | else | |
4380 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
4381 | } | |
4382 | else if (SCM_FRACTIONP (x)) | |
4383 | { | |
e11e83f3 | 4384 | if (SCM_I_INUMP (y)) |
cba42c93 | 4385 | return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)), |
f92e85f7 MV |
4386 | SCM_FRACTION_DENOMINATOR (x)); |
4387 | else if (SCM_BIGP (y)) | |
cba42c93 | 4388 | return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)), |
f92e85f7 MV |
4389 | SCM_FRACTION_DENOMINATOR (x)); |
4390 | else if (SCM_REALP (y)) | |
55f26379 | 4391 | return scm_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y)); |
f92e85f7 MV |
4392 | else if (SCM_COMPLEXP (y)) |
4393 | { | |
4394 | double xx = scm_i_fraction2double (x); | |
8507ec80 | 4395 | return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y), |
f92e85f7 MV |
4396 | xx * SCM_COMPLEX_IMAG (y)); |
4397 | } | |
4398 | else if (SCM_FRACTIONP (y)) | |
4399 | /* a/b * c/d = ac / bd */ | |
cba42c93 | 4400 | return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
4401 | SCM_FRACTION_NUMERATOR (y)), |
4402 | scm_product (SCM_FRACTION_DENOMINATOR (x), | |
4403 | SCM_FRACTION_DENOMINATOR (y))); | |
0aacf84e MD |
4404 | else |
4405 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 4406 | } |
0aacf84e | 4407 | else |
f4c627b3 | 4408 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product); |
0f2d19dd JB |
4409 | } |
4410 | ||
7351e207 MV |
4411 | #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \ |
4412 | || (defined (HAVE_FINITE) && defined (HAVE_ISNAN))) | |
4413 | #define ALLOW_DIVIDE_BY_ZERO | |
4414 | /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */ | |
4415 | #endif | |
0f2d19dd | 4416 | |
ba74ef4e MV |
4417 | /* The code below for complex division is adapted from the GNU |
4418 | libstdc++, which adapted it from f2c's libF77, and is subject to | |
4419 | this copyright: */ | |
4420 | ||
4421 | /**************************************************************** | |
4422 | Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore. | |
4423 | ||
4424 | Permission to use, copy, modify, and distribute this software | |
4425 | and its documentation for any purpose and without fee is hereby | |
4426 | granted, provided that the above copyright notice appear in all | |
4427 | copies and that both that the copyright notice and this | |
4428 | permission notice and warranty disclaimer appear in supporting | |
4429 | documentation, and that the names of AT&T Bell Laboratories or | |
4430 | Bellcore or any of their entities not be used in advertising or | |
4431 | publicity pertaining to distribution of the software without | |
4432 | specific, written prior permission. | |
4433 | ||
4434 | AT&T and Bellcore disclaim all warranties with regard to this | |
4435 | software, including all implied warranties of merchantability | |
4436 | and fitness. In no event shall AT&T or Bellcore be liable for | |
4437 | any special, indirect or consequential damages or any damages | |
4438 | whatsoever resulting from loss of use, data or profits, whether | |
4439 | in an action of contract, negligence or other tortious action, | |
4440 | arising out of or in connection with the use or performance of | |
4441 | this software. | |
4442 | ****************************************************************/ | |
4443 | ||
9de33deb | 4444 | SCM_GPROC1 (s_divide, "/", scm_tc7_asubr, scm_divide, g_divide); |
609c3d30 MG |
4445 | /* Divide the first argument by the product of the remaining |
4446 | arguments. If called with one argument @var{z1}, 1/@var{z1} is | |
4447 | returned. */ | |
c05e97b7 | 4448 | #define FUNC_NAME s_divide |
f92e85f7 MV |
4449 | static SCM |
4450 | scm_i_divide (SCM x, SCM y, int inexact) | |
0f2d19dd | 4451 | { |
f8de44c1 DH |
4452 | double a; |
4453 | ||
0aacf84e MD |
4454 | if (SCM_UNBNDP (y)) |
4455 | { | |
4456 | if (SCM_UNBNDP (x)) | |
4457 | SCM_WTA_DISPATCH_0 (g_divide, s_divide); | |
e11e83f3 | 4458 | else if (SCM_I_INUMP (x)) |
0aacf84e | 4459 | { |
e11e83f3 | 4460 | long xx = SCM_I_INUM (x); |
0aacf84e MD |
4461 | if (xx == 1 || xx == -1) |
4462 | return x; | |
7351e207 | 4463 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e MD |
4464 | else if (xx == 0) |
4465 | scm_num_overflow (s_divide); | |
7351e207 | 4466 | #endif |
0aacf84e | 4467 | else |
f92e85f7 MV |
4468 | { |
4469 | if (inexact) | |
55f26379 | 4470 | return scm_from_double (1.0 / (double) xx); |
cba42c93 | 4471 | else return scm_i_make_ratio (SCM_I_MAKINUM(1), x); |
f92e85f7 | 4472 | } |
0aacf84e MD |
4473 | } |
4474 | else if (SCM_BIGP (x)) | |
f92e85f7 MV |
4475 | { |
4476 | if (inexact) | |
55f26379 | 4477 | return scm_from_double (1.0 / scm_i_big2dbl (x)); |
cba42c93 | 4478 | else return scm_i_make_ratio (SCM_I_MAKINUM(1), x); |
f92e85f7 | 4479 | } |
0aacf84e MD |
4480 | else if (SCM_REALP (x)) |
4481 | { | |
4482 | double xx = SCM_REAL_VALUE (x); | |
7351e207 | 4483 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
4484 | if (xx == 0.0) |
4485 | scm_num_overflow (s_divide); | |
4486 | else | |
7351e207 | 4487 | #endif |
55f26379 | 4488 | return scm_from_double (1.0 / xx); |
0aacf84e MD |
4489 | } |
4490 | else if (SCM_COMPLEXP (x)) | |
4491 | { | |
4492 | double r = SCM_COMPLEX_REAL (x); | |
4493 | double i = SCM_COMPLEX_IMAG (x); | |
4494 | if (r <= i) | |
4495 | { | |
4496 | double t = r / i; | |
4497 | double d = i * (1.0 + t * t); | |
8507ec80 | 4498 | return scm_c_make_rectangular (t / d, -1.0 / d); |
0aacf84e MD |
4499 | } |
4500 | else | |
4501 | { | |
4502 | double t = i / r; | |
4503 | double d = r * (1.0 + t * t); | |
8507ec80 | 4504 | return scm_c_make_rectangular (1.0 / d, -t / d); |
0aacf84e MD |
4505 | } |
4506 | } | |
f92e85f7 | 4507 | else if (SCM_FRACTIONP (x)) |
cba42c93 | 4508 | return scm_i_make_ratio (SCM_FRACTION_DENOMINATOR (x), |
f92e85f7 | 4509 | SCM_FRACTION_NUMERATOR (x)); |
0aacf84e MD |
4510 | else |
4511 | SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide); | |
f8de44c1 | 4512 | } |
f8de44c1 | 4513 | |
e11e83f3 | 4514 | if (SCM_I_INUMP (x)) |
0aacf84e | 4515 | { |
e11e83f3 MV |
4516 | long xx = SCM_I_INUM (x); |
4517 | if (SCM_I_INUMP (y)) | |
0aacf84e | 4518 | { |
e11e83f3 | 4519 | long yy = SCM_I_INUM (y); |
0aacf84e MD |
4520 | if (yy == 0) |
4521 | { | |
7351e207 | 4522 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e | 4523 | scm_num_overflow (s_divide); |
7351e207 | 4524 | #else |
55f26379 | 4525 | return scm_from_double ((double) xx / (double) yy); |
7351e207 | 4526 | #endif |
0aacf84e MD |
4527 | } |
4528 | else if (xx % yy != 0) | |
f92e85f7 MV |
4529 | { |
4530 | if (inexact) | |
55f26379 | 4531 | return scm_from_double ((double) xx / (double) yy); |
cba42c93 | 4532 | else return scm_i_make_ratio (x, y); |
f92e85f7 | 4533 | } |
0aacf84e MD |
4534 | else |
4535 | { | |
4536 | long z = xx / yy; | |
4537 | if (SCM_FIXABLE (z)) | |
d956fa6f | 4538 | return SCM_I_MAKINUM (z); |
0aacf84e MD |
4539 | else |
4540 | return scm_i_long2big (z); | |
4541 | } | |
f872b822 | 4542 | } |
0aacf84e | 4543 | else if (SCM_BIGP (y)) |
f92e85f7 MV |
4544 | { |
4545 | if (inexact) | |
55f26379 | 4546 | return scm_from_double ((double) xx / scm_i_big2dbl (y)); |
cba42c93 | 4547 | else return scm_i_make_ratio (x, y); |
f92e85f7 | 4548 | } |
0aacf84e MD |
4549 | else if (SCM_REALP (y)) |
4550 | { | |
4551 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 4552 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
4553 | if (yy == 0.0) |
4554 | scm_num_overflow (s_divide); | |
4555 | else | |
7351e207 | 4556 | #endif |
55f26379 | 4557 | return scm_from_double ((double) xx / yy); |
ba74ef4e | 4558 | } |
0aacf84e MD |
4559 | else if (SCM_COMPLEXP (y)) |
4560 | { | |
4561 | a = xx; | |
4562 | complex_div: /* y _must_ be a complex number */ | |
4563 | { | |
4564 | double r = SCM_COMPLEX_REAL (y); | |
4565 | double i = SCM_COMPLEX_IMAG (y); | |
4566 | if (r <= i) | |
4567 | { | |
4568 | double t = r / i; | |
4569 | double d = i * (1.0 + t * t); | |
8507ec80 | 4570 | return scm_c_make_rectangular ((a * t) / d, -a / d); |
0aacf84e MD |
4571 | } |
4572 | else | |
4573 | { | |
4574 | double t = i / r; | |
4575 | double d = r * (1.0 + t * t); | |
8507ec80 | 4576 | return scm_c_make_rectangular (a / d, -(a * t) / d); |
0aacf84e MD |
4577 | } |
4578 | } | |
4579 | } | |
f92e85f7 MV |
4580 | else if (SCM_FRACTIONP (y)) |
4581 | /* a / b/c = ac / b */ | |
cba42c93 | 4582 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 | 4583 | SCM_FRACTION_NUMERATOR (y)); |
0aacf84e MD |
4584 | else |
4585 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f8de44c1 | 4586 | } |
0aacf84e MD |
4587 | else if (SCM_BIGP (x)) |
4588 | { | |
e11e83f3 | 4589 | if (SCM_I_INUMP (y)) |
0aacf84e | 4590 | { |
e11e83f3 | 4591 | long int yy = SCM_I_INUM (y); |
0aacf84e MD |
4592 | if (yy == 0) |
4593 | { | |
7351e207 | 4594 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e | 4595 | scm_num_overflow (s_divide); |
7351e207 | 4596 | #else |
0aacf84e MD |
4597 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); |
4598 | scm_remember_upto_here_1 (x); | |
4599 | return (sgn == 0) ? scm_nan () : scm_inf (); | |
7351e207 | 4600 | #endif |
0aacf84e MD |
4601 | } |
4602 | else if (yy == 1) | |
4603 | return x; | |
4604 | else | |
4605 | { | |
4606 | /* FIXME: HMM, what are the relative performance issues here? | |
4607 | We need to test. Is it faster on average to test | |
4608 | divisible_p, then perform whichever operation, or is it | |
4609 | faster to perform the integer div opportunistically and | |
4610 | switch to real if there's a remainder? For now we take the | |
4611 | middle ground: test, then if divisible, use the faster div | |
4612 | func. */ | |
4613 | ||
4614 | long abs_yy = yy < 0 ? -yy : yy; | |
4615 | int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy); | |
4616 | ||
4617 | if (divisible_p) | |
4618 | { | |
4619 | SCM result = scm_i_mkbig (); | |
4620 | mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy); | |
4621 | scm_remember_upto_here_1 (x); | |
4622 | if (yy < 0) | |
4623 | mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result)); | |
4624 | return scm_i_normbig (result); | |
4625 | } | |
4626 | else | |
f92e85f7 MV |
4627 | { |
4628 | if (inexact) | |
55f26379 | 4629 | return scm_from_double (scm_i_big2dbl (x) / (double) yy); |
cba42c93 | 4630 | else return scm_i_make_ratio (x, y); |
f92e85f7 | 4631 | } |
0aacf84e MD |
4632 | } |
4633 | } | |
4634 | else if (SCM_BIGP (y)) | |
4635 | { | |
4636 | int y_is_zero = (mpz_sgn (SCM_I_BIG_MPZ (y)) == 0); | |
4637 | if (y_is_zero) | |
4638 | { | |
ca46fb90 | 4639 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e | 4640 | scm_num_overflow (s_divide); |
f872b822 | 4641 | #else |
0aacf84e MD |
4642 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); |
4643 | scm_remember_upto_here_1 (x); | |
4644 | return (sgn == 0) ? scm_nan () : scm_inf (); | |
f872b822 | 4645 | #endif |
0aacf84e MD |
4646 | } |
4647 | else | |
4648 | { | |
4649 | /* big_x / big_y */ | |
4650 | int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x), | |
4651 | SCM_I_BIG_MPZ (y)); | |
4652 | if (divisible_p) | |
4653 | { | |
4654 | SCM result = scm_i_mkbig (); | |
4655 | mpz_divexact (SCM_I_BIG_MPZ (result), | |
4656 | SCM_I_BIG_MPZ (x), | |
4657 | SCM_I_BIG_MPZ (y)); | |
4658 | scm_remember_upto_here_2 (x, y); | |
4659 | return scm_i_normbig (result); | |
4660 | } | |
4661 | else | |
4662 | { | |
f92e85f7 MV |
4663 | if (inexact) |
4664 | { | |
4665 | double dbx = mpz_get_d (SCM_I_BIG_MPZ (x)); | |
4666 | double dby = mpz_get_d (SCM_I_BIG_MPZ (y)); | |
4667 | scm_remember_upto_here_2 (x, y); | |
55f26379 | 4668 | return scm_from_double (dbx / dby); |
f92e85f7 | 4669 | } |
cba42c93 | 4670 | else return scm_i_make_ratio (x, y); |
0aacf84e MD |
4671 | } |
4672 | } | |
4673 | } | |
4674 | else if (SCM_REALP (y)) | |
4675 | { | |
4676 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 4677 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
4678 | if (yy == 0.0) |
4679 | scm_num_overflow (s_divide); | |
4680 | else | |
7351e207 | 4681 | #endif |
55f26379 | 4682 | return scm_from_double (scm_i_big2dbl (x) / yy); |
0aacf84e MD |
4683 | } |
4684 | else if (SCM_COMPLEXP (y)) | |
4685 | { | |
4686 | a = scm_i_big2dbl (x); | |
4687 | goto complex_div; | |
4688 | } | |
f92e85f7 | 4689 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 4690 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 | 4691 | SCM_FRACTION_NUMERATOR (y)); |
0aacf84e MD |
4692 | else |
4693 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f872b822 | 4694 | } |
0aacf84e MD |
4695 | else if (SCM_REALP (x)) |
4696 | { | |
4697 | double rx = SCM_REAL_VALUE (x); | |
e11e83f3 | 4698 | if (SCM_I_INUMP (y)) |
0aacf84e | 4699 | { |
e11e83f3 | 4700 | long int yy = SCM_I_INUM (y); |
7351e207 | 4701 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e MD |
4702 | if (yy == 0) |
4703 | scm_num_overflow (s_divide); | |
4704 | else | |
7351e207 | 4705 | #endif |
55f26379 | 4706 | return scm_from_double (rx / (double) yy); |
0aacf84e MD |
4707 | } |
4708 | else if (SCM_BIGP (y)) | |
4709 | { | |
4710 | double dby = mpz_get_d (SCM_I_BIG_MPZ (y)); | |
4711 | scm_remember_upto_here_1 (y); | |
55f26379 | 4712 | return scm_from_double (rx / dby); |
0aacf84e MD |
4713 | } |
4714 | else if (SCM_REALP (y)) | |
4715 | { | |
4716 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 4717 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
4718 | if (yy == 0.0) |
4719 | scm_num_overflow (s_divide); | |
4720 | else | |
7351e207 | 4721 | #endif |
55f26379 | 4722 | return scm_from_double (rx / yy); |
0aacf84e MD |
4723 | } |
4724 | else if (SCM_COMPLEXP (y)) | |
4725 | { | |
4726 | a = rx; | |
4727 | goto complex_div; | |
4728 | } | |
f92e85f7 | 4729 | else if (SCM_FRACTIONP (y)) |
55f26379 | 4730 | return scm_from_double (rx / scm_i_fraction2double (y)); |
0aacf84e MD |
4731 | else |
4732 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f872b822 | 4733 | } |
0aacf84e MD |
4734 | else if (SCM_COMPLEXP (x)) |
4735 | { | |
4736 | double rx = SCM_COMPLEX_REAL (x); | |
4737 | double ix = SCM_COMPLEX_IMAG (x); | |
e11e83f3 | 4738 | if (SCM_I_INUMP (y)) |
0aacf84e | 4739 | { |
e11e83f3 | 4740 | long int yy = SCM_I_INUM (y); |
7351e207 | 4741 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e MD |
4742 | if (yy == 0) |
4743 | scm_num_overflow (s_divide); | |
4744 | else | |
7351e207 | 4745 | #endif |
0aacf84e MD |
4746 | { |
4747 | double d = yy; | |
8507ec80 | 4748 | return scm_c_make_rectangular (rx / d, ix / d); |
0aacf84e MD |
4749 | } |
4750 | } | |
4751 | else if (SCM_BIGP (y)) | |
4752 | { | |
4753 | double dby = mpz_get_d (SCM_I_BIG_MPZ (y)); | |
4754 | scm_remember_upto_here_1 (y); | |
8507ec80 | 4755 | return scm_c_make_rectangular (rx / dby, ix / dby); |
0aacf84e MD |
4756 | } |
4757 | else if (SCM_REALP (y)) | |
4758 | { | |
4759 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 4760 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
4761 | if (yy == 0.0) |
4762 | scm_num_overflow (s_divide); | |
4763 | else | |
7351e207 | 4764 | #endif |
8507ec80 | 4765 | return scm_c_make_rectangular (rx / yy, ix / yy); |
0aacf84e MD |
4766 | } |
4767 | else if (SCM_COMPLEXP (y)) | |
4768 | { | |
4769 | double ry = SCM_COMPLEX_REAL (y); | |
4770 | double iy = SCM_COMPLEX_IMAG (y); | |
4771 | if (ry <= iy) | |
4772 | { | |
4773 | double t = ry / iy; | |
4774 | double d = iy * (1.0 + t * t); | |
8507ec80 | 4775 | return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d); |
0aacf84e MD |
4776 | } |
4777 | else | |
4778 | { | |
4779 | double t = iy / ry; | |
4780 | double d = ry * (1.0 + t * t); | |
8507ec80 | 4781 | return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d); |
0aacf84e MD |
4782 | } |
4783 | } | |
f92e85f7 MV |
4784 | else if (SCM_FRACTIONP (y)) |
4785 | { | |
4786 | double yy = scm_i_fraction2double (y); | |
8507ec80 | 4787 | return scm_c_make_rectangular (rx / yy, ix / yy); |
f92e85f7 | 4788 | } |
0aacf84e MD |
4789 | else |
4790 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f8de44c1 | 4791 | } |
f92e85f7 MV |
4792 | else if (SCM_FRACTIONP (x)) |
4793 | { | |
e11e83f3 | 4794 | if (SCM_I_INUMP (y)) |
f92e85f7 | 4795 | { |
e11e83f3 | 4796 | long int yy = SCM_I_INUM (y); |
f92e85f7 MV |
4797 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
4798 | if (yy == 0) | |
4799 | scm_num_overflow (s_divide); | |
4800 | else | |
4801 | #endif | |
cba42c93 | 4802 | return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
4803 | scm_product (SCM_FRACTION_DENOMINATOR (x), y)); |
4804 | } | |
4805 | else if (SCM_BIGP (y)) | |
4806 | { | |
cba42c93 | 4807 | return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
4808 | scm_product (SCM_FRACTION_DENOMINATOR (x), y)); |
4809 | } | |
4810 | else if (SCM_REALP (y)) | |
4811 | { | |
4812 | double yy = SCM_REAL_VALUE (y); | |
4813 | #ifndef ALLOW_DIVIDE_BY_ZERO | |
4814 | if (yy == 0.0) | |
4815 | scm_num_overflow (s_divide); | |
4816 | else | |
4817 | #endif | |
55f26379 | 4818 | return scm_from_double (scm_i_fraction2double (x) / yy); |
f92e85f7 MV |
4819 | } |
4820 | else if (SCM_COMPLEXP (y)) | |
4821 | { | |
4822 | a = scm_i_fraction2double (x); | |
4823 | goto complex_div; | |
4824 | } | |
4825 | else if (SCM_FRACTIONP (y)) | |
cba42c93 | 4826 | return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
4827 | scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))); |
4828 | else | |
4829 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
4830 | } | |
0aacf84e | 4831 | else |
f8de44c1 | 4832 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide); |
0f2d19dd | 4833 | } |
f92e85f7 MV |
4834 | |
4835 | SCM | |
4836 | scm_divide (SCM x, SCM y) | |
4837 | { | |
4838 | return scm_i_divide (x, y, 0); | |
4839 | } | |
4840 | ||
4841 | static SCM scm_divide2real (SCM x, SCM y) | |
4842 | { | |
4843 | return scm_i_divide (x, y, 1); | |
4844 | } | |
c05e97b7 | 4845 | #undef FUNC_NAME |
0f2d19dd | 4846 | |
fa605590 | 4847 | |
0f2d19dd | 4848 | double |
6e8d25a6 | 4849 | scm_asinh (double x) |
0f2d19dd | 4850 | { |
fa605590 KR |
4851 | #if HAVE_ASINH |
4852 | return asinh (x); | |
4853 | #else | |
4854 | #define asinh scm_asinh | |
f872b822 | 4855 | return log (x + sqrt (x * x + 1)); |
fa605590 | 4856 | #endif |
0f2d19dd | 4857 | } |
fa605590 KR |
4858 | SCM_GPROC1 (s_asinh, "$asinh", scm_tc7_dsubr, (SCM (*)()) asinh, g_asinh); |
4859 | /* "Return the inverse hyperbolic sine of @var{x}." | |
4860 | */ | |
0f2d19dd JB |
4861 | |
4862 | ||
0f2d19dd | 4863 | double |
6e8d25a6 | 4864 | scm_acosh (double x) |
0f2d19dd | 4865 | { |
fa605590 KR |
4866 | #if HAVE_ACOSH |
4867 | return acosh (x); | |
4868 | #else | |
4869 | #define acosh scm_acosh | |
f872b822 | 4870 | return log (x + sqrt (x * x - 1)); |
fa605590 | 4871 | #endif |
0f2d19dd | 4872 | } |
fa605590 KR |
4873 | SCM_GPROC1 (s_acosh, "$acosh", scm_tc7_dsubr, (SCM (*)()) acosh, g_acosh); |
4874 | /* "Return the inverse hyperbolic cosine of @var{x}." | |
4875 | */ | |
0f2d19dd JB |
4876 | |
4877 | ||
0f2d19dd | 4878 | double |
6e8d25a6 | 4879 | scm_atanh (double x) |
0f2d19dd | 4880 | { |
fa605590 KR |
4881 | #if HAVE_ATANH |
4882 | return atanh (x); | |
4883 | #else | |
4884 | #define atanh scm_atanh | |
f872b822 | 4885 | return 0.5 * log ((1 + x) / (1 - x)); |
fa605590 | 4886 | #endif |
0f2d19dd | 4887 | } |
fa605590 KR |
4888 | SCM_GPROC1 (s_atanh, "$atanh", scm_tc7_dsubr, (SCM (*)()) atanh, g_atanh); |
4889 | /* "Return the inverse hyperbolic tangent of @var{x}." | |
4890 | */ | |
0f2d19dd JB |
4891 | |
4892 | ||
f92e85f7 MV |
4893 | /* XXX - eventually, we should remove this definition of scm_round and |
4894 | rename scm_round_number to scm_round. Likewise for scm_truncate | |
4895 | and scm_truncate_number. | |
4896 | */ | |
4897 | ||
0f2d19dd | 4898 | double |
6e8d25a6 | 4899 | scm_truncate (double x) |
0f2d19dd | 4900 | { |
fa605590 KR |
4901 | #if HAVE_TRUNC |
4902 | return trunc (x); | |
4903 | #else | |
f872b822 MD |
4904 | if (x < 0.0) |
4905 | return -floor (-x); | |
4906 | return floor (x); | |
fa605590 | 4907 | #endif |
0f2d19dd | 4908 | } |
0f2d19dd | 4909 | |
6187f48b KR |
4910 | /* scm_round is done using floor(x+0.5) to round to nearest and with |
4911 | half-way case (ie. when x is an integer plus 0.5) going upwards. Then | |
4912 | half-way cases are identified and adjusted down if the round-upwards | |
4913 | didn't give the desired even integer. | |
4914 | ||
4915 | "plus_half == result" identifies a half-way case. If plus_half, which is | |
4916 | x + 0.5, is an integer then x must be an integer plus 0.5. | |
4917 | ||
4918 | An odd "result" value is identified with result/2 != floor(result/2). | |
4919 | This is done with plus_half, since that value is ready for use sooner in | |
4920 | a pipelined cpu, and we're already requiring plus_half == result. | |
4921 | ||
4922 | Note however that we need to be careful when x is big and already an | |
4923 | integer. In that case "x+0.5" may round to an adjacent integer, causing | |
4924 | us to return such a value, incorrectly. For instance if the hardware is | |
4925 | in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF | |
4926 | (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value | |
4927 | returned. Or if the hardware is in round-upwards mode, then other bigger | |
4928 | values like say x == 2^128 will see x+0.5 rounding up to the next higher | |
4929 | representable value, 2^128+2^76 (or whatever), again incorrect. | |
4930 | ||
4931 | These bad roundings of x+0.5 are avoided by testing at the start whether | |
4932 | x is already an integer. If it is then clearly that's the desired result | |
4933 | already. And if it's not then the exponent must be small enough to allow | |
4934 | an 0.5 to be represented, and hence added without a bad rounding. */ | |
4935 | ||
0f2d19dd | 4936 | double |
6e8d25a6 | 4937 | scm_round (double x) |
0f2d19dd | 4938 | { |
6187f48b KR |
4939 | double plus_half, result; |
4940 | ||
4941 | if (x == floor (x)) | |
4942 | return x; | |
4943 | ||
4944 | plus_half = x + 0.5; | |
4945 | result = floor (plus_half); | |
0f2d19dd | 4946 | /* Adjust so that the scm_round is towards even. */ |
0aacf84e MD |
4947 | return ((plus_half == result && plus_half / 2 != floor (plus_half / 2)) |
4948 | ? result - 1 | |
4949 | : result); | |
0f2d19dd JB |
4950 | } |
4951 | ||
f92e85f7 MV |
4952 | SCM_DEFINE (scm_truncate_number, "truncate", 1, 0, 0, |
4953 | (SCM x), | |
4954 | "Round the number @var{x} towards zero.") | |
4955 | #define FUNC_NAME s_scm_truncate_number | |
4956 | { | |
73e4de09 | 4957 | if (scm_is_false (scm_negative_p (x))) |
f92e85f7 MV |
4958 | return scm_floor (x); |
4959 | else | |
4960 | return scm_ceiling (x); | |
4961 | } | |
4962 | #undef FUNC_NAME | |
4963 | ||
4964 | static SCM exactly_one_half; | |
4965 | ||
4966 | SCM_DEFINE (scm_round_number, "round", 1, 0, 0, | |
4967 | (SCM x), | |
4968 | "Round the number @var{x} towards the nearest integer. " | |
4969 | "When it is exactly halfway between two integers, " | |
4970 | "round towards the even one.") | |
4971 | #define FUNC_NAME s_scm_round_number | |
4972 | { | |
e11e83f3 | 4973 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
bae30667 KR |
4974 | return x; |
4975 | else if (SCM_REALP (x)) | |
55f26379 | 4976 | return scm_from_double (scm_round (SCM_REAL_VALUE (x))); |
f92e85f7 | 4977 | else |
bae30667 KR |
4978 | { |
4979 | /* OPTIMIZE-ME: Fraction case could be done more efficiently by a | |
4980 | single quotient+remainder division then examining to see which way | |
4981 | the rounding should go. */ | |
4982 | SCM plus_half = scm_sum (x, exactly_one_half); | |
4983 | SCM result = scm_floor (plus_half); | |
4984 | /* Adjust so that the scm_round is towards even. */ | |
73e4de09 MV |
4985 | if (scm_is_true (scm_num_eq_p (plus_half, result)) |
4986 | && scm_is_true (scm_odd_p (result))) | |
d956fa6f | 4987 | return scm_difference (result, SCM_I_MAKINUM (1)); |
bae30667 KR |
4988 | else |
4989 | return result; | |
4990 | } | |
f92e85f7 MV |
4991 | } |
4992 | #undef FUNC_NAME | |
4993 | ||
4994 | SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0, | |
4995 | (SCM x), | |
4996 | "Round the number @var{x} towards minus infinity.") | |
4997 | #define FUNC_NAME s_scm_floor | |
4998 | { | |
e11e83f3 | 4999 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
f92e85f7 MV |
5000 | return x; |
5001 | else if (SCM_REALP (x)) | |
55f26379 | 5002 | return scm_from_double (floor (SCM_REAL_VALUE (x))); |
f92e85f7 MV |
5003 | else if (SCM_FRACTIONP (x)) |
5004 | { | |
5005 | SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x), | |
5006 | SCM_FRACTION_DENOMINATOR (x)); | |
73e4de09 | 5007 | if (scm_is_false (scm_negative_p (x))) |
f92e85f7 MV |
5008 | { |
5009 | /* For positive x, rounding towards zero is correct. */ | |
5010 | return q; | |
5011 | } | |
5012 | else | |
5013 | { | |
5014 | /* For negative x, we need to return q-1 unless x is an | |
5015 | integer. But fractions are never integer, per our | |
5016 | assumptions. */ | |
d956fa6f | 5017 | return scm_difference (q, SCM_I_MAKINUM (1)); |
f92e85f7 MV |
5018 | } |
5019 | } | |
5020 | else | |
5021 | SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor); | |
5022 | } | |
5023 | #undef FUNC_NAME | |
5024 | ||
5025 | SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0, | |
5026 | (SCM x), | |
5027 | "Round the number @var{x} towards infinity.") | |
5028 | #define FUNC_NAME s_scm_ceiling | |
5029 | { | |
e11e83f3 | 5030 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
f92e85f7 MV |
5031 | return x; |
5032 | else if (SCM_REALP (x)) | |
55f26379 | 5033 | return scm_from_double (ceil (SCM_REAL_VALUE (x))); |
f92e85f7 MV |
5034 | else if (SCM_FRACTIONP (x)) |
5035 | { | |
5036 | SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x), | |
5037 | SCM_FRACTION_DENOMINATOR (x)); | |
73e4de09 | 5038 | if (scm_is_false (scm_positive_p (x))) |
f92e85f7 MV |
5039 | { |
5040 | /* For negative x, rounding towards zero is correct. */ | |
5041 | return q; | |
5042 | } | |
5043 | else | |
5044 | { | |
5045 | /* For positive x, we need to return q+1 unless x is an | |
5046 | integer. But fractions are never integer, per our | |
5047 | assumptions. */ | |
d956fa6f | 5048 | return scm_sum (q, SCM_I_MAKINUM (1)); |
f92e85f7 MV |
5049 | } |
5050 | } | |
5051 | else | |
5052 | SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling); | |
5053 | } | |
5054 | #undef FUNC_NAME | |
0f2d19dd | 5055 | |
14b18ed6 | 5056 | SCM_GPROC1 (s_i_sqrt, "$sqrt", scm_tc7_dsubr, (SCM (*)()) sqrt, g_i_sqrt); |
942e5b91 MG |
5057 | /* "Return the square root of the real number @var{x}." |
5058 | */ | |
14b18ed6 | 5059 | SCM_GPROC1 (s_i_abs, "$abs", scm_tc7_dsubr, (SCM (*)()) fabs, g_i_abs); |
942e5b91 MG |
5060 | /* "Return the absolute value of the real number @var{x}." |
5061 | */ | |
14b18ed6 | 5062 | SCM_GPROC1 (s_i_exp, "$exp", scm_tc7_dsubr, (SCM (*)()) exp, g_i_exp); |
942e5b91 MG |
5063 | /* "Return the @var{x}th power of e." |
5064 | */ | |
14b18ed6 | 5065 | SCM_GPROC1 (s_i_log, "$log", scm_tc7_dsubr, (SCM (*)()) log, g_i_log); |
b3fcac34 | 5066 | /* "Return the natural logarithm of the real number @var{x}." |
942e5b91 | 5067 | */ |
14b18ed6 | 5068 | SCM_GPROC1 (s_i_sin, "$sin", scm_tc7_dsubr, (SCM (*)()) sin, g_i_sin); |
942e5b91 MG |
5069 | /* "Return the sine of the real number @var{x}." |
5070 | */ | |
14b18ed6 | 5071 | SCM_GPROC1 (s_i_cos, "$cos", scm_tc7_dsubr, (SCM (*)()) cos, g_i_cos); |
942e5b91 MG |
5072 | /* "Return the cosine of the real number @var{x}." |
5073 | */ | |
14b18ed6 | 5074 | SCM_GPROC1 (s_i_tan, "$tan", scm_tc7_dsubr, (SCM (*)()) tan, g_i_tan); |
942e5b91 MG |
5075 | /* "Return the tangent of the real number @var{x}." |
5076 | */ | |
14b18ed6 | 5077 | SCM_GPROC1 (s_i_asin, "$asin", scm_tc7_dsubr, (SCM (*)()) asin, g_i_asin); |
942e5b91 MG |
5078 | /* "Return the arc sine of the real number @var{x}." |
5079 | */ | |
14b18ed6 | 5080 | SCM_GPROC1 (s_i_acos, "$acos", scm_tc7_dsubr, (SCM (*)()) acos, g_i_acos); |
942e5b91 MG |
5081 | /* "Return the arc cosine of the real number @var{x}." |
5082 | */ | |
14b18ed6 | 5083 | SCM_GPROC1 (s_i_atan, "$atan", scm_tc7_dsubr, (SCM (*)()) atan, g_i_atan); |
942e5b91 MG |
5084 | /* "Return the arc tangent of the real number @var{x}." |
5085 | */ | |
14b18ed6 | 5086 | SCM_GPROC1 (s_i_sinh, "$sinh", scm_tc7_dsubr, (SCM (*)()) sinh, g_i_sinh); |
942e5b91 MG |
5087 | /* "Return the hyperbolic sine of the real number @var{x}." |
5088 | */ | |
14b18ed6 | 5089 | SCM_GPROC1 (s_i_cosh, "$cosh", scm_tc7_dsubr, (SCM (*)()) cosh, g_i_cosh); |
942e5b91 MG |
5090 | /* "Return the hyperbolic cosine of the real number @var{x}." |
5091 | */ | |
14b18ed6 | 5092 | SCM_GPROC1 (s_i_tanh, "$tanh", scm_tc7_dsubr, (SCM (*)()) tanh, g_i_tanh); |
942e5b91 MG |
5093 | /* "Return the hyperbolic tangent of the real number @var{x}." |
5094 | */ | |
f872b822 MD |
5095 | |
5096 | struct dpair | |
5097 | { | |
5098 | double x, y; | |
5099 | }; | |
5100 | ||
27c37006 NJ |
5101 | static void scm_two_doubles (SCM x, |
5102 | SCM y, | |
3eeba8d4 JB |
5103 | const char *sstring, |
5104 | struct dpair * xy); | |
f872b822 MD |
5105 | |
5106 | static void | |
27c37006 NJ |
5107 | scm_two_doubles (SCM x, SCM y, const char *sstring, struct dpair *xy) |
5108 | { | |
e11e83f3 MV |
5109 | if (SCM_I_INUMP (x)) |
5110 | xy->x = SCM_I_INUM (x); | |
0aacf84e | 5111 | else if (SCM_BIGP (x)) |
1be6b49c | 5112 | xy->x = scm_i_big2dbl (x); |
0aacf84e | 5113 | else if (SCM_REALP (x)) |
27c37006 | 5114 | xy->x = SCM_REAL_VALUE (x); |
f92e85f7 MV |
5115 | else if (SCM_FRACTIONP (x)) |
5116 | xy->x = scm_i_fraction2double (x); | |
0aacf84e | 5117 | else |
27c37006 | 5118 | scm_wrong_type_arg (sstring, SCM_ARG1, x); |
98cb6e75 | 5119 | |
e11e83f3 MV |
5120 | if (SCM_I_INUMP (y)) |
5121 | xy->y = SCM_I_INUM (y); | |
0aacf84e | 5122 | else if (SCM_BIGP (y)) |
1be6b49c | 5123 | xy->y = scm_i_big2dbl (y); |
0aacf84e | 5124 | else if (SCM_REALP (y)) |
27c37006 | 5125 | xy->y = SCM_REAL_VALUE (y); |
f92e85f7 MV |
5126 | else if (SCM_FRACTIONP (y)) |
5127 | xy->y = scm_i_fraction2double (y); | |
0aacf84e | 5128 | else |
27c37006 | 5129 | scm_wrong_type_arg (sstring, SCM_ARG2, y); |
0f2d19dd JB |
5130 | } |
5131 | ||
5132 | ||
a1ec6916 | 5133 | SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0, |
27c37006 NJ |
5134 | (SCM x, SCM y), |
5135 | "Return @var{x} raised to the power of @var{y}. This\n" | |
0137a31b | 5136 | "procedure does not accept complex arguments.") |
1bbd0b84 | 5137 | #define FUNC_NAME s_scm_sys_expt |
0f2d19dd JB |
5138 | { |
5139 | struct dpair xy; | |
27c37006 | 5140 | scm_two_doubles (x, y, FUNC_NAME, &xy); |
55f26379 | 5141 | return scm_from_double (pow (xy.x, xy.y)); |
0f2d19dd | 5142 | } |
1bbd0b84 | 5143 | #undef FUNC_NAME |
0f2d19dd JB |
5144 | |
5145 | ||
a1ec6916 | 5146 | SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0, |
27c37006 NJ |
5147 | (SCM x, SCM y), |
5148 | "Return the arc tangent of the two arguments @var{x} and\n" | |
5149 | "@var{y}. This is similar to calculating the arc tangent of\n" | |
5150 | "@var{x} / @var{y}, except that the signs of both arguments\n" | |
0137a31b MG |
5151 | "are used to determine the quadrant of the result. This\n" |
5152 | "procedure does not accept complex arguments.") | |
1bbd0b84 | 5153 | #define FUNC_NAME s_scm_sys_atan2 |
0f2d19dd JB |
5154 | { |
5155 | struct dpair xy; | |
27c37006 | 5156 | scm_two_doubles (x, y, FUNC_NAME, &xy); |
55f26379 | 5157 | return scm_from_double (atan2 (xy.x, xy.y)); |
0f2d19dd | 5158 | } |
1bbd0b84 | 5159 | #undef FUNC_NAME |
0f2d19dd | 5160 | |
8507ec80 MV |
5161 | SCM |
5162 | scm_c_make_rectangular (double re, double im) | |
5163 | { | |
5164 | if (im == 0.0) | |
5165 | return scm_from_double (re); | |
5166 | else | |
5167 | { | |
5168 | SCM z; | |
5169 | SCM_NEWSMOB (z, scm_tc16_complex, scm_gc_malloc (sizeof (scm_t_complex), | |
5170 | "complex")); | |
5171 | SCM_COMPLEX_REAL (z) = re; | |
5172 | SCM_COMPLEX_IMAG (z) = im; | |
5173 | return z; | |
5174 | } | |
5175 | } | |
0f2d19dd | 5176 | |
a1ec6916 | 5177 | SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0, |
bb628794 | 5178 | (SCM real, SCM imaginary), |
942e5b91 MG |
5179 | "Return a complex number constructed of the given @var{real} and\n" |
5180 | "@var{imaginary} parts.") | |
1bbd0b84 | 5181 | #define FUNC_NAME s_scm_make_rectangular |
0f2d19dd JB |
5182 | { |
5183 | struct dpair xy; | |
bb628794 | 5184 | scm_two_doubles (real, imaginary, FUNC_NAME, &xy); |
8507ec80 | 5185 | return scm_c_make_rectangular (xy.x, xy.y); |
0f2d19dd | 5186 | } |
1bbd0b84 | 5187 | #undef FUNC_NAME |
0f2d19dd | 5188 | |
8507ec80 MV |
5189 | SCM |
5190 | scm_c_make_polar (double mag, double ang) | |
5191 | { | |
5192 | double s, c; | |
5193 | #if HAVE_SINCOS | |
5194 | sincos (ang, &s, &c); | |
5195 | #else | |
5196 | s = sin (ang); | |
5197 | c = cos (ang); | |
5198 | #endif | |
5199 | return scm_c_make_rectangular (mag * c, mag * s); | |
5200 | } | |
0f2d19dd | 5201 | |
a1ec6916 | 5202 | SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0, |
27c37006 | 5203 | (SCM x, SCM y), |
942e5b91 | 5204 | "Return the complex number @var{x} * e^(i * @var{y}).") |
1bbd0b84 | 5205 | #define FUNC_NAME s_scm_make_polar |
0f2d19dd JB |
5206 | { |
5207 | struct dpair xy; | |
27c37006 | 5208 | scm_two_doubles (x, y, FUNC_NAME, &xy); |
8507ec80 | 5209 | return scm_c_make_polar (xy.x, xy.y); |
0f2d19dd | 5210 | } |
1bbd0b84 | 5211 | #undef FUNC_NAME |
0f2d19dd JB |
5212 | |
5213 | ||
152f82bf | 5214 | SCM_GPROC (s_real_part, "real-part", 1, 0, 0, scm_real_part, g_real_part); |
942e5b91 MG |
5215 | /* "Return the real part of the number @var{z}." |
5216 | */ | |
0f2d19dd | 5217 | SCM |
6e8d25a6 | 5218 | scm_real_part (SCM z) |
0f2d19dd | 5219 | { |
e11e83f3 | 5220 | if (SCM_I_INUMP (z)) |
c2ff8ab0 | 5221 | return z; |
0aacf84e | 5222 | else if (SCM_BIGP (z)) |
c2ff8ab0 | 5223 | return z; |
0aacf84e | 5224 | else if (SCM_REALP (z)) |
c2ff8ab0 | 5225 | return z; |
0aacf84e | 5226 | else if (SCM_COMPLEXP (z)) |
55f26379 | 5227 | return scm_from_double (SCM_COMPLEX_REAL (z)); |
f92e85f7 | 5228 | else if (SCM_FRACTIONP (z)) |
2fa2d879 | 5229 | return z; |
0aacf84e | 5230 | else |
c2ff8ab0 | 5231 | SCM_WTA_DISPATCH_1 (g_real_part, z, SCM_ARG1, s_real_part); |
0f2d19dd JB |
5232 | } |
5233 | ||
5234 | ||
152f82bf | 5235 | SCM_GPROC (s_imag_part, "imag-part", 1, 0, 0, scm_imag_part, g_imag_part); |
942e5b91 MG |
5236 | /* "Return the imaginary part of the number @var{z}." |
5237 | */ | |
0f2d19dd | 5238 | SCM |
6e8d25a6 | 5239 | scm_imag_part (SCM z) |
0f2d19dd | 5240 | { |
e11e83f3 | 5241 | if (SCM_I_INUMP (z)) |
f872b822 | 5242 | return SCM_INUM0; |
0aacf84e | 5243 | else if (SCM_BIGP (z)) |
f872b822 | 5244 | return SCM_INUM0; |
0aacf84e | 5245 | else if (SCM_REALP (z)) |
c2ff8ab0 | 5246 | return scm_flo0; |
0aacf84e | 5247 | else if (SCM_COMPLEXP (z)) |
55f26379 | 5248 | return scm_from_double (SCM_COMPLEX_IMAG (z)); |
f92e85f7 MV |
5249 | else if (SCM_FRACTIONP (z)) |
5250 | return SCM_INUM0; | |
0aacf84e | 5251 | else |
c2ff8ab0 | 5252 | SCM_WTA_DISPATCH_1 (g_imag_part, z, SCM_ARG1, s_imag_part); |
0f2d19dd JB |
5253 | } |
5254 | ||
f92e85f7 MV |
5255 | SCM_GPROC (s_numerator, "numerator", 1, 0, 0, scm_numerator, g_numerator); |
5256 | /* "Return the numerator of the number @var{z}." | |
5257 | */ | |
5258 | SCM | |
5259 | scm_numerator (SCM z) | |
5260 | { | |
e11e83f3 | 5261 | if (SCM_I_INUMP (z)) |
f92e85f7 MV |
5262 | return z; |
5263 | else if (SCM_BIGP (z)) | |
5264 | return z; | |
5265 | else if (SCM_FRACTIONP (z)) | |
5266 | { | |
5267 | scm_i_fraction_reduce (z); | |
5268 | return SCM_FRACTION_NUMERATOR (z); | |
5269 | } | |
5270 | else if (SCM_REALP (z)) | |
5271 | return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z))); | |
5272 | else | |
5273 | SCM_WTA_DISPATCH_1 (g_numerator, z, SCM_ARG1, s_numerator); | |
5274 | } | |
5275 | ||
5276 | ||
5277 | SCM_GPROC (s_denominator, "denominator", 1, 0, 0, scm_denominator, g_denominator); | |
5278 | /* "Return the denominator of the number @var{z}." | |
5279 | */ | |
5280 | SCM | |
5281 | scm_denominator (SCM z) | |
5282 | { | |
e11e83f3 | 5283 | if (SCM_I_INUMP (z)) |
d956fa6f | 5284 | return SCM_I_MAKINUM (1); |
f92e85f7 | 5285 | else if (SCM_BIGP (z)) |
d956fa6f | 5286 | return SCM_I_MAKINUM (1); |
f92e85f7 MV |
5287 | else if (SCM_FRACTIONP (z)) |
5288 | { | |
5289 | scm_i_fraction_reduce (z); | |
5290 | return SCM_FRACTION_DENOMINATOR (z); | |
5291 | } | |
5292 | else if (SCM_REALP (z)) | |
5293 | return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z))); | |
5294 | else | |
5295 | SCM_WTA_DISPATCH_1 (g_denominator, z, SCM_ARG1, s_denominator); | |
5296 | } | |
0f2d19dd | 5297 | |
9de33deb | 5298 | SCM_GPROC (s_magnitude, "magnitude", 1, 0, 0, scm_magnitude, g_magnitude); |
942e5b91 MG |
5299 | /* "Return the magnitude of the number @var{z}. This is the same as\n" |
5300 | * "@code{abs} for real arguments, but also allows complex numbers." | |
5301 | */ | |
0f2d19dd | 5302 | SCM |
6e8d25a6 | 5303 | scm_magnitude (SCM z) |
0f2d19dd | 5304 | { |
e11e83f3 | 5305 | if (SCM_I_INUMP (z)) |
0aacf84e | 5306 | { |
e11e83f3 | 5307 | long int zz = SCM_I_INUM (z); |
0aacf84e MD |
5308 | if (zz >= 0) |
5309 | return z; | |
5310 | else if (SCM_POSFIXABLE (-zz)) | |
d956fa6f | 5311 | return SCM_I_MAKINUM (-zz); |
0aacf84e MD |
5312 | else |
5313 | return scm_i_long2big (-zz); | |
5986c47d | 5314 | } |
0aacf84e MD |
5315 | else if (SCM_BIGP (z)) |
5316 | { | |
5317 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (z)); | |
5318 | scm_remember_upto_here_1 (z); | |
5319 | if (sgn < 0) | |
5320 | return scm_i_clonebig (z, 0); | |
5321 | else | |
5322 | return z; | |
5986c47d | 5323 | } |
0aacf84e | 5324 | else if (SCM_REALP (z)) |
55f26379 | 5325 | return scm_from_double (fabs (SCM_REAL_VALUE (z))); |
0aacf84e | 5326 | else if (SCM_COMPLEXP (z)) |
55f26379 | 5327 | return scm_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z))); |
f92e85f7 MV |
5328 | else if (SCM_FRACTIONP (z)) |
5329 | { | |
73e4de09 | 5330 | if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z)))) |
f92e85f7 | 5331 | return z; |
cba42c93 | 5332 | return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED), |
f92e85f7 MV |
5333 | SCM_FRACTION_DENOMINATOR (z)); |
5334 | } | |
0aacf84e | 5335 | else |
c2ff8ab0 | 5336 | SCM_WTA_DISPATCH_1 (g_magnitude, z, SCM_ARG1, s_magnitude); |
0f2d19dd JB |
5337 | } |
5338 | ||
5339 | ||
9de33deb | 5340 | SCM_GPROC (s_angle, "angle", 1, 0, 0, scm_angle, g_angle); |
942e5b91 MG |
5341 | /* "Return the angle of the complex number @var{z}." |
5342 | */ | |
0f2d19dd | 5343 | SCM |
6e8d25a6 | 5344 | scm_angle (SCM z) |
0f2d19dd | 5345 | { |
c8ae173e | 5346 | /* atan(0,-1) is pi and it'd be possible to have that as a constant like |
55f26379 | 5347 | scm_flo0 to save allocating a new flonum with scm_from_double each time. |
c8ae173e KR |
5348 | But if atan2 follows the floating point rounding mode, then the value |
5349 | is not a constant. Maybe it'd be close enough though. */ | |
e11e83f3 | 5350 | if (SCM_I_INUMP (z)) |
0aacf84e | 5351 | { |
e11e83f3 | 5352 | if (SCM_I_INUM (z) >= 0) |
c8ae173e | 5353 | return scm_flo0; |
0aacf84e | 5354 | else |
55f26379 | 5355 | return scm_from_double (atan2 (0.0, -1.0)); |
f872b822 | 5356 | } |
0aacf84e MD |
5357 | else if (SCM_BIGP (z)) |
5358 | { | |
5359 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (z)); | |
5360 | scm_remember_upto_here_1 (z); | |
5361 | if (sgn < 0) | |
55f26379 | 5362 | return scm_from_double (atan2 (0.0, -1.0)); |
0aacf84e | 5363 | else |
c8ae173e | 5364 | return scm_flo0; |
0f2d19dd | 5365 | } |
0aacf84e | 5366 | else if (SCM_REALP (z)) |
c8ae173e KR |
5367 | { |
5368 | if (SCM_REAL_VALUE (z) >= 0) | |
5369 | return scm_flo0; | |
5370 | else | |
55f26379 | 5371 | return scm_from_double (atan2 (0.0, -1.0)); |
c8ae173e | 5372 | } |
0aacf84e | 5373 | else if (SCM_COMPLEXP (z)) |
55f26379 | 5374 | return scm_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z))); |
f92e85f7 MV |
5375 | else if (SCM_FRACTIONP (z)) |
5376 | { | |
73e4de09 | 5377 | if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z)))) |
f92e85f7 | 5378 | return scm_flo0; |
55f26379 | 5379 | else return scm_from_double (atan2 (0.0, -1.0)); |
f92e85f7 | 5380 | } |
0aacf84e | 5381 | else |
f4c627b3 | 5382 | SCM_WTA_DISPATCH_1 (g_angle, z, SCM_ARG1, s_angle); |
0f2d19dd JB |
5383 | } |
5384 | ||
5385 | ||
3c9a524f DH |
5386 | SCM_GPROC (s_exact_to_inexact, "exact->inexact", 1, 0, 0, scm_exact_to_inexact, g_exact_to_inexact); |
5387 | /* Convert the number @var{x} to its inexact representation.\n" | |
5388 | */ | |
5389 | SCM | |
5390 | scm_exact_to_inexact (SCM z) | |
5391 | { | |
e11e83f3 | 5392 | if (SCM_I_INUMP (z)) |
55f26379 | 5393 | return scm_from_double ((double) SCM_I_INUM (z)); |
3c9a524f | 5394 | else if (SCM_BIGP (z)) |
55f26379 | 5395 | return scm_from_double (scm_i_big2dbl (z)); |
f92e85f7 | 5396 | else if (SCM_FRACTIONP (z)) |
55f26379 | 5397 | return scm_from_double (scm_i_fraction2double (z)); |
3c9a524f DH |
5398 | else if (SCM_INEXACTP (z)) |
5399 | return z; | |
5400 | else | |
5401 | SCM_WTA_DISPATCH_1 (g_exact_to_inexact, z, 1, s_exact_to_inexact); | |
5402 | } | |
5403 | ||
5404 | ||
a1ec6916 | 5405 | SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0, |
1bbd0b84 | 5406 | (SCM z), |
1e6808ea | 5407 | "Return an exact number that is numerically closest to @var{z}.") |
1bbd0b84 | 5408 | #define FUNC_NAME s_scm_inexact_to_exact |
0f2d19dd | 5409 | { |
e11e83f3 | 5410 | if (SCM_I_INUMP (z)) |
f872b822 | 5411 | return z; |
0aacf84e | 5412 | else if (SCM_BIGP (z)) |
f872b822 | 5413 | return z; |
0aacf84e MD |
5414 | else if (SCM_REALP (z)) |
5415 | { | |
f92e85f7 MV |
5416 | if (xisinf (SCM_REAL_VALUE (z)) || xisnan (SCM_REAL_VALUE (z))) |
5417 | SCM_OUT_OF_RANGE (1, z); | |
2be24db4 | 5418 | else |
f92e85f7 MV |
5419 | { |
5420 | mpq_t frac; | |
5421 | SCM q; | |
5422 | ||
5423 | mpq_init (frac); | |
5424 | mpq_set_d (frac, SCM_REAL_VALUE (z)); | |
cba42c93 | 5425 | q = scm_i_make_ratio (scm_i_mpz2num (mpq_numref (frac)), |
f92e85f7 MV |
5426 | scm_i_mpz2num (mpq_denref (frac))); |
5427 | ||
cba42c93 | 5428 | /* When scm_i_make_ratio throws, we leak the memory allocated |
f92e85f7 MV |
5429 | for frac... |
5430 | */ | |
5431 | mpq_clear (frac); | |
5432 | return q; | |
5433 | } | |
c2ff8ab0 | 5434 | } |
f92e85f7 MV |
5435 | else if (SCM_FRACTIONP (z)) |
5436 | return z; | |
0aacf84e | 5437 | else |
c2ff8ab0 | 5438 | SCM_WRONG_TYPE_ARG (1, z); |
0f2d19dd | 5439 | } |
1bbd0b84 | 5440 | #undef FUNC_NAME |
0f2d19dd | 5441 | |
f92e85f7 MV |
5442 | SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0, |
5443 | (SCM x, SCM err), | |
5444 | "Return an exact number that is within @var{err} of @var{x}.") | |
5445 | #define FUNC_NAME s_scm_rationalize | |
5446 | { | |
e11e83f3 | 5447 | if (SCM_I_INUMP (x)) |
f92e85f7 MV |
5448 | return x; |
5449 | else if (SCM_BIGP (x)) | |
5450 | return x; | |
5451 | else if ((SCM_REALP (x)) || SCM_FRACTIONP (x)) | |
5452 | { | |
5453 | /* Use continued fractions to find closest ratio. All | |
5454 | arithmetic is done with exact numbers. | |
5455 | */ | |
5456 | ||
5457 | SCM ex = scm_inexact_to_exact (x); | |
5458 | SCM int_part = scm_floor (ex); | |
d956fa6f MV |
5459 | SCM tt = SCM_I_MAKINUM (1); |
5460 | SCM a1 = SCM_I_MAKINUM (0), a2 = SCM_I_MAKINUM (1), a = SCM_I_MAKINUM (0); | |
5461 | SCM b1 = SCM_I_MAKINUM (1), b2 = SCM_I_MAKINUM (0), b = SCM_I_MAKINUM (0); | |
f92e85f7 MV |
5462 | SCM rx; |
5463 | int i = 0; | |
5464 | ||
73e4de09 | 5465 | if (scm_is_true (scm_num_eq_p (ex, int_part))) |
f92e85f7 MV |
5466 | return ex; |
5467 | ||
5468 | ex = scm_difference (ex, int_part); /* x = x-int_part */ | |
5469 | rx = scm_divide (ex, SCM_UNDEFINED); /* rx = 1/x */ | |
5470 | ||
5471 | /* We stop after a million iterations just to be absolutely sure | |
5472 | that we don't go into an infinite loop. The process normally | |
5473 | converges after less than a dozen iterations. | |
5474 | */ | |
5475 | ||
5476 | err = scm_abs (err); | |
5477 | while (++i < 1000000) | |
5478 | { | |
5479 | a = scm_sum (scm_product (a1, tt), a2); /* a = a1*tt + a2 */ | |
5480 | b = scm_sum (scm_product (b1, tt), b2); /* b = b1*tt + b2 */ | |
73e4de09 MV |
5481 | if (scm_is_false (scm_zero_p (b)) && /* b != 0 */ |
5482 | scm_is_false | |
f92e85f7 MV |
5483 | (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))), |
5484 | err))) /* abs(x-a/b) <= err */ | |
02164269 MV |
5485 | { |
5486 | SCM res = scm_sum (int_part, scm_divide (a, b)); | |
73e4de09 MV |
5487 | if (scm_is_false (scm_exact_p (x)) |
5488 | || scm_is_false (scm_exact_p (err))) | |
02164269 MV |
5489 | return scm_exact_to_inexact (res); |
5490 | else | |
5491 | return res; | |
5492 | } | |
f92e85f7 MV |
5493 | rx = scm_divide (scm_difference (rx, tt), /* rx = 1/(rx - tt) */ |
5494 | SCM_UNDEFINED); | |
5495 | tt = scm_floor (rx); /* tt = floor (rx) */ | |
5496 | a2 = a1; | |
5497 | b2 = b1; | |
5498 | a1 = a; | |
5499 | b1 = b; | |
5500 | } | |
5501 | scm_num_overflow (s_scm_rationalize); | |
5502 | } | |
5503 | else | |
5504 | SCM_WRONG_TYPE_ARG (1, x); | |
5505 | } | |
5506 | #undef FUNC_NAME | |
5507 | ||
73e4de09 MV |
5508 | /* conversion functions */ |
5509 | ||
5510 | int | |
5511 | scm_is_integer (SCM val) | |
5512 | { | |
5513 | return scm_is_true (scm_integer_p (val)); | |
5514 | } | |
5515 | ||
5516 | int | |
5517 | scm_is_signed_integer (SCM val, scm_t_intmax min, scm_t_intmax max) | |
5518 | { | |
e11e83f3 | 5519 | if (SCM_I_INUMP (val)) |
73e4de09 | 5520 | { |
e11e83f3 | 5521 | scm_t_signed_bits n = SCM_I_INUM (val); |
73e4de09 MV |
5522 | return n >= min && n <= max; |
5523 | } | |
5524 | else if (SCM_BIGP (val)) | |
5525 | { | |
5526 | if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM) | |
5527 | return 0; | |
5528 | else if (min >= LONG_MIN && max <= LONG_MAX) | |
d956fa6f MV |
5529 | { |
5530 | if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val))) | |
5531 | { | |
5532 | long n = mpz_get_si (SCM_I_BIG_MPZ (val)); | |
5533 | return n >= min && n <= max; | |
5534 | } | |
5535 | else | |
5536 | return 0; | |
5537 | } | |
73e4de09 MV |
5538 | else |
5539 | { | |
d956fa6f MV |
5540 | scm_t_intmax n; |
5541 | size_t count; | |
73e4de09 | 5542 | |
d956fa6f MV |
5543 | if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2) |
5544 | > CHAR_BIT*sizeof (scm_t_uintmax)) | |
5545 | return 0; | |
5546 | ||
5547 | mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0, | |
5548 | SCM_I_BIG_MPZ (val)); | |
73e4de09 | 5549 | |
d956fa6f | 5550 | if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0) |
73e4de09 | 5551 | { |
d956fa6f MV |
5552 | if (n < 0) |
5553 | return 0; | |
73e4de09 | 5554 | } |
73e4de09 MV |
5555 | else |
5556 | { | |
d956fa6f MV |
5557 | n = -n; |
5558 | if (n >= 0) | |
5559 | return 0; | |
73e4de09 | 5560 | } |
d956fa6f MV |
5561 | |
5562 | return n >= min && n <= max; | |
73e4de09 MV |
5563 | } |
5564 | } | |
73e4de09 MV |
5565 | else |
5566 | return 0; | |
5567 | } | |
5568 | ||
5569 | int | |
5570 | scm_is_unsigned_integer (SCM val, scm_t_uintmax min, scm_t_uintmax max) | |
5571 | { | |
e11e83f3 | 5572 | if (SCM_I_INUMP (val)) |
73e4de09 | 5573 | { |
e11e83f3 | 5574 | scm_t_signed_bits n = SCM_I_INUM (val); |
73e4de09 MV |
5575 | return n >= 0 && ((scm_t_uintmax)n) >= min && ((scm_t_uintmax)n) <= max; |
5576 | } | |
5577 | else if (SCM_BIGP (val)) | |
5578 | { | |
5579 | if (max <= SCM_MOST_POSITIVE_FIXNUM) | |
5580 | return 0; | |
5581 | else if (max <= ULONG_MAX) | |
d956fa6f MV |
5582 | { |
5583 | if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val))) | |
5584 | { | |
5585 | unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val)); | |
5586 | return n >= min && n <= max; | |
5587 | } | |
5588 | else | |
5589 | return 0; | |
5590 | } | |
73e4de09 MV |
5591 | else |
5592 | { | |
d956fa6f MV |
5593 | scm_t_uintmax n; |
5594 | size_t count; | |
73e4de09 | 5595 | |
d956fa6f MV |
5596 | if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0) |
5597 | return 0; | |
73e4de09 | 5598 | |
d956fa6f MV |
5599 | if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2) |
5600 | > CHAR_BIT*sizeof (scm_t_uintmax)) | |
73e4de09 | 5601 | return 0; |
d956fa6f MV |
5602 | |
5603 | mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0, | |
5604 | SCM_I_BIG_MPZ (val)); | |
73e4de09 | 5605 | |
d956fa6f | 5606 | return n >= min && n <= max; |
73e4de09 MV |
5607 | } |
5608 | } | |
73e4de09 MV |
5609 | else |
5610 | return 0; | |
5611 | } | |
5612 | ||
bfd7932e MV |
5613 | #define TYPE scm_t_intmax |
5614 | #define TYPE_MIN min | |
5615 | #define TYPE_MAX max | |
5616 | #define SIZEOF_TYPE 0 | |
5617 | #define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, scm_t_intmax min, scm_t_intmax max) | |
5618 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg) | |
5619 | #include "libguile/conv-integer.i.c" | |
5620 | ||
5621 | #define TYPE scm_t_uintmax | |
5622 | #define TYPE_MIN min | |
5623 | #define TYPE_MAX max | |
5624 | #define SIZEOF_TYPE 0 | |
5625 | #define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, scm_t_uintmax min, scm_t_uintmax max) | |
5626 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg) | |
5627 | #include "libguile/conv-uinteger.i.c" | |
5628 | ||
5629 | #define TYPE scm_t_int8 | |
5630 | #define TYPE_MIN SCM_T_INT8_MIN | |
5631 | #define TYPE_MAX SCM_T_INT8_MAX | |
5632 | #define SIZEOF_TYPE 1 | |
5633 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg) | |
5634 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg) | |
5635 | #include "libguile/conv-integer.i.c" | |
5636 | ||
5637 | #define TYPE scm_t_uint8 | |
5638 | #define TYPE_MIN 0 | |
5639 | #define TYPE_MAX SCM_T_UINT8_MAX | |
5640 | #define SIZEOF_TYPE 1 | |
5641 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg) | |
5642 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg) | |
5643 | #include "libguile/conv-uinteger.i.c" | |
5644 | ||
5645 | #define TYPE scm_t_int16 | |
5646 | #define TYPE_MIN SCM_T_INT16_MIN | |
5647 | #define TYPE_MAX SCM_T_INT16_MAX | |
5648 | #define SIZEOF_TYPE 2 | |
5649 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg) | |
5650 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg) | |
5651 | #include "libguile/conv-integer.i.c" | |
5652 | ||
5653 | #define TYPE scm_t_uint16 | |
5654 | #define TYPE_MIN 0 | |
5655 | #define TYPE_MAX SCM_T_UINT16_MAX | |
5656 | #define SIZEOF_TYPE 2 | |
5657 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg) | |
5658 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg) | |
5659 | #include "libguile/conv-uinteger.i.c" | |
5660 | ||
5661 | #define TYPE scm_t_int32 | |
5662 | #define TYPE_MIN SCM_T_INT32_MIN | |
5663 | #define TYPE_MAX SCM_T_INT32_MAX | |
5664 | #define SIZEOF_TYPE 4 | |
5665 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg) | |
5666 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg) | |
5667 | #include "libguile/conv-integer.i.c" | |
5668 | ||
5669 | #define TYPE scm_t_uint32 | |
5670 | #define TYPE_MIN 0 | |
5671 | #define TYPE_MAX SCM_T_UINT32_MAX | |
5672 | #define SIZEOF_TYPE 4 | |
5673 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg) | |
5674 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg) | |
5675 | #include "libguile/conv-uinteger.i.c" | |
5676 | ||
5677 | #if SCM_HAVE_T_INT64 | |
5678 | ||
5679 | #define TYPE scm_t_int64 | |
5680 | #define TYPE_MIN SCM_T_INT64_MIN | |
5681 | #define TYPE_MAX SCM_T_INT64_MAX | |
5682 | #define SIZEOF_TYPE 8 | |
5683 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg) | |
5684 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg) | |
5685 | #include "libguile/conv-integer.i.c" | |
5686 | ||
5687 | #define TYPE scm_t_uint64 | |
5688 | #define TYPE_MIN 0 | |
5689 | #define TYPE_MAX SCM_T_UINT64_MAX | |
5690 | #define SIZEOF_TYPE 8 | |
5691 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg) | |
5692 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg) | |
5693 | #include "libguile/conv-uinteger.i.c" | |
73e4de09 | 5694 | |
bfd7932e | 5695 | #endif |
73e4de09 MV |
5696 | |
5697 | int | |
5698 | scm_is_real (SCM val) | |
5699 | { | |
5700 | return scm_is_true (scm_real_p (val)); | |
5701 | } | |
5702 | ||
55f26379 MV |
5703 | int |
5704 | scm_is_rational (SCM val) | |
5705 | { | |
5706 | return scm_is_true (scm_rational_p (val)); | |
5707 | } | |
5708 | ||
73e4de09 MV |
5709 | double |
5710 | scm_to_double (SCM val) | |
5711 | { | |
55f26379 MV |
5712 | if (SCM_I_INUMP (val)) |
5713 | return SCM_I_INUM (val); | |
5714 | else if (SCM_BIGP (val)) | |
5715 | return scm_i_big2dbl (val); | |
5716 | else if (SCM_FRACTIONP (val)) | |
5717 | return scm_i_fraction2double (val); | |
5718 | else if (SCM_REALP (val)) | |
5719 | return SCM_REAL_VALUE (val); | |
5720 | else | |
5721 | scm_wrong_type_arg (NULL, 0, val); | |
73e4de09 MV |
5722 | } |
5723 | ||
5724 | SCM | |
5725 | scm_from_double (double val) | |
5726 | { | |
55f26379 MV |
5727 | SCM z = scm_double_cell (scm_tc16_real, 0, 0, 0); |
5728 | SCM_REAL_VALUE (z) = val; | |
5729 | return z; | |
73e4de09 MV |
5730 | } |
5731 | ||
55f26379 MV |
5732 | #if SCM_ENABLE_DISCOURAGED == 1 |
5733 | ||
5734 | float | |
5735 | scm_num2float (SCM num, unsigned long int pos, const char *s_caller) | |
5736 | { | |
5737 | if (SCM_BIGP (num)) | |
5738 | { | |
5739 | float res = mpz_get_d (SCM_I_BIG_MPZ (num)); | |
5740 | if (!xisinf (res)) | |
5741 | return res; | |
5742 | else | |
5743 | scm_out_of_range (NULL, num); | |
5744 | } | |
5745 | else | |
5746 | return scm_to_double (num); | |
5747 | } | |
5748 | ||
5749 | double | |
5750 | scm_num2double (SCM num, unsigned long int pos, const char *s_caller) | |
5751 | { | |
5752 | if (SCM_BIGP (num)) | |
5753 | { | |
5754 | double res = mpz_get_d (SCM_I_BIG_MPZ (num)); | |
5755 | if (!xisinf (res)) | |
5756 | return res; | |
5757 | else | |
5758 | scm_out_of_range (NULL, num); | |
5759 | } | |
5760 | else | |
5761 | return scm_to_double (num); | |
5762 | } | |
5763 | ||
5764 | #endif | |
5765 | ||
8507ec80 MV |
5766 | int |
5767 | scm_is_complex (SCM val) | |
5768 | { | |
5769 | return scm_is_true (scm_complex_p (val)); | |
5770 | } | |
5771 | ||
5772 | double | |
5773 | scm_c_real_part (SCM z) | |
5774 | { | |
5775 | if (SCM_COMPLEXP (z)) | |
5776 | return SCM_COMPLEX_REAL (z); | |
5777 | else | |
5778 | { | |
5779 | /* Use the scm_real_part to get proper error checking and | |
5780 | dispatching. | |
5781 | */ | |
5782 | return scm_to_double (scm_real_part (z)); | |
5783 | } | |
5784 | } | |
5785 | ||
5786 | double | |
5787 | scm_c_imag_part (SCM z) | |
5788 | { | |
5789 | if (SCM_COMPLEXP (z)) | |
5790 | return SCM_COMPLEX_IMAG (z); | |
5791 | else | |
5792 | { | |
5793 | /* Use the scm_imag_part to get proper error checking and | |
5794 | dispatching. The result will almost always be 0.0, but not | |
5795 | always. | |
5796 | */ | |
5797 | return scm_to_double (scm_imag_part (z)); | |
5798 | } | |
5799 | } | |
5800 | ||
5801 | double | |
5802 | scm_c_magnitude (SCM z) | |
5803 | { | |
5804 | return scm_to_double (scm_magnitude (z)); | |
5805 | } | |
5806 | ||
5807 | double | |
5808 | scm_c_angle (SCM z) | |
5809 | { | |
5810 | return scm_to_double (scm_angle (z)); | |
5811 | } | |
5812 | ||
5813 | int | |
5814 | scm_is_number (SCM z) | |
5815 | { | |
5816 | return scm_is_true (scm_number_p (z)); | |
5817 | } | |
5818 | ||
0f2d19dd JB |
5819 | void |
5820 | scm_init_numbers () | |
0f2d19dd | 5821 | { |
0b799eea MV |
5822 | int i; |
5823 | ||
713a4259 KR |
5824 | mpz_init_set_si (z_negative_one, -1); |
5825 | ||
a261c0e9 DH |
5826 | /* It may be possible to tune the performance of some algorithms by using |
5827 | * the following constants to avoid the creation of bignums. Please, before | |
5828 | * using these values, remember the two rules of program optimization: | |
5829 | * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */ | |
86d31dfe | 5830 | scm_c_define ("most-positive-fixnum", |
d956fa6f | 5831 | SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM)); |
86d31dfe | 5832 | scm_c_define ("most-negative-fixnum", |
d956fa6f | 5833 | SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM)); |
a261c0e9 | 5834 | |
f3ae5d60 MD |
5835 | scm_add_feature ("complex"); |
5836 | scm_add_feature ("inexact"); | |
55f26379 | 5837 | scm_flo0 = scm_from_double (0.0); |
0b799eea MV |
5838 | |
5839 | /* determine floating point precision */ | |
55f26379 | 5840 | for (i=2; i <= SCM_MAX_DBL_RADIX; ++i) |
0b799eea MV |
5841 | { |
5842 | init_dblprec(&scm_dblprec[i-2],i); | |
5843 | init_fx_radix(fx_per_radix[i-2],i); | |
5844 | } | |
f872b822 | 5845 | #ifdef DBL_DIG |
0b799eea MV |
5846 | /* hard code precision for base 10 if the preprocessor tells us to... */ |
5847 | scm_dblprec[10-2] = (DBL_DIG > 20) ? 20 : DBL_DIG; | |
5848 | #endif | |
1be6b49c ML |
5849 | |
5850 | #ifdef GUILE_DEBUG | |
5851 | check_sanity (); | |
5852 | #endif | |
f92e85f7 | 5853 | |
d956fa6f MV |
5854 | exactly_one_half = scm_permanent_object (scm_divide (SCM_I_MAKINUM (1), |
5855 | SCM_I_MAKINUM (2))); | |
a0599745 | 5856 | #include "libguile/numbers.x" |
0f2d19dd | 5857 | } |
89e00824 ML |
5858 | |
5859 | /* | |
5860 | Local Variables: | |
5861 | c-file-style: "gnu" | |
5862 | End: | |
5863 | */ |