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