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