Commit | Line | Data |
---|---|---|
07b390d5 | 1 | /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
03cce0ce MW |
2 | * 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
3 | * 2014 Free Software Foundation, Inc. | |
ba74ef4e MV |
4 | * |
5 | * Portions Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories | |
6 | * and Bellcore. See scm_divide. | |
7 | * | |
f81e080b | 8 | * |
73be1d9e | 9 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
10 | * modify it under the terms of the GNU Lesser General Public License |
11 | * as published by the Free Software Foundation; either version 3 of | |
12 | * the License, or (at your option) any later version. | |
0f2d19dd | 13 | * |
53befeb7 NJ |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | * Lesser General Public License for more details. | |
0f2d19dd | 18 | * |
73be1d9e MV |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | * 02110-1301 USA | |
73be1d9e | 23 | */ |
1bbd0b84 | 24 | |
0f2d19dd | 25 | \f |
ca46fb90 | 26 | /* General assumptions: |
ca46fb90 RB |
27 | * All objects satisfying SCM_BIGP() are too large to fit in a fixnum. |
28 | * If an object satisfies integer?, it's either an inum, a bignum, or a real. | |
29 | * If floor (r) == r, r is an int, and mpz_set_d will DTRT. | |
c7218482 | 30 | * XXX What about infinities? They are equal to their own floor! -mhw |
f92e85f7 | 31 | * All objects satisfying SCM_FRACTIONP are never an integer. |
ca46fb90 RB |
32 | */ |
33 | ||
34 | /* TODO: | |
35 | ||
36 | - see if special casing bignums and reals in integer-exponent when | |
37 | possible (to use mpz_pow and mpf_pow_ui) is faster. | |
38 | ||
39 | - look in to better short-circuiting of common cases in | |
40 | integer-expt and elsewhere. | |
41 | ||
42 | - see if direct mpz operations can help in ash and elsewhere. | |
43 | ||
44 | */ | |
0f2d19dd | 45 | |
dbb605f5 | 46 | #ifdef HAVE_CONFIG_H |
ee33d62a RB |
47 | # include <config.h> |
48 | #endif | |
49 | ||
bbec4602 | 50 | #include <verify.h> |
6f82b8f6 | 51 | #include <assert.h> |
bbec4602 | 52 | |
0f2d19dd | 53 | #include <math.h> |
fc194577 | 54 | #include <string.h> |
3f47e526 MG |
55 | #include <unicase.h> |
56 | #include <unictype.h> | |
f92e85f7 | 57 | |
8ab3d8a0 KR |
58 | #if HAVE_COMPLEX_H |
59 | #include <complex.h> | |
60 | #endif | |
61 | ||
07b390d5 LC |
62 | #include <stdarg.h> |
63 | ||
a0599745 | 64 | #include "libguile/_scm.h" |
a0599745 MD |
65 | #include "libguile/feature.h" |
66 | #include "libguile/ports.h" | |
67 | #include "libguile/root.h" | |
68 | #include "libguile/smob.h" | |
69 | #include "libguile/strings.h" | |
864e7d42 | 70 | #include "libguile/bdw-gc.h" |
a0599745 MD |
71 | |
72 | #include "libguile/validate.h" | |
73 | #include "libguile/numbers.h" | |
1be6b49c | 74 | #include "libguile/deprecation.h" |
f4c627b3 | 75 | |
f92e85f7 MV |
76 | #include "libguile/eq.h" |
77 | ||
8ab3d8a0 KR |
78 | /* values per glibc, if not already defined */ |
79 | #ifndef M_LOG10E | |
80 | #define M_LOG10E 0.43429448190325182765 | |
81 | #endif | |
85bdb6ac MW |
82 | #ifndef M_LN2 |
83 | #define M_LN2 0.69314718055994530942 | |
84 | #endif | |
8ab3d8a0 KR |
85 | #ifndef M_PI |
86 | #define M_PI 3.14159265358979323846 | |
87 | #endif | |
88 | ||
cba521fe MW |
89 | /* FIXME: We assume that FLT_RADIX is 2 */ |
90 | verify (FLT_RADIX == 2); | |
91 | ||
e25f3727 AW |
92 | typedef scm_t_signed_bits scm_t_inum; |
93 | #define scm_from_inum(x) (scm_from_signed_integer (x)) | |
94 | ||
4cc2e41c MW |
95 | /* Test an inum to see if it can be converted to a double without loss |
96 | of precision. Note that this will sometimes return 0 even when 1 | |
97 | could have been returned, e.g. for large powers of 2. It is designed | |
98 | to be a fast check to optimize common cases. */ | |
99 | #define INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE(n) \ | |
100 | (SCM_I_FIXNUM_BIT-1 <= DBL_MANT_DIG \ | |
101 | || ((n) ^ ((n) >> (SCM_I_FIXNUM_BIT-1))) < (1L << DBL_MANT_DIG)) | |
07b390d5 LC |
102 | |
103 | #if ! HAVE_DECL_MPZ_INITS | |
104 | ||
105 | /* GMP < 5.0.0 lacks `mpz_inits' and `mpz_clears'. Provide them. */ | |
106 | ||
107 | #define VARARG_MPZ_ITERATOR(func) \ | |
108 | static void \ | |
109 | func ## s (mpz_t x, ...) \ | |
110 | { \ | |
111 | va_list ap; \ | |
112 | \ | |
113 | va_start (ap, x); \ | |
114 | while (x != NULL) \ | |
115 | { \ | |
116 | func (x); \ | |
117 | x = va_arg (ap, mpz_ptr); \ | |
118 | } \ | |
119 | va_end (ap); \ | |
120 | } | |
121 | ||
122 | VARARG_MPZ_ITERATOR (mpz_init) | |
123 | VARARG_MPZ_ITERATOR (mpz_clear) | |
124 | ||
125 | #endif | |
126 | ||
0f2d19dd | 127 | \f |
f4c627b3 | 128 | |
ca46fb90 RB |
129 | /* |
130 | Wonder if this might be faster for some of our code? A switch on | |
131 | the numtag would jump directly to the right case, and the | |
132 | SCM_I_NUMTAG code might be faster than repeated SCM_FOOP tests... | |
133 | ||
134 | #define SCM_I_NUMTAG_NOTNUM 0 | |
135 | #define SCM_I_NUMTAG_INUM 1 | |
136 | #define SCM_I_NUMTAG_BIG scm_tc16_big | |
137 | #define SCM_I_NUMTAG_REAL scm_tc16_real | |
138 | #define SCM_I_NUMTAG_COMPLEX scm_tc16_complex | |
139 | #define SCM_I_NUMTAG(x) \ | |
e11e83f3 | 140 | (SCM_I_INUMP(x) ? SCM_I_NUMTAG_INUM \ |
ca46fb90 | 141 | : (SCM_IMP(x) ? SCM_I_NUMTAG_NOTNUM \ |
534c55a9 | 142 | : (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) ? SCM_TYP16(x) \ |
ca46fb90 RB |
143 | : SCM_I_NUMTAG_NOTNUM))) |
144 | */ | |
f92e85f7 | 145 | /* the macro above will not work as is with fractions */ |
f4c627b3 DH |
146 | |
147 | ||
b57bf272 AW |
148 | /* Default to 1, because as we used to hard-code `free' as the |
149 | deallocator, we know that overriding these functions with | |
150 | instrumented `malloc' / `free' is OK. */ | |
151 | int scm_install_gmp_memory_functions = 1; | |
e7efe8e7 | 152 | static SCM flo0; |
ff62c168 | 153 | static SCM exactly_one_half; |
a5f6b751 | 154 | static SCM flo_log10e; |
e7efe8e7 | 155 | |
34d19ef6 | 156 | #define SCM_SWAP(x, y) do { SCM __t = x; x = y; y = __t; } while (0) |
09fb7599 | 157 | |
56e55ac7 | 158 | /* FLOBUFLEN is the maximum number of characters neccessary for the |
3a9809df DH |
159 | * printed or scm_string representation of an inexact number. |
160 | */ | |
0b799eea | 161 | #define FLOBUFLEN (40+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10) |
3a9809df | 162 | |
b127c712 | 163 | |
ad79736c AW |
164 | #if !defined (HAVE_ASINH) |
165 | static double asinh (double x) { return log (x + sqrt (x * x + 1)); } | |
166 | #endif | |
167 | #if !defined (HAVE_ACOSH) | |
168 | static double acosh (double x) { return log (x + sqrt (x * x - 1)); } | |
169 | #endif | |
170 | #if !defined (HAVE_ATANH) | |
171 | static double atanh (double x) { return 0.5 * log ((1 + x) / (1 - x)); } | |
172 | #endif | |
173 | ||
18d78c5e MW |
174 | /* mpz_cmp_d in GMP before 4.2 didn't recognise infinities, so |
175 | xmpz_cmp_d uses an explicit check. Starting with GMP 4.2 (released | |
176 | in March 2006), mpz_cmp_d now handles infinities properly. */ | |
f8a8200b | 177 | #if 1 |
b127c712 | 178 | #define xmpz_cmp_d(z, d) \ |
2e65b52f | 179 | (isinf (d) ? (d < 0.0 ? 1 : -1) : mpz_cmp_d (z, d)) |
b127c712 KR |
180 | #else |
181 | #define xmpz_cmp_d(z, d) mpz_cmp_d (z, d) | |
182 | #endif | |
183 | ||
f92e85f7 | 184 | |
4b26c03e | 185 | #if defined (GUILE_I) |
03976fee | 186 | #if defined HAVE_COMPLEX_DOUBLE |
8ab3d8a0 KR |
187 | |
188 | /* For an SCM object Z which is a complex number (ie. satisfies | |
189 | SCM_COMPLEXP), return its value as a C level "complex double". */ | |
190 | #define SCM_COMPLEX_VALUE(z) \ | |
4b26c03e | 191 | (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z)) |
8ab3d8a0 | 192 | |
7a35784c | 193 | static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED; |
8ab3d8a0 KR |
194 | |
195 | /* Convert a C "complex double" to an SCM value. */ | |
7a35784c | 196 | static inline SCM |
8ab3d8a0 KR |
197 | scm_from_complex_double (complex double z) |
198 | { | |
199 | return scm_c_make_rectangular (creal (z), cimag (z)); | |
200 | } | |
bca69a9f | 201 | |
8ab3d8a0 | 202 | #endif /* HAVE_COMPLEX_DOUBLE */ |
bca69a9f | 203 | #endif /* GUILE_I */ |
8ab3d8a0 | 204 | |
0f2d19dd JB |
205 | \f |
206 | ||
713a4259 | 207 | static mpz_t z_negative_one; |
ac0c002c DH |
208 | |
209 | \f | |
b57bf272 | 210 | |
864e7d42 LC |
211 | /* Clear the `mpz_t' embedded in bignum PTR. */ |
212 | static void | |
6922d92f | 213 | finalize_bignum (void *ptr, void *data) |
864e7d42 LC |
214 | { |
215 | SCM bignum; | |
216 | ||
217 | bignum = PTR2SCM (ptr); | |
218 | mpz_clear (SCM_I_BIG_MPZ (bignum)); | |
219 | } | |
220 | ||
b57bf272 AW |
221 | /* The next three functions (custom_libgmp_*) are passed to |
222 | mp_set_memory_functions (in GMP) so that memory used by the digits | |
223 | themselves is known to the garbage collector. This is needed so | |
224 | that GC will be run at appropriate times. Otherwise, a program which | |
225 | creates many large bignums would malloc a huge amount of memory | |
226 | before the GC runs. */ | |
227 | static void * | |
228 | custom_gmp_malloc (size_t alloc_size) | |
229 | { | |
230 | return scm_malloc (alloc_size); | |
231 | } | |
232 | ||
233 | static void * | |
234 | custom_gmp_realloc (void *old_ptr, size_t old_size, size_t new_size) | |
235 | { | |
236 | return scm_realloc (old_ptr, new_size); | |
237 | } | |
238 | ||
239 | static void | |
240 | custom_gmp_free (void *ptr, size_t size) | |
241 | { | |
242 | free (ptr); | |
243 | } | |
244 | ||
245 | ||
d017fcdf LC |
246 | /* Return a new uninitialized bignum. */ |
247 | static inline SCM | |
248 | make_bignum (void) | |
249 | { | |
250 | scm_t_bits *p; | |
251 | ||
252 | /* Allocate one word for the type tag and enough room for an `mpz_t'. */ | |
253 | p = scm_gc_malloc_pointerless (sizeof (scm_t_bits) + sizeof (mpz_t), | |
254 | "bignum"); | |
255 | p[0] = scm_tc16_big; | |
256 | ||
75ba64d6 | 257 | scm_i_set_finalizer (p, finalize_bignum, NULL); |
864e7d42 | 258 | |
d017fcdf LC |
259 | return SCM_PACK (p); |
260 | } | |
ac0c002c | 261 | |
864e7d42 | 262 | |
189171c5 | 263 | SCM |
ca46fb90 RB |
264 | scm_i_mkbig () |
265 | { | |
266 | /* Return a newly created bignum. */ | |
d017fcdf | 267 | SCM z = make_bignum (); |
ca46fb90 RB |
268 | mpz_init (SCM_I_BIG_MPZ (z)); |
269 | return z; | |
270 | } | |
271 | ||
e25f3727 AW |
272 | static SCM |
273 | scm_i_inum2big (scm_t_inum x) | |
274 | { | |
275 | /* Return a newly created bignum initialized to X. */ | |
276 | SCM z = make_bignum (); | |
277 | #if SIZEOF_VOID_P == SIZEOF_LONG | |
278 | mpz_init_set_si (SCM_I_BIG_MPZ (z), x); | |
279 | #else | |
280 | /* Note that in this case, you'll also have to check all mpz_*_ui and | |
281 | mpz_*_si invocations in Guile. */ | |
282 | #error creation of mpz not implemented for this inum size | |
283 | #endif | |
284 | return z; | |
285 | } | |
286 | ||
189171c5 | 287 | SCM |
c71b0706 MV |
288 | scm_i_long2big (long x) |
289 | { | |
290 | /* Return a newly created bignum initialized to X. */ | |
d017fcdf | 291 | SCM z = make_bignum (); |
c71b0706 MV |
292 | mpz_init_set_si (SCM_I_BIG_MPZ (z), x); |
293 | return z; | |
294 | } | |
295 | ||
189171c5 | 296 | SCM |
c71b0706 MV |
297 | scm_i_ulong2big (unsigned long x) |
298 | { | |
299 | /* Return a newly created bignum initialized to X. */ | |
d017fcdf | 300 | SCM z = make_bignum (); |
c71b0706 MV |
301 | mpz_init_set_ui (SCM_I_BIG_MPZ (z), x); |
302 | return z; | |
303 | } | |
304 | ||
189171c5 | 305 | SCM |
ca46fb90 RB |
306 | scm_i_clonebig (SCM src_big, int same_sign_p) |
307 | { | |
308 | /* Copy src_big's value, negate it if same_sign_p is false, and return. */ | |
d017fcdf | 309 | SCM z = make_bignum (); |
ca46fb90 | 310 | mpz_init_set (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (src_big)); |
0aacf84e MD |
311 | if (!same_sign_p) |
312 | mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z)); | |
ca46fb90 RB |
313 | return z; |
314 | } | |
315 | ||
189171c5 | 316 | int |
ca46fb90 RB |
317 | scm_i_bigcmp (SCM x, SCM y) |
318 | { | |
319 | /* Return neg if x < y, pos if x > y, and 0 if x == y */ | |
320 | /* presume we already know x and y are bignums */ | |
321 | int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
322 | scm_remember_upto_here_2 (x, y); | |
323 | return result; | |
324 | } | |
325 | ||
189171c5 | 326 | SCM |
ca46fb90 RB |
327 | scm_i_dbl2big (double d) |
328 | { | |
329 | /* results are only defined if d is an integer */ | |
d017fcdf | 330 | SCM z = make_bignum (); |
ca46fb90 RB |
331 | mpz_init_set_d (SCM_I_BIG_MPZ (z), d); |
332 | return z; | |
333 | } | |
334 | ||
f92e85f7 MV |
335 | /* Convert a integer in double representation to a SCM number. */ |
336 | ||
189171c5 | 337 | SCM |
f92e85f7 MV |
338 | scm_i_dbl2num (double u) |
339 | { | |
340 | /* SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM are both | |
341 | powers of 2, so there's no rounding when making "double" values | |
342 | from them. If plain SCM_MOST_POSITIVE_FIXNUM was used it could | |
343 | get rounded on a 64-bit machine, hence the "+1". | |
344 | ||
345 | The use of floor() to force to an integer value ensures we get a | |
346 | "numerically closest" value without depending on how a | |
347 | double->long cast or how mpz_set_d will round. For reference, | |
348 | double->long probably follows the hardware rounding mode, | |
349 | mpz_set_d truncates towards zero. */ | |
350 | ||
351 | /* XXX - what happens when SCM_MOST_POSITIVE_FIXNUM etc is not | |
352 | representable as a double? */ | |
353 | ||
354 | if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1) | |
355 | && u >= (double) SCM_MOST_NEGATIVE_FIXNUM) | |
e25f3727 | 356 | return SCM_I_MAKINUM ((scm_t_inum) u); |
f92e85f7 MV |
357 | else |
358 | return scm_i_dbl2big (u); | |
359 | } | |
360 | ||
1eb6a33a MW |
361 | static SCM round_right_shift_exact_integer (SCM n, long count); |
362 | ||
363 | /* scm_i_big2dbl_2exp() is like frexp for bignums: it converts the | |
364 | bignum b into a normalized significand and exponent such that | |
365 | b = significand * 2^exponent and 1/2 <= abs(significand) < 1. | |
366 | The return value is the significand rounded to the closest | |
367 | representable double, and the exponent is placed into *expon_p. | |
368 | If b is zero, then the returned exponent and significand are both | |
369 | zero. */ | |
370 | ||
371 | static double | |
372 | scm_i_big2dbl_2exp (SCM b, long *expon_p) | |
ca46fb90 | 373 | { |
1eb6a33a MW |
374 | size_t bits = mpz_sizeinbase (SCM_I_BIG_MPZ (b), 2); |
375 | size_t shift = 0; | |
089c9a59 KR |
376 | |
377 | if (bits > DBL_MANT_DIG) | |
378 | { | |
1eb6a33a MW |
379 | shift = bits - DBL_MANT_DIG; |
380 | b = round_right_shift_exact_integer (b, shift); | |
381 | if (SCM_I_INUMP (b)) | |
089c9a59 | 382 | { |
1eb6a33a MW |
383 | int expon; |
384 | double signif = frexp (SCM_I_INUM (b), &expon); | |
385 | *expon_p = expon + shift; | |
386 | return signif; | |
089c9a59 KR |
387 | } |
388 | } | |
389 | ||
1eb6a33a MW |
390 | { |
391 | long expon; | |
392 | double signif = mpz_get_d_2exp (&expon, SCM_I_BIG_MPZ (b)); | |
393 | scm_remember_upto_here_1 (b); | |
394 | *expon_p = expon + shift; | |
395 | return signif; | |
396 | } | |
397 | } | |
398 | ||
399 | /* scm_i_big2dbl() rounds to the closest representable double, | |
400 | in accordance with R5RS exact->inexact. */ | |
401 | double | |
402 | scm_i_big2dbl (SCM b) | |
403 | { | |
404 | long expon; | |
405 | double signif = scm_i_big2dbl_2exp (b, &expon); | |
406 | return ldexp (signif, expon); | |
ca46fb90 RB |
407 | } |
408 | ||
189171c5 | 409 | SCM |
ca46fb90 RB |
410 | scm_i_normbig (SCM b) |
411 | { | |
412 | /* convert a big back to a fixnum if it'll fit */ | |
413 | /* presume b is a bignum */ | |
414 | if (mpz_fits_slong_p (SCM_I_BIG_MPZ (b))) | |
415 | { | |
e25f3727 | 416 | scm_t_inum val = mpz_get_si (SCM_I_BIG_MPZ (b)); |
ca46fb90 | 417 | if (SCM_FIXABLE (val)) |
d956fa6f | 418 | b = SCM_I_MAKINUM (val); |
ca46fb90 RB |
419 | } |
420 | return b; | |
421 | } | |
f872b822 | 422 | |
f92e85f7 MV |
423 | static SCM_C_INLINE_KEYWORD SCM |
424 | scm_i_mpz2num (mpz_t b) | |
425 | { | |
426 | /* convert a mpz number to a SCM number. */ | |
427 | if (mpz_fits_slong_p (b)) | |
428 | { | |
e25f3727 | 429 | scm_t_inum val = mpz_get_si (b); |
f92e85f7 | 430 | if (SCM_FIXABLE (val)) |
d956fa6f | 431 | return SCM_I_MAKINUM (val); |
f92e85f7 MV |
432 | } |
433 | ||
434 | { | |
d017fcdf | 435 | SCM z = make_bignum (); |
f92e85f7 MV |
436 | mpz_init_set (SCM_I_BIG_MPZ (z), b); |
437 | return z; | |
438 | } | |
439 | } | |
440 | ||
a285b18c MW |
441 | /* Make the ratio NUMERATOR/DENOMINATOR, where: |
442 | 1. NUMERATOR and DENOMINATOR are exact integers | |
443 | 2. NUMERATOR and DENOMINATOR are reduced to lowest terms: gcd(n,d) == 1 */ | |
cba42c93 | 444 | static SCM |
a285b18c | 445 | scm_i_make_ratio_already_reduced (SCM numerator, SCM denominator) |
f92e85f7 | 446 | { |
a285b18c MW |
447 | /* Flip signs so that the denominator is positive. */ |
448 | if (scm_is_false (scm_positive_p (denominator))) | |
f92e85f7 | 449 | { |
a285b18c | 450 | if (SCM_UNLIKELY (scm_is_eq (denominator, SCM_INUM0))) |
f92e85f7 | 451 | scm_num_overflow ("make-ratio"); |
a285b18c | 452 | else |
f92e85f7 | 453 | { |
a285b18c MW |
454 | numerator = scm_difference (numerator, SCM_UNDEFINED); |
455 | denominator = scm_difference (denominator, SCM_UNDEFINED); | |
f92e85f7 MV |
456 | } |
457 | } | |
a285b18c MW |
458 | |
459 | /* Check for the integer case */ | |
460 | if (scm_is_eq (denominator, SCM_INUM1)) | |
461 | return numerator; | |
462 | ||
463 | return scm_double_cell (scm_tc16_fraction, | |
464 | SCM_UNPACK (numerator), | |
465 | SCM_UNPACK (denominator), 0); | |
466 | } | |
467 | ||
468 | static SCM scm_exact_integer_quotient (SCM x, SCM y); | |
469 | ||
470 | /* Make the ratio NUMERATOR/DENOMINATOR */ | |
471 | static SCM | |
472 | scm_i_make_ratio (SCM numerator, SCM denominator) | |
473 | #define FUNC_NAME "make-ratio" | |
474 | { | |
475 | /* Make sure the arguments are proper */ | |
476 | if (!SCM_LIKELY (SCM_I_INUMP (numerator) || SCM_BIGP (numerator))) | |
477 | SCM_WRONG_TYPE_ARG (1, numerator); | |
478 | else if (!SCM_LIKELY (SCM_I_INUMP (denominator) || SCM_BIGP (denominator))) | |
479 | SCM_WRONG_TYPE_ARG (2, denominator); | |
480 | else | |
f92e85f7 | 481 | { |
a285b18c MW |
482 | SCM the_gcd = scm_gcd (numerator, denominator); |
483 | if (!(scm_is_eq (the_gcd, SCM_INUM1))) | |
c60e130c | 484 | { |
a285b18c MW |
485 | /* Reduce to lowest terms */ |
486 | numerator = scm_exact_integer_quotient (numerator, the_gcd); | |
487 | denominator = scm_exact_integer_quotient (denominator, the_gcd); | |
f92e85f7 | 488 | } |
a285b18c | 489 | return scm_i_make_ratio_already_reduced (numerator, denominator); |
f92e85f7 | 490 | } |
f92e85f7 | 491 | } |
c60e130c | 492 | #undef FUNC_NAME |
f92e85f7 | 493 | |
98237784 MW |
494 | static mpz_t scm_i_divide2double_lo2b; |
495 | ||
496 | /* Return the double that is closest to the exact rational N/D, with | |
497 | ties rounded toward even mantissas. N and D must be exact | |
498 | integers. */ | |
499 | static double | |
500 | scm_i_divide2double (SCM n, SCM d) | |
501 | { | |
502 | int neg; | |
503 | mpz_t nn, dd, lo, hi, x; | |
504 | ssize_t e; | |
505 | ||
c8248c8e | 506 | if (SCM_LIKELY (SCM_I_INUMP (d))) |
98237784 | 507 | { |
4cc2e41c MW |
508 | if (SCM_LIKELY |
509 | (SCM_I_INUMP (n) | |
510 | && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (n)) | |
511 | && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (d)))) | |
c8248c8e MW |
512 | /* If both N and D can be losslessly converted to doubles, then |
513 | we can rely on IEEE floating point to do proper rounding much | |
514 | faster than we can. */ | |
515 | return ((double) SCM_I_INUM (n)) / ((double) SCM_I_INUM (d)); | |
516 | ||
98237784 MW |
517 | if (SCM_UNLIKELY (scm_is_eq (d, SCM_INUM0))) |
518 | { | |
519 | if (scm_is_true (scm_positive_p (n))) | |
520 | return 1.0 / 0.0; | |
521 | else if (scm_is_true (scm_negative_p (n))) | |
522 | return -1.0 / 0.0; | |
523 | else | |
524 | return 0.0 / 0.0; | |
525 | } | |
c8248c8e | 526 | |
98237784 MW |
527 | mpz_init_set_si (dd, SCM_I_INUM (d)); |
528 | } | |
529 | else | |
530 | mpz_init_set (dd, SCM_I_BIG_MPZ (d)); | |
531 | ||
532 | if (SCM_I_INUMP (n)) | |
533 | mpz_init_set_si (nn, SCM_I_INUM (n)); | |
534 | else | |
535 | mpz_init_set (nn, SCM_I_BIG_MPZ (n)); | |
536 | ||
537 | neg = (mpz_sgn (nn) < 0) ^ (mpz_sgn (dd) < 0); | |
538 | mpz_abs (nn, nn); | |
539 | mpz_abs (dd, dd); | |
540 | ||
541 | /* Now we need to find the value of e such that: | |
542 | ||
543 | For e <= 0: | |
544 | b^{p-1} - 1/2b <= b^-e n / d < b^p - 1/2 [1A] | |
545 | (2 b^p - 1) <= 2 b b^-e n / d < (2 b^p - 1) b [2A] | |
546 | (2 b^p - 1) d <= 2 b b^-e n < (2 b^p - 1) d b [3A] | |
547 | ||
548 | For e >= 0: | |
549 | b^{p-1} - 1/2b <= n / b^e d < b^p - 1/2 [1B] | |
550 | (2 b^p - 1) <= 2 b n / b^e d < (2 b^p - 1) b [2B] | |
551 | (2 b^p - 1) d b^e <= 2 b n < (2 b^p - 1) d b b^e [3B] | |
552 | ||
553 | where: p = DBL_MANT_DIG | |
554 | b = FLT_RADIX (here assumed to be 2) | |
555 | ||
556 | After rounding, the mantissa must be an integer between b^{p-1} and | |
557 | (b^p - 1), except for subnormal numbers. In the inequations [1A] | |
558 | and [1B], the middle expression represents the mantissa *before* | |
559 | rounding, and therefore is bounded by the range of values that will | |
560 | round to a floating-point number with the exponent e. The upper | |
561 | bound is (b^p - 1 + 1/2) = (b^p - 1/2), and is exclusive because | |
562 | ties will round up to the next power of b. The lower bound is | |
563 | (b^{p-1} - 1/2b), and is inclusive because ties will round toward | |
564 | this power of b. Here we subtract 1/2b instead of 1/2 because it | |
565 | is in the range of the next smaller exponent, where the | |
566 | representable numbers are closer together by a factor of b. | |
567 | ||
568 | Inequations [2A] and [2B] are derived from [1A] and [1B] by | |
569 | multiplying by 2b, and in [3A] and [3B] we multiply by the | |
570 | denominator of the middle value to obtain integer expressions. | |
571 | ||
572 | In the code below, we refer to the three expressions in [3A] or | |
573 | [3B] as lo, x, and hi. If the number is normalizable, we will | |
574 | achieve the goal: lo <= x < hi */ | |
575 | ||
576 | /* Make an initial guess for e */ | |
577 | e = mpz_sizeinbase (nn, 2) - mpz_sizeinbase (dd, 2) - (DBL_MANT_DIG-1); | |
578 | if (e < DBL_MIN_EXP - DBL_MANT_DIG) | |
579 | e = DBL_MIN_EXP - DBL_MANT_DIG; | |
580 | ||
581 | /* Compute the initial values of lo, x, and hi | |
582 | based on the initial guess of e */ | |
583 | mpz_inits (lo, hi, x, NULL); | |
584 | mpz_mul_2exp (x, nn, 2 + ((e < 0) ? -e : 0)); | |
585 | mpz_mul (lo, dd, scm_i_divide2double_lo2b); | |
586 | if (e > 0) | |
587 | mpz_mul_2exp (lo, lo, e); | |
588 | mpz_mul_2exp (hi, lo, 1); | |
589 | ||
590 | /* Adjust e as needed to satisfy the inequality lo <= x < hi, | |
591 | (but without making e less then the minimum exponent) */ | |
592 | while (mpz_cmp (x, lo) < 0 && e > DBL_MIN_EXP - DBL_MANT_DIG) | |
593 | { | |
594 | mpz_mul_2exp (x, x, 1); | |
595 | e--; | |
596 | } | |
597 | while (mpz_cmp (x, hi) >= 0) | |
598 | { | |
599 | /* If we ever used lo's value again, | |
600 | we would need to double lo here. */ | |
601 | mpz_mul_2exp (hi, hi, 1); | |
602 | e++; | |
603 | } | |
604 | ||
605 | /* Now compute the rounded mantissa: | |
606 | n / b^e d (if e >= 0) | |
607 | n b^-e / d (if e <= 0) */ | |
608 | { | |
609 | int cmp; | |
610 | double result; | |
611 | ||
612 | if (e < 0) | |
613 | mpz_mul_2exp (nn, nn, -e); | |
614 | else | |
615 | mpz_mul_2exp (dd, dd, e); | |
616 | ||
617 | /* mpz does not directly support rounded right | |
618 | shifts, so we have to do it the hard way. | |
619 | For efficiency, we reuse lo and hi. | |
620 | hi == quotient, lo == remainder */ | |
621 | mpz_fdiv_qr (hi, lo, nn, dd); | |
622 | ||
623 | /* The fractional part of the unrounded mantissa would be | |
624 | remainder/dividend, i.e. lo/dd. So we have a tie if | |
625 | lo/dd = 1/2. Multiplying both sides by 2*dd yields the | |
626 | integer expression 2*lo = dd. Here we do that comparison | |
627 | to decide whether to round up or down. */ | |
628 | mpz_mul_2exp (lo, lo, 1); | |
629 | cmp = mpz_cmp (lo, dd); | |
630 | if (cmp > 0 || (cmp == 0 && mpz_odd_p (hi))) | |
631 | mpz_add_ui (hi, hi, 1); | |
632 | ||
633 | result = ldexp (mpz_get_d (hi), e); | |
634 | if (neg) | |
635 | result = -result; | |
636 | ||
637 | mpz_clears (nn, dd, lo, hi, x, NULL); | |
638 | return result; | |
639 | } | |
640 | } | |
641 | ||
f92e85f7 MV |
642 | double |
643 | scm_i_fraction2double (SCM z) | |
644 | { | |
98237784 MW |
645 | return scm_i_divide2double (SCM_FRACTION_NUMERATOR (z), |
646 | SCM_FRACTION_DENOMINATOR (z)); | |
f92e85f7 MV |
647 | } |
648 | ||
00472a22 MW |
649 | static SCM |
650 | scm_i_from_double (double val) | |
651 | { | |
652 | SCM z; | |
653 | ||
654 | z = PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_double), "real")); | |
655 | ||
656 | SCM_SET_CELL_TYPE (z, scm_tc16_real); | |
657 | SCM_REAL_VALUE (z) = val; | |
658 | ||
659 | return z; | |
660 | } | |
661 | ||
2519490c MW |
662 | SCM_PRIMITIVE_GENERIC (scm_exact_p, "exact?", 1, 0, 0, |
663 | (SCM x), | |
942e5b91 MG |
664 | "Return @code{#t} if @var{x} is an exact number, @code{#f}\n" |
665 | "otherwise.") | |
1bbd0b84 | 666 | #define FUNC_NAME s_scm_exact_p |
0f2d19dd | 667 | { |
41df63cf MW |
668 | if (SCM_INEXACTP (x)) |
669 | return SCM_BOOL_F; | |
670 | else if (SCM_NUMBERP (x)) | |
0aacf84e | 671 | return SCM_BOOL_T; |
41df63cf | 672 | else |
2519490c | 673 | SCM_WTA_DISPATCH_1 (g_scm_exact_p, x, 1, s_scm_exact_p); |
41df63cf MW |
674 | } |
675 | #undef FUNC_NAME | |
676 | ||
022dda69 MG |
677 | int |
678 | scm_is_exact (SCM val) | |
679 | { | |
680 | return scm_is_true (scm_exact_p (val)); | |
681 | } | |
41df63cf | 682 | |
2519490c | 683 | SCM_PRIMITIVE_GENERIC (scm_inexact_p, "inexact?", 1, 0, 0, |
41df63cf MW |
684 | (SCM x), |
685 | "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n" | |
686 | "else.") | |
687 | #define FUNC_NAME s_scm_inexact_p | |
688 | { | |
689 | if (SCM_INEXACTP (x)) | |
f92e85f7 | 690 | return SCM_BOOL_T; |
41df63cf | 691 | else if (SCM_NUMBERP (x)) |
eb927cb9 | 692 | return SCM_BOOL_F; |
41df63cf | 693 | else |
2519490c | 694 | SCM_WTA_DISPATCH_1 (g_scm_inexact_p, x, 1, s_scm_inexact_p); |
0f2d19dd | 695 | } |
1bbd0b84 | 696 | #undef FUNC_NAME |
0f2d19dd | 697 | |
022dda69 MG |
698 | int |
699 | scm_is_inexact (SCM val) | |
700 | { | |
701 | return scm_is_true (scm_inexact_p (val)); | |
702 | } | |
4219f20d | 703 | |
2519490c | 704 | SCM_PRIMITIVE_GENERIC (scm_odd_p, "odd?", 1, 0, 0, |
1bbd0b84 | 705 | (SCM n), |
942e5b91 MG |
706 | "Return @code{#t} if @var{n} is an odd number, @code{#f}\n" |
707 | "otherwise.") | |
1bbd0b84 | 708 | #define FUNC_NAME s_scm_odd_p |
0f2d19dd | 709 | { |
e11e83f3 | 710 | if (SCM_I_INUMP (n)) |
0aacf84e | 711 | { |
e25f3727 | 712 | scm_t_inum val = SCM_I_INUM (n); |
73e4de09 | 713 | return scm_from_bool ((val & 1L) != 0); |
0aacf84e MD |
714 | } |
715 | else if (SCM_BIGP (n)) | |
716 | { | |
717 | int odd_p = mpz_odd_p (SCM_I_BIG_MPZ (n)); | |
718 | scm_remember_upto_here_1 (n); | |
73e4de09 | 719 | return scm_from_bool (odd_p); |
0aacf84e | 720 | } |
f92e85f7 MV |
721 | else if (SCM_REALP (n)) |
722 | { | |
2519490c | 723 | double val = SCM_REAL_VALUE (n); |
19374ad2 | 724 | if (isfinite (val)) |
2519490c MW |
725 | { |
726 | double rem = fabs (fmod (val, 2.0)); | |
727 | if (rem == 1.0) | |
728 | return SCM_BOOL_T; | |
729 | else if (rem == 0.0) | |
730 | return SCM_BOOL_F; | |
731 | } | |
f92e85f7 | 732 | } |
2519490c | 733 | SCM_WTA_DISPATCH_1 (g_scm_odd_p, n, 1, s_scm_odd_p); |
0f2d19dd | 734 | } |
1bbd0b84 | 735 | #undef FUNC_NAME |
0f2d19dd | 736 | |
4219f20d | 737 | |
2519490c | 738 | SCM_PRIMITIVE_GENERIC (scm_even_p, "even?", 1, 0, 0, |
1bbd0b84 | 739 | (SCM n), |
942e5b91 MG |
740 | "Return @code{#t} if @var{n} is an even number, @code{#f}\n" |
741 | "otherwise.") | |
1bbd0b84 | 742 | #define FUNC_NAME s_scm_even_p |
0f2d19dd | 743 | { |
e11e83f3 | 744 | if (SCM_I_INUMP (n)) |
0aacf84e | 745 | { |
e25f3727 | 746 | scm_t_inum val = SCM_I_INUM (n); |
73e4de09 | 747 | return scm_from_bool ((val & 1L) == 0); |
0aacf84e MD |
748 | } |
749 | else if (SCM_BIGP (n)) | |
750 | { | |
751 | int even_p = mpz_even_p (SCM_I_BIG_MPZ (n)); | |
752 | scm_remember_upto_here_1 (n); | |
73e4de09 | 753 | return scm_from_bool (even_p); |
0aacf84e | 754 | } |
f92e85f7 MV |
755 | else if (SCM_REALP (n)) |
756 | { | |
2519490c | 757 | double val = SCM_REAL_VALUE (n); |
19374ad2 | 758 | if (isfinite (val)) |
2519490c MW |
759 | { |
760 | double rem = fabs (fmod (val, 2.0)); | |
761 | if (rem == 1.0) | |
762 | return SCM_BOOL_F; | |
763 | else if (rem == 0.0) | |
764 | return SCM_BOOL_T; | |
765 | } | |
f92e85f7 | 766 | } |
2519490c | 767 | SCM_WTA_DISPATCH_1 (g_scm_even_p, n, 1, s_scm_even_p); |
0f2d19dd | 768 | } |
1bbd0b84 | 769 | #undef FUNC_NAME |
0f2d19dd | 770 | |
2519490c MW |
771 | SCM_PRIMITIVE_GENERIC (scm_finite_p, "finite?", 1, 0, 0, |
772 | (SCM x), | |
10391e06 AW |
773 | "Return @code{#t} if the real number @var{x} is neither\n" |
774 | "infinite nor a NaN, @code{#f} otherwise.") | |
7112615f MW |
775 | #define FUNC_NAME s_scm_finite_p |
776 | { | |
777 | if (SCM_REALP (x)) | |
19374ad2 | 778 | return scm_from_bool (isfinite (SCM_REAL_VALUE (x))); |
10391e06 | 779 | else if (scm_is_real (x)) |
7112615f MW |
780 | return SCM_BOOL_T; |
781 | else | |
2519490c | 782 | SCM_WTA_DISPATCH_1 (g_scm_finite_p, x, 1, s_scm_finite_p); |
7112615f MW |
783 | } |
784 | #undef FUNC_NAME | |
785 | ||
2519490c MW |
786 | SCM_PRIMITIVE_GENERIC (scm_inf_p, "inf?", 1, 0, 0, |
787 | (SCM x), | |
788 | "Return @code{#t} if the real number @var{x} is @samp{+inf.0} or\n" | |
789 | "@samp{-inf.0}. Otherwise return @code{#f}.") | |
7351e207 MV |
790 | #define FUNC_NAME s_scm_inf_p |
791 | { | |
b1092b3a | 792 | if (SCM_REALP (x)) |
2e65b52f | 793 | return scm_from_bool (isinf (SCM_REAL_VALUE (x))); |
10391e06 | 794 | else if (scm_is_real (x)) |
7351e207 | 795 | return SCM_BOOL_F; |
10391e06 | 796 | else |
2519490c | 797 | SCM_WTA_DISPATCH_1 (g_scm_inf_p, x, 1, s_scm_inf_p); |
7351e207 MV |
798 | } |
799 | #undef FUNC_NAME | |
800 | ||
2519490c MW |
801 | SCM_PRIMITIVE_GENERIC (scm_nan_p, "nan?", 1, 0, 0, |
802 | (SCM x), | |
10391e06 AW |
803 | "Return @code{#t} if the real number @var{x} is a NaN,\n" |
804 | "or @code{#f} otherwise.") | |
7351e207 MV |
805 | #define FUNC_NAME s_scm_nan_p |
806 | { | |
10391e06 AW |
807 | if (SCM_REALP (x)) |
808 | return scm_from_bool (isnan (SCM_REAL_VALUE (x))); | |
809 | else if (scm_is_real (x)) | |
7351e207 | 810 | return SCM_BOOL_F; |
10391e06 | 811 | else |
2519490c | 812 | SCM_WTA_DISPATCH_1 (g_scm_nan_p, x, 1, s_scm_nan_p); |
7351e207 MV |
813 | } |
814 | #undef FUNC_NAME | |
815 | ||
816 | /* Guile's idea of infinity. */ | |
817 | static double guile_Inf; | |
818 | ||
819 | /* Guile's idea of not a number. */ | |
820 | static double guile_NaN; | |
821 | ||
822 | static void | |
823 | guile_ieee_init (void) | |
824 | { | |
7351e207 MV |
825 | /* Some version of gcc on some old version of Linux used to crash when |
826 | trying to make Inf and NaN. */ | |
827 | ||
240a27d2 KR |
828 | #ifdef INFINITY |
829 | /* C99 INFINITY, when available. | |
830 | FIXME: The standard allows for INFINITY to be something that overflows | |
831 | at compile time. We ought to have a configure test to check for that | |
832 | before trying to use it. (But in practice we believe this is not a | |
833 | problem on any system guile is likely to target.) */ | |
834 | guile_Inf = INFINITY; | |
56a3dcd4 | 835 | #elif defined HAVE_DINFINITY |
240a27d2 | 836 | /* OSF */ |
7351e207 | 837 | extern unsigned int DINFINITY[2]; |
eaa94eaa | 838 | guile_Inf = (*((double *) (DINFINITY))); |
7351e207 MV |
839 | #else |
840 | double tmp = 1e+10; | |
841 | guile_Inf = tmp; | |
842 | for (;;) | |
843 | { | |
844 | guile_Inf *= 1e+10; | |
845 | if (guile_Inf == tmp) | |
846 | break; | |
847 | tmp = guile_Inf; | |
848 | } | |
849 | #endif | |
850 | ||
240a27d2 KR |
851 | #ifdef NAN |
852 | /* C99 NAN, when available */ | |
853 | guile_NaN = NAN; | |
56a3dcd4 | 854 | #elif defined HAVE_DQNAN |
eaa94eaa LC |
855 | { |
856 | /* OSF */ | |
857 | extern unsigned int DQNAN[2]; | |
858 | guile_NaN = (*((double *)(DQNAN))); | |
859 | } | |
7351e207 MV |
860 | #else |
861 | guile_NaN = guile_Inf / guile_Inf; | |
862 | #endif | |
7351e207 MV |
863 | } |
864 | ||
865 | SCM_DEFINE (scm_inf, "inf", 0, 0, 0, | |
866 | (void), | |
867 | "Return Inf.") | |
868 | #define FUNC_NAME s_scm_inf | |
869 | { | |
870 | static int initialized = 0; | |
871 | if (! initialized) | |
872 | { | |
873 | guile_ieee_init (); | |
874 | initialized = 1; | |
875 | } | |
00472a22 | 876 | return scm_i_from_double (guile_Inf); |
7351e207 MV |
877 | } |
878 | #undef FUNC_NAME | |
879 | ||
880 | SCM_DEFINE (scm_nan, "nan", 0, 0, 0, | |
881 | (void), | |
882 | "Return NaN.") | |
883 | #define FUNC_NAME s_scm_nan | |
884 | { | |
885 | static int initialized = 0; | |
0aacf84e | 886 | if (!initialized) |
7351e207 MV |
887 | { |
888 | guile_ieee_init (); | |
889 | initialized = 1; | |
890 | } | |
00472a22 | 891 | return scm_i_from_double (guile_NaN); |
7351e207 MV |
892 | } |
893 | #undef FUNC_NAME | |
894 | ||
4219f20d | 895 | |
a48d60b1 MD |
896 | SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0, |
897 | (SCM x), | |
898 | "Return the absolute value of @var{x}.") | |
2519490c | 899 | #define FUNC_NAME s_scm_abs |
0f2d19dd | 900 | { |
e11e83f3 | 901 | if (SCM_I_INUMP (x)) |
0aacf84e | 902 | { |
e25f3727 | 903 | scm_t_inum xx = SCM_I_INUM (x); |
0aacf84e MD |
904 | if (xx >= 0) |
905 | return x; | |
906 | else if (SCM_POSFIXABLE (-xx)) | |
d956fa6f | 907 | return SCM_I_MAKINUM (-xx); |
0aacf84e | 908 | else |
e25f3727 | 909 | return scm_i_inum2big (-xx); |
4219f20d | 910 | } |
9b9ef10c MW |
911 | else if (SCM_LIKELY (SCM_REALP (x))) |
912 | { | |
913 | double xx = SCM_REAL_VALUE (x); | |
914 | /* If x is a NaN then xx<0 is false so we return x unchanged */ | |
915 | if (xx < 0.0) | |
00472a22 | 916 | return scm_i_from_double (-xx); |
9b9ef10c MW |
917 | /* Handle signed zeroes properly */ |
918 | else if (SCM_UNLIKELY (xx == 0.0)) | |
919 | return flo0; | |
920 | else | |
921 | return x; | |
922 | } | |
0aacf84e MD |
923 | else if (SCM_BIGP (x)) |
924 | { | |
925 | const int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
926 | if (sgn < 0) | |
927 | return scm_i_clonebig (x, 0); | |
928 | else | |
929 | return x; | |
4219f20d | 930 | } |
f92e85f7 MV |
931 | else if (SCM_FRACTIONP (x)) |
932 | { | |
73e4de09 | 933 | if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x)))) |
f92e85f7 | 934 | return x; |
a285b18c MW |
935 | return scm_i_make_ratio_already_reduced |
936 | (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED), | |
937 | SCM_FRACTION_DENOMINATOR (x)); | |
f92e85f7 | 938 | } |
0aacf84e | 939 | else |
a48d60b1 | 940 | SCM_WTA_DISPATCH_1 (g_scm_abs, x, 1, s_scm_abs); |
0f2d19dd | 941 | } |
a48d60b1 | 942 | #undef FUNC_NAME |
0f2d19dd | 943 | |
4219f20d | 944 | |
2519490c MW |
945 | SCM_PRIMITIVE_GENERIC (scm_quotient, "quotient", 2, 0, 0, |
946 | (SCM x, SCM y), | |
947 | "Return the quotient of the numbers @var{x} and @var{y}.") | |
948 | #define FUNC_NAME s_scm_quotient | |
0f2d19dd | 949 | { |
495a39c4 | 950 | if (SCM_LIKELY (scm_is_integer (x))) |
0aacf84e | 951 | { |
495a39c4 | 952 | if (SCM_LIKELY (scm_is_integer (y))) |
a8da6d93 | 953 | return scm_truncate_quotient (x, y); |
0aacf84e | 954 | else |
2519490c | 955 | SCM_WTA_DISPATCH_2 (g_scm_quotient, x, y, SCM_ARG2, s_scm_quotient); |
f872b822 | 956 | } |
0aacf84e | 957 | else |
2519490c | 958 | SCM_WTA_DISPATCH_2 (g_scm_quotient, x, y, SCM_ARG1, s_scm_quotient); |
0f2d19dd | 959 | } |
2519490c | 960 | #undef FUNC_NAME |
0f2d19dd | 961 | |
2519490c MW |
962 | SCM_PRIMITIVE_GENERIC (scm_remainder, "remainder", 2, 0, 0, |
963 | (SCM x, SCM y), | |
964 | "Return the remainder of the numbers @var{x} and @var{y}.\n" | |
965 | "@lisp\n" | |
966 | "(remainder 13 4) @result{} 1\n" | |
967 | "(remainder -13 4) @result{} -1\n" | |
968 | "@end lisp") | |
969 | #define FUNC_NAME s_scm_remainder | |
0f2d19dd | 970 | { |
495a39c4 | 971 | if (SCM_LIKELY (scm_is_integer (x))) |
0aacf84e | 972 | { |
495a39c4 | 973 | if (SCM_LIKELY (scm_is_integer (y))) |
a8da6d93 | 974 | return scm_truncate_remainder (x, y); |
0aacf84e | 975 | else |
2519490c | 976 | SCM_WTA_DISPATCH_2 (g_scm_remainder, x, y, SCM_ARG2, s_scm_remainder); |
f872b822 | 977 | } |
0aacf84e | 978 | else |
2519490c | 979 | SCM_WTA_DISPATCH_2 (g_scm_remainder, x, y, SCM_ARG1, s_scm_remainder); |
0f2d19dd | 980 | } |
2519490c | 981 | #undef FUNC_NAME |
0f2d19dd | 982 | |
89a7e495 | 983 | |
2519490c MW |
984 | SCM_PRIMITIVE_GENERIC (scm_modulo, "modulo", 2, 0, 0, |
985 | (SCM x, SCM y), | |
986 | "Return the modulo of the numbers @var{x} and @var{y}.\n" | |
987 | "@lisp\n" | |
988 | "(modulo 13 4) @result{} 1\n" | |
989 | "(modulo -13 4) @result{} 3\n" | |
990 | "@end lisp") | |
991 | #define FUNC_NAME s_scm_modulo | |
0f2d19dd | 992 | { |
495a39c4 | 993 | if (SCM_LIKELY (scm_is_integer (x))) |
0aacf84e | 994 | { |
495a39c4 | 995 | if (SCM_LIKELY (scm_is_integer (y))) |
a8da6d93 | 996 | return scm_floor_remainder (x, y); |
0aacf84e | 997 | else |
2519490c | 998 | SCM_WTA_DISPATCH_2 (g_scm_modulo, x, y, SCM_ARG2, s_scm_modulo); |
828865c3 | 999 | } |
0aacf84e | 1000 | else |
2519490c | 1001 | SCM_WTA_DISPATCH_2 (g_scm_modulo, x, y, SCM_ARG1, s_scm_modulo); |
0f2d19dd | 1002 | } |
2519490c | 1003 | #undef FUNC_NAME |
0f2d19dd | 1004 | |
a285b18c MW |
1005 | /* Return the exact integer q such that n = q*d, for exact integers n |
1006 | and d, where d is known in advance to divide n evenly (with zero | |
1007 | remainder). For large integers, this can be computed more | |
1008 | efficiently than when the remainder is unknown. */ | |
1009 | static SCM | |
1010 | scm_exact_integer_quotient (SCM n, SCM d) | |
1011 | #define FUNC_NAME "exact-integer-quotient" | |
1012 | { | |
1013 | if (SCM_LIKELY (SCM_I_INUMP (n))) | |
1014 | { | |
1015 | scm_t_inum nn = SCM_I_INUM (n); | |
1016 | if (SCM_LIKELY (SCM_I_INUMP (d))) | |
1017 | { | |
1018 | scm_t_inum dd = SCM_I_INUM (d); | |
1019 | if (SCM_UNLIKELY (dd == 0)) | |
1020 | scm_num_overflow ("exact-integer-quotient"); | |
1021 | else | |
1022 | { | |
1023 | scm_t_inum qq = nn / dd; | |
1024 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
1025 | return SCM_I_MAKINUM (qq); | |
1026 | else | |
1027 | return scm_i_inum2big (qq); | |
1028 | } | |
1029 | } | |
1030 | else if (SCM_LIKELY (SCM_BIGP (d))) | |
1031 | { | |
1032 | /* n is an inum and d is a bignum. Given that d is known to | |
1033 | divide n evenly, there are only two possibilities: n is 0, | |
1034 | or else n is fixnum-min and d is abs(fixnum-min). */ | |
1035 | if (nn == 0) | |
1036 | return SCM_INUM0; | |
1037 | else | |
1038 | return SCM_I_MAKINUM (-1); | |
1039 | } | |
1040 | else | |
1041 | SCM_WRONG_TYPE_ARG (2, d); | |
1042 | } | |
1043 | else if (SCM_LIKELY (SCM_BIGP (n))) | |
1044 | { | |
1045 | if (SCM_LIKELY (SCM_I_INUMP (d))) | |
1046 | { | |
1047 | scm_t_inum dd = SCM_I_INUM (d); | |
1048 | if (SCM_UNLIKELY (dd == 0)) | |
1049 | scm_num_overflow ("exact-integer-quotient"); | |
1050 | else if (SCM_UNLIKELY (dd == 1)) | |
1051 | return n; | |
1052 | else | |
1053 | { | |
1054 | SCM q = scm_i_mkbig (); | |
1055 | if (dd > 0) | |
1056 | mpz_divexact_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), dd); | |
1057 | else | |
1058 | { | |
1059 | mpz_divexact_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), -dd); | |
1060 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
1061 | } | |
1062 | scm_remember_upto_here_1 (n); | |
1063 | return scm_i_normbig (q); | |
1064 | } | |
1065 | } | |
1066 | else if (SCM_LIKELY (SCM_BIGP (d))) | |
1067 | { | |
1068 | SCM q = scm_i_mkbig (); | |
1069 | mpz_divexact (SCM_I_BIG_MPZ (q), | |
1070 | SCM_I_BIG_MPZ (n), | |
1071 | SCM_I_BIG_MPZ (d)); | |
1072 | scm_remember_upto_here_2 (n, d); | |
1073 | return scm_i_normbig (q); | |
1074 | } | |
1075 | else | |
1076 | SCM_WRONG_TYPE_ARG (2, d); | |
1077 | } | |
1078 | else | |
1079 | SCM_WRONG_TYPE_ARG (1, n); | |
1080 | } | |
1081 | #undef FUNC_NAME | |
1082 | ||
5fbf680b MW |
1083 | /* two_valued_wta_dispatch_2 is a version of SCM_WTA_DISPATCH_2 for |
1084 | two-valued functions. It is called from primitive generics that take | |
1085 | two arguments and return two values, when the core procedure is | |
1086 | unable to handle the given argument types. If there are GOOPS | |
1087 | methods for this primitive generic, it dispatches to GOOPS and, if | |
1088 | successful, expects two values to be returned, which are placed in | |
1089 | *rp1 and *rp2. If there are no GOOPS methods, it throws a | |
1090 | wrong-type-arg exception. | |
1091 | ||
1092 | FIXME: This obviously belongs somewhere else, but until we decide on | |
1093 | the right API, it is here as a static function, because it is needed | |
1094 | by the *_divide functions below. | |
1095 | */ | |
1096 | static void | |
1097 | two_valued_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos, | |
1098 | const char *subr, SCM *rp1, SCM *rp2) | |
1099 | { | |
1100 | if (SCM_UNPACK (gf)) | |
1101 | scm_i_extract_values_2 (scm_call_generic_2 (gf, a1, a2), rp1, rp2); | |
1102 | else | |
1103 | scm_wrong_type_arg (subr, pos, (pos == SCM_ARG1) ? a1 : a2); | |
1104 | } | |
1105 | ||
a8da6d93 MW |
1106 | SCM_DEFINE (scm_euclidean_quotient, "euclidean-quotient", 2, 0, 0, |
1107 | (SCM x, SCM y), | |
1108 | "Return the integer @var{q} such that\n" | |
1109 | "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
1110 | "where @math{0 <= @var{r} < abs(@var{y})}.\n" | |
1111 | "@lisp\n" | |
1112 | "(euclidean-quotient 123 10) @result{} 12\n" | |
1113 | "(euclidean-quotient 123 -10) @result{} -12\n" | |
1114 | "(euclidean-quotient -123 10) @result{} -13\n" | |
1115 | "(euclidean-quotient -123 -10) @result{} 13\n" | |
1116 | "(euclidean-quotient -123.2 -63.5) @result{} 2.0\n" | |
1117 | "(euclidean-quotient 16/3 -10/7) @result{} -3\n" | |
1118 | "@end lisp") | |
ff62c168 MW |
1119 | #define FUNC_NAME s_scm_euclidean_quotient |
1120 | { | |
a8da6d93 MW |
1121 | if (scm_is_false (scm_negative_p (y))) |
1122 | return scm_floor_quotient (x, y); | |
ff62c168 | 1123 | else |
a8da6d93 | 1124 | return scm_ceiling_quotient (x, y); |
ff62c168 MW |
1125 | } |
1126 | #undef FUNC_NAME | |
1127 | ||
a8da6d93 MW |
1128 | SCM_DEFINE (scm_euclidean_remainder, "euclidean-remainder", 2, 0, 0, |
1129 | (SCM x, SCM y), | |
1130 | "Return the real number @var{r} such that\n" | |
1131 | "@math{0 <= @var{r} < abs(@var{y})} and\n" | |
1132 | "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
1133 | "for some integer @var{q}.\n" | |
1134 | "@lisp\n" | |
1135 | "(euclidean-remainder 123 10) @result{} 3\n" | |
1136 | "(euclidean-remainder 123 -10) @result{} 3\n" | |
1137 | "(euclidean-remainder -123 10) @result{} 7\n" | |
1138 | "(euclidean-remainder -123 -10) @result{} 7\n" | |
1139 | "(euclidean-remainder -123.2 -63.5) @result{} 3.8\n" | |
1140 | "(euclidean-remainder 16/3 -10/7) @result{} 22/21\n" | |
1141 | "@end lisp") | |
ff62c168 MW |
1142 | #define FUNC_NAME s_scm_euclidean_remainder |
1143 | { | |
a8da6d93 MW |
1144 | if (scm_is_false (scm_negative_p (y))) |
1145 | return scm_floor_remainder (x, y); | |
ff62c168 | 1146 | else |
a8da6d93 | 1147 | return scm_ceiling_remainder (x, y); |
ff62c168 MW |
1148 | } |
1149 | #undef FUNC_NAME | |
1150 | ||
a8da6d93 MW |
1151 | SCM_DEFINE (scm_i_euclidean_divide, "euclidean/", 2, 0, 0, |
1152 | (SCM x, SCM y), | |
1153 | "Return the integer @var{q} and the real number @var{r}\n" | |
1154 | "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
1155 | "and @math{0 <= @var{r} < abs(@var{y})}.\n" | |
1156 | "@lisp\n" | |
1157 | "(euclidean/ 123 10) @result{} 12 and 3\n" | |
1158 | "(euclidean/ 123 -10) @result{} -12 and 3\n" | |
1159 | "(euclidean/ -123 10) @result{} -13 and 7\n" | |
1160 | "(euclidean/ -123 -10) @result{} 13 and 7\n" | |
1161 | "(euclidean/ -123.2 -63.5) @result{} 2.0 and 3.8\n" | |
1162 | "(euclidean/ 16/3 -10/7) @result{} -3 and 22/21\n" | |
1163 | "@end lisp") | |
5fbf680b MW |
1164 | #define FUNC_NAME s_scm_i_euclidean_divide |
1165 | { | |
a8da6d93 MW |
1166 | if (scm_is_false (scm_negative_p (y))) |
1167 | return scm_i_floor_divide (x, y); | |
1168 | else | |
1169 | return scm_i_ceiling_divide (x, y); | |
5fbf680b MW |
1170 | } |
1171 | #undef FUNC_NAME | |
1172 | ||
5fbf680b MW |
1173 | void |
1174 | scm_euclidean_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
ff62c168 | 1175 | { |
a8da6d93 MW |
1176 | if (scm_is_false (scm_negative_p (y))) |
1177 | return scm_floor_divide (x, y, qp, rp); | |
ff62c168 | 1178 | else |
a8da6d93 | 1179 | return scm_ceiling_divide (x, y, qp, rp); |
ff62c168 MW |
1180 | } |
1181 | ||
8f9da340 MW |
1182 | static SCM scm_i_inexact_floor_quotient (double x, double y); |
1183 | static SCM scm_i_exact_rational_floor_quotient (SCM x, SCM y); | |
1184 | ||
1185 | SCM_PRIMITIVE_GENERIC (scm_floor_quotient, "floor-quotient", 2, 0, 0, | |
1186 | (SCM x, SCM y), | |
1187 | "Return the floor of @math{@var{x} / @var{y}}.\n" | |
1188 | "@lisp\n" | |
1189 | "(floor-quotient 123 10) @result{} 12\n" | |
1190 | "(floor-quotient 123 -10) @result{} -13\n" | |
1191 | "(floor-quotient -123 10) @result{} -13\n" | |
1192 | "(floor-quotient -123 -10) @result{} 12\n" | |
1193 | "(floor-quotient -123.2 -63.5) @result{} 1.0\n" | |
1194 | "(floor-quotient 16/3 -10/7) @result{} -4\n" | |
1195 | "@end lisp") | |
1196 | #define FUNC_NAME s_scm_floor_quotient | |
1197 | { | |
1198 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
1199 | { | |
1200 | scm_t_inum xx = SCM_I_INUM (x); | |
1201 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1202 | { | |
1203 | scm_t_inum yy = SCM_I_INUM (y); | |
1204 | scm_t_inum xx1 = xx; | |
1205 | scm_t_inum qq; | |
1206 | if (SCM_LIKELY (yy > 0)) | |
1207 | { | |
1208 | if (SCM_UNLIKELY (xx < 0)) | |
1209 | xx1 = xx - yy + 1; | |
1210 | } | |
1211 | else if (SCM_UNLIKELY (yy == 0)) | |
1212 | scm_num_overflow (s_scm_floor_quotient); | |
1213 | else if (xx > 0) | |
1214 | xx1 = xx - yy - 1; | |
1215 | qq = xx1 / yy; | |
1216 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
1217 | return SCM_I_MAKINUM (qq); | |
1218 | else | |
1219 | return scm_i_inum2big (qq); | |
1220 | } | |
1221 | else if (SCM_BIGP (y)) | |
1222 | { | |
1223 | int sign = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
1224 | scm_remember_upto_here_1 (y); | |
1225 | if (sign > 0) | |
1226 | return SCM_I_MAKINUM ((xx < 0) ? -1 : 0); | |
1227 | else | |
1228 | return SCM_I_MAKINUM ((xx > 0) ? -1 : 0); | |
1229 | } | |
1230 | else if (SCM_REALP (y)) | |
1231 | return scm_i_inexact_floor_quotient (xx, SCM_REAL_VALUE (y)); | |
1232 | else if (SCM_FRACTIONP (y)) | |
1233 | return scm_i_exact_rational_floor_quotient (x, y); | |
1234 | else | |
1235 | SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2, | |
1236 | s_scm_floor_quotient); | |
1237 | } | |
1238 | else if (SCM_BIGP (x)) | |
1239 | { | |
1240 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1241 | { | |
1242 | scm_t_inum yy = SCM_I_INUM (y); | |
1243 | if (SCM_UNLIKELY (yy == 0)) | |
1244 | scm_num_overflow (s_scm_floor_quotient); | |
1245 | else if (SCM_UNLIKELY (yy == 1)) | |
1246 | return x; | |
1247 | else | |
1248 | { | |
1249 | SCM q = scm_i_mkbig (); | |
1250 | if (yy > 0) | |
1251 | mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy); | |
1252 | else | |
1253 | { | |
1254 | mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy); | |
1255 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
1256 | } | |
1257 | scm_remember_upto_here_1 (x); | |
1258 | return scm_i_normbig (q); | |
1259 | } | |
1260 | } | |
1261 | else if (SCM_BIGP (y)) | |
1262 | { | |
1263 | SCM q = scm_i_mkbig (); | |
1264 | mpz_fdiv_q (SCM_I_BIG_MPZ (q), | |
1265 | SCM_I_BIG_MPZ (x), | |
1266 | SCM_I_BIG_MPZ (y)); | |
1267 | scm_remember_upto_here_2 (x, y); | |
1268 | return scm_i_normbig (q); | |
1269 | } | |
1270 | else if (SCM_REALP (y)) | |
1271 | return scm_i_inexact_floor_quotient | |
1272 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
1273 | else if (SCM_FRACTIONP (y)) | |
1274 | return scm_i_exact_rational_floor_quotient (x, y); | |
1275 | else | |
1276 | SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2, | |
1277 | s_scm_floor_quotient); | |
1278 | } | |
1279 | else if (SCM_REALP (x)) | |
1280 | { | |
1281 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
1282 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1283 | return scm_i_inexact_floor_quotient | |
1284 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
1285 | else | |
1286 | SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2, | |
1287 | s_scm_floor_quotient); | |
1288 | } | |
1289 | else if (SCM_FRACTIONP (x)) | |
1290 | { | |
1291 | if (SCM_REALP (y)) | |
1292 | return scm_i_inexact_floor_quotient | |
1293 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
1294 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1295 | return scm_i_exact_rational_floor_quotient (x, y); | |
1296 | else | |
1297 | SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2, | |
1298 | s_scm_floor_quotient); | |
1299 | } | |
1300 | else | |
1301 | SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG1, | |
1302 | s_scm_floor_quotient); | |
1303 | } | |
1304 | #undef FUNC_NAME | |
1305 | ||
1306 | static SCM | |
1307 | scm_i_inexact_floor_quotient (double x, double y) | |
1308 | { | |
1309 | if (SCM_UNLIKELY (y == 0)) | |
1310 | scm_num_overflow (s_scm_floor_quotient); /* or return a NaN? */ | |
1311 | else | |
00472a22 | 1312 | return scm_i_from_double (floor (x / y)); |
8f9da340 MW |
1313 | } |
1314 | ||
1315 | static SCM | |
1316 | scm_i_exact_rational_floor_quotient (SCM x, SCM y) | |
1317 | { | |
1318 | return scm_floor_quotient | |
1319 | (scm_product (scm_numerator (x), scm_denominator (y)), | |
1320 | scm_product (scm_numerator (y), scm_denominator (x))); | |
1321 | } | |
1322 | ||
1323 | static SCM scm_i_inexact_floor_remainder (double x, double y); | |
1324 | static SCM scm_i_exact_rational_floor_remainder (SCM x, SCM y); | |
1325 | ||
1326 | SCM_PRIMITIVE_GENERIC (scm_floor_remainder, "floor-remainder", 2, 0, 0, | |
1327 | (SCM x, SCM y), | |
1328 | "Return the real number @var{r} such that\n" | |
1329 | "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
1330 | "where @math{@var{q} = floor(@var{x} / @var{y})}.\n" | |
1331 | "@lisp\n" | |
1332 | "(floor-remainder 123 10) @result{} 3\n" | |
1333 | "(floor-remainder 123 -10) @result{} -7\n" | |
1334 | "(floor-remainder -123 10) @result{} 7\n" | |
1335 | "(floor-remainder -123 -10) @result{} -3\n" | |
1336 | "(floor-remainder -123.2 -63.5) @result{} -59.7\n" | |
1337 | "(floor-remainder 16/3 -10/7) @result{} -8/21\n" | |
1338 | "@end lisp") | |
1339 | #define FUNC_NAME s_scm_floor_remainder | |
1340 | { | |
1341 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
1342 | { | |
1343 | scm_t_inum xx = SCM_I_INUM (x); | |
1344 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1345 | { | |
1346 | scm_t_inum yy = SCM_I_INUM (y); | |
1347 | if (SCM_UNLIKELY (yy == 0)) | |
1348 | scm_num_overflow (s_scm_floor_remainder); | |
1349 | else | |
1350 | { | |
1351 | scm_t_inum rr = xx % yy; | |
1352 | int needs_adjustment; | |
1353 | ||
1354 | if (SCM_LIKELY (yy > 0)) | |
1355 | needs_adjustment = (rr < 0); | |
1356 | else | |
1357 | needs_adjustment = (rr > 0); | |
1358 | ||
1359 | if (needs_adjustment) | |
1360 | rr += yy; | |
1361 | return SCM_I_MAKINUM (rr); | |
1362 | } | |
1363 | } | |
1364 | else if (SCM_BIGP (y)) | |
1365 | { | |
1366 | int sign = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
1367 | scm_remember_upto_here_1 (y); | |
1368 | if (sign > 0) | |
1369 | { | |
1370 | if (xx < 0) | |
1371 | { | |
1372 | SCM r = scm_i_mkbig (); | |
1373 | mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx); | |
1374 | scm_remember_upto_here_1 (y); | |
1375 | return scm_i_normbig (r); | |
1376 | } | |
1377 | else | |
1378 | return x; | |
1379 | } | |
1380 | else if (xx <= 0) | |
1381 | return x; | |
1382 | else | |
1383 | { | |
1384 | SCM r = scm_i_mkbig (); | |
1385 | mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx); | |
1386 | scm_remember_upto_here_1 (y); | |
1387 | return scm_i_normbig (r); | |
1388 | } | |
1389 | } | |
1390 | else if (SCM_REALP (y)) | |
1391 | return scm_i_inexact_floor_remainder (xx, SCM_REAL_VALUE (y)); | |
1392 | else if (SCM_FRACTIONP (y)) | |
1393 | return scm_i_exact_rational_floor_remainder (x, y); | |
1394 | else | |
1395 | SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2, | |
1396 | s_scm_floor_remainder); | |
1397 | } | |
1398 | else if (SCM_BIGP (x)) | |
1399 | { | |
1400 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1401 | { | |
1402 | scm_t_inum yy = SCM_I_INUM (y); | |
1403 | if (SCM_UNLIKELY (yy == 0)) | |
1404 | scm_num_overflow (s_scm_floor_remainder); | |
1405 | else | |
1406 | { | |
1407 | scm_t_inum rr; | |
1408 | if (yy > 0) | |
1409 | rr = mpz_fdiv_ui (SCM_I_BIG_MPZ (x), yy); | |
1410 | else | |
1411 | rr = -mpz_cdiv_ui (SCM_I_BIG_MPZ (x), -yy); | |
1412 | scm_remember_upto_here_1 (x); | |
1413 | return SCM_I_MAKINUM (rr); | |
1414 | } | |
1415 | } | |
1416 | else if (SCM_BIGP (y)) | |
1417 | { | |
1418 | SCM r = scm_i_mkbig (); | |
1419 | mpz_fdiv_r (SCM_I_BIG_MPZ (r), | |
1420 | SCM_I_BIG_MPZ (x), | |
1421 | SCM_I_BIG_MPZ (y)); | |
1422 | scm_remember_upto_here_2 (x, y); | |
1423 | return scm_i_normbig (r); | |
1424 | } | |
1425 | else if (SCM_REALP (y)) | |
1426 | return scm_i_inexact_floor_remainder | |
1427 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
1428 | else if (SCM_FRACTIONP (y)) | |
1429 | return scm_i_exact_rational_floor_remainder (x, y); | |
1430 | else | |
1431 | SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2, | |
1432 | s_scm_floor_remainder); | |
1433 | } | |
1434 | else if (SCM_REALP (x)) | |
1435 | { | |
1436 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
1437 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1438 | return scm_i_inexact_floor_remainder | |
1439 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
1440 | else | |
1441 | SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2, | |
1442 | s_scm_floor_remainder); | |
1443 | } | |
1444 | else if (SCM_FRACTIONP (x)) | |
1445 | { | |
1446 | if (SCM_REALP (y)) | |
1447 | return scm_i_inexact_floor_remainder | |
1448 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
1449 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1450 | return scm_i_exact_rational_floor_remainder (x, y); | |
1451 | else | |
1452 | SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2, | |
1453 | s_scm_floor_remainder); | |
1454 | } | |
1455 | else | |
1456 | SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG1, | |
1457 | s_scm_floor_remainder); | |
1458 | } | |
1459 | #undef FUNC_NAME | |
1460 | ||
1461 | static SCM | |
1462 | scm_i_inexact_floor_remainder (double x, double y) | |
1463 | { | |
1464 | /* Although it would be more efficient to use fmod here, we can't | |
1465 | because it would in some cases produce results inconsistent with | |
1466 | scm_i_inexact_floor_quotient, such that x != q * y + r (not even | |
1467 | close). In particular, when x is very close to a multiple of y, | |
1468 | then r might be either 0.0 or y, but those two cases must | |
1469 | correspond to different choices of q. If r = 0.0 then q must be | |
1470 | x/y, and if r = y then q must be x/y-1. If quotient chooses one | |
1471 | and remainder chooses the other, it would be bad. */ | |
1472 | if (SCM_UNLIKELY (y == 0)) | |
1473 | scm_num_overflow (s_scm_floor_remainder); /* or return a NaN? */ | |
1474 | else | |
00472a22 | 1475 | return scm_i_from_double (x - y * floor (x / y)); |
8f9da340 MW |
1476 | } |
1477 | ||
1478 | static SCM | |
1479 | scm_i_exact_rational_floor_remainder (SCM x, SCM y) | |
1480 | { | |
1481 | SCM xd = scm_denominator (x); | |
1482 | SCM yd = scm_denominator (y); | |
1483 | SCM r1 = scm_floor_remainder (scm_product (scm_numerator (x), yd), | |
1484 | scm_product (scm_numerator (y), xd)); | |
1485 | return scm_divide (r1, scm_product (xd, yd)); | |
1486 | } | |
1487 | ||
1488 | ||
1489 | static void scm_i_inexact_floor_divide (double x, double y, | |
1490 | SCM *qp, SCM *rp); | |
1491 | static void scm_i_exact_rational_floor_divide (SCM x, SCM y, | |
1492 | SCM *qp, SCM *rp); | |
1493 | ||
1494 | SCM_PRIMITIVE_GENERIC (scm_i_floor_divide, "floor/", 2, 0, 0, | |
1495 | (SCM x, SCM y), | |
1496 | "Return the integer @var{q} and the real number @var{r}\n" | |
1497 | "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
1498 | "and @math{@var{q} = floor(@var{x} / @var{y})}.\n" | |
1499 | "@lisp\n" | |
1500 | "(floor/ 123 10) @result{} 12 and 3\n" | |
1501 | "(floor/ 123 -10) @result{} -13 and -7\n" | |
1502 | "(floor/ -123 10) @result{} -13 and 7\n" | |
1503 | "(floor/ -123 -10) @result{} 12 and -3\n" | |
1504 | "(floor/ -123.2 -63.5) @result{} 1.0 and -59.7\n" | |
1505 | "(floor/ 16/3 -10/7) @result{} -4 and -8/21\n" | |
1506 | "@end lisp") | |
1507 | #define FUNC_NAME s_scm_i_floor_divide | |
1508 | { | |
1509 | SCM q, r; | |
1510 | ||
1511 | scm_floor_divide(x, y, &q, &r); | |
1512 | return scm_values (scm_list_2 (q, r)); | |
1513 | } | |
1514 | #undef FUNC_NAME | |
1515 | ||
1516 | #define s_scm_floor_divide s_scm_i_floor_divide | |
1517 | #define g_scm_floor_divide g_scm_i_floor_divide | |
1518 | ||
1519 | void | |
1520 | scm_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
1521 | { | |
1522 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
1523 | { | |
1524 | scm_t_inum xx = SCM_I_INUM (x); | |
1525 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1526 | { | |
1527 | scm_t_inum yy = SCM_I_INUM (y); | |
1528 | if (SCM_UNLIKELY (yy == 0)) | |
1529 | scm_num_overflow (s_scm_floor_divide); | |
1530 | else | |
1531 | { | |
1532 | scm_t_inum qq = xx / yy; | |
1533 | scm_t_inum rr = xx % yy; | |
1534 | int needs_adjustment; | |
1535 | ||
1536 | if (SCM_LIKELY (yy > 0)) | |
1537 | needs_adjustment = (rr < 0); | |
1538 | else | |
1539 | needs_adjustment = (rr > 0); | |
1540 | ||
1541 | if (needs_adjustment) | |
1542 | { | |
1543 | rr += yy; | |
1544 | qq--; | |
1545 | } | |
1546 | ||
1547 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
1548 | *qp = SCM_I_MAKINUM (qq); | |
1549 | else | |
1550 | *qp = scm_i_inum2big (qq); | |
1551 | *rp = SCM_I_MAKINUM (rr); | |
1552 | } | |
1553 | return; | |
1554 | } | |
1555 | else if (SCM_BIGP (y)) | |
1556 | { | |
1557 | int sign = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
1558 | scm_remember_upto_here_1 (y); | |
1559 | if (sign > 0) | |
1560 | { | |
1561 | if (xx < 0) | |
1562 | { | |
1563 | SCM r = scm_i_mkbig (); | |
1564 | mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx); | |
1565 | scm_remember_upto_here_1 (y); | |
1566 | *qp = SCM_I_MAKINUM (-1); | |
1567 | *rp = scm_i_normbig (r); | |
1568 | } | |
1569 | else | |
1570 | { | |
1571 | *qp = SCM_INUM0; | |
1572 | *rp = x; | |
1573 | } | |
1574 | } | |
1575 | else if (xx <= 0) | |
1576 | { | |
1577 | *qp = SCM_INUM0; | |
1578 | *rp = x; | |
1579 | } | |
1580 | else | |
1581 | { | |
1582 | SCM r = scm_i_mkbig (); | |
1583 | mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx); | |
1584 | scm_remember_upto_here_1 (y); | |
1585 | *qp = SCM_I_MAKINUM (-1); | |
1586 | *rp = scm_i_normbig (r); | |
1587 | } | |
1588 | return; | |
1589 | } | |
1590 | else if (SCM_REALP (y)) | |
1591 | return scm_i_inexact_floor_divide (xx, SCM_REAL_VALUE (y), qp, rp); | |
1592 | else if (SCM_FRACTIONP (y)) | |
1593 | return scm_i_exact_rational_floor_divide (x, y, qp, rp); | |
1594 | else | |
1595 | return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2, | |
1596 | s_scm_floor_divide, qp, rp); | |
1597 | } | |
1598 | else if (SCM_BIGP (x)) | |
1599 | { | |
1600 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1601 | { | |
1602 | scm_t_inum yy = SCM_I_INUM (y); | |
1603 | if (SCM_UNLIKELY (yy == 0)) | |
1604 | scm_num_overflow (s_scm_floor_divide); | |
1605 | else | |
1606 | { | |
1607 | SCM q = scm_i_mkbig (); | |
1608 | SCM r = scm_i_mkbig (); | |
1609 | if (yy > 0) | |
1610 | mpz_fdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
1611 | SCM_I_BIG_MPZ (x), yy); | |
1612 | else | |
1613 | { | |
1614 | mpz_cdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
1615 | SCM_I_BIG_MPZ (x), -yy); | |
1616 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
1617 | } | |
1618 | scm_remember_upto_here_1 (x); | |
1619 | *qp = scm_i_normbig (q); | |
1620 | *rp = scm_i_normbig (r); | |
1621 | } | |
1622 | return; | |
1623 | } | |
1624 | else if (SCM_BIGP (y)) | |
1625 | { | |
1626 | SCM q = scm_i_mkbig (); | |
1627 | SCM r = scm_i_mkbig (); | |
1628 | mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
1629 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
1630 | scm_remember_upto_here_2 (x, y); | |
1631 | *qp = scm_i_normbig (q); | |
1632 | *rp = scm_i_normbig (r); | |
1633 | return; | |
1634 | } | |
1635 | else if (SCM_REALP (y)) | |
1636 | return scm_i_inexact_floor_divide | |
1637 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp); | |
1638 | else if (SCM_FRACTIONP (y)) | |
1639 | return scm_i_exact_rational_floor_divide (x, y, qp, rp); | |
1640 | else | |
1641 | return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2, | |
1642 | s_scm_floor_divide, qp, rp); | |
1643 | } | |
1644 | else if (SCM_REALP (x)) | |
1645 | { | |
1646 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
1647 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1648 | return scm_i_inexact_floor_divide | |
1649 | (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp); | |
1650 | else | |
1651 | return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2, | |
1652 | s_scm_floor_divide, qp, rp); | |
1653 | } | |
1654 | else if (SCM_FRACTIONP (x)) | |
1655 | { | |
1656 | if (SCM_REALP (y)) | |
1657 | return scm_i_inexact_floor_divide | |
1658 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp); | |
1659 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1660 | return scm_i_exact_rational_floor_divide (x, y, qp, rp); | |
1661 | else | |
1662 | return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2, | |
1663 | s_scm_floor_divide, qp, rp); | |
1664 | } | |
1665 | else | |
1666 | return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG1, | |
1667 | s_scm_floor_divide, qp, rp); | |
1668 | } | |
1669 | ||
1670 | static void | |
1671 | scm_i_inexact_floor_divide (double x, double y, SCM *qp, SCM *rp) | |
1672 | { | |
1673 | if (SCM_UNLIKELY (y == 0)) | |
1674 | scm_num_overflow (s_scm_floor_divide); /* or return a NaN? */ | |
1675 | else | |
1676 | { | |
1677 | double q = floor (x / y); | |
1678 | double r = x - q * y; | |
00472a22 MW |
1679 | *qp = scm_i_from_double (q); |
1680 | *rp = scm_i_from_double (r); | |
8f9da340 MW |
1681 | } |
1682 | } | |
1683 | ||
1684 | static void | |
1685 | scm_i_exact_rational_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
1686 | { | |
1687 | SCM r1; | |
1688 | SCM xd = scm_denominator (x); | |
1689 | SCM yd = scm_denominator (y); | |
1690 | ||
1691 | scm_floor_divide (scm_product (scm_numerator (x), yd), | |
1692 | scm_product (scm_numerator (y), xd), | |
1693 | qp, &r1); | |
1694 | *rp = scm_divide (r1, scm_product (xd, yd)); | |
1695 | } | |
1696 | ||
1697 | static SCM scm_i_inexact_ceiling_quotient (double x, double y); | |
1698 | static SCM scm_i_exact_rational_ceiling_quotient (SCM x, SCM y); | |
1699 | ||
1700 | SCM_PRIMITIVE_GENERIC (scm_ceiling_quotient, "ceiling-quotient", 2, 0, 0, | |
1701 | (SCM x, SCM y), | |
1702 | "Return the ceiling of @math{@var{x} / @var{y}}.\n" | |
1703 | "@lisp\n" | |
1704 | "(ceiling-quotient 123 10) @result{} 13\n" | |
1705 | "(ceiling-quotient 123 -10) @result{} -12\n" | |
1706 | "(ceiling-quotient -123 10) @result{} -12\n" | |
1707 | "(ceiling-quotient -123 -10) @result{} 13\n" | |
1708 | "(ceiling-quotient -123.2 -63.5) @result{} 2.0\n" | |
1709 | "(ceiling-quotient 16/3 -10/7) @result{} -3\n" | |
1710 | "@end lisp") | |
1711 | #define FUNC_NAME s_scm_ceiling_quotient | |
1712 | { | |
1713 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
1714 | { | |
1715 | scm_t_inum xx = SCM_I_INUM (x); | |
1716 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1717 | { | |
1718 | scm_t_inum yy = SCM_I_INUM (y); | |
1719 | if (SCM_UNLIKELY (yy == 0)) | |
1720 | scm_num_overflow (s_scm_ceiling_quotient); | |
1721 | else | |
1722 | { | |
1723 | scm_t_inum xx1 = xx; | |
1724 | scm_t_inum qq; | |
1725 | if (SCM_LIKELY (yy > 0)) | |
1726 | { | |
1727 | if (SCM_LIKELY (xx >= 0)) | |
1728 | xx1 = xx + yy - 1; | |
1729 | } | |
8f9da340 MW |
1730 | else if (xx < 0) |
1731 | xx1 = xx + yy + 1; | |
1732 | qq = xx1 / yy; | |
1733 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
1734 | return SCM_I_MAKINUM (qq); | |
1735 | else | |
1736 | return scm_i_inum2big (qq); | |
1737 | } | |
1738 | } | |
1739 | else if (SCM_BIGP (y)) | |
1740 | { | |
1741 | int sign = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
1742 | scm_remember_upto_here_1 (y); | |
1743 | if (SCM_LIKELY (sign > 0)) | |
1744 | { | |
1745 | if (SCM_LIKELY (xx > 0)) | |
1746 | return SCM_INUM1; | |
1747 | else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM) | |
1748 | && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y), | |
1749 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
1750 | { | |
1751 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
1752 | scm_remember_upto_here_1 (y); | |
1753 | return SCM_I_MAKINUM (-1); | |
1754 | } | |
1755 | else | |
1756 | return SCM_INUM0; | |
1757 | } | |
1758 | else if (xx >= 0) | |
1759 | return SCM_INUM0; | |
1760 | else | |
1761 | return SCM_INUM1; | |
1762 | } | |
1763 | else if (SCM_REALP (y)) | |
1764 | return scm_i_inexact_ceiling_quotient (xx, SCM_REAL_VALUE (y)); | |
1765 | else if (SCM_FRACTIONP (y)) | |
1766 | return scm_i_exact_rational_ceiling_quotient (x, y); | |
1767 | else | |
1768 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2, | |
1769 | s_scm_ceiling_quotient); | |
1770 | } | |
1771 | else if (SCM_BIGP (x)) | |
1772 | { | |
1773 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1774 | { | |
1775 | scm_t_inum yy = SCM_I_INUM (y); | |
1776 | if (SCM_UNLIKELY (yy == 0)) | |
1777 | scm_num_overflow (s_scm_ceiling_quotient); | |
1778 | else if (SCM_UNLIKELY (yy == 1)) | |
1779 | return x; | |
1780 | else | |
1781 | { | |
1782 | SCM q = scm_i_mkbig (); | |
1783 | if (yy > 0) | |
1784 | mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy); | |
1785 | else | |
1786 | { | |
1787 | mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy); | |
1788 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
1789 | } | |
1790 | scm_remember_upto_here_1 (x); | |
1791 | return scm_i_normbig (q); | |
1792 | } | |
1793 | } | |
1794 | else if (SCM_BIGP (y)) | |
1795 | { | |
1796 | SCM q = scm_i_mkbig (); | |
1797 | mpz_cdiv_q (SCM_I_BIG_MPZ (q), | |
1798 | SCM_I_BIG_MPZ (x), | |
1799 | SCM_I_BIG_MPZ (y)); | |
1800 | scm_remember_upto_here_2 (x, y); | |
1801 | return scm_i_normbig (q); | |
1802 | } | |
1803 | else if (SCM_REALP (y)) | |
1804 | return scm_i_inexact_ceiling_quotient | |
1805 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
1806 | else if (SCM_FRACTIONP (y)) | |
1807 | return scm_i_exact_rational_ceiling_quotient (x, y); | |
1808 | else | |
1809 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2, | |
1810 | s_scm_ceiling_quotient); | |
1811 | } | |
1812 | else if (SCM_REALP (x)) | |
1813 | { | |
1814 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
1815 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1816 | return scm_i_inexact_ceiling_quotient | |
1817 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
1818 | else | |
1819 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2, | |
1820 | s_scm_ceiling_quotient); | |
1821 | } | |
1822 | else if (SCM_FRACTIONP (x)) | |
1823 | { | |
1824 | if (SCM_REALP (y)) | |
1825 | return scm_i_inexact_ceiling_quotient | |
1826 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
1827 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1828 | return scm_i_exact_rational_ceiling_quotient (x, y); | |
1829 | else | |
1830 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2, | |
1831 | s_scm_ceiling_quotient); | |
1832 | } | |
1833 | else | |
1834 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG1, | |
1835 | s_scm_ceiling_quotient); | |
1836 | } | |
1837 | #undef FUNC_NAME | |
1838 | ||
1839 | static SCM | |
1840 | scm_i_inexact_ceiling_quotient (double x, double y) | |
1841 | { | |
1842 | if (SCM_UNLIKELY (y == 0)) | |
1843 | scm_num_overflow (s_scm_ceiling_quotient); /* or return a NaN? */ | |
1844 | else | |
00472a22 | 1845 | return scm_i_from_double (ceil (x / y)); |
8f9da340 MW |
1846 | } |
1847 | ||
1848 | static SCM | |
1849 | scm_i_exact_rational_ceiling_quotient (SCM x, SCM y) | |
1850 | { | |
1851 | return scm_ceiling_quotient | |
1852 | (scm_product (scm_numerator (x), scm_denominator (y)), | |
1853 | scm_product (scm_numerator (y), scm_denominator (x))); | |
1854 | } | |
1855 | ||
1856 | static SCM scm_i_inexact_ceiling_remainder (double x, double y); | |
1857 | static SCM scm_i_exact_rational_ceiling_remainder (SCM x, SCM y); | |
1858 | ||
1859 | SCM_PRIMITIVE_GENERIC (scm_ceiling_remainder, "ceiling-remainder", 2, 0, 0, | |
1860 | (SCM x, SCM y), | |
1861 | "Return the real number @var{r} such that\n" | |
1862 | "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
1863 | "where @math{@var{q} = ceiling(@var{x} / @var{y})}.\n" | |
1864 | "@lisp\n" | |
1865 | "(ceiling-remainder 123 10) @result{} -7\n" | |
1866 | "(ceiling-remainder 123 -10) @result{} 3\n" | |
1867 | "(ceiling-remainder -123 10) @result{} -3\n" | |
1868 | "(ceiling-remainder -123 -10) @result{} 7\n" | |
1869 | "(ceiling-remainder -123.2 -63.5) @result{} 3.8\n" | |
1870 | "(ceiling-remainder 16/3 -10/7) @result{} 22/21\n" | |
1871 | "@end lisp") | |
1872 | #define FUNC_NAME s_scm_ceiling_remainder | |
1873 | { | |
1874 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
1875 | { | |
1876 | scm_t_inum xx = SCM_I_INUM (x); | |
1877 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1878 | { | |
1879 | scm_t_inum yy = SCM_I_INUM (y); | |
1880 | if (SCM_UNLIKELY (yy == 0)) | |
1881 | scm_num_overflow (s_scm_ceiling_remainder); | |
1882 | else | |
1883 | { | |
1884 | scm_t_inum rr = xx % yy; | |
1885 | int needs_adjustment; | |
1886 | ||
1887 | if (SCM_LIKELY (yy > 0)) | |
1888 | needs_adjustment = (rr > 0); | |
1889 | else | |
1890 | needs_adjustment = (rr < 0); | |
1891 | ||
1892 | if (needs_adjustment) | |
1893 | rr -= yy; | |
1894 | return SCM_I_MAKINUM (rr); | |
1895 | } | |
1896 | } | |
1897 | else if (SCM_BIGP (y)) | |
1898 | { | |
1899 | int sign = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
1900 | scm_remember_upto_here_1 (y); | |
1901 | if (SCM_LIKELY (sign > 0)) | |
1902 | { | |
1903 | if (SCM_LIKELY (xx > 0)) | |
1904 | { | |
1905 | SCM r = scm_i_mkbig (); | |
1906 | mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx); | |
1907 | scm_remember_upto_here_1 (y); | |
1908 | mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r)); | |
1909 | return scm_i_normbig (r); | |
1910 | } | |
1911 | else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM) | |
1912 | && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y), | |
1913 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
1914 | { | |
1915 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
1916 | scm_remember_upto_here_1 (y); | |
1917 | return SCM_INUM0; | |
1918 | } | |
1919 | else | |
1920 | return x; | |
1921 | } | |
1922 | else if (xx >= 0) | |
1923 | return x; | |
1924 | else | |
1925 | { | |
1926 | SCM r = scm_i_mkbig (); | |
1927 | mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx); | |
1928 | scm_remember_upto_here_1 (y); | |
1929 | mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r)); | |
1930 | return scm_i_normbig (r); | |
1931 | } | |
1932 | } | |
1933 | else if (SCM_REALP (y)) | |
1934 | return scm_i_inexact_ceiling_remainder (xx, SCM_REAL_VALUE (y)); | |
1935 | else if (SCM_FRACTIONP (y)) | |
1936 | return scm_i_exact_rational_ceiling_remainder (x, y); | |
1937 | else | |
1938 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2, | |
1939 | s_scm_ceiling_remainder); | |
1940 | } | |
1941 | else if (SCM_BIGP (x)) | |
1942 | { | |
1943 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
1944 | { | |
1945 | scm_t_inum yy = SCM_I_INUM (y); | |
1946 | if (SCM_UNLIKELY (yy == 0)) | |
1947 | scm_num_overflow (s_scm_ceiling_remainder); | |
1948 | else | |
1949 | { | |
1950 | scm_t_inum rr; | |
1951 | if (yy > 0) | |
1952 | rr = -mpz_cdiv_ui (SCM_I_BIG_MPZ (x), yy); | |
1953 | else | |
1954 | rr = mpz_fdiv_ui (SCM_I_BIG_MPZ (x), -yy); | |
1955 | scm_remember_upto_here_1 (x); | |
1956 | return SCM_I_MAKINUM (rr); | |
1957 | } | |
1958 | } | |
1959 | else if (SCM_BIGP (y)) | |
1960 | { | |
1961 | SCM r = scm_i_mkbig (); | |
1962 | mpz_cdiv_r (SCM_I_BIG_MPZ (r), | |
1963 | SCM_I_BIG_MPZ (x), | |
1964 | SCM_I_BIG_MPZ (y)); | |
1965 | scm_remember_upto_here_2 (x, y); | |
1966 | return scm_i_normbig (r); | |
1967 | } | |
1968 | else if (SCM_REALP (y)) | |
1969 | return scm_i_inexact_ceiling_remainder | |
1970 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
1971 | else if (SCM_FRACTIONP (y)) | |
1972 | return scm_i_exact_rational_ceiling_remainder (x, y); | |
1973 | else | |
1974 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2, | |
1975 | s_scm_ceiling_remainder); | |
1976 | } | |
1977 | else if (SCM_REALP (x)) | |
1978 | { | |
1979 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
1980 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1981 | return scm_i_inexact_ceiling_remainder | |
1982 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
1983 | else | |
1984 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2, | |
1985 | s_scm_ceiling_remainder); | |
1986 | } | |
1987 | else if (SCM_FRACTIONP (x)) | |
1988 | { | |
1989 | if (SCM_REALP (y)) | |
1990 | return scm_i_inexact_ceiling_remainder | |
1991 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
1992 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
1993 | return scm_i_exact_rational_ceiling_remainder (x, y); | |
1994 | else | |
1995 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2, | |
1996 | s_scm_ceiling_remainder); | |
1997 | } | |
1998 | else | |
1999 | SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG1, | |
2000 | s_scm_ceiling_remainder); | |
2001 | } | |
2002 | #undef FUNC_NAME | |
2003 | ||
2004 | static SCM | |
2005 | scm_i_inexact_ceiling_remainder (double x, double y) | |
2006 | { | |
2007 | /* Although it would be more efficient to use fmod here, we can't | |
2008 | because it would in some cases produce results inconsistent with | |
2009 | scm_i_inexact_ceiling_quotient, such that x != q * y + r (not even | |
2010 | close). In particular, when x is very close to a multiple of y, | |
2011 | then r might be either 0.0 or -y, but those two cases must | |
2012 | correspond to different choices of q. If r = 0.0 then q must be | |
2013 | x/y, and if r = -y then q must be x/y+1. If quotient chooses one | |
2014 | and remainder chooses the other, it would be bad. */ | |
2015 | if (SCM_UNLIKELY (y == 0)) | |
2016 | scm_num_overflow (s_scm_ceiling_remainder); /* or return a NaN? */ | |
2017 | else | |
00472a22 | 2018 | return scm_i_from_double (x - y * ceil (x / y)); |
8f9da340 MW |
2019 | } |
2020 | ||
2021 | static SCM | |
2022 | scm_i_exact_rational_ceiling_remainder (SCM x, SCM y) | |
2023 | { | |
2024 | SCM xd = scm_denominator (x); | |
2025 | SCM yd = scm_denominator (y); | |
2026 | SCM r1 = scm_ceiling_remainder (scm_product (scm_numerator (x), yd), | |
2027 | scm_product (scm_numerator (y), xd)); | |
2028 | return scm_divide (r1, scm_product (xd, yd)); | |
2029 | } | |
2030 | ||
2031 | static void scm_i_inexact_ceiling_divide (double x, double y, | |
2032 | SCM *qp, SCM *rp); | |
2033 | static void scm_i_exact_rational_ceiling_divide (SCM x, SCM y, | |
2034 | SCM *qp, SCM *rp); | |
2035 | ||
2036 | SCM_PRIMITIVE_GENERIC (scm_i_ceiling_divide, "ceiling/", 2, 0, 0, | |
2037 | (SCM x, SCM y), | |
2038 | "Return the integer @var{q} and the real number @var{r}\n" | |
2039 | "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
2040 | "and @math{@var{q} = ceiling(@var{x} / @var{y})}.\n" | |
2041 | "@lisp\n" | |
2042 | "(ceiling/ 123 10) @result{} 13 and -7\n" | |
2043 | "(ceiling/ 123 -10) @result{} -12 and 3\n" | |
2044 | "(ceiling/ -123 10) @result{} -12 and -3\n" | |
2045 | "(ceiling/ -123 -10) @result{} 13 and 7\n" | |
2046 | "(ceiling/ -123.2 -63.5) @result{} 2.0 and 3.8\n" | |
2047 | "(ceiling/ 16/3 -10/7) @result{} -3 and 22/21\n" | |
2048 | "@end lisp") | |
2049 | #define FUNC_NAME s_scm_i_ceiling_divide | |
2050 | { | |
2051 | SCM q, r; | |
2052 | ||
2053 | scm_ceiling_divide(x, y, &q, &r); | |
2054 | return scm_values (scm_list_2 (q, r)); | |
2055 | } | |
2056 | #undef FUNC_NAME | |
2057 | ||
2058 | #define s_scm_ceiling_divide s_scm_i_ceiling_divide | |
2059 | #define g_scm_ceiling_divide g_scm_i_ceiling_divide | |
2060 | ||
2061 | void | |
2062 | scm_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
2063 | { | |
2064 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
2065 | { | |
2066 | scm_t_inum xx = SCM_I_INUM (x); | |
2067 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2068 | { | |
2069 | scm_t_inum yy = SCM_I_INUM (y); | |
2070 | if (SCM_UNLIKELY (yy == 0)) | |
2071 | scm_num_overflow (s_scm_ceiling_divide); | |
2072 | else | |
2073 | { | |
2074 | scm_t_inum qq = xx / yy; | |
2075 | scm_t_inum rr = xx % yy; | |
2076 | int needs_adjustment; | |
2077 | ||
2078 | if (SCM_LIKELY (yy > 0)) | |
2079 | needs_adjustment = (rr > 0); | |
2080 | else | |
2081 | needs_adjustment = (rr < 0); | |
2082 | ||
2083 | if (needs_adjustment) | |
2084 | { | |
2085 | rr -= yy; | |
2086 | qq++; | |
2087 | } | |
2088 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
2089 | *qp = SCM_I_MAKINUM (qq); | |
2090 | else | |
2091 | *qp = scm_i_inum2big (qq); | |
2092 | *rp = SCM_I_MAKINUM (rr); | |
2093 | } | |
2094 | return; | |
2095 | } | |
2096 | else if (SCM_BIGP (y)) | |
2097 | { | |
2098 | int sign = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
2099 | scm_remember_upto_here_1 (y); | |
2100 | if (SCM_LIKELY (sign > 0)) | |
2101 | { | |
2102 | if (SCM_LIKELY (xx > 0)) | |
2103 | { | |
2104 | SCM r = scm_i_mkbig (); | |
2105 | mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx); | |
2106 | scm_remember_upto_here_1 (y); | |
2107 | mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r)); | |
2108 | *qp = SCM_INUM1; | |
2109 | *rp = scm_i_normbig (r); | |
2110 | } | |
2111 | else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM) | |
2112 | && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y), | |
2113 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
2114 | { | |
2115 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
2116 | scm_remember_upto_here_1 (y); | |
2117 | *qp = SCM_I_MAKINUM (-1); | |
2118 | *rp = SCM_INUM0; | |
2119 | } | |
2120 | else | |
2121 | { | |
2122 | *qp = SCM_INUM0; | |
2123 | *rp = x; | |
2124 | } | |
2125 | } | |
2126 | else if (xx >= 0) | |
2127 | { | |
2128 | *qp = SCM_INUM0; | |
2129 | *rp = x; | |
2130 | } | |
2131 | else | |
2132 | { | |
2133 | SCM r = scm_i_mkbig (); | |
2134 | mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx); | |
2135 | scm_remember_upto_here_1 (y); | |
2136 | mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r)); | |
2137 | *qp = SCM_INUM1; | |
2138 | *rp = scm_i_normbig (r); | |
2139 | } | |
2140 | return; | |
2141 | } | |
2142 | else if (SCM_REALP (y)) | |
2143 | return scm_i_inexact_ceiling_divide (xx, SCM_REAL_VALUE (y), qp, rp); | |
2144 | else if (SCM_FRACTIONP (y)) | |
2145 | return scm_i_exact_rational_ceiling_divide (x, y, qp, rp); | |
2146 | else | |
2147 | return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2, | |
2148 | s_scm_ceiling_divide, qp, rp); | |
2149 | } | |
2150 | else if (SCM_BIGP (x)) | |
2151 | { | |
2152 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2153 | { | |
2154 | scm_t_inum yy = SCM_I_INUM (y); | |
2155 | if (SCM_UNLIKELY (yy == 0)) | |
2156 | scm_num_overflow (s_scm_ceiling_divide); | |
2157 | else | |
2158 | { | |
2159 | SCM q = scm_i_mkbig (); | |
2160 | SCM r = scm_i_mkbig (); | |
2161 | if (yy > 0) | |
2162 | mpz_cdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
2163 | SCM_I_BIG_MPZ (x), yy); | |
2164 | else | |
2165 | { | |
2166 | mpz_fdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
2167 | SCM_I_BIG_MPZ (x), -yy); | |
2168 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
2169 | } | |
2170 | scm_remember_upto_here_1 (x); | |
2171 | *qp = scm_i_normbig (q); | |
2172 | *rp = scm_i_normbig (r); | |
2173 | } | |
2174 | return; | |
2175 | } | |
2176 | else if (SCM_BIGP (y)) | |
2177 | { | |
2178 | SCM q = scm_i_mkbig (); | |
2179 | SCM r = scm_i_mkbig (); | |
2180 | mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
2181 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
2182 | scm_remember_upto_here_2 (x, y); | |
2183 | *qp = scm_i_normbig (q); | |
2184 | *rp = scm_i_normbig (r); | |
2185 | return; | |
2186 | } | |
2187 | else if (SCM_REALP (y)) | |
2188 | return scm_i_inexact_ceiling_divide | |
2189 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp); | |
2190 | else if (SCM_FRACTIONP (y)) | |
2191 | return scm_i_exact_rational_ceiling_divide (x, y, qp, rp); | |
2192 | else | |
2193 | return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2, | |
2194 | s_scm_ceiling_divide, qp, rp); | |
2195 | } | |
2196 | else if (SCM_REALP (x)) | |
2197 | { | |
2198 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
2199 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2200 | return scm_i_inexact_ceiling_divide | |
2201 | (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp); | |
2202 | else | |
2203 | return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2, | |
2204 | s_scm_ceiling_divide, qp, rp); | |
2205 | } | |
2206 | else if (SCM_FRACTIONP (x)) | |
2207 | { | |
2208 | if (SCM_REALP (y)) | |
2209 | return scm_i_inexact_ceiling_divide | |
2210 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp); | |
2211 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2212 | return scm_i_exact_rational_ceiling_divide (x, y, qp, rp); | |
2213 | else | |
2214 | return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2, | |
2215 | s_scm_ceiling_divide, qp, rp); | |
2216 | } | |
2217 | else | |
2218 | return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG1, | |
2219 | s_scm_ceiling_divide, qp, rp); | |
2220 | } | |
2221 | ||
2222 | static void | |
2223 | scm_i_inexact_ceiling_divide (double x, double y, SCM *qp, SCM *rp) | |
2224 | { | |
2225 | if (SCM_UNLIKELY (y == 0)) | |
2226 | scm_num_overflow (s_scm_ceiling_divide); /* or return a NaN? */ | |
2227 | else | |
2228 | { | |
2229 | double q = ceil (x / y); | |
2230 | double r = x - q * y; | |
00472a22 MW |
2231 | *qp = scm_i_from_double (q); |
2232 | *rp = scm_i_from_double (r); | |
8f9da340 MW |
2233 | } |
2234 | } | |
2235 | ||
2236 | static void | |
2237 | scm_i_exact_rational_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
2238 | { | |
2239 | SCM r1; | |
2240 | SCM xd = scm_denominator (x); | |
2241 | SCM yd = scm_denominator (y); | |
2242 | ||
2243 | scm_ceiling_divide (scm_product (scm_numerator (x), yd), | |
2244 | scm_product (scm_numerator (y), xd), | |
2245 | qp, &r1); | |
2246 | *rp = scm_divide (r1, scm_product (xd, yd)); | |
2247 | } | |
2248 | ||
2249 | static SCM scm_i_inexact_truncate_quotient (double x, double y); | |
2250 | static SCM scm_i_exact_rational_truncate_quotient (SCM x, SCM y); | |
2251 | ||
2252 | SCM_PRIMITIVE_GENERIC (scm_truncate_quotient, "truncate-quotient", 2, 0, 0, | |
2253 | (SCM x, SCM y), | |
2254 | "Return @math{@var{x} / @var{y}} rounded toward zero.\n" | |
2255 | "@lisp\n" | |
2256 | "(truncate-quotient 123 10) @result{} 12\n" | |
2257 | "(truncate-quotient 123 -10) @result{} -12\n" | |
2258 | "(truncate-quotient -123 10) @result{} -12\n" | |
2259 | "(truncate-quotient -123 -10) @result{} 12\n" | |
2260 | "(truncate-quotient -123.2 -63.5) @result{} 1.0\n" | |
2261 | "(truncate-quotient 16/3 -10/7) @result{} -3\n" | |
2262 | "@end lisp") | |
2263 | #define FUNC_NAME s_scm_truncate_quotient | |
2264 | { | |
2265 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
2266 | { | |
2267 | scm_t_inum xx = SCM_I_INUM (x); | |
2268 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2269 | { | |
2270 | scm_t_inum yy = SCM_I_INUM (y); | |
2271 | if (SCM_UNLIKELY (yy == 0)) | |
2272 | scm_num_overflow (s_scm_truncate_quotient); | |
2273 | else | |
2274 | { | |
2275 | scm_t_inum qq = xx / yy; | |
2276 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
2277 | return SCM_I_MAKINUM (qq); | |
2278 | else | |
2279 | return scm_i_inum2big (qq); | |
2280 | } | |
2281 | } | |
2282 | else if (SCM_BIGP (y)) | |
2283 | { | |
2284 | if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM) | |
2285 | && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y), | |
2286 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
2287 | { | |
2288 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
2289 | scm_remember_upto_here_1 (y); | |
2290 | return SCM_I_MAKINUM (-1); | |
2291 | } | |
2292 | else | |
2293 | return SCM_INUM0; | |
2294 | } | |
2295 | else if (SCM_REALP (y)) | |
2296 | return scm_i_inexact_truncate_quotient (xx, SCM_REAL_VALUE (y)); | |
2297 | else if (SCM_FRACTIONP (y)) | |
2298 | return scm_i_exact_rational_truncate_quotient (x, y); | |
2299 | else | |
2300 | SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2, | |
2301 | s_scm_truncate_quotient); | |
2302 | } | |
2303 | else if (SCM_BIGP (x)) | |
2304 | { | |
2305 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2306 | { | |
2307 | scm_t_inum yy = SCM_I_INUM (y); | |
2308 | if (SCM_UNLIKELY (yy == 0)) | |
2309 | scm_num_overflow (s_scm_truncate_quotient); | |
2310 | else if (SCM_UNLIKELY (yy == 1)) | |
2311 | return x; | |
2312 | else | |
2313 | { | |
2314 | SCM q = scm_i_mkbig (); | |
2315 | if (yy > 0) | |
2316 | mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy); | |
2317 | else | |
2318 | { | |
2319 | mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy); | |
2320 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
2321 | } | |
2322 | scm_remember_upto_here_1 (x); | |
2323 | return scm_i_normbig (q); | |
2324 | } | |
2325 | } | |
2326 | else if (SCM_BIGP (y)) | |
2327 | { | |
2328 | SCM q = scm_i_mkbig (); | |
2329 | mpz_tdiv_q (SCM_I_BIG_MPZ (q), | |
2330 | SCM_I_BIG_MPZ (x), | |
2331 | SCM_I_BIG_MPZ (y)); | |
2332 | scm_remember_upto_here_2 (x, y); | |
2333 | return scm_i_normbig (q); | |
2334 | } | |
2335 | else if (SCM_REALP (y)) | |
2336 | return scm_i_inexact_truncate_quotient | |
2337 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
2338 | else if (SCM_FRACTIONP (y)) | |
2339 | return scm_i_exact_rational_truncate_quotient (x, y); | |
2340 | else | |
2341 | SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2, | |
2342 | s_scm_truncate_quotient); | |
2343 | } | |
2344 | else if (SCM_REALP (x)) | |
2345 | { | |
2346 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
2347 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2348 | return scm_i_inexact_truncate_quotient | |
2349 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
2350 | else | |
2351 | SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2, | |
2352 | s_scm_truncate_quotient); | |
2353 | } | |
2354 | else if (SCM_FRACTIONP (x)) | |
2355 | { | |
2356 | if (SCM_REALP (y)) | |
2357 | return scm_i_inexact_truncate_quotient | |
2358 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
2359 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2360 | return scm_i_exact_rational_truncate_quotient (x, y); | |
2361 | else | |
2362 | SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2, | |
2363 | s_scm_truncate_quotient); | |
2364 | } | |
2365 | else | |
2366 | SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG1, | |
2367 | s_scm_truncate_quotient); | |
2368 | } | |
2369 | #undef FUNC_NAME | |
2370 | ||
2371 | static SCM | |
2372 | scm_i_inexact_truncate_quotient (double x, double y) | |
2373 | { | |
2374 | if (SCM_UNLIKELY (y == 0)) | |
2375 | scm_num_overflow (s_scm_truncate_quotient); /* or return a NaN? */ | |
2376 | else | |
00472a22 | 2377 | return scm_i_from_double (trunc (x / y)); |
8f9da340 MW |
2378 | } |
2379 | ||
2380 | static SCM | |
2381 | scm_i_exact_rational_truncate_quotient (SCM x, SCM y) | |
2382 | { | |
2383 | return scm_truncate_quotient | |
2384 | (scm_product (scm_numerator (x), scm_denominator (y)), | |
2385 | scm_product (scm_numerator (y), scm_denominator (x))); | |
2386 | } | |
2387 | ||
2388 | static SCM scm_i_inexact_truncate_remainder (double x, double y); | |
2389 | static SCM scm_i_exact_rational_truncate_remainder (SCM x, SCM y); | |
2390 | ||
2391 | SCM_PRIMITIVE_GENERIC (scm_truncate_remainder, "truncate-remainder", 2, 0, 0, | |
2392 | (SCM x, SCM y), | |
2393 | "Return the real number @var{r} such that\n" | |
2394 | "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
2395 | "where @math{@var{q} = truncate(@var{x} / @var{y})}.\n" | |
2396 | "@lisp\n" | |
2397 | "(truncate-remainder 123 10) @result{} 3\n" | |
2398 | "(truncate-remainder 123 -10) @result{} 3\n" | |
2399 | "(truncate-remainder -123 10) @result{} -3\n" | |
2400 | "(truncate-remainder -123 -10) @result{} -3\n" | |
2401 | "(truncate-remainder -123.2 -63.5) @result{} -59.7\n" | |
2402 | "(truncate-remainder 16/3 -10/7) @result{} 22/21\n" | |
2403 | "@end lisp") | |
2404 | #define FUNC_NAME s_scm_truncate_remainder | |
2405 | { | |
2406 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
2407 | { | |
2408 | scm_t_inum xx = SCM_I_INUM (x); | |
2409 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2410 | { | |
2411 | scm_t_inum yy = SCM_I_INUM (y); | |
2412 | if (SCM_UNLIKELY (yy == 0)) | |
2413 | scm_num_overflow (s_scm_truncate_remainder); | |
2414 | else | |
2415 | return SCM_I_MAKINUM (xx % yy); | |
2416 | } | |
2417 | else if (SCM_BIGP (y)) | |
2418 | { | |
2419 | if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM) | |
2420 | && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y), | |
2421 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
2422 | { | |
2423 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
2424 | scm_remember_upto_here_1 (y); | |
2425 | return SCM_INUM0; | |
2426 | } | |
2427 | else | |
2428 | return x; | |
2429 | } | |
2430 | else if (SCM_REALP (y)) | |
2431 | return scm_i_inexact_truncate_remainder (xx, SCM_REAL_VALUE (y)); | |
2432 | else if (SCM_FRACTIONP (y)) | |
2433 | return scm_i_exact_rational_truncate_remainder (x, y); | |
2434 | else | |
2435 | SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2, | |
2436 | s_scm_truncate_remainder); | |
2437 | } | |
2438 | else if (SCM_BIGP (x)) | |
2439 | { | |
2440 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2441 | { | |
2442 | scm_t_inum yy = SCM_I_INUM (y); | |
2443 | if (SCM_UNLIKELY (yy == 0)) | |
2444 | scm_num_overflow (s_scm_truncate_remainder); | |
2445 | else | |
2446 | { | |
2447 | scm_t_inum rr = (mpz_tdiv_ui (SCM_I_BIG_MPZ (x), | |
2448 | (yy > 0) ? yy : -yy) | |
2449 | * mpz_sgn (SCM_I_BIG_MPZ (x))); | |
2450 | scm_remember_upto_here_1 (x); | |
2451 | return SCM_I_MAKINUM (rr); | |
2452 | } | |
2453 | } | |
2454 | else if (SCM_BIGP (y)) | |
2455 | { | |
2456 | SCM r = scm_i_mkbig (); | |
2457 | mpz_tdiv_r (SCM_I_BIG_MPZ (r), | |
2458 | SCM_I_BIG_MPZ (x), | |
2459 | SCM_I_BIG_MPZ (y)); | |
2460 | scm_remember_upto_here_2 (x, y); | |
2461 | return scm_i_normbig (r); | |
2462 | } | |
2463 | else if (SCM_REALP (y)) | |
2464 | return scm_i_inexact_truncate_remainder | |
2465 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
2466 | else if (SCM_FRACTIONP (y)) | |
2467 | return scm_i_exact_rational_truncate_remainder (x, y); | |
2468 | else | |
2469 | SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2, | |
2470 | s_scm_truncate_remainder); | |
2471 | } | |
2472 | else if (SCM_REALP (x)) | |
2473 | { | |
2474 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
2475 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2476 | return scm_i_inexact_truncate_remainder | |
2477 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
2478 | else | |
2479 | SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2, | |
2480 | s_scm_truncate_remainder); | |
2481 | } | |
2482 | else if (SCM_FRACTIONP (x)) | |
2483 | { | |
2484 | if (SCM_REALP (y)) | |
2485 | return scm_i_inexact_truncate_remainder | |
2486 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
2487 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2488 | return scm_i_exact_rational_truncate_remainder (x, y); | |
2489 | else | |
2490 | SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2, | |
2491 | s_scm_truncate_remainder); | |
2492 | } | |
2493 | else | |
2494 | SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG1, | |
2495 | s_scm_truncate_remainder); | |
2496 | } | |
2497 | #undef FUNC_NAME | |
2498 | ||
2499 | static SCM | |
2500 | scm_i_inexact_truncate_remainder (double x, double y) | |
2501 | { | |
2502 | /* Although it would be more efficient to use fmod here, we can't | |
2503 | because it would in some cases produce results inconsistent with | |
2504 | scm_i_inexact_truncate_quotient, such that x != q * y + r (not even | |
2505 | close). In particular, when x is very close to a multiple of y, | |
2506 | then r might be either 0.0 or sgn(x)*|y|, but those two cases must | |
2507 | correspond to different choices of q. If quotient chooses one and | |
2508 | remainder chooses the other, it would be bad. */ | |
2509 | if (SCM_UNLIKELY (y == 0)) | |
2510 | scm_num_overflow (s_scm_truncate_remainder); /* or return a NaN? */ | |
2511 | else | |
00472a22 | 2512 | return scm_i_from_double (x - y * trunc (x / y)); |
8f9da340 MW |
2513 | } |
2514 | ||
2515 | static SCM | |
2516 | scm_i_exact_rational_truncate_remainder (SCM x, SCM y) | |
2517 | { | |
2518 | SCM xd = scm_denominator (x); | |
2519 | SCM yd = scm_denominator (y); | |
2520 | SCM r1 = scm_truncate_remainder (scm_product (scm_numerator (x), yd), | |
2521 | scm_product (scm_numerator (y), xd)); | |
2522 | return scm_divide (r1, scm_product (xd, yd)); | |
2523 | } | |
2524 | ||
2525 | ||
2526 | static void scm_i_inexact_truncate_divide (double x, double y, | |
2527 | SCM *qp, SCM *rp); | |
2528 | static void scm_i_exact_rational_truncate_divide (SCM x, SCM y, | |
2529 | SCM *qp, SCM *rp); | |
2530 | ||
2531 | SCM_PRIMITIVE_GENERIC (scm_i_truncate_divide, "truncate/", 2, 0, 0, | |
2532 | (SCM x, SCM y), | |
2533 | "Return the integer @var{q} and the real number @var{r}\n" | |
2534 | "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
2535 | "and @math{@var{q} = truncate(@var{x} / @var{y})}.\n" | |
2536 | "@lisp\n" | |
2537 | "(truncate/ 123 10) @result{} 12 and 3\n" | |
2538 | "(truncate/ 123 -10) @result{} -12 and 3\n" | |
2539 | "(truncate/ -123 10) @result{} -12 and -3\n" | |
2540 | "(truncate/ -123 -10) @result{} 12 and -3\n" | |
2541 | "(truncate/ -123.2 -63.5) @result{} 1.0 and -59.7\n" | |
2542 | "(truncate/ 16/3 -10/7) @result{} -3 and 22/21\n" | |
2543 | "@end lisp") | |
2544 | #define FUNC_NAME s_scm_i_truncate_divide | |
2545 | { | |
2546 | SCM q, r; | |
2547 | ||
2548 | scm_truncate_divide(x, y, &q, &r); | |
2549 | return scm_values (scm_list_2 (q, r)); | |
2550 | } | |
2551 | #undef FUNC_NAME | |
2552 | ||
2553 | #define s_scm_truncate_divide s_scm_i_truncate_divide | |
2554 | #define g_scm_truncate_divide g_scm_i_truncate_divide | |
2555 | ||
2556 | void | |
2557 | scm_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
2558 | { | |
2559 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
2560 | { | |
2561 | scm_t_inum xx = SCM_I_INUM (x); | |
2562 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2563 | { | |
2564 | scm_t_inum yy = SCM_I_INUM (y); | |
2565 | if (SCM_UNLIKELY (yy == 0)) | |
2566 | scm_num_overflow (s_scm_truncate_divide); | |
2567 | else | |
2568 | { | |
2569 | scm_t_inum qq = xx / yy; | |
2570 | scm_t_inum rr = xx % yy; | |
2571 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
2572 | *qp = SCM_I_MAKINUM (qq); | |
2573 | else | |
2574 | *qp = scm_i_inum2big (qq); | |
2575 | *rp = SCM_I_MAKINUM (rr); | |
2576 | } | |
2577 | return; | |
2578 | } | |
2579 | else if (SCM_BIGP (y)) | |
2580 | { | |
2581 | if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM) | |
2582 | && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y), | |
2583 | - SCM_MOST_NEGATIVE_FIXNUM) == 0)) | |
2584 | { | |
2585 | /* Special case: x == fixnum-min && y == abs (fixnum-min) */ | |
2586 | scm_remember_upto_here_1 (y); | |
2587 | *qp = SCM_I_MAKINUM (-1); | |
2588 | *rp = SCM_INUM0; | |
2589 | } | |
2590 | else | |
2591 | { | |
2592 | *qp = SCM_INUM0; | |
2593 | *rp = x; | |
2594 | } | |
2595 | return; | |
2596 | } | |
2597 | else if (SCM_REALP (y)) | |
2598 | return scm_i_inexact_truncate_divide (xx, SCM_REAL_VALUE (y), qp, rp); | |
2599 | else if (SCM_FRACTIONP (y)) | |
2600 | return scm_i_exact_rational_truncate_divide (x, y, qp, rp); | |
2601 | else | |
2602 | return two_valued_wta_dispatch_2 | |
2603 | (g_scm_truncate_divide, x, y, SCM_ARG2, | |
2604 | s_scm_truncate_divide, qp, rp); | |
2605 | } | |
2606 | else if (SCM_BIGP (x)) | |
2607 | { | |
2608 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2609 | { | |
2610 | scm_t_inum yy = SCM_I_INUM (y); | |
2611 | if (SCM_UNLIKELY (yy == 0)) | |
2612 | scm_num_overflow (s_scm_truncate_divide); | |
2613 | else | |
2614 | { | |
2615 | SCM q = scm_i_mkbig (); | |
2616 | scm_t_inum rr; | |
2617 | if (yy > 0) | |
2618 | rr = mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), | |
2619 | SCM_I_BIG_MPZ (x), yy); | |
2620 | else | |
2621 | { | |
2622 | rr = mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), | |
2623 | SCM_I_BIG_MPZ (x), -yy); | |
2624 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
2625 | } | |
2626 | rr *= mpz_sgn (SCM_I_BIG_MPZ (x)); | |
2627 | scm_remember_upto_here_1 (x); | |
2628 | *qp = scm_i_normbig (q); | |
2629 | *rp = SCM_I_MAKINUM (rr); | |
2630 | } | |
2631 | return; | |
2632 | } | |
2633 | else if (SCM_BIGP (y)) | |
2634 | { | |
2635 | SCM q = scm_i_mkbig (); | |
2636 | SCM r = scm_i_mkbig (); | |
2637 | mpz_tdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
2638 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
2639 | scm_remember_upto_here_2 (x, y); | |
2640 | *qp = scm_i_normbig (q); | |
2641 | *rp = scm_i_normbig (r); | |
2642 | } | |
2643 | else if (SCM_REALP (y)) | |
2644 | return scm_i_inexact_truncate_divide | |
2645 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp); | |
2646 | else if (SCM_FRACTIONP (y)) | |
2647 | return scm_i_exact_rational_truncate_divide (x, y, qp, rp); | |
2648 | else | |
2649 | return two_valued_wta_dispatch_2 | |
2650 | (g_scm_truncate_divide, x, y, SCM_ARG2, | |
2651 | s_scm_truncate_divide, qp, rp); | |
2652 | } | |
2653 | else if (SCM_REALP (x)) | |
2654 | { | |
2655 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
2656 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2657 | return scm_i_inexact_truncate_divide | |
2658 | (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp); | |
2659 | else | |
2660 | return two_valued_wta_dispatch_2 | |
2661 | (g_scm_truncate_divide, x, y, SCM_ARG2, | |
2662 | s_scm_truncate_divide, qp, rp); | |
2663 | } | |
2664 | else if (SCM_FRACTIONP (x)) | |
2665 | { | |
2666 | if (SCM_REALP (y)) | |
2667 | return scm_i_inexact_truncate_divide | |
2668 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp); | |
2669 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2670 | return scm_i_exact_rational_truncate_divide (x, y, qp, rp); | |
2671 | else | |
2672 | return two_valued_wta_dispatch_2 | |
2673 | (g_scm_truncate_divide, x, y, SCM_ARG2, | |
2674 | s_scm_truncate_divide, qp, rp); | |
2675 | } | |
2676 | else | |
2677 | return two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG1, | |
2678 | s_scm_truncate_divide, qp, rp); | |
2679 | } | |
2680 | ||
2681 | static void | |
2682 | scm_i_inexact_truncate_divide (double x, double y, SCM *qp, SCM *rp) | |
2683 | { | |
2684 | if (SCM_UNLIKELY (y == 0)) | |
2685 | scm_num_overflow (s_scm_truncate_divide); /* or return a NaN? */ | |
2686 | else | |
2687 | { | |
c15fe499 MW |
2688 | double q = trunc (x / y); |
2689 | double r = x - q * y; | |
00472a22 MW |
2690 | *qp = scm_i_from_double (q); |
2691 | *rp = scm_i_from_double (r); | |
8f9da340 MW |
2692 | } |
2693 | } | |
2694 | ||
2695 | static void | |
2696 | scm_i_exact_rational_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
2697 | { | |
2698 | SCM r1; | |
2699 | SCM xd = scm_denominator (x); | |
2700 | SCM yd = scm_denominator (y); | |
2701 | ||
2702 | scm_truncate_divide (scm_product (scm_numerator (x), yd), | |
2703 | scm_product (scm_numerator (y), xd), | |
2704 | qp, &r1); | |
2705 | *rp = scm_divide (r1, scm_product (xd, yd)); | |
2706 | } | |
2707 | ||
ff62c168 MW |
2708 | static SCM scm_i_inexact_centered_quotient (double x, double y); |
2709 | static SCM scm_i_bigint_centered_quotient (SCM x, SCM y); | |
03ddd15b | 2710 | static SCM scm_i_exact_rational_centered_quotient (SCM x, SCM y); |
ff62c168 | 2711 | |
8f9da340 MW |
2712 | SCM_PRIMITIVE_GENERIC (scm_centered_quotient, "centered-quotient", 2, 0, 0, |
2713 | (SCM x, SCM y), | |
2714 | "Return the integer @var{q} such that\n" | |
2715 | "@math{@var{x} = @var{q}*@var{y} + @var{r}} where\n" | |
2716 | "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n" | |
2717 | "@lisp\n" | |
2718 | "(centered-quotient 123 10) @result{} 12\n" | |
2719 | "(centered-quotient 123 -10) @result{} -12\n" | |
2720 | "(centered-quotient -123 10) @result{} -12\n" | |
2721 | "(centered-quotient -123 -10) @result{} 12\n" | |
2722 | "(centered-quotient -123.2 -63.5) @result{} 2.0\n" | |
2723 | "(centered-quotient 16/3 -10/7) @result{} -4\n" | |
2724 | "@end lisp") | |
2725 | #define FUNC_NAME s_scm_centered_quotient | |
2726 | { | |
2727 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
2728 | { | |
2729 | scm_t_inum xx = SCM_I_INUM (x); | |
2730 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2731 | { | |
2732 | scm_t_inum yy = SCM_I_INUM (y); | |
2733 | if (SCM_UNLIKELY (yy == 0)) | |
2734 | scm_num_overflow (s_scm_centered_quotient); | |
2735 | else | |
2736 | { | |
2737 | scm_t_inum qq = xx / yy; | |
2738 | scm_t_inum rr = xx % yy; | |
2739 | if (SCM_LIKELY (xx > 0)) | |
2740 | { | |
2741 | if (SCM_LIKELY (yy > 0)) | |
2742 | { | |
2743 | if (rr >= (yy + 1) / 2) | |
2744 | qq++; | |
2745 | } | |
2746 | else | |
2747 | { | |
2748 | if (rr >= (1 - yy) / 2) | |
2749 | qq--; | |
2750 | } | |
2751 | } | |
2752 | else | |
2753 | { | |
2754 | if (SCM_LIKELY (yy > 0)) | |
2755 | { | |
2756 | if (rr < -yy / 2) | |
2757 | qq--; | |
2758 | } | |
2759 | else | |
2760 | { | |
2761 | if (rr < yy / 2) | |
2762 | qq++; | |
2763 | } | |
2764 | } | |
2765 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
2766 | return SCM_I_MAKINUM (qq); | |
2767 | else | |
2768 | return scm_i_inum2big (qq); | |
2769 | } | |
2770 | } | |
2771 | else if (SCM_BIGP (y)) | |
2772 | { | |
2773 | /* Pass a denormalized bignum version of x (even though it | |
2774 | can fit in a fixnum) to scm_i_bigint_centered_quotient */ | |
2775 | return scm_i_bigint_centered_quotient (scm_i_long2big (xx), y); | |
2776 | } | |
2777 | else if (SCM_REALP (y)) | |
2778 | return scm_i_inexact_centered_quotient (xx, SCM_REAL_VALUE (y)); | |
2779 | else if (SCM_FRACTIONP (y)) | |
2780 | return scm_i_exact_rational_centered_quotient (x, y); | |
2781 | else | |
2782 | SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2, | |
2783 | s_scm_centered_quotient); | |
2784 | } | |
2785 | else if (SCM_BIGP (x)) | |
2786 | { | |
2787 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2788 | { | |
2789 | scm_t_inum yy = SCM_I_INUM (y); | |
2790 | if (SCM_UNLIKELY (yy == 0)) | |
2791 | scm_num_overflow (s_scm_centered_quotient); | |
2792 | else if (SCM_UNLIKELY (yy == 1)) | |
2793 | return x; | |
2794 | else | |
2795 | { | |
2796 | SCM q = scm_i_mkbig (); | |
2797 | scm_t_inum rr; | |
2798 | /* Arrange for rr to initially be non-positive, | |
2799 | because that simplifies the test to see | |
2800 | if it is within the needed bounds. */ | |
2801 | if (yy > 0) | |
2802 | { | |
2803 | rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), | |
2804 | SCM_I_BIG_MPZ (x), yy); | |
2805 | scm_remember_upto_here_1 (x); | |
2806 | if (rr < -yy / 2) | |
2807 | mpz_sub_ui (SCM_I_BIG_MPZ (q), | |
2808 | SCM_I_BIG_MPZ (q), 1); | |
2809 | } | |
2810 | else | |
2811 | { | |
2812 | rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), | |
2813 | SCM_I_BIG_MPZ (x), -yy); | |
2814 | scm_remember_upto_here_1 (x); | |
2815 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
2816 | if (rr < yy / 2) | |
2817 | mpz_add_ui (SCM_I_BIG_MPZ (q), | |
2818 | SCM_I_BIG_MPZ (q), 1); | |
2819 | } | |
2820 | return scm_i_normbig (q); | |
2821 | } | |
2822 | } | |
2823 | else if (SCM_BIGP (y)) | |
2824 | return scm_i_bigint_centered_quotient (x, y); | |
2825 | else if (SCM_REALP (y)) | |
2826 | return scm_i_inexact_centered_quotient | |
2827 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
2828 | else if (SCM_FRACTIONP (y)) | |
2829 | return scm_i_exact_rational_centered_quotient (x, y); | |
2830 | else | |
2831 | SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2, | |
2832 | s_scm_centered_quotient); | |
2833 | } | |
2834 | else if (SCM_REALP (x)) | |
2835 | { | |
2836 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
2837 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2838 | return scm_i_inexact_centered_quotient | |
2839 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
2840 | else | |
2841 | SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2, | |
2842 | s_scm_centered_quotient); | |
2843 | } | |
2844 | else if (SCM_FRACTIONP (x)) | |
2845 | { | |
2846 | if (SCM_REALP (y)) | |
2847 | return scm_i_inexact_centered_quotient | |
2848 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
2849 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
2850 | return scm_i_exact_rational_centered_quotient (x, y); | |
2851 | else | |
2852 | SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2, | |
2853 | s_scm_centered_quotient); | |
2854 | } | |
2855 | else | |
2856 | SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG1, | |
2857 | s_scm_centered_quotient); | |
2858 | } | |
2859 | #undef FUNC_NAME | |
2860 | ||
2861 | static SCM | |
2862 | scm_i_inexact_centered_quotient (double x, double y) | |
2863 | { | |
2864 | if (SCM_LIKELY (y > 0)) | |
00472a22 | 2865 | return scm_i_from_double (floor (x/y + 0.5)); |
8f9da340 | 2866 | else if (SCM_LIKELY (y < 0)) |
00472a22 | 2867 | return scm_i_from_double (ceil (x/y - 0.5)); |
8f9da340 MW |
2868 | else if (y == 0) |
2869 | scm_num_overflow (s_scm_centered_quotient); /* or return a NaN? */ | |
2870 | else | |
2871 | return scm_nan (); | |
2872 | } | |
2873 | ||
2874 | /* Assumes that both x and y are bigints, though | |
2875 | x might be able to fit into a fixnum. */ | |
2876 | static SCM | |
2877 | scm_i_bigint_centered_quotient (SCM x, SCM y) | |
2878 | { | |
2879 | SCM q, r, min_r; | |
2880 | ||
2881 | /* Note that x might be small enough to fit into a | |
2882 | fixnum, so we must not let it escape into the wild */ | |
2883 | q = scm_i_mkbig (); | |
2884 | r = scm_i_mkbig (); | |
2885 | ||
2886 | /* min_r will eventually become -abs(y)/2 */ | |
2887 | min_r = scm_i_mkbig (); | |
2888 | mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r), | |
2889 | SCM_I_BIG_MPZ (y), 1); | |
2890 | ||
2891 | /* Arrange for rr to initially be non-positive, | |
2892 | because that simplifies the test to see | |
2893 | if it is within the needed bounds. */ | |
2894 | if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0) | |
2895 | { | |
2896 | mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
2897 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
2898 | scm_remember_upto_here_2 (x, y); | |
2899 | mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r)); | |
2900 | if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0) | |
2901 | mpz_sub_ui (SCM_I_BIG_MPZ (q), | |
2902 | SCM_I_BIG_MPZ (q), 1); | |
2903 | } | |
2904 | else | |
2905 | { | |
2906 | mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
2907 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
2908 | scm_remember_upto_here_2 (x, y); | |
2909 | if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0) | |
2910 | mpz_add_ui (SCM_I_BIG_MPZ (q), | |
2911 | SCM_I_BIG_MPZ (q), 1); | |
2912 | } | |
2913 | scm_remember_upto_here_2 (r, min_r); | |
2914 | return scm_i_normbig (q); | |
2915 | } | |
2916 | ||
2917 | static SCM | |
2918 | scm_i_exact_rational_centered_quotient (SCM x, SCM y) | |
2919 | { | |
2920 | return scm_centered_quotient | |
2921 | (scm_product (scm_numerator (x), scm_denominator (y)), | |
2922 | scm_product (scm_numerator (y), scm_denominator (x))); | |
2923 | } | |
2924 | ||
2925 | static SCM scm_i_inexact_centered_remainder (double x, double y); | |
2926 | static SCM scm_i_bigint_centered_remainder (SCM x, SCM y); | |
2927 | static SCM scm_i_exact_rational_centered_remainder (SCM x, SCM y); | |
2928 | ||
2929 | SCM_PRIMITIVE_GENERIC (scm_centered_remainder, "centered-remainder", 2, 0, 0, | |
2930 | (SCM x, SCM y), | |
2931 | "Return the real number @var{r} such that\n" | |
2932 | "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}\n" | |
2933 | "and @math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
2934 | "for some integer @var{q}.\n" | |
2935 | "@lisp\n" | |
2936 | "(centered-remainder 123 10) @result{} 3\n" | |
2937 | "(centered-remainder 123 -10) @result{} 3\n" | |
2938 | "(centered-remainder -123 10) @result{} -3\n" | |
2939 | "(centered-remainder -123 -10) @result{} -3\n" | |
2940 | "(centered-remainder -123.2 -63.5) @result{} 3.8\n" | |
2941 | "(centered-remainder 16/3 -10/7) @result{} -8/21\n" | |
2942 | "@end lisp") | |
2943 | #define FUNC_NAME s_scm_centered_remainder | |
2944 | { | |
2945 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
2946 | { | |
2947 | scm_t_inum xx = SCM_I_INUM (x); | |
2948 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
2949 | { | |
2950 | scm_t_inum yy = SCM_I_INUM (y); | |
2951 | if (SCM_UNLIKELY (yy == 0)) | |
2952 | scm_num_overflow (s_scm_centered_remainder); | |
2953 | else | |
2954 | { | |
2955 | scm_t_inum rr = xx % yy; | |
2956 | if (SCM_LIKELY (xx > 0)) | |
2957 | { | |
2958 | if (SCM_LIKELY (yy > 0)) | |
2959 | { | |
2960 | if (rr >= (yy + 1) / 2) | |
2961 | rr -= yy; | |
2962 | } | |
2963 | else | |
2964 | { | |
2965 | if (rr >= (1 - yy) / 2) | |
2966 | rr += yy; | |
2967 | } | |
2968 | } | |
2969 | else | |
2970 | { | |
2971 | if (SCM_LIKELY (yy > 0)) | |
2972 | { | |
2973 | if (rr < -yy / 2) | |
2974 | rr += yy; | |
2975 | } | |
2976 | else | |
2977 | { | |
2978 | if (rr < yy / 2) | |
2979 | rr -= yy; | |
2980 | } | |
2981 | } | |
2982 | return SCM_I_MAKINUM (rr); | |
2983 | } | |
2984 | } | |
2985 | else if (SCM_BIGP (y)) | |
2986 | { | |
2987 | /* Pass a denormalized bignum version of x (even though it | |
2988 | can fit in a fixnum) to scm_i_bigint_centered_remainder */ | |
2989 | return scm_i_bigint_centered_remainder (scm_i_long2big (xx), y); | |
2990 | } | |
2991 | else if (SCM_REALP (y)) | |
2992 | return scm_i_inexact_centered_remainder (xx, SCM_REAL_VALUE (y)); | |
2993 | else if (SCM_FRACTIONP (y)) | |
2994 | return scm_i_exact_rational_centered_remainder (x, y); | |
2995 | else | |
2996 | SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2, | |
2997 | s_scm_centered_remainder); | |
2998 | } | |
2999 | else if (SCM_BIGP (x)) | |
3000 | { | |
3001 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
3002 | { | |
3003 | scm_t_inum yy = SCM_I_INUM (y); | |
3004 | if (SCM_UNLIKELY (yy == 0)) | |
3005 | scm_num_overflow (s_scm_centered_remainder); | |
3006 | else | |
3007 | { | |
3008 | scm_t_inum rr; | |
3009 | /* Arrange for rr to initially be non-positive, | |
3010 | because that simplifies the test to see | |
3011 | if it is within the needed bounds. */ | |
3012 | if (yy > 0) | |
3013 | { | |
3014 | rr = - mpz_cdiv_ui (SCM_I_BIG_MPZ (x), yy); | |
3015 | scm_remember_upto_here_1 (x); | |
3016 | if (rr < -yy / 2) | |
3017 | rr += yy; | |
3018 | } | |
3019 | else | |
3020 | { | |
3021 | rr = - mpz_cdiv_ui (SCM_I_BIG_MPZ (x), -yy); | |
3022 | scm_remember_upto_here_1 (x); | |
3023 | if (rr < yy / 2) | |
3024 | rr -= yy; | |
3025 | } | |
3026 | return SCM_I_MAKINUM (rr); | |
3027 | } | |
3028 | } | |
3029 | else if (SCM_BIGP (y)) | |
3030 | return scm_i_bigint_centered_remainder (x, y); | |
3031 | else if (SCM_REALP (y)) | |
3032 | return scm_i_inexact_centered_remainder | |
3033 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); | |
3034 | else if (SCM_FRACTIONP (y)) | |
3035 | return scm_i_exact_rational_centered_remainder (x, y); | |
3036 | else | |
3037 | SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2, | |
3038 | s_scm_centered_remainder); | |
3039 | } | |
3040 | else if (SCM_REALP (x)) | |
3041 | { | |
3042 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
3043 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
3044 | return scm_i_inexact_centered_remainder | |
3045 | (SCM_REAL_VALUE (x), scm_to_double (y)); | |
3046 | else | |
3047 | SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2, | |
3048 | s_scm_centered_remainder); | |
3049 | } | |
3050 | else if (SCM_FRACTIONP (x)) | |
3051 | { | |
3052 | if (SCM_REALP (y)) | |
3053 | return scm_i_inexact_centered_remainder | |
3054 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); | |
3055 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
3056 | return scm_i_exact_rational_centered_remainder (x, y); | |
3057 | else | |
3058 | SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2, | |
3059 | s_scm_centered_remainder); | |
3060 | } | |
3061 | else | |
3062 | SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG1, | |
3063 | s_scm_centered_remainder); | |
3064 | } | |
3065 | #undef FUNC_NAME | |
3066 | ||
3067 | static SCM | |
3068 | scm_i_inexact_centered_remainder (double x, double y) | |
3069 | { | |
3070 | double q; | |
3071 | ||
3072 | /* Although it would be more efficient to use fmod here, we can't | |
3073 | because it would in some cases produce results inconsistent with | |
3074 | scm_i_inexact_centered_quotient, such that x != r + q * y (not even | |
3075 | close). In particular, when x-y/2 is very close to a multiple of | |
3076 | y, then r might be either -abs(y/2) or abs(y/2)-epsilon, but those | |
3077 | two cases must correspond to different choices of q. If quotient | |
3078 | chooses one and remainder chooses the other, it would be bad. */ | |
3079 | if (SCM_LIKELY (y > 0)) | |
3080 | q = floor (x/y + 0.5); | |
3081 | else if (SCM_LIKELY (y < 0)) | |
3082 | q = ceil (x/y - 0.5); | |
3083 | else if (y == 0) | |
3084 | scm_num_overflow (s_scm_centered_remainder); /* or return a NaN? */ | |
3085 | else | |
3086 | return scm_nan (); | |
00472a22 | 3087 | return scm_i_from_double (x - q * y); |
8f9da340 MW |
3088 | } |
3089 | ||
3090 | /* Assumes that both x and y are bigints, though | |
3091 | x might be able to fit into a fixnum. */ | |
3092 | static SCM | |
3093 | scm_i_bigint_centered_remainder (SCM x, SCM y) | |
3094 | { | |
3095 | SCM r, min_r; | |
3096 | ||
3097 | /* Note that x might be small enough to fit into a | |
3098 | fixnum, so we must not let it escape into the wild */ | |
3099 | r = scm_i_mkbig (); | |
3100 | ||
3101 | /* min_r will eventually become -abs(y)/2 */ | |
3102 | min_r = scm_i_mkbig (); | |
3103 | mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r), | |
3104 | SCM_I_BIG_MPZ (y), 1); | |
3105 | ||
3106 | /* Arrange for rr to initially be non-positive, | |
3107 | because that simplifies the test to see | |
3108 | if it is within the needed bounds. */ | |
3109 | if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0) | |
3110 | { | |
3111 | mpz_cdiv_r (SCM_I_BIG_MPZ (r), | |
3112 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3113 | mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r)); | |
3114 | if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0) | |
3115 | mpz_add (SCM_I_BIG_MPZ (r), | |
3116 | SCM_I_BIG_MPZ (r), | |
3117 | SCM_I_BIG_MPZ (y)); | |
3118 | } | |
3119 | else | |
3120 | { | |
3121 | mpz_fdiv_r (SCM_I_BIG_MPZ (r), | |
3122 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3123 | if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0) | |
3124 | mpz_sub (SCM_I_BIG_MPZ (r), | |
3125 | SCM_I_BIG_MPZ (r), | |
3126 | SCM_I_BIG_MPZ (y)); | |
3127 | } | |
3128 | scm_remember_upto_here_2 (x, y); | |
3129 | return scm_i_normbig (r); | |
3130 | } | |
3131 | ||
3132 | static SCM | |
3133 | scm_i_exact_rational_centered_remainder (SCM x, SCM y) | |
3134 | { | |
3135 | SCM xd = scm_denominator (x); | |
3136 | SCM yd = scm_denominator (y); | |
3137 | SCM r1 = scm_centered_remainder (scm_product (scm_numerator (x), yd), | |
3138 | scm_product (scm_numerator (y), xd)); | |
3139 | return scm_divide (r1, scm_product (xd, yd)); | |
3140 | } | |
3141 | ||
3142 | ||
3143 | static void scm_i_inexact_centered_divide (double x, double y, | |
3144 | SCM *qp, SCM *rp); | |
3145 | static void scm_i_bigint_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp); | |
3146 | static void scm_i_exact_rational_centered_divide (SCM x, SCM y, | |
3147 | SCM *qp, SCM *rp); | |
3148 | ||
3149 | SCM_PRIMITIVE_GENERIC (scm_i_centered_divide, "centered/", 2, 0, 0, | |
3150 | (SCM x, SCM y), | |
3151 | "Return the integer @var{q} and the real number @var{r}\n" | |
3152 | "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
3153 | "and @math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n" | |
3154 | "@lisp\n" | |
3155 | "(centered/ 123 10) @result{} 12 and 3\n" | |
3156 | "(centered/ 123 -10) @result{} -12 and 3\n" | |
3157 | "(centered/ -123 10) @result{} -12 and -3\n" | |
3158 | "(centered/ -123 -10) @result{} 12 and -3\n" | |
3159 | "(centered/ -123.2 -63.5) @result{} 2.0 and 3.8\n" | |
3160 | "(centered/ 16/3 -10/7) @result{} -4 and -8/21\n" | |
3161 | "@end lisp") | |
3162 | #define FUNC_NAME s_scm_i_centered_divide | |
3163 | { | |
3164 | SCM q, r; | |
3165 | ||
3166 | scm_centered_divide(x, y, &q, &r); | |
3167 | return scm_values (scm_list_2 (q, r)); | |
3168 | } | |
3169 | #undef FUNC_NAME | |
3170 | ||
3171 | #define s_scm_centered_divide s_scm_i_centered_divide | |
3172 | #define g_scm_centered_divide g_scm_i_centered_divide | |
3173 | ||
3174 | void | |
3175 | scm_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
3176 | { | |
3177 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
3178 | { | |
3179 | scm_t_inum xx = SCM_I_INUM (x); | |
3180 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
3181 | { | |
3182 | scm_t_inum yy = SCM_I_INUM (y); | |
3183 | if (SCM_UNLIKELY (yy == 0)) | |
3184 | scm_num_overflow (s_scm_centered_divide); | |
3185 | else | |
3186 | { | |
3187 | scm_t_inum qq = xx / yy; | |
3188 | scm_t_inum rr = xx % yy; | |
3189 | if (SCM_LIKELY (xx > 0)) | |
3190 | { | |
3191 | if (SCM_LIKELY (yy > 0)) | |
3192 | { | |
3193 | if (rr >= (yy + 1) / 2) | |
3194 | { qq++; rr -= yy; } | |
3195 | } | |
3196 | else | |
3197 | { | |
3198 | if (rr >= (1 - yy) / 2) | |
3199 | { qq--; rr += yy; } | |
3200 | } | |
3201 | } | |
3202 | else | |
3203 | { | |
3204 | if (SCM_LIKELY (yy > 0)) | |
3205 | { | |
3206 | if (rr < -yy / 2) | |
3207 | { qq--; rr += yy; } | |
3208 | } | |
3209 | else | |
3210 | { | |
3211 | if (rr < yy / 2) | |
3212 | { qq++; rr -= yy; } | |
3213 | } | |
3214 | } | |
3215 | if (SCM_LIKELY (SCM_FIXABLE (qq))) | |
3216 | *qp = SCM_I_MAKINUM (qq); | |
3217 | else | |
3218 | *qp = scm_i_inum2big (qq); | |
3219 | *rp = SCM_I_MAKINUM (rr); | |
3220 | } | |
3221 | return; | |
3222 | } | |
3223 | else if (SCM_BIGP (y)) | |
3224 | { | |
3225 | /* Pass a denormalized bignum version of x (even though it | |
3226 | can fit in a fixnum) to scm_i_bigint_centered_divide */ | |
3227 | return scm_i_bigint_centered_divide (scm_i_long2big (xx), y, qp, rp); | |
3228 | } | |
3229 | else if (SCM_REALP (y)) | |
3230 | return scm_i_inexact_centered_divide (xx, SCM_REAL_VALUE (y), qp, rp); | |
3231 | else if (SCM_FRACTIONP (y)) | |
3232 | return scm_i_exact_rational_centered_divide (x, y, qp, rp); | |
3233 | else | |
3234 | return two_valued_wta_dispatch_2 | |
3235 | (g_scm_centered_divide, x, y, SCM_ARG2, | |
3236 | s_scm_centered_divide, qp, rp); | |
3237 | } | |
3238 | else if (SCM_BIGP (x)) | |
3239 | { | |
3240 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
3241 | { | |
3242 | scm_t_inum yy = SCM_I_INUM (y); | |
3243 | if (SCM_UNLIKELY (yy == 0)) | |
3244 | scm_num_overflow (s_scm_centered_divide); | |
3245 | else | |
3246 | { | |
3247 | SCM q = scm_i_mkbig (); | |
3248 | scm_t_inum rr; | |
3249 | /* Arrange for rr to initially be non-positive, | |
3250 | because that simplifies the test to see | |
3251 | if it is within the needed bounds. */ | |
3252 | if (yy > 0) | |
3253 | { | |
3254 | rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), | |
3255 | SCM_I_BIG_MPZ (x), yy); | |
3256 | scm_remember_upto_here_1 (x); | |
3257 | if (rr < -yy / 2) | |
3258 | { | |
3259 | mpz_sub_ui (SCM_I_BIG_MPZ (q), | |
3260 | SCM_I_BIG_MPZ (q), 1); | |
3261 | rr += yy; | |
3262 | } | |
3263 | } | |
3264 | else | |
3265 | { | |
3266 | rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), | |
3267 | SCM_I_BIG_MPZ (x), -yy); | |
3268 | scm_remember_upto_here_1 (x); | |
3269 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); | |
3270 | if (rr < yy / 2) | |
3271 | { | |
3272 | mpz_add_ui (SCM_I_BIG_MPZ (q), | |
3273 | SCM_I_BIG_MPZ (q), 1); | |
3274 | rr -= yy; | |
3275 | } | |
3276 | } | |
3277 | *qp = scm_i_normbig (q); | |
3278 | *rp = SCM_I_MAKINUM (rr); | |
3279 | } | |
3280 | return; | |
3281 | } | |
3282 | else if (SCM_BIGP (y)) | |
3283 | return scm_i_bigint_centered_divide (x, y, qp, rp); | |
3284 | else if (SCM_REALP (y)) | |
3285 | return scm_i_inexact_centered_divide | |
3286 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp); | |
3287 | else if (SCM_FRACTIONP (y)) | |
3288 | return scm_i_exact_rational_centered_divide (x, y, qp, rp); | |
3289 | else | |
3290 | return two_valued_wta_dispatch_2 | |
3291 | (g_scm_centered_divide, x, y, SCM_ARG2, | |
3292 | s_scm_centered_divide, qp, rp); | |
3293 | } | |
3294 | else if (SCM_REALP (x)) | |
3295 | { | |
3296 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
3297 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
3298 | return scm_i_inexact_centered_divide | |
3299 | (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp); | |
3300 | else | |
3301 | return two_valued_wta_dispatch_2 | |
3302 | (g_scm_centered_divide, x, y, SCM_ARG2, | |
3303 | s_scm_centered_divide, qp, rp); | |
3304 | } | |
3305 | else if (SCM_FRACTIONP (x)) | |
3306 | { | |
3307 | if (SCM_REALP (y)) | |
3308 | return scm_i_inexact_centered_divide | |
3309 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp); | |
3310 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
3311 | return scm_i_exact_rational_centered_divide (x, y, qp, rp); | |
3312 | else | |
3313 | return two_valued_wta_dispatch_2 | |
3314 | (g_scm_centered_divide, x, y, SCM_ARG2, | |
3315 | s_scm_centered_divide, qp, rp); | |
3316 | } | |
3317 | else | |
3318 | return two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG1, | |
3319 | s_scm_centered_divide, qp, rp); | |
3320 | } | |
3321 | ||
3322 | static void | |
3323 | scm_i_inexact_centered_divide (double x, double y, SCM *qp, SCM *rp) | |
3324 | { | |
3325 | double q, r; | |
3326 | ||
3327 | if (SCM_LIKELY (y > 0)) | |
3328 | q = floor (x/y + 0.5); | |
3329 | else if (SCM_LIKELY (y < 0)) | |
3330 | q = ceil (x/y - 0.5); | |
3331 | else if (y == 0) | |
3332 | scm_num_overflow (s_scm_centered_divide); /* or return a NaN? */ | |
3333 | else | |
3334 | q = guile_NaN; | |
3335 | r = x - q * y; | |
00472a22 MW |
3336 | *qp = scm_i_from_double (q); |
3337 | *rp = scm_i_from_double (r); | |
8f9da340 MW |
3338 | } |
3339 | ||
3340 | /* Assumes that both x and y are bigints, though | |
3341 | x might be able to fit into a fixnum. */ | |
3342 | static void | |
3343 | scm_i_bigint_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
3344 | { | |
3345 | SCM q, r, min_r; | |
3346 | ||
3347 | /* Note that x might be small enough to fit into a | |
3348 | fixnum, so we must not let it escape into the wild */ | |
3349 | q = scm_i_mkbig (); | |
3350 | r = scm_i_mkbig (); | |
3351 | ||
3352 | /* min_r will eventually become -abs(y/2) */ | |
3353 | min_r = scm_i_mkbig (); | |
3354 | mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r), | |
3355 | SCM_I_BIG_MPZ (y), 1); | |
3356 | ||
3357 | /* Arrange for rr to initially be non-positive, | |
3358 | because that simplifies the test to see | |
3359 | if it is within the needed bounds. */ | |
3360 | if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0) | |
3361 | { | |
3362 | mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
3363 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3364 | mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r)); | |
3365 | if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0) | |
3366 | { | |
3367 | mpz_sub_ui (SCM_I_BIG_MPZ (q), | |
3368 | SCM_I_BIG_MPZ (q), 1); | |
3369 | mpz_add (SCM_I_BIG_MPZ (r), | |
3370 | SCM_I_BIG_MPZ (r), | |
3371 | SCM_I_BIG_MPZ (y)); | |
3372 | } | |
3373 | } | |
3374 | else | |
3375 | { | |
3376 | mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), | |
3377 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3378 | if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0) | |
3379 | { | |
3380 | mpz_add_ui (SCM_I_BIG_MPZ (q), | |
3381 | SCM_I_BIG_MPZ (q), 1); | |
3382 | mpz_sub (SCM_I_BIG_MPZ (r), | |
3383 | SCM_I_BIG_MPZ (r), | |
3384 | SCM_I_BIG_MPZ (y)); | |
3385 | } | |
3386 | } | |
3387 | scm_remember_upto_here_2 (x, y); | |
3388 | *qp = scm_i_normbig (q); | |
3389 | *rp = scm_i_normbig (r); | |
3390 | } | |
3391 | ||
3392 | static void | |
3393 | scm_i_exact_rational_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp) | |
3394 | { | |
3395 | SCM r1; | |
3396 | SCM xd = scm_denominator (x); | |
3397 | SCM yd = scm_denominator (y); | |
3398 | ||
3399 | scm_centered_divide (scm_product (scm_numerator (x), yd), | |
3400 | scm_product (scm_numerator (y), xd), | |
3401 | qp, &r1); | |
3402 | *rp = scm_divide (r1, scm_product (xd, yd)); | |
3403 | } | |
3404 | ||
3405 | static SCM scm_i_inexact_round_quotient (double x, double y); | |
3406 | static SCM scm_i_bigint_round_quotient (SCM x, SCM y); | |
3407 | static SCM scm_i_exact_rational_round_quotient (SCM x, SCM y); | |
3408 | ||
3409 | SCM_PRIMITIVE_GENERIC (scm_round_quotient, "round-quotient", 2, 0, 0, | |
ff62c168 | 3410 | (SCM x, SCM y), |
8f9da340 MW |
3411 | "Return @math{@var{x} / @var{y}} to the nearest integer,\n" |
3412 | "with ties going to the nearest even integer.\n" | |
ff62c168 | 3413 | "@lisp\n" |
8f9da340 MW |
3414 | "(round-quotient 123 10) @result{} 12\n" |
3415 | "(round-quotient 123 -10) @result{} -12\n" | |
3416 | "(round-quotient -123 10) @result{} -12\n" | |
3417 | "(round-quotient -123 -10) @result{} 12\n" | |
3418 | "(round-quotient 125 10) @result{} 12\n" | |
3419 | "(round-quotient 127 10) @result{} 13\n" | |
3420 | "(round-quotient 135 10) @result{} 14\n" | |
3421 | "(round-quotient -123.2 -63.5) @result{} 2.0\n" | |
3422 | "(round-quotient 16/3 -10/7) @result{} -4\n" | |
ff62c168 | 3423 | "@end lisp") |
8f9da340 | 3424 | #define FUNC_NAME s_scm_round_quotient |
ff62c168 MW |
3425 | { |
3426 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
3427 | { | |
4a46bc2a | 3428 | scm_t_inum xx = SCM_I_INUM (x); |
ff62c168 MW |
3429 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
3430 | { | |
3431 | scm_t_inum yy = SCM_I_INUM (y); | |
3432 | if (SCM_UNLIKELY (yy == 0)) | |
8f9da340 | 3433 | scm_num_overflow (s_scm_round_quotient); |
ff62c168 MW |
3434 | else |
3435 | { | |
ff62c168 | 3436 | scm_t_inum qq = xx / yy; |
4a46bc2a | 3437 | scm_t_inum rr = xx % yy; |
8f9da340 MW |
3438 | scm_t_inum ay = yy; |
3439 | scm_t_inum r2 = 2 * rr; | |
3440 | ||
3441 | if (SCM_LIKELY (yy < 0)) | |
ff62c168 | 3442 | { |
8f9da340 MW |
3443 | ay = -ay; |
3444 | r2 = -r2; | |
3445 | } | |
3446 | ||
3447 | if (qq & 1L) | |
3448 | { | |
3449 | if (r2 >= ay) | |
3450 | qq++; | |
3451 | else if (r2 <= -ay) | |
3452 | qq--; | |
ff62c168 MW |
3453 | } |
3454 | else | |
3455 | { | |
8f9da340 MW |
3456 | if (r2 > ay) |
3457 | qq++; | |
3458 | else if (r2 < -ay) | |
3459 | qq--; | |
ff62c168 | 3460 | } |
4a46bc2a MW |
3461 | if (SCM_LIKELY (SCM_FIXABLE (qq))) |
3462 | return SCM_I_MAKINUM (qq); | |
3463 | else | |
3464 | return scm_i_inum2big (qq); | |
ff62c168 MW |
3465 | } |
3466 | } | |
3467 | else if (SCM_BIGP (y)) | |
3468 | { | |
3469 | /* Pass a denormalized bignum version of x (even though it | |
8f9da340 MW |
3470 | can fit in a fixnum) to scm_i_bigint_round_quotient */ |
3471 | return scm_i_bigint_round_quotient (scm_i_long2big (xx), y); | |
ff62c168 MW |
3472 | } |
3473 | else if (SCM_REALP (y)) | |
8f9da340 | 3474 | return scm_i_inexact_round_quotient (xx, SCM_REAL_VALUE (y)); |
ff62c168 | 3475 | else if (SCM_FRACTIONP (y)) |
8f9da340 | 3476 | return scm_i_exact_rational_round_quotient (x, y); |
ff62c168 | 3477 | else |
8f9da340 MW |
3478 | SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2, |
3479 | s_scm_round_quotient); | |
ff62c168 MW |
3480 | } |
3481 | else if (SCM_BIGP (x)) | |
3482 | { | |
3483 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
3484 | { | |
3485 | scm_t_inum yy = SCM_I_INUM (y); | |
3486 | if (SCM_UNLIKELY (yy == 0)) | |
8f9da340 | 3487 | scm_num_overflow (s_scm_round_quotient); |
4a46bc2a MW |
3488 | else if (SCM_UNLIKELY (yy == 1)) |
3489 | return x; | |
ff62c168 MW |
3490 | else |
3491 | { | |
3492 | SCM q = scm_i_mkbig (); | |
3493 | scm_t_inum rr; | |
8f9da340 MW |
3494 | int needs_adjustment; |
3495 | ||
ff62c168 MW |
3496 | if (yy > 0) |
3497 | { | |
8f9da340 MW |
3498 | rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), |
3499 | SCM_I_BIG_MPZ (x), yy); | |
3500 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) | |
3501 | needs_adjustment = (2*rr >= yy); | |
3502 | else | |
3503 | needs_adjustment = (2*rr > yy); | |
ff62c168 MW |
3504 | } |
3505 | else | |
3506 | { | |
3507 | rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), | |
3508 | SCM_I_BIG_MPZ (x), -yy); | |
ff62c168 | 3509 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); |
8f9da340 MW |
3510 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) |
3511 | needs_adjustment = (2*rr <= yy); | |
3512 | else | |
3513 | needs_adjustment = (2*rr < yy); | |
ff62c168 | 3514 | } |
8f9da340 MW |
3515 | scm_remember_upto_here_1 (x); |
3516 | if (needs_adjustment) | |
3517 | mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1); | |
ff62c168 MW |
3518 | return scm_i_normbig (q); |
3519 | } | |
3520 | } | |
3521 | else if (SCM_BIGP (y)) | |
8f9da340 | 3522 | return scm_i_bigint_round_quotient (x, y); |
ff62c168 | 3523 | else if (SCM_REALP (y)) |
8f9da340 | 3524 | return scm_i_inexact_round_quotient |
ff62c168 MW |
3525 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); |
3526 | else if (SCM_FRACTIONP (y)) | |
8f9da340 | 3527 | return scm_i_exact_rational_round_quotient (x, y); |
ff62c168 | 3528 | else |
8f9da340 MW |
3529 | SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2, |
3530 | s_scm_round_quotient); | |
ff62c168 MW |
3531 | } |
3532 | else if (SCM_REALP (x)) | |
3533 | { | |
3534 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
3535 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
8f9da340 | 3536 | return scm_i_inexact_round_quotient |
ff62c168 MW |
3537 | (SCM_REAL_VALUE (x), scm_to_double (y)); |
3538 | else | |
8f9da340 MW |
3539 | SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2, |
3540 | s_scm_round_quotient); | |
ff62c168 MW |
3541 | } |
3542 | else if (SCM_FRACTIONP (x)) | |
3543 | { | |
3544 | if (SCM_REALP (y)) | |
8f9da340 | 3545 | return scm_i_inexact_round_quotient |
ff62c168 | 3546 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); |
03ddd15b | 3547 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) |
8f9da340 | 3548 | return scm_i_exact_rational_round_quotient (x, y); |
ff62c168 | 3549 | else |
8f9da340 MW |
3550 | SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2, |
3551 | s_scm_round_quotient); | |
ff62c168 MW |
3552 | } |
3553 | else | |
8f9da340 MW |
3554 | SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG1, |
3555 | s_scm_round_quotient); | |
ff62c168 MW |
3556 | } |
3557 | #undef FUNC_NAME | |
3558 | ||
3559 | static SCM | |
8f9da340 | 3560 | scm_i_inexact_round_quotient (double x, double y) |
ff62c168 | 3561 | { |
8f9da340 MW |
3562 | if (SCM_UNLIKELY (y == 0)) |
3563 | scm_num_overflow (s_scm_round_quotient); /* or return a NaN? */ | |
ff62c168 | 3564 | else |
00472a22 | 3565 | return scm_i_from_double (scm_c_round (x / y)); |
ff62c168 MW |
3566 | } |
3567 | ||
3568 | /* Assumes that both x and y are bigints, though | |
3569 | x might be able to fit into a fixnum. */ | |
3570 | static SCM | |
8f9da340 | 3571 | scm_i_bigint_round_quotient (SCM x, SCM y) |
ff62c168 | 3572 | { |
8f9da340 MW |
3573 | SCM q, r, r2; |
3574 | int cmp, needs_adjustment; | |
ff62c168 MW |
3575 | |
3576 | /* Note that x might be small enough to fit into a | |
3577 | fixnum, so we must not let it escape into the wild */ | |
3578 | q = scm_i_mkbig (); | |
3579 | r = scm_i_mkbig (); | |
8f9da340 | 3580 | r2 = scm_i_mkbig (); |
ff62c168 | 3581 | |
8f9da340 MW |
3582 | mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), |
3583 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3584 | mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */ | |
3585 | scm_remember_upto_here_2 (x, r); | |
ff62c168 | 3586 | |
8f9da340 MW |
3587 | cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y)); |
3588 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) | |
3589 | needs_adjustment = (cmp >= 0); | |
ff62c168 | 3590 | else |
8f9da340 MW |
3591 | needs_adjustment = (cmp > 0); |
3592 | scm_remember_upto_here_2 (r2, y); | |
3593 | ||
3594 | if (needs_adjustment) | |
3595 | mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1); | |
3596 | ||
ff62c168 MW |
3597 | return scm_i_normbig (q); |
3598 | } | |
3599 | ||
ff62c168 | 3600 | static SCM |
8f9da340 | 3601 | scm_i_exact_rational_round_quotient (SCM x, SCM y) |
ff62c168 | 3602 | { |
8f9da340 | 3603 | return scm_round_quotient |
03ddd15b MW |
3604 | (scm_product (scm_numerator (x), scm_denominator (y)), |
3605 | scm_product (scm_numerator (y), scm_denominator (x))); | |
ff62c168 MW |
3606 | } |
3607 | ||
8f9da340 MW |
3608 | static SCM scm_i_inexact_round_remainder (double x, double y); |
3609 | static SCM scm_i_bigint_round_remainder (SCM x, SCM y); | |
3610 | static SCM scm_i_exact_rational_round_remainder (SCM x, SCM y); | |
ff62c168 | 3611 | |
8f9da340 | 3612 | SCM_PRIMITIVE_GENERIC (scm_round_remainder, "round-remainder", 2, 0, 0, |
ff62c168 MW |
3613 | (SCM x, SCM y), |
3614 | "Return the real number @var{r} such that\n" | |
8f9da340 MW |
3615 | "@math{@var{x} = @var{q}*@var{y} + @var{r}}, where\n" |
3616 | "@var{q} is @math{@var{x} / @var{y}} rounded to the\n" | |
3617 | "nearest integer, with ties going to the nearest\n" | |
3618 | "even integer.\n" | |
ff62c168 | 3619 | "@lisp\n" |
8f9da340 MW |
3620 | "(round-remainder 123 10) @result{} 3\n" |
3621 | "(round-remainder 123 -10) @result{} 3\n" | |
3622 | "(round-remainder -123 10) @result{} -3\n" | |
3623 | "(round-remainder -123 -10) @result{} -3\n" | |
3624 | "(round-remainder 125 10) @result{} 5\n" | |
3625 | "(round-remainder 127 10) @result{} -3\n" | |
3626 | "(round-remainder 135 10) @result{} -5\n" | |
3627 | "(round-remainder -123.2 -63.5) @result{} 3.8\n" | |
3628 | "(round-remainder 16/3 -10/7) @result{} -8/21\n" | |
ff62c168 | 3629 | "@end lisp") |
8f9da340 | 3630 | #define FUNC_NAME s_scm_round_remainder |
ff62c168 MW |
3631 | { |
3632 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
3633 | { | |
4a46bc2a | 3634 | scm_t_inum xx = SCM_I_INUM (x); |
ff62c168 MW |
3635 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
3636 | { | |
3637 | scm_t_inum yy = SCM_I_INUM (y); | |
3638 | if (SCM_UNLIKELY (yy == 0)) | |
8f9da340 | 3639 | scm_num_overflow (s_scm_round_remainder); |
ff62c168 MW |
3640 | else |
3641 | { | |
8f9da340 | 3642 | scm_t_inum qq = xx / yy; |
ff62c168 | 3643 | scm_t_inum rr = xx % yy; |
8f9da340 MW |
3644 | scm_t_inum ay = yy; |
3645 | scm_t_inum r2 = 2 * rr; | |
3646 | ||
3647 | if (SCM_LIKELY (yy < 0)) | |
ff62c168 | 3648 | { |
8f9da340 MW |
3649 | ay = -ay; |
3650 | r2 = -r2; | |
3651 | } | |
3652 | ||
3653 | if (qq & 1L) | |
3654 | { | |
3655 | if (r2 >= ay) | |
3656 | rr -= yy; | |
3657 | else if (r2 <= -ay) | |
3658 | rr += yy; | |
ff62c168 MW |
3659 | } |
3660 | else | |
3661 | { | |
8f9da340 MW |
3662 | if (r2 > ay) |
3663 | rr -= yy; | |
3664 | else if (r2 < -ay) | |
3665 | rr += yy; | |
ff62c168 MW |
3666 | } |
3667 | return SCM_I_MAKINUM (rr); | |
3668 | } | |
3669 | } | |
3670 | else if (SCM_BIGP (y)) | |
3671 | { | |
3672 | /* Pass a denormalized bignum version of x (even though it | |
8f9da340 MW |
3673 | can fit in a fixnum) to scm_i_bigint_round_remainder */ |
3674 | return scm_i_bigint_round_remainder | |
3675 | (scm_i_long2big (xx), y); | |
ff62c168 MW |
3676 | } |
3677 | else if (SCM_REALP (y)) | |
8f9da340 | 3678 | return scm_i_inexact_round_remainder (xx, SCM_REAL_VALUE (y)); |
ff62c168 | 3679 | else if (SCM_FRACTIONP (y)) |
8f9da340 | 3680 | return scm_i_exact_rational_round_remainder (x, y); |
ff62c168 | 3681 | else |
8f9da340 MW |
3682 | SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2, |
3683 | s_scm_round_remainder); | |
ff62c168 MW |
3684 | } |
3685 | else if (SCM_BIGP (x)) | |
3686 | { | |
3687 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
3688 | { | |
3689 | scm_t_inum yy = SCM_I_INUM (y); | |
3690 | if (SCM_UNLIKELY (yy == 0)) | |
8f9da340 | 3691 | scm_num_overflow (s_scm_round_remainder); |
ff62c168 MW |
3692 | else |
3693 | { | |
8f9da340 | 3694 | SCM q = scm_i_mkbig (); |
ff62c168 | 3695 | scm_t_inum rr; |
8f9da340 MW |
3696 | int needs_adjustment; |
3697 | ||
ff62c168 MW |
3698 | if (yy > 0) |
3699 | { | |
8f9da340 MW |
3700 | rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), |
3701 | SCM_I_BIG_MPZ (x), yy); | |
3702 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) | |
3703 | needs_adjustment = (2*rr >= yy); | |
3704 | else | |
3705 | needs_adjustment = (2*rr > yy); | |
ff62c168 MW |
3706 | } |
3707 | else | |
3708 | { | |
8f9da340 MW |
3709 | rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), |
3710 | SCM_I_BIG_MPZ (x), -yy); | |
3711 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) | |
3712 | needs_adjustment = (2*rr <= yy); | |
3713 | else | |
3714 | needs_adjustment = (2*rr < yy); | |
ff62c168 | 3715 | } |
8f9da340 MW |
3716 | scm_remember_upto_here_2 (x, q); |
3717 | if (needs_adjustment) | |
3718 | rr -= yy; | |
ff62c168 MW |
3719 | return SCM_I_MAKINUM (rr); |
3720 | } | |
3721 | } | |
3722 | else if (SCM_BIGP (y)) | |
8f9da340 | 3723 | return scm_i_bigint_round_remainder (x, y); |
ff62c168 | 3724 | else if (SCM_REALP (y)) |
8f9da340 | 3725 | return scm_i_inexact_round_remainder |
ff62c168 MW |
3726 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y)); |
3727 | else if (SCM_FRACTIONP (y)) | |
8f9da340 | 3728 | return scm_i_exact_rational_round_remainder (x, y); |
ff62c168 | 3729 | else |
8f9da340 MW |
3730 | SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2, |
3731 | s_scm_round_remainder); | |
ff62c168 MW |
3732 | } |
3733 | else if (SCM_REALP (x)) | |
3734 | { | |
3735 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
3736 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
8f9da340 | 3737 | return scm_i_inexact_round_remainder |
ff62c168 MW |
3738 | (SCM_REAL_VALUE (x), scm_to_double (y)); |
3739 | else | |
8f9da340 MW |
3740 | SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2, |
3741 | s_scm_round_remainder); | |
ff62c168 MW |
3742 | } |
3743 | else if (SCM_FRACTIONP (x)) | |
3744 | { | |
3745 | if (SCM_REALP (y)) | |
8f9da340 | 3746 | return scm_i_inexact_round_remainder |
ff62c168 | 3747 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y)); |
03ddd15b | 3748 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) |
8f9da340 | 3749 | return scm_i_exact_rational_round_remainder (x, y); |
ff62c168 | 3750 | else |
8f9da340 MW |
3751 | SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2, |
3752 | s_scm_round_remainder); | |
ff62c168 MW |
3753 | } |
3754 | else | |
8f9da340 MW |
3755 | SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG1, |
3756 | s_scm_round_remainder); | |
ff62c168 MW |
3757 | } |
3758 | #undef FUNC_NAME | |
3759 | ||
3760 | static SCM | |
8f9da340 | 3761 | scm_i_inexact_round_remainder (double x, double y) |
ff62c168 | 3762 | { |
ff62c168 MW |
3763 | /* Although it would be more efficient to use fmod here, we can't |
3764 | because it would in some cases produce results inconsistent with | |
8f9da340 | 3765 | scm_i_inexact_round_quotient, such that x != r + q * y (not even |
ff62c168 | 3766 | close). In particular, when x-y/2 is very close to a multiple of |
8f9da340 MW |
3767 | y, then r might be either -abs(y/2) or abs(y/2), but those two |
3768 | cases must correspond to different choices of q. If quotient | |
ff62c168 | 3769 | chooses one and remainder chooses the other, it would be bad. */ |
8f9da340 MW |
3770 | |
3771 | if (SCM_UNLIKELY (y == 0)) | |
3772 | scm_num_overflow (s_scm_round_remainder); /* or return a NaN? */ | |
ff62c168 | 3773 | else |
8f9da340 MW |
3774 | { |
3775 | double q = scm_c_round (x / y); | |
00472a22 | 3776 | return scm_i_from_double (x - q * y); |
8f9da340 | 3777 | } |
ff62c168 MW |
3778 | } |
3779 | ||
3780 | /* Assumes that both x and y are bigints, though | |
3781 | x might be able to fit into a fixnum. */ | |
3782 | static SCM | |
8f9da340 | 3783 | scm_i_bigint_round_remainder (SCM x, SCM y) |
ff62c168 | 3784 | { |
8f9da340 MW |
3785 | SCM q, r, r2; |
3786 | int cmp, needs_adjustment; | |
ff62c168 MW |
3787 | |
3788 | /* Note that x might be small enough to fit into a | |
3789 | fixnum, so we must not let it escape into the wild */ | |
8f9da340 | 3790 | q = scm_i_mkbig (); |
ff62c168 | 3791 | r = scm_i_mkbig (); |
8f9da340 | 3792 | r2 = scm_i_mkbig (); |
ff62c168 | 3793 | |
8f9da340 MW |
3794 | mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), |
3795 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
3796 | scm_remember_upto_here_1 (x); | |
3797 | mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */ | |
ff62c168 | 3798 | |
8f9da340 MW |
3799 | cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y)); |
3800 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) | |
3801 | needs_adjustment = (cmp >= 0); | |
ff62c168 | 3802 | else |
8f9da340 MW |
3803 | needs_adjustment = (cmp > 0); |
3804 | scm_remember_upto_here_2 (q, r2); | |
3805 | ||
3806 | if (needs_adjustment) | |
3807 | mpz_sub (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y)); | |
3808 | ||
3809 | scm_remember_upto_here_1 (y); | |
ff62c168 MW |
3810 | return scm_i_normbig (r); |
3811 | } | |
3812 | ||
ff62c168 | 3813 | static SCM |
8f9da340 | 3814 | scm_i_exact_rational_round_remainder (SCM x, SCM y) |
ff62c168 | 3815 | { |
03ddd15b MW |
3816 | SCM xd = scm_denominator (x); |
3817 | SCM yd = scm_denominator (y); | |
8f9da340 MW |
3818 | SCM r1 = scm_round_remainder (scm_product (scm_numerator (x), yd), |
3819 | scm_product (scm_numerator (y), xd)); | |
03ddd15b | 3820 | return scm_divide (r1, scm_product (xd, yd)); |
ff62c168 MW |
3821 | } |
3822 | ||
3823 | ||
8f9da340 MW |
3824 | static void scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp); |
3825 | static void scm_i_bigint_round_divide (SCM x, SCM y, SCM *qp, SCM *rp); | |
3826 | static void scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp); | |
ff62c168 | 3827 | |
8f9da340 | 3828 | SCM_PRIMITIVE_GENERIC (scm_i_round_divide, "round/", 2, 0, 0, |
ff62c168 MW |
3829 | (SCM x, SCM y), |
3830 | "Return the integer @var{q} and the real number @var{r}\n" | |
3831 | "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n" | |
8f9da340 MW |
3832 | "and @var{q} is @math{@var{x} / @var{y}} rounded to the\n" |
3833 | "nearest integer, with ties going to the nearest even integer.\n" | |
ff62c168 | 3834 | "@lisp\n" |
8f9da340 MW |
3835 | "(round/ 123 10) @result{} 12 and 3\n" |
3836 | "(round/ 123 -10) @result{} -12 and 3\n" | |
3837 | "(round/ -123 10) @result{} -12 and -3\n" | |
3838 | "(round/ -123 -10) @result{} 12 and -3\n" | |
3839 | "(round/ 125 10) @result{} 12 and 5\n" | |
3840 | "(round/ 127 10) @result{} 13 and -3\n" | |
3841 | "(round/ 135 10) @result{} 14 and -5\n" | |
3842 | "(round/ -123.2 -63.5) @result{} 2.0 and 3.8\n" | |
3843 | "(round/ 16/3 -10/7) @result{} -4 and -8/21\n" | |
ff62c168 | 3844 | "@end lisp") |
8f9da340 | 3845 | #define FUNC_NAME s_scm_i_round_divide |
5fbf680b MW |
3846 | { |
3847 | SCM q, r; | |
3848 | ||
8f9da340 | 3849 | scm_round_divide(x, y, &q, &r); |
5fbf680b MW |
3850 | return scm_values (scm_list_2 (q, r)); |
3851 | } | |
3852 | #undef FUNC_NAME | |
3853 | ||
8f9da340 MW |
3854 | #define s_scm_round_divide s_scm_i_round_divide |
3855 | #define g_scm_round_divide g_scm_i_round_divide | |
5fbf680b MW |
3856 | |
3857 | void | |
8f9da340 | 3858 | scm_round_divide (SCM x, SCM y, SCM *qp, SCM *rp) |
ff62c168 MW |
3859 | { |
3860 | if (SCM_LIKELY (SCM_I_INUMP (x))) | |
3861 | { | |
4a46bc2a | 3862 | scm_t_inum xx = SCM_I_INUM (x); |
ff62c168 MW |
3863 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
3864 | { | |
3865 | scm_t_inum yy = SCM_I_INUM (y); | |
3866 | if (SCM_UNLIKELY (yy == 0)) | |
8f9da340 | 3867 | scm_num_overflow (s_scm_round_divide); |
ff62c168 MW |
3868 | else |
3869 | { | |
ff62c168 | 3870 | scm_t_inum qq = xx / yy; |
4a46bc2a | 3871 | scm_t_inum rr = xx % yy; |
8f9da340 MW |
3872 | scm_t_inum ay = yy; |
3873 | scm_t_inum r2 = 2 * rr; | |
3874 | ||
3875 | if (SCM_LIKELY (yy < 0)) | |
ff62c168 | 3876 | { |
8f9da340 MW |
3877 | ay = -ay; |
3878 | r2 = -r2; | |
3879 | } | |
3880 | ||
3881 | if (qq & 1L) | |
3882 | { | |
3883 | if (r2 >= ay) | |
3884 | { qq++; rr -= yy; } | |
3885 | else if (r2 <= -ay) | |
3886 | { qq--; rr += yy; } | |
ff62c168 MW |
3887 | } |
3888 | else | |
3889 | { | |
8f9da340 MW |
3890 | if (r2 > ay) |
3891 | { qq++; rr -= yy; } | |
3892 | else if (r2 < -ay) | |
3893 | { qq--; rr += yy; } | |
ff62c168 | 3894 | } |
4a46bc2a | 3895 | if (SCM_LIKELY (SCM_FIXABLE (qq))) |
5fbf680b | 3896 | *qp = SCM_I_MAKINUM (qq); |
4a46bc2a | 3897 | else |
5fbf680b MW |
3898 | *qp = scm_i_inum2big (qq); |
3899 | *rp = SCM_I_MAKINUM (rr); | |
ff62c168 | 3900 | } |
5fbf680b | 3901 | return; |
ff62c168 MW |
3902 | } |
3903 | else if (SCM_BIGP (y)) | |
3904 | { | |
3905 | /* Pass a denormalized bignum version of x (even though it | |
8f9da340 MW |
3906 | can fit in a fixnum) to scm_i_bigint_round_divide */ |
3907 | return scm_i_bigint_round_divide | |
3908 | (scm_i_long2big (SCM_I_INUM (x)), y, qp, rp); | |
ff62c168 MW |
3909 | } |
3910 | else if (SCM_REALP (y)) | |
8f9da340 | 3911 | return scm_i_inexact_round_divide (xx, SCM_REAL_VALUE (y), qp, rp); |
ff62c168 | 3912 | else if (SCM_FRACTIONP (y)) |
8f9da340 | 3913 | return scm_i_exact_rational_round_divide (x, y, qp, rp); |
ff62c168 | 3914 | else |
8f9da340 MW |
3915 | return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2, |
3916 | s_scm_round_divide, qp, rp); | |
ff62c168 MW |
3917 | } |
3918 | else if (SCM_BIGP (x)) | |
3919 | { | |
3920 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
3921 | { | |
3922 | scm_t_inum yy = SCM_I_INUM (y); | |
3923 | if (SCM_UNLIKELY (yy == 0)) | |
8f9da340 | 3924 | scm_num_overflow (s_scm_round_divide); |
ff62c168 MW |
3925 | else |
3926 | { | |
3927 | SCM q = scm_i_mkbig (); | |
3928 | scm_t_inum rr; | |
8f9da340 MW |
3929 | int needs_adjustment; |
3930 | ||
ff62c168 MW |
3931 | if (yy > 0) |
3932 | { | |
8f9da340 MW |
3933 | rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), |
3934 | SCM_I_BIG_MPZ (x), yy); | |
3935 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) | |
3936 | needs_adjustment = (2*rr >= yy); | |
3937 | else | |
3938 | needs_adjustment = (2*rr > yy); | |
ff62c168 MW |
3939 | } |
3940 | else | |
3941 | { | |
3942 | rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), | |
3943 | SCM_I_BIG_MPZ (x), -yy); | |
ff62c168 | 3944 | mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q)); |
8f9da340 MW |
3945 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) |
3946 | needs_adjustment = (2*rr <= yy); | |
3947 | else | |
3948 | needs_adjustment = (2*rr < yy); | |
3949 | } | |
3950 | scm_remember_upto_here_1 (x); | |
3951 | if (needs_adjustment) | |
3952 | { | |
3953 | mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1); | |
3954 | rr -= yy; | |
ff62c168 | 3955 | } |
5fbf680b MW |
3956 | *qp = scm_i_normbig (q); |
3957 | *rp = SCM_I_MAKINUM (rr); | |
ff62c168 | 3958 | } |
5fbf680b | 3959 | return; |
ff62c168 MW |
3960 | } |
3961 | else if (SCM_BIGP (y)) | |
8f9da340 | 3962 | return scm_i_bigint_round_divide (x, y, qp, rp); |
ff62c168 | 3963 | else if (SCM_REALP (y)) |
8f9da340 | 3964 | return scm_i_inexact_round_divide |
5fbf680b | 3965 | (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp); |
ff62c168 | 3966 | else if (SCM_FRACTIONP (y)) |
8f9da340 | 3967 | return scm_i_exact_rational_round_divide (x, y, qp, rp); |
ff62c168 | 3968 | else |
8f9da340 MW |
3969 | return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2, |
3970 | s_scm_round_divide, qp, rp); | |
ff62c168 MW |
3971 | } |
3972 | else if (SCM_REALP (x)) | |
3973 | { | |
3974 | if (SCM_REALP (y) || SCM_I_INUMP (y) || | |
3975 | SCM_BIGP (y) || SCM_FRACTIONP (y)) | |
8f9da340 | 3976 | return scm_i_inexact_round_divide |
5fbf680b | 3977 | (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp); |
03ddd15b | 3978 | else |
8f9da340 MW |
3979 | return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2, |
3980 | s_scm_round_divide, qp, rp); | |
ff62c168 MW |
3981 | } |
3982 | else if (SCM_FRACTIONP (x)) | |
3983 | { | |
3984 | if (SCM_REALP (y)) | |
8f9da340 | 3985 | return scm_i_inexact_round_divide |
5fbf680b | 3986 | (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp); |
03ddd15b | 3987 | else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y)) |
8f9da340 | 3988 | return scm_i_exact_rational_round_divide (x, y, qp, rp); |
ff62c168 | 3989 | else |
8f9da340 MW |
3990 | return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2, |
3991 | s_scm_round_divide, qp, rp); | |
ff62c168 MW |
3992 | } |
3993 | else | |
8f9da340 MW |
3994 | return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG1, |
3995 | s_scm_round_divide, qp, rp); | |
ff62c168 | 3996 | } |
ff62c168 | 3997 | |
5fbf680b | 3998 | static void |
8f9da340 | 3999 | scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp) |
ff62c168 | 4000 | { |
8f9da340 MW |
4001 | if (SCM_UNLIKELY (y == 0)) |
4002 | scm_num_overflow (s_scm_round_divide); /* or return a NaN? */ | |
ff62c168 | 4003 | else |
8f9da340 MW |
4004 | { |
4005 | double q = scm_c_round (x / y); | |
4006 | double r = x - q * y; | |
00472a22 MW |
4007 | *qp = scm_i_from_double (q); |
4008 | *rp = scm_i_from_double (r); | |
8f9da340 | 4009 | } |
ff62c168 MW |
4010 | } |
4011 | ||
4012 | /* Assumes that both x and y are bigints, though | |
4013 | x might be able to fit into a fixnum. */ | |
5fbf680b | 4014 | static void |
8f9da340 | 4015 | scm_i_bigint_round_divide (SCM x, SCM y, SCM *qp, SCM *rp) |
ff62c168 | 4016 | { |
8f9da340 MW |
4017 | SCM q, r, r2; |
4018 | int cmp, needs_adjustment; | |
ff62c168 MW |
4019 | |
4020 | /* Note that x might be small enough to fit into a | |
4021 | fixnum, so we must not let it escape into the wild */ | |
4022 | q = scm_i_mkbig (); | |
4023 | r = scm_i_mkbig (); | |
8f9da340 | 4024 | r2 = scm_i_mkbig (); |
ff62c168 | 4025 | |
8f9da340 MW |
4026 | mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r), |
4027 | SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
4028 | scm_remember_upto_here_1 (x); | |
4029 | mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */ | |
ff62c168 | 4030 | |
8f9da340 MW |
4031 | cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y)); |
4032 | if (mpz_odd_p (SCM_I_BIG_MPZ (q))) | |
4033 | needs_adjustment = (cmp >= 0); | |
ff62c168 | 4034 | else |
8f9da340 MW |
4035 | needs_adjustment = (cmp > 0); |
4036 | ||
4037 | if (needs_adjustment) | |
ff62c168 | 4038 | { |
8f9da340 MW |
4039 | mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1); |
4040 | mpz_sub (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y)); | |
ff62c168 | 4041 | } |
8f9da340 MW |
4042 | |
4043 | scm_remember_upto_here_2 (r2, y); | |
5fbf680b MW |
4044 | *qp = scm_i_normbig (q); |
4045 | *rp = scm_i_normbig (r); | |
ff62c168 MW |
4046 | } |
4047 | ||
5fbf680b | 4048 | static void |
8f9da340 | 4049 | scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp) |
ff62c168 | 4050 | { |
03ddd15b MW |
4051 | SCM r1; |
4052 | SCM xd = scm_denominator (x); | |
4053 | SCM yd = scm_denominator (y); | |
4054 | ||
8f9da340 MW |
4055 | scm_round_divide (scm_product (scm_numerator (x), yd), |
4056 | scm_product (scm_numerator (y), xd), | |
4057 | qp, &r1); | |
03ddd15b | 4058 | *rp = scm_divide (r1, scm_product (xd, yd)); |
ff62c168 MW |
4059 | } |
4060 | ||
4061 | ||
78d3deb1 AW |
4062 | SCM_PRIMITIVE_GENERIC (scm_i_gcd, "gcd", 0, 2, 1, |
4063 | (SCM x, SCM y, SCM rest), | |
4064 | "Return the greatest common divisor of all parameter values.\n" | |
4065 | "If called without arguments, 0 is returned.") | |
4066 | #define FUNC_NAME s_scm_i_gcd | |
4067 | { | |
4068 | while (!scm_is_null (rest)) | |
4069 | { x = scm_gcd (x, y); | |
4070 | y = scm_car (rest); | |
4071 | rest = scm_cdr (rest); | |
4072 | } | |
4073 | return scm_gcd (x, y); | |
4074 | } | |
4075 | #undef FUNC_NAME | |
4076 | ||
4077 | #define s_gcd s_scm_i_gcd | |
4078 | #define g_gcd g_scm_i_gcd | |
4079 | ||
0f2d19dd | 4080 | SCM |
6e8d25a6 | 4081 | scm_gcd (SCM x, SCM y) |
0f2d19dd | 4082 | { |
a2dead1b | 4083 | if (SCM_UNLIKELY (SCM_UNBNDP (y))) |
1dd79792 | 4084 | return SCM_UNBNDP (x) ? SCM_INUM0 : scm_abs (x); |
ca46fb90 | 4085 | |
a2dead1b | 4086 | if (SCM_LIKELY (SCM_I_INUMP (x))) |
ca46fb90 | 4087 | { |
a2dead1b | 4088 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
ca46fb90 | 4089 | { |
e25f3727 AW |
4090 | scm_t_inum xx = SCM_I_INUM (x); |
4091 | scm_t_inum yy = SCM_I_INUM (y); | |
4092 | scm_t_inum u = xx < 0 ? -xx : xx; | |
4093 | scm_t_inum v = yy < 0 ? -yy : yy; | |
4094 | scm_t_inum result; | |
a2dead1b | 4095 | if (SCM_UNLIKELY (xx == 0)) |
0aacf84e | 4096 | result = v; |
a2dead1b | 4097 | else if (SCM_UNLIKELY (yy == 0)) |
0aacf84e MD |
4098 | result = u; |
4099 | else | |
4100 | { | |
a2dead1b | 4101 | int k = 0; |
0aacf84e | 4102 | /* Determine a common factor 2^k */ |
a2dead1b | 4103 | while (((u | v) & 1) == 0) |
0aacf84e | 4104 | { |
a2dead1b | 4105 | k++; |
0aacf84e MD |
4106 | u >>= 1; |
4107 | v >>= 1; | |
4108 | } | |
4109 | /* Now, any factor 2^n can be eliminated */ | |
a2dead1b MW |
4110 | if ((u & 1) == 0) |
4111 | while ((u & 1) == 0) | |
4112 | u >>= 1; | |
0aacf84e | 4113 | else |
a2dead1b MW |
4114 | while ((v & 1) == 0) |
4115 | v >>= 1; | |
4116 | /* Both u and v are now odd. Subtract the smaller one | |
4117 | from the larger one to produce an even number, remove | |
4118 | more factors of two, and repeat. */ | |
4119 | while (u != v) | |
0aacf84e | 4120 | { |
a2dead1b MW |
4121 | if (u > v) |
4122 | { | |
4123 | u -= v; | |
4124 | while ((u & 1) == 0) | |
4125 | u >>= 1; | |
4126 | } | |
4127 | else | |
4128 | { | |
4129 | v -= u; | |
4130 | while ((v & 1) == 0) | |
4131 | v >>= 1; | |
4132 | } | |
0aacf84e | 4133 | } |
a2dead1b | 4134 | result = u << k; |
0aacf84e MD |
4135 | } |
4136 | return (SCM_POSFIXABLE (result) | |
d956fa6f | 4137 | ? SCM_I_MAKINUM (result) |
e25f3727 | 4138 | : scm_i_inum2big (result)); |
ca46fb90 RB |
4139 | } |
4140 | else if (SCM_BIGP (y)) | |
4141 | { | |
0bff4dce KR |
4142 | SCM_SWAP (x, y); |
4143 | goto big_inum; | |
ca46fb90 | 4144 | } |
3bbca1f7 MW |
4145 | else if (SCM_REALP (y) && scm_is_integer (y)) |
4146 | goto handle_inexacts; | |
ca46fb90 RB |
4147 | else |
4148 | SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd); | |
f872b822 | 4149 | } |
ca46fb90 RB |
4150 | else if (SCM_BIGP (x)) |
4151 | { | |
e11e83f3 | 4152 | if (SCM_I_INUMP (y)) |
ca46fb90 | 4153 | { |
e25f3727 AW |
4154 | scm_t_bits result; |
4155 | scm_t_inum yy; | |
0bff4dce | 4156 | big_inum: |
e11e83f3 | 4157 | yy = SCM_I_INUM (y); |
8c5b0afc KR |
4158 | if (yy == 0) |
4159 | return scm_abs (x); | |
0aacf84e MD |
4160 | if (yy < 0) |
4161 | yy = -yy; | |
ca46fb90 RB |
4162 | result = mpz_gcd_ui (NULL, SCM_I_BIG_MPZ (x), yy); |
4163 | scm_remember_upto_here_1 (x); | |
0aacf84e | 4164 | return (SCM_POSFIXABLE (result) |
d956fa6f | 4165 | ? SCM_I_MAKINUM (result) |
e25f3727 | 4166 | : scm_from_unsigned_integer (result)); |
ca46fb90 RB |
4167 | } |
4168 | else if (SCM_BIGP (y)) | |
4169 | { | |
4170 | SCM result = scm_i_mkbig (); | |
0aacf84e MD |
4171 | mpz_gcd (SCM_I_BIG_MPZ (result), |
4172 | SCM_I_BIG_MPZ (x), | |
4173 | SCM_I_BIG_MPZ (y)); | |
4174 | scm_remember_upto_here_2 (x, y); | |
ca46fb90 RB |
4175 | return scm_i_normbig (result); |
4176 | } | |
3bbca1f7 MW |
4177 | else if (SCM_REALP (y) && scm_is_integer (y)) |
4178 | goto handle_inexacts; | |
4179 | else | |
4180 | SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd); | |
4181 | } | |
4182 | else if (SCM_REALP (x) && scm_is_integer (x)) | |
4183 | { | |
4184 | if (SCM_I_INUMP (y) || SCM_BIGP (y) | |
4185 | || (SCM_REALP (y) && scm_is_integer (y))) | |
4186 | { | |
4187 | handle_inexacts: | |
4188 | return scm_exact_to_inexact (scm_gcd (scm_inexact_to_exact (x), | |
4189 | scm_inexact_to_exact (y))); | |
4190 | } | |
ca46fb90 RB |
4191 | else |
4192 | SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd); | |
09fb7599 | 4193 | } |
ca46fb90 | 4194 | else |
09fb7599 | 4195 | SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG1, s_gcd); |
0f2d19dd JB |
4196 | } |
4197 | ||
78d3deb1 AW |
4198 | SCM_PRIMITIVE_GENERIC (scm_i_lcm, "lcm", 0, 2, 1, |
4199 | (SCM x, SCM y, SCM rest), | |
4200 | "Return the least common multiple of the arguments.\n" | |
4201 | "If called without arguments, 1 is returned.") | |
4202 | #define FUNC_NAME s_scm_i_lcm | |
4203 | { | |
4204 | while (!scm_is_null (rest)) | |
4205 | { x = scm_lcm (x, y); | |
4206 | y = scm_car (rest); | |
4207 | rest = scm_cdr (rest); | |
4208 | } | |
4209 | return scm_lcm (x, y); | |
4210 | } | |
4211 | #undef FUNC_NAME | |
4212 | ||
4213 | #define s_lcm s_scm_i_lcm | |
4214 | #define g_lcm g_scm_i_lcm | |
4215 | ||
0f2d19dd | 4216 | SCM |
6e8d25a6 | 4217 | scm_lcm (SCM n1, SCM n2) |
0f2d19dd | 4218 | { |
3bbca1f7 MW |
4219 | if (SCM_UNLIKELY (SCM_UNBNDP (n2))) |
4220 | return SCM_UNBNDP (n1) ? SCM_INUM1 : scm_abs (n1); | |
09fb7599 | 4221 | |
3bbca1f7 | 4222 | if (SCM_LIKELY (SCM_I_INUMP (n1))) |
ca46fb90 | 4223 | { |
3bbca1f7 | 4224 | if (SCM_LIKELY (SCM_I_INUMP (n2))) |
ca46fb90 RB |
4225 | { |
4226 | SCM d = scm_gcd (n1, n2); | |
bc36d050 | 4227 | if (scm_is_eq (d, SCM_INUM0)) |
ca46fb90 RB |
4228 | return d; |
4229 | else | |
4230 | return scm_abs (scm_product (n1, scm_quotient (n2, d))); | |
4231 | } | |
3bbca1f7 | 4232 | else if (SCM_LIKELY (SCM_BIGP (n2))) |
ca46fb90 RB |
4233 | { |
4234 | /* inum n1, big n2 */ | |
4235 | inumbig: | |
4236 | { | |
4237 | SCM result = scm_i_mkbig (); | |
e25f3727 | 4238 | scm_t_inum nn1 = SCM_I_INUM (n1); |
ca46fb90 RB |
4239 | if (nn1 == 0) return SCM_INUM0; |
4240 | if (nn1 < 0) nn1 = - nn1; | |
4241 | mpz_lcm_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n2), nn1); | |
4242 | scm_remember_upto_here_1 (n2); | |
4243 | return result; | |
4244 | } | |
4245 | } | |
3bbca1f7 MW |
4246 | else if (SCM_REALP (n2) && scm_is_integer (n2)) |
4247 | goto handle_inexacts; | |
4248 | else | |
4249 | SCM_WTA_DISPATCH_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm); | |
ca46fb90 | 4250 | } |
3bbca1f7 | 4251 | else if (SCM_LIKELY (SCM_BIGP (n1))) |
ca46fb90 RB |
4252 | { |
4253 | /* big n1 */ | |
e11e83f3 | 4254 | if (SCM_I_INUMP (n2)) |
ca46fb90 RB |
4255 | { |
4256 | SCM_SWAP (n1, n2); | |
4257 | goto inumbig; | |
4258 | } | |
3bbca1f7 | 4259 | else if (SCM_LIKELY (SCM_BIGP (n2))) |
ca46fb90 RB |
4260 | { |
4261 | SCM result = scm_i_mkbig (); | |
4262 | mpz_lcm(SCM_I_BIG_MPZ (result), | |
4263 | SCM_I_BIG_MPZ (n1), | |
4264 | SCM_I_BIG_MPZ (n2)); | |
4265 | scm_remember_upto_here_2(n1, n2); | |
4266 | /* shouldn't need to normalize b/c lcm of 2 bigs should be big */ | |
4267 | return result; | |
4268 | } | |
3bbca1f7 MW |
4269 | else if (SCM_REALP (n2) && scm_is_integer (n2)) |
4270 | goto handle_inexacts; | |
4271 | else | |
4272 | SCM_WTA_DISPATCH_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm); | |
4273 | } | |
4274 | else if (SCM_REALP (n1) && scm_is_integer (n1)) | |
4275 | { | |
4276 | if (SCM_I_INUMP (n2) || SCM_BIGP (n2) | |
4277 | || (SCM_REALP (n2) && scm_is_integer (n2))) | |
4278 | { | |
4279 | handle_inexacts: | |
4280 | return scm_exact_to_inexact (scm_lcm (scm_inexact_to_exact (n1), | |
4281 | scm_inexact_to_exact (n2))); | |
4282 | } | |
4283 | else | |
4284 | SCM_WTA_DISPATCH_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm); | |
f872b822 | 4285 | } |
3bbca1f7 MW |
4286 | else |
4287 | SCM_WTA_DISPATCH_2 (g_lcm, n1, n2, SCM_ARG1, s_lcm); | |
0f2d19dd JB |
4288 | } |
4289 | ||
8a525303 GB |
4290 | /* Emulating 2's complement bignums with sign magnitude arithmetic: |
4291 | ||
4292 | Logand: | |
4293 | X Y Result Method: | |
4294 | (len) | |
4295 | + + + x (map digit:logand X Y) | |
4296 | + - + x (map digit:logand X (lognot (+ -1 Y))) | |
4297 | - + + y (map digit:logand (lognot (+ -1 X)) Y) | |
4298 | - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y))) | |
4299 | ||
4300 | Logior: | |
4301 | X Y Result Method: | |
4302 | ||
4303 | + + + (map digit:logior X Y) | |
4304 | + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y))) | |
4305 | - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y))) | |
4306 | - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y))) | |
4307 | ||
4308 | Logxor: | |
4309 | X Y Result Method: | |
4310 | ||
4311 | + + + (map digit:logxor X Y) | |
4312 | + - - (+ 1 (map digit:logxor X (+ -1 Y))) | |
4313 | - + - (+ 1 (map digit:logxor (+ -1 X) Y)) | |
4314 | - - + (map digit:logxor (+ -1 X) (+ -1 Y)) | |
4315 | ||
4316 | Logtest: | |
4317 | X Y Result | |
4318 | ||
4319 | + + (any digit:logand X Y) | |
4320 | + - (any digit:logand X (lognot (+ -1 Y))) | |
4321 | - + (any digit:logand (lognot (+ -1 X)) Y) | |
4322 | - - #t | |
4323 | ||
4324 | */ | |
4325 | ||
78d3deb1 AW |
4326 | SCM_DEFINE (scm_i_logand, "logand", 0, 2, 1, |
4327 | (SCM x, SCM y, SCM rest), | |
4328 | "Return the bitwise AND of the integer arguments.\n\n" | |
4329 | "@lisp\n" | |
4330 | "(logand) @result{} -1\n" | |
4331 | "(logand 7) @result{} 7\n" | |
4332 | "(logand #b111 #b011 #b001) @result{} 1\n" | |
4333 | "@end lisp") | |
4334 | #define FUNC_NAME s_scm_i_logand | |
4335 | { | |
4336 | while (!scm_is_null (rest)) | |
4337 | { x = scm_logand (x, y); | |
4338 | y = scm_car (rest); | |
4339 | rest = scm_cdr (rest); | |
4340 | } | |
4341 | return scm_logand (x, y); | |
4342 | } | |
4343 | #undef FUNC_NAME | |
4344 | ||
4345 | #define s_scm_logand s_scm_i_logand | |
4346 | ||
4347 | SCM scm_logand (SCM n1, SCM n2) | |
1bbd0b84 | 4348 | #define FUNC_NAME s_scm_logand |
0f2d19dd | 4349 | { |
e25f3727 | 4350 | scm_t_inum nn1; |
9a00c9fc | 4351 | |
0aacf84e MD |
4352 | if (SCM_UNBNDP (n2)) |
4353 | { | |
4354 | if (SCM_UNBNDP (n1)) | |
d956fa6f | 4355 | return SCM_I_MAKINUM (-1); |
0aacf84e MD |
4356 | else if (!SCM_NUMBERP (n1)) |
4357 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
4358 | else if (SCM_NUMBERP (n1)) | |
4359 | return n1; | |
4360 | else | |
4361 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
d28da049 | 4362 | } |
09fb7599 | 4363 | |
e11e83f3 | 4364 | if (SCM_I_INUMP (n1)) |
0aacf84e | 4365 | { |
e11e83f3 MV |
4366 | nn1 = SCM_I_INUM (n1); |
4367 | if (SCM_I_INUMP (n2)) | |
0aacf84e | 4368 | { |
e25f3727 | 4369 | scm_t_inum nn2 = SCM_I_INUM (n2); |
d956fa6f | 4370 | return SCM_I_MAKINUM (nn1 & nn2); |
0aacf84e MD |
4371 | } |
4372 | else if SCM_BIGP (n2) | |
4373 | { | |
4374 | intbig: | |
2e16a342 | 4375 | if (nn1 == 0) |
0aacf84e MD |
4376 | return SCM_INUM0; |
4377 | { | |
4378 | SCM result_z = scm_i_mkbig (); | |
4379 | mpz_t nn1_z; | |
4380 | mpz_init_set_si (nn1_z, nn1); | |
4381 | mpz_and (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2)); | |
4382 | scm_remember_upto_here_1 (n2); | |
4383 | mpz_clear (nn1_z); | |
4384 | return scm_i_normbig (result_z); | |
4385 | } | |
4386 | } | |
4387 | else | |
4388 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
4389 | } | |
4390 | else if (SCM_BIGP (n1)) | |
4391 | { | |
e11e83f3 | 4392 | if (SCM_I_INUMP (n2)) |
0aacf84e MD |
4393 | { |
4394 | SCM_SWAP (n1, n2); | |
e11e83f3 | 4395 | nn1 = SCM_I_INUM (n1); |
0aacf84e MD |
4396 | goto intbig; |
4397 | } | |
4398 | else if (SCM_BIGP (n2)) | |
4399 | { | |
4400 | SCM result_z = scm_i_mkbig (); | |
4401 | mpz_and (SCM_I_BIG_MPZ (result_z), | |
4402 | SCM_I_BIG_MPZ (n1), | |
4403 | SCM_I_BIG_MPZ (n2)); | |
4404 | scm_remember_upto_here_2 (n1, n2); | |
4405 | return scm_i_normbig (result_z); | |
4406 | } | |
4407 | else | |
4408 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
09fb7599 | 4409 | } |
0aacf84e | 4410 | else |
09fb7599 | 4411 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); |
0f2d19dd | 4412 | } |
1bbd0b84 | 4413 | #undef FUNC_NAME |
0f2d19dd | 4414 | |
09fb7599 | 4415 | |
78d3deb1 AW |
4416 | SCM_DEFINE (scm_i_logior, "logior", 0, 2, 1, |
4417 | (SCM x, SCM y, SCM rest), | |
4418 | "Return the bitwise OR of the integer arguments.\n\n" | |
4419 | "@lisp\n" | |
4420 | "(logior) @result{} 0\n" | |
4421 | "(logior 7) @result{} 7\n" | |
4422 | "(logior #b000 #b001 #b011) @result{} 3\n" | |
4423 | "@end lisp") | |
4424 | #define FUNC_NAME s_scm_i_logior | |
4425 | { | |
4426 | while (!scm_is_null (rest)) | |
4427 | { x = scm_logior (x, y); | |
4428 | y = scm_car (rest); | |
4429 | rest = scm_cdr (rest); | |
4430 | } | |
4431 | return scm_logior (x, y); | |
4432 | } | |
4433 | #undef FUNC_NAME | |
4434 | ||
4435 | #define s_scm_logior s_scm_i_logior | |
4436 | ||
4437 | SCM scm_logior (SCM n1, SCM n2) | |
1bbd0b84 | 4438 | #define FUNC_NAME s_scm_logior |
0f2d19dd | 4439 | { |
e25f3727 | 4440 | scm_t_inum nn1; |
9a00c9fc | 4441 | |
0aacf84e MD |
4442 | if (SCM_UNBNDP (n2)) |
4443 | { | |
4444 | if (SCM_UNBNDP (n1)) | |
4445 | return SCM_INUM0; | |
4446 | else if (SCM_NUMBERP (n1)) | |
4447 | return n1; | |
4448 | else | |
4449 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
d28da049 | 4450 | } |
09fb7599 | 4451 | |
e11e83f3 | 4452 | if (SCM_I_INUMP (n1)) |
0aacf84e | 4453 | { |
e11e83f3 MV |
4454 | nn1 = SCM_I_INUM (n1); |
4455 | if (SCM_I_INUMP (n2)) | |
0aacf84e | 4456 | { |
e11e83f3 | 4457 | long nn2 = SCM_I_INUM (n2); |
d956fa6f | 4458 | return SCM_I_MAKINUM (nn1 | nn2); |
0aacf84e MD |
4459 | } |
4460 | else if (SCM_BIGP (n2)) | |
4461 | { | |
4462 | intbig: | |
4463 | if (nn1 == 0) | |
4464 | return n2; | |
4465 | { | |
4466 | SCM result_z = scm_i_mkbig (); | |
4467 | mpz_t nn1_z; | |
4468 | mpz_init_set_si (nn1_z, nn1); | |
4469 | mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2)); | |
4470 | scm_remember_upto_here_1 (n2); | |
4471 | mpz_clear (nn1_z); | |
9806de0d | 4472 | return scm_i_normbig (result_z); |
0aacf84e MD |
4473 | } |
4474 | } | |
4475 | else | |
4476 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
4477 | } | |
4478 | else if (SCM_BIGP (n1)) | |
4479 | { | |
e11e83f3 | 4480 | if (SCM_I_INUMP (n2)) |
0aacf84e MD |
4481 | { |
4482 | SCM_SWAP (n1, n2); | |
e11e83f3 | 4483 | nn1 = SCM_I_INUM (n1); |
0aacf84e MD |
4484 | goto intbig; |
4485 | } | |
4486 | else if (SCM_BIGP (n2)) | |
4487 | { | |
4488 | SCM result_z = scm_i_mkbig (); | |
4489 | mpz_ior (SCM_I_BIG_MPZ (result_z), | |
4490 | SCM_I_BIG_MPZ (n1), | |
4491 | SCM_I_BIG_MPZ (n2)); | |
4492 | scm_remember_upto_here_2 (n1, n2); | |
9806de0d | 4493 | return scm_i_normbig (result_z); |
0aacf84e MD |
4494 | } |
4495 | else | |
4496 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
09fb7599 | 4497 | } |
0aacf84e | 4498 | else |
09fb7599 | 4499 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); |
0f2d19dd | 4500 | } |
1bbd0b84 | 4501 | #undef FUNC_NAME |
0f2d19dd | 4502 | |
09fb7599 | 4503 | |
78d3deb1 AW |
4504 | SCM_DEFINE (scm_i_logxor, "logxor", 0, 2, 1, |
4505 | (SCM x, SCM y, SCM rest), | |
3c3db128 GH |
4506 | "Return the bitwise XOR of the integer arguments. A bit is\n" |
4507 | "set in the result if it is set in an odd number of arguments.\n" | |
4508 | "@lisp\n" | |
4509 | "(logxor) @result{} 0\n" | |
4510 | "(logxor 7) @result{} 7\n" | |
4511 | "(logxor #b000 #b001 #b011) @result{} 2\n" | |
4512 | "(logxor #b000 #b001 #b011 #b011) @result{} 1\n" | |
1e6808ea | 4513 | "@end lisp") |
78d3deb1 AW |
4514 | #define FUNC_NAME s_scm_i_logxor |
4515 | { | |
4516 | while (!scm_is_null (rest)) | |
4517 | { x = scm_logxor (x, y); | |
4518 | y = scm_car (rest); | |
4519 | rest = scm_cdr (rest); | |
4520 | } | |
4521 | return scm_logxor (x, y); | |
4522 | } | |
4523 | #undef FUNC_NAME | |
4524 | ||
4525 | #define s_scm_logxor s_scm_i_logxor | |
4526 | ||
4527 | SCM scm_logxor (SCM n1, SCM n2) | |
1bbd0b84 | 4528 | #define FUNC_NAME s_scm_logxor |
0f2d19dd | 4529 | { |
e25f3727 | 4530 | scm_t_inum nn1; |
9a00c9fc | 4531 | |
0aacf84e MD |
4532 | if (SCM_UNBNDP (n2)) |
4533 | { | |
4534 | if (SCM_UNBNDP (n1)) | |
4535 | return SCM_INUM0; | |
4536 | else if (SCM_NUMBERP (n1)) | |
4537 | return n1; | |
4538 | else | |
4539 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); | |
d28da049 | 4540 | } |
09fb7599 | 4541 | |
e11e83f3 | 4542 | if (SCM_I_INUMP (n1)) |
0aacf84e | 4543 | { |
e11e83f3 MV |
4544 | nn1 = SCM_I_INUM (n1); |
4545 | if (SCM_I_INUMP (n2)) | |
0aacf84e | 4546 | { |
e25f3727 | 4547 | scm_t_inum nn2 = SCM_I_INUM (n2); |
d956fa6f | 4548 | return SCM_I_MAKINUM (nn1 ^ nn2); |
0aacf84e MD |
4549 | } |
4550 | else if (SCM_BIGP (n2)) | |
4551 | { | |
4552 | intbig: | |
4553 | { | |
4554 | SCM result_z = scm_i_mkbig (); | |
4555 | mpz_t nn1_z; | |
4556 | mpz_init_set_si (nn1_z, nn1); | |
4557 | mpz_xor (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2)); | |
4558 | scm_remember_upto_here_1 (n2); | |
4559 | mpz_clear (nn1_z); | |
4560 | return scm_i_normbig (result_z); | |
4561 | } | |
4562 | } | |
4563 | else | |
4564 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
4565 | } | |
4566 | else if (SCM_BIGP (n1)) | |
4567 | { | |
e11e83f3 | 4568 | if (SCM_I_INUMP (n2)) |
0aacf84e MD |
4569 | { |
4570 | SCM_SWAP (n1, n2); | |
e11e83f3 | 4571 | nn1 = SCM_I_INUM (n1); |
0aacf84e MD |
4572 | goto intbig; |
4573 | } | |
4574 | else if (SCM_BIGP (n2)) | |
4575 | { | |
4576 | SCM result_z = scm_i_mkbig (); | |
4577 | mpz_xor (SCM_I_BIG_MPZ (result_z), | |
4578 | SCM_I_BIG_MPZ (n1), | |
4579 | SCM_I_BIG_MPZ (n2)); | |
4580 | scm_remember_upto_here_2 (n1, n2); | |
4581 | return scm_i_normbig (result_z); | |
4582 | } | |
4583 | else | |
4584 | SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); | |
09fb7599 | 4585 | } |
0aacf84e | 4586 | else |
09fb7599 | 4587 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n1); |
0f2d19dd | 4588 | } |
1bbd0b84 | 4589 | #undef FUNC_NAME |
0f2d19dd | 4590 | |
09fb7599 | 4591 | |
a1ec6916 | 4592 | SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0, |
1e6808ea | 4593 | (SCM j, SCM k), |
ba6e7231 KR |
4594 | "Test whether @var{j} and @var{k} have any 1 bits in common.\n" |
4595 | "This is equivalent to @code{(not (zero? (logand j k)))}, but\n" | |
4596 | "without actually calculating the @code{logand}, just testing\n" | |
4597 | "for non-zero.\n" | |
4598 | "\n" | |
1e6808ea | 4599 | "@lisp\n" |
b380b885 MD |
4600 | "(logtest #b0100 #b1011) @result{} #f\n" |
4601 | "(logtest #b0100 #b0111) @result{} #t\n" | |
1e6808ea | 4602 | "@end lisp") |
1bbd0b84 | 4603 | #define FUNC_NAME s_scm_logtest |
0f2d19dd | 4604 | { |
e25f3727 | 4605 | scm_t_inum nj; |
9a00c9fc | 4606 | |
e11e83f3 | 4607 | if (SCM_I_INUMP (j)) |
0aacf84e | 4608 | { |
e11e83f3 MV |
4609 | nj = SCM_I_INUM (j); |
4610 | if (SCM_I_INUMP (k)) | |
0aacf84e | 4611 | { |
e25f3727 | 4612 | scm_t_inum nk = SCM_I_INUM (k); |
73e4de09 | 4613 | return scm_from_bool (nj & nk); |
0aacf84e MD |
4614 | } |
4615 | else if (SCM_BIGP (k)) | |
4616 | { | |
4617 | intbig: | |
4618 | if (nj == 0) | |
4619 | return SCM_BOOL_F; | |
4620 | { | |
4621 | SCM result; | |
4622 | mpz_t nj_z; | |
4623 | mpz_init_set_si (nj_z, nj); | |
4624 | mpz_and (nj_z, nj_z, SCM_I_BIG_MPZ (k)); | |
4625 | scm_remember_upto_here_1 (k); | |
73e4de09 | 4626 | result = scm_from_bool (mpz_sgn (nj_z) != 0); |
0aacf84e MD |
4627 | mpz_clear (nj_z); |
4628 | return result; | |
4629 | } | |
4630 | } | |
4631 | else | |
4632 | SCM_WRONG_TYPE_ARG (SCM_ARG2, k); | |
4633 | } | |
4634 | else if (SCM_BIGP (j)) | |
4635 | { | |
e11e83f3 | 4636 | if (SCM_I_INUMP (k)) |
0aacf84e MD |
4637 | { |
4638 | SCM_SWAP (j, k); | |
e11e83f3 | 4639 | nj = SCM_I_INUM (j); |
0aacf84e MD |
4640 | goto intbig; |
4641 | } | |
4642 | else if (SCM_BIGP (k)) | |
4643 | { | |
4644 | SCM result; | |
4645 | mpz_t result_z; | |
4646 | mpz_init (result_z); | |
4647 | mpz_and (result_z, | |
4648 | SCM_I_BIG_MPZ (j), | |
4649 | SCM_I_BIG_MPZ (k)); | |
4650 | scm_remember_upto_here_2 (j, k); | |
73e4de09 | 4651 | result = scm_from_bool (mpz_sgn (result_z) != 0); |
0aacf84e MD |
4652 | mpz_clear (result_z); |
4653 | return result; | |
4654 | } | |
4655 | else | |
4656 | SCM_WRONG_TYPE_ARG (SCM_ARG2, k); | |
4657 | } | |
4658 | else | |
4659 | SCM_WRONG_TYPE_ARG (SCM_ARG1, j); | |
0f2d19dd | 4660 | } |
1bbd0b84 | 4661 | #undef FUNC_NAME |
0f2d19dd | 4662 | |
c1bfcf60 | 4663 | |
a1ec6916 | 4664 | SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0, |
2cd04b42 | 4665 | (SCM index, SCM j), |
ba6e7231 KR |
4666 | "Test whether bit number @var{index} in @var{j} is set.\n" |
4667 | "@var{index} starts from 0 for the least significant bit.\n" | |
4668 | "\n" | |
1e6808ea | 4669 | "@lisp\n" |
b380b885 MD |
4670 | "(logbit? 0 #b1101) @result{} #t\n" |
4671 | "(logbit? 1 #b1101) @result{} #f\n" | |
4672 | "(logbit? 2 #b1101) @result{} #t\n" | |
4673 | "(logbit? 3 #b1101) @result{} #t\n" | |
4674 | "(logbit? 4 #b1101) @result{} #f\n" | |
1e6808ea | 4675 | "@end lisp") |
1bbd0b84 | 4676 | #define FUNC_NAME s_scm_logbit_p |
0f2d19dd | 4677 | { |
78166ad5 | 4678 | unsigned long int iindex; |
5efd3c7d | 4679 | iindex = scm_to_ulong (index); |
78166ad5 | 4680 | |
e11e83f3 | 4681 | if (SCM_I_INUMP (j)) |
0d75f6d8 | 4682 | { |
03cce0ce MW |
4683 | if (iindex < SCM_LONG_BIT - 1) |
4684 | /* Arrange for the number to be converted to unsigned before | |
4685 | checking the bit, to ensure that we're testing the bit in a | |
4686 | two's complement representation (regardless of the native | |
4687 | representation. */ | |
4688 | return scm_from_bool ((1UL << iindex) & SCM_I_INUM (j)); | |
4689 | else | |
4690 | /* Portably check the sign. */ | |
4691 | return scm_from_bool (SCM_I_INUM (j) < 0); | |
0d75f6d8 | 4692 | } |
0aacf84e MD |
4693 | else if (SCM_BIGP (j)) |
4694 | { | |
4695 | int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex); | |
4696 | scm_remember_upto_here_1 (j); | |
73e4de09 | 4697 | return scm_from_bool (val); |
0aacf84e MD |
4698 | } |
4699 | else | |
78166ad5 | 4700 | SCM_WRONG_TYPE_ARG (SCM_ARG2, j); |
0f2d19dd | 4701 | } |
1bbd0b84 | 4702 | #undef FUNC_NAME |
0f2d19dd | 4703 | |
78166ad5 | 4704 | |
a1ec6916 | 4705 | SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0, |
1bbd0b84 | 4706 | (SCM n), |
4d814788 | 4707 | "Return the integer which is the ones-complement of the integer\n" |
1e6808ea MG |
4708 | "argument.\n" |
4709 | "\n" | |
b380b885 MD |
4710 | "@lisp\n" |
4711 | "(number->string (lognot #b10000000) 2)\n" | |
4712 | " @result{} \"-10000001\"\n" | |
4713 | "(number->string (lognot #b0) 2)\n" | |
4714 | " @result{} \"-1\"\n" | |
1e6808ea | 4715 | "@end lisp") |
1bbd0b84 | 4716 | #define FUNC_NAME s_scm_lognot |
0f2d19dd | 4717 | { |
e11e83f3 | 4718 | if (SCM_I_INUMP (n)) { |
f9811f9f KR |
4719 | /* No overflow here, just need to toggle all the bits making up the inum. |
4720 | Enhancement: No need to strip the tag and add it back, could just xor | |
4721 | a block of 1 bits, if that worked with the various debug versions of | |
4722 | the SCM typedef. */ | |
e11e83f3 | 4723 | return SCM_I_MAKINUM (~ SCM_I_INUM (n)); |
f9811f9f KR |
4724 | |
4725 | } else if (SCM_BIGP (n)) { | |
4726 | SCM result = scm_i_mkbig (); | |
4727 | mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n)); | |
4728 | scm_remember_upto_here_1 (n); | |
4729 | return result; | |
4730 | ||
4731 | } else { | |
4732 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); | |
4733 | } | |
0f2d19dd | 4734 | } |
1bbd0b84 | 4735 | #undef FUNC_NAME |
0f2d19dd | 4736 | |
518b7508 KR |
4737 | /* returns 0 if IN is not an integer. OUT must already be |
4738 | initialized. */ | |
4739 | static int | |
4740 | coerce_to_big (SCM in, mpz_t out) | |
4741 | { | |
4742 | if (SCM_BIGP (in)) | |
4743 | mpz_set (out, SCM_I_BIG_MPZ (in)); | |
e11e83f3 MV |
4744 | else if (SCM_I_INUMP (in)) |
4745 | mpz_set_si (out, SCM_I_INUM (in)); | |
518b7508 KR |
4746 | else |
4747 | return 0; | |
4748 | ||
4749 | return 1; | |
4750 | } | |
4751 | ||
d885e204 | 4752 | SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0, |
518b7508 KR |
4753 | (SCM n, SCM k, SCM m), |
4754 | "Return @var{n} raised to the integer exponent\n" | |
4755 | "@var{k}, modulo @var{m}.\n" | |
4756 | "\n" | |
4757 | "@lisp\n" | |
4758 | "(modulo-expt 2 3 5)\n" | |
4759 | " @result{} 3\n" | |
4760 | "@end lisp") | |
d885e204 | 4761 | #define FUNC_NAME s_scm_modulo_expt |
518b7508 KR |
4762 | { |
4763 | mpz_t n_tmp; | |
4764 | mpz_t k_tmp; | |
4765 | mpz_t m_tmp; | |
4766 | ||
4767 | /* There are two classes of error we might encounter -- | |
4768 | 1) Math errors, which we'll report by calling scm_num_overflow, | |
4769 | and | |
4770 | 2) wrong-type errors, which of course we'll report by calling | |
4771 | SCM_WRONG_TYPE_ARG. | |
4772 | We don't report those errors immediately, however; instead we do | |
4773 | some cleanup first. These variables tell us which error (if | |
4774 | any) we should report after cleaning up. | |
4775 | */ | |
4776 | int report_overflow = 0; | |
4777 | ||
4778 | int position_of_wrong_type = 0; | |
4779 | SCM value_of_wrong_type = SCM_INUM0; | |
4780 | ||
4781 | SCM result = SCM_UNDEFINED; | |
4782 | ||
4783 | mpz_init (n_tmp); | |
4784 | mpz_init (k_tmp); | |
4785 | mpz_init (m_tmp); | |
4786 | ||
bc36d050 | 4787 | if (scm_is_eq (m, SCM_INUM0)) |
518b7508 KR |
4788 | { |
4789 | report_overflow = 1; | |
4790 | goto cleanup; | |
4791 | } | |
4792 | ||
4793 | if (!coerce_to_big (n, n_tmp)) | |
4794 | { | |
4795 | value_of_wrong_type = n; | |
4796 | position_of_wrong_type = 1; | |
4797 | goto cleanup; | |
4798 | } | |
4799 | ||
4800 | if (!coerce_to_big (k, k_tmp)) | |
4801 | { | |
4802 | value_of_wrong_type = k; | |
4803 | position_of_wrong_type = 2; | |
4804 | goto cleanup; | |
4805 | } | |
4806 | ||
4807 | if (!coerce_to_big (m, m_tmp)) | |
4808 | { | |
4809 | value_of_wrong_type = m; | |
4810 | position_of_wrong_type = 3; | |
4811 | goto cleanup; | |
4812 | } | |
4813 | ||
4814 | /* if the exponent K is negative, and we simply call mpz_powm, we | |
4815 | will get a divide-by-zero exception when an inverse 1/n mod m | |
4816 | doesn't exist (or is not unique). Since exceptions are hard to | |
4817 | handle, we'll attempt the inversion "by hand" -- that way, we get | |
4818 | a simple failure code, which is easy to handle. */ | |
4819 | ||
4820 | if (-1 == mpz_sgn (k_tmp)) | |
4821 | { | |
4822 | if (!mpz_invert (n_tmp, n_tmp, m_tmp)) | |
4823 | { | |
4824 | report_overflow = 1; | |
4825 | goto cleanup; | |
4826 | } | |
4827 | mpz_neg (k_tmp, k_tmp); | |
4828 | } | |
4829 | ||
4830 | result = scm_i_mkbig (); | |
4831 | mpz_powm (SCM_I_BIG_MPZ (result), | |
4832 | n_tmp, | |
4833 | k_tmp, | |
4834 | m_tmp); | |
b7b8c575 KR |
4835 | |
4836 | if (mpz_sgn (m_tmp) < 0 && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0) | |
4837 | mpz_add (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), m_tmp); | |
4838 | ||
518b7508 KR |
4839 | cleanup: |
4840 | mpz_clear (m_tmp); | |
4841 | mpz_clear (k_tmp); | |
4842 | mpz_clear (n_tmp); | |
4843 | ||
4844 | if (report_overflow) | |
4845 | scm_num_overflow (FUNC_NAME); | |
4846 | ||
4847 | if (position_of_wrong_type) | |
4848 | SCM_WRONG_TYPE_ARG (position_of_wrong_type, | |
4849 | value_of_wrong_type); | |
4850 | ||
4851 | return scm_i_normbig (result); | |
4852 | } | |
4853 | #undef FUNC_NAME | |
4854 | ||
a1ec6916 | 4855 | SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0, |
2cd04b42 | 4856 | (SCM n, SCM k), |
ba6e7231 KR |
4857 | "Return @var{n} raised to the power @var{k}. @var{k} must be an\n" |
4858 | "exact integer, @var{n} can be any number.\n" | |
4859 | "\n" | |
2519490c MW |
4860 | "Negative @var{k} is supported, and results in\n" |
4861 | "@math{1/@var{n}^abs(@var{k})} in the usual way.\n" | |
4862 | "@math{@var{n}^0} is 1, as usual, and that\n" | |
ba6e7231 | 4863 | "includes @math{0^0} is 1.\n" |
1e6808ea | 4864 | "\n" |
b380b885 | 4865 | "@lisp\n" |
ba6e7231 KR |
4866 | "(integer-expt 2 5) @result{} 32\n" |
4867 | "(integer-expt -3 3) @result{} -27\n" | |
4868 | "(integer-expt 5 -3) @result{} 1/125\n" | |
4869 | "(integer-expt 0 0) @result{} 1\n" | |
b380b885 | 4870 | "@end lisp") |
1bbd0b84 | 4871 | #define FUNC_NAME s_scm_integer_expt |
0f2d19dd | 4872 | { |
e25f3727 | 4873 | scm_t_inum i2 = 0; |
1c35cb19 RB |
4874 | SCM z_i2 = SCM_BOOL_F; |
4875 | int i2_is_big = 0; | |
d956fa6f | 4876 | SCM acc = SCM_I_MAKINUM (1L); |
ca46fb90 | 4877 | |
bfe1f03a MW |
4878 | /* Specifically refrain from checking the type of the first argument. |
4879 | This allows us to exponentiate any object that can be multiplied. | |
4880 | If we must raise to a negative power, we must also be able to | |
4881 | take its reciprocal. */ | |
4882 | if (!SCM_LIKELY (SCM_I_INUMP (k)) && !SCM_LIKELY (SCM_BIGP (k))) | |
01c7284a | 4883 | SCM_WRONG_TYPE_ARG (2, k); |
5a8fc758 | 4884 | |
bfe1f03a MW |
4885 | if (SCM_UNLIKELY (scm_is_eq (k, SCM_INUM0))) |
4886 | return SCM_INUM1; /* n^(exact0) is exact 1, regardless of n */ | |
4887 | else if (SCM_UNLIKELY (scm_is_eq (n, SCM_I_MAKINUM (-1L)))) | |
4888 | return scm_is_false (scm_even_p (k)) ? n : SCM_INUM1; | |
4889 | /* The next check is necessary only because R6RS specifies different | |
4890 | behavior for 0^(-k) than for (/ 0). If n is not a scheme number, | |
4891 | we simply skip this case and move on. */ | |
4892 | else if (SCM_NUMBERP (n) && scm_is_true (scm_zero_p (n))) | |
4893 | { | |
4894 | /* k cannot be 0 at this point, because we | |
4895 | have already checked for that case above */ | |
4896 | if (scm_is_true (scm_positive_p (k))) | |
01c7284a MW |
4897 | return n; |
4898 | else /* return NaN for (0 ^ k) for negative k per R6RS */ | |
4899 | return scm_nan (); | |
4900 | } | |
a285b18c MW |
4901 | else if (SCM_FRACTIONP (n)) |
4902 | { | |
4903 | /* Optimize the fraction case by (a/b)^k ==> (a^k)/(b^k), to avoid | |
4904 | needless reduction of intermediate products to lowest terms. | |
4905 | If a and b have no common factors, then a^k and b^k have no | |
4906 | common factors. Use 'scm_i_make_ratio_already_reduced' to | |
4907 | construct the final result, so that no gcd computations are | |
4908 | needed to exponentiate a fraction. */ | |
4909 | if (scm_is_true (scm_positive_p (k))) | |
4910 | return scm_i_make_ratio_already_reduced | |
4911 | (scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k), | |
4912 | scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k)); | |
4913 | else | |
4914 | { | |
4915 | k = scm_difference (k, SCM_UNDEFINED); | |
4916 | return scm_i_make_ratio_already_reduced | |
4917 | (scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k), | |
4918 | scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k)); | |
4919 | } | |
4920 | } | |
ca46fb90 | 4921 | |
e11e83f3 MV |
4922 | if (SCM_I_INUMP (k)) |
4923 | i2 = SCM_I_INUM (k); | |
ca46fb90 RB |
4924 | else if (SCM_BIGP (k)) |
4925 | { | |
4926 | z_i2 = scm_i_clonebig (k, 1); | |
ca46fb90 RB |
4927 | scm_remember_upto_here_1 (k); |
4928 | i2_is_big = 1; | |
4929 | } | |
2830fd91 | 4930 | else |
ca46fb90 RB |
4931 | SCM_WRONG_TYPE_ARG (2, k); |
4932 | ||
4933 | if (i2_is_big) | |
f872b822 | 4934 | { |
ca46fb90 RB |
4935 | if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == -1) |
4936 | { | |
4937 | mpz_neg (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2)); | |
4938 | n = scm_divide (n, SCM_UNDEFINED); | |
4939 | } | |
4940 | while (1) | |
4941 | { | |
4942 | if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == 0) | |
4943 | { | |
ca46fb90 RB |
4944 | return acc; |
4945 | } | |
4946 | if (mpz_cmp_ui(SCM_I_BIG_MPZ (z_i2), 1) == 0) | |
4947 | { | |
ca46fb90 RB |
4948 | return scm_product (acc, n); |
4949 | } | |
4950 | if (mpz_tstbit(SCM_I_BIG_MPZ (z_i2), 0)) | |
4951 | acc = scm_product (acc, n); | |
4952 | n = scm_product (n, n); | |
4953 | mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2), 1); | |
4954 | } | |
f872b822 | 4955 | } |
ca46fb90 | 4956 | else |
f872b822 | 4957 | { |
ca46fb90 RB |
4958 | if (i2 < 0) |
4959 | { | |
4960 | i2 = -i2; | |
4961 | n = scm_divide (n, SCM_UNDEFINED); | |
4962 | } | |
4963 | while (1) | |
4964 | { | |
4965 | if (0 == i2) | |
4966 | return acc; | |
4967 | if (1 == i2) | |
4968 | return scm_product (acc, n); | |
4969 | if (i2 & 1) | |
4970 | acc = scm_product (acc, n); | |
4971 | n = scm_product (n, n); | |
4972 | i2 >>= 1; | |
4973 | } | |
f872b822 | 4974 | } |
0f2d19dd | 4975 | } |
1bbd0b84 | 4976 | #undef FUNC_NAME |
0f2d19dd | 4977 | |
e08a12b5 MW |
4978 | /* Efficiently compute (N * 2^COUNT), |
4979 | where N is an exact integer, and COUNT > 0. */ | |
4980 | static SCM | |
4981 | left_shift_exact_integer (SCM n, long count) | |
4982 | { | |
4983 | if (SCM_I_INUMP (n)) | |
4984 | { | |
4985 | scm_t_inum nn = SCM_I_INUM (n); | |
4986 | ||
d360671c | 4987 | /* Left shift of count >= SCM_I_FIXNUM_BIT-1 will almost[*] always |
e08a12b5 MW |
4988 | overflow a non-zero fixnum. For smaller shifts we check the |
4989 | bits going into positions above SCM_I_FIXNUM_BIT-1. If they're | |
4990 | all 0s for nn>=0, or all 1s for nn<0 then there's no overflow. | |
d360671c MW |
4991 | Those bits are "nn >> (SCM_I_FIXNUM_BIT-1 - count)". |
4992 | ||
4993 | [*] There's one exception: | |
4994 | (-1) << SCM_I_FIXNUM_BIT-1 == SCM_MOST_NEGATIVE_FIXNUM */ | |
e08a12b5 MW |
4995 | |
4996 | if (nn == 0) | |
4997 | return n; | |
4998 | else if (count < SCM_I_FIXNUM_BIT-1 && | |
4999 | ((scm_t_bits) (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - count)) + 1) | |
5000 | <= 1)) | |
03cce0ce | 5001 | return SCM_I_MAKINUM (nn < 0 ? -(-nn << count) : (nn << count)); |
e08a12b5 MW |
5002 | else |
5003 | { | |
5004 | SCM result = scm_i_inum2big (nn); | |
5005 | mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), | |
5006 | count); | |
d360671c | 5007 | return scm_i_normbig (result); |
1ea0803e | 5008 | } |
e08a12b5 MW |
5009 | } |
5010 | else if (SCM_BIGP (n)) | |
5011 | { | |
5012 | SCM result = scm_i_mkbig (); | |
5013 | mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n), count); | |
5014 | scm_remember_upto_here_1 (n); | |
5015 | return result; | |
5016 | } | |
5017 | else | |
6f82b8f6 | 5018 | assert (0); |
e08a12b5 MW |
5019 | } |
5020 | ||
5021 | /* Efficiently compute floor (N / 2^COUNT), | |
5022 | where N is an exact integer and COUNT > 0. */ | |
5023 | static SCM | |
5024 | floor_right_shift_exact_integer (SCM n, long count) | |
5025 | { | |
5026 | if (SCM_I_INUMP (n)) | |
5027 | { | |
5028 | scm_t_inum nn = SCM_I_INUM (n); | |
5029 | ||
5030 | if (count >= SCM_I_FIXNUM_BIT) | |
5031 | return (nn >= 0 ? SCM_INUM0 : SCM_I_MAKINUM (-1)); | |
5032 | else | |
5033 | return SCM_I_MAKINUM (SCM_SRS (nn, count)); | |
5034 | } | |
5035 | else if (SCM_BIGP (n)) | |
5036 | { | |
5037 | SCM result = scm_i_mkbig (); | |
5038 | mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n), | |
5039 | count); | |
5040 | scm_remember_upto_here_1 (n); | |
5041 | return scm_i_normbig (result); | |
5042 | } | |
5043 | else | |
6f82b8f6 | 5044 | assert (0); |
e08a12b5 MW |
5045 | } |
5046 | ||
5047 | /* Efficiently compute round (N / 2^COUNT), | |
5048 | where N is an exact integer and COUNT > 0. */ | |
5049 | static SCM | |
5050 | round_right_shift_exact_integer (SCM n, long count) | |
5051 | { | |
5052 | if (SCM_I_INUMP (n)) | |
5053 | { | |
5054 | if (count >= SCM_I_FIXNUM_BIT) | |
5055 | return SCM_INUM0; | |
5056 | else | |
5057 | { | |
5058 | scm_t_inum nn = SCM_I_INUM (n); | |
5059 | scm_t_inum qq = SCM_SRS (nn, count); | |
5060 | ||
5061 | if (0 == (nn & (1L << (count-1)))) | |
5062 | return SCM_I_MAKINUM (qq); /* round down */ | |
5063 | else if (nn & ((1L << (count-1)) - 1)) | |
5064 | return SCM_I_MAKINUM (qq + 1); /* round up */ | |
5065 | else | |
5066 | return SCM_I_MAKINUM ((~1L) & (qq + 1)); /* round to even */ | |
5067 | } | |
5068 | } | |
5069 | else if (SCM_BIGP (n)) | |
5070 | { | |
5071 | SCM q = scm_i_mkbig (); | |
5072 | ||
5073 | mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), count); | |
5074 | if (mpz_tstbit (SCM_I_BIG_MPZ (n), count-1) | |
5075 | && (mpz_odd_p (SCM_I_BIG_MPZ (q)) | |
5076 | || (mpz_scan1 (SCM_I_BIG_MPZ (n), 0) < count-1))) | |
5077 | mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1); | |
5078 | scm_remember_upto_here_1 (n); | |
5079 | return scm_i_normbig (q); | |
5080 | } | |
5081 | else | |
6f82b8f6 | 5082 | assert (0); |
e08a12b5 MW |
5083 | } |
5084 | ||
a1ec6916 | 5085 | SCM_DEFINE (scm_ash, "ash", 2, 0, 0, |
e08a12b5 MW |
5086 | (SCM n, SCM count), |
5087 | "Return @math{floor(@var{n} * 2^@var{count})}.\n" | |
5088 | "@var{n} and @var{count} must be exact integers.\n" | |
1e6808ea | 5089 | "\n" |
e08a12b5 MW |
5090 | "With @var{n} viewed as an infinite-precision twos-complement\n" |
5091 | "integer, @code{ash} means a left shift introducing zero bits\n" | |
5092 | "when @var{count} is positive, or a right shift dropping bits\n" | |
5093 | "when @var{count} is negative. This is an ``arithmetic'' shift.\n" | |
1e6808ea | 5094 | "\n" |
b380b885 | 5095 | "@lisp\n" |
1e6808ea MG |
5096 | "(number->string (ash #b1 3) 2) @result{} \"1000\"\n" |
5097 | "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n" | |
32f19569 KR |
5098 | "\n" |
5099 | ";; -23 is bits ...11101001, -6 is bits ...111010\n" | |
5100 | "(ash -23 -2) @result{} -6\n" | |
a3c8b9fc | 5101 | "@end lisp") |
1bbd0b84 | 5102 | #define FUNC_NAME s_scm_ash |
0f2d19dd | 5103 | { |
e08a12b5 | 5104 | if (SCM_I_INUMP (n) || SCM_BIGP (n)) |
788aca27 | 5105 | { |
e08a12b5 | 5106 | long bits_to_shift = scm_to_long (count); |
788aca27 KR |
5107 | |
5108 | if (bits_to_shift > 0) | |
e08a12b5 MW |
5109 | return left_shift_exact_integer (n, bits_to_shift); |
5110 | else if (SCM_LIKELY (bits_to_shift < 0)) | |
5111 | return floor_right_shift_exact_integer (n, -bits_to_shift); | |
788aca27 | 5112 | else |
e08a12b5 | 5113 | return n; |
788aca27 | 5114 | } |
e08a12b5 MW |
5115 | else |
5116 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); | |
5117 | } | |
5118 | #undef FUNC_NAME | |
788aca27 | 5119 | |
e08a12b5 MW |
5120 | SCM_DEFINE (scm_round_ash, "round-ash", 2, 0, 0, |
5121 | (SCM n, SCM count), | |
5122 | "Return @math{round(@var{n} * 2^@var{count})}.\n" | |
5123 | "@var{n} and @var{count} must be exact integers.\n" | |
5124 | "\n" | |
5125 | "With @var{n} viewed as an infinite-precision twos-complement\n" | |
5126 | "integer, @code{round-ash} means a left shift introducing zero\n" | |
5127 | "bits when @var{count} is positive, or a right shift rounding\n" | |
5128 | "to the nearest integer (with ties going to the nearest even\n" | |
5129 | "integer) when @var{count} is negative. This is a rounded\n" | |
5130 | "``arithmetic'' shift.\n" | |
5131 | "\n" | |
5132 | "@lisp\n" | |
5133 | "(number->string (round-ash #b1 3) 2) @result{} \"1000\"\n" | |
5134 | "(number->string (round-ash #b1010 -1) 2) @result{} \"101\"\n" | |
5135 | "(number->string (round-ash #b1010 -2) 2) @result{} \"10\"\n" | |
5136 | "(number->string (round-ash #b1011 -2) 2) @result{} \"11\"\n" | |
5137 | "(number->string (round-ash #b1101 -2) 2) @result{} \"11\"\n" | |
5138 | "(number->string (round-ash #b1110 -2) 2) @result{} \"100\"\n" | |
5139 | "@end lisp") | |
5140 | #define FUNC_NAME s_scm_round_ash | |
5141 | { | |
5142 | if (SCM_I_INUMP (n) || SCM_BIGP (n)) | |
5143 | { | |
5144 | long bits_to_shift = scm_to_long (count); | |
788aca27 | 5145 | |
e08a12b5 MW |
5146 | if (bits_to_shift > 0) |
5147 | return left_shift_exact_integer (n, bits_to_shift); | |
5148 | else if (SCM_LIKELY (bits_to_shift < 0)) | |
5149 | return round_right_shift_exact_integer (n, -bits_to_shift); | |
ca46fb90 | 5150 | else |
e08a12b5 | 5151 | return n; |
ca46fb90 RB |
5152 | } |
5153 | else | |
e08a12b5 | 5154 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); |
0f2d19dd | 5155 | } |
1bbd0b84 | 5156 | #undef FUNC_NAME |
0f2d19dd | 5157 | |
3c9f20f8 | 5158 | |
a1ec6916 | 5159 | SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0, |
1bbd0b84 | 5160 | (SCM n, SCM start, SCM end), |
1e6808ea MG |
5161 | "Return the integer composed of the @var{start} (inclusive)\n" |
5162 | "through @var{end} (exclusive) bits of @var{n}. The\n" | |
5163 | "@var{start}th bit becomes the 0-th bit in the result.\n" | |
5164 | "\n" | |
b380b885 MD |
5165 | "@lisp\n" |
5166 | "(number->string (bit-extract #b1101101010 0 4) 2)\n" | |
5167 | " @result{} \"1010\"\n" | |
5168 | "(number->string (bit-extract #b1101101010 4 9) 2)\n" | |
5169 | " @result{} \"10110\"\n" | |
5170 | "@end lisp") | |
1bbd0b84 | 5171 | #define FUNC_NAME s_scm_bit_extract |
0f2d19dd | 5172 | { |
7f848242 | 5173 | unsigned long int istart, iend, bits; |
5efd3c7d MV |
5174 | istart = scm_to_ulong (start); |
5175 | iend = scm_to_ulong (end); | |
c1bfcf60 | 5176 | SCM_ASSERT_RANGE (3, end, (iend >= istart)); |
78166ad5 | 5177 | |
7f848242 KR |
5178 | /* how many bits to keep */ |
5179 | bits = iend - istart; | |
5180 | ||
e11e83f3 | 5181 | if (SCM_I_INUMP (n)) |
0aacf84e | 5182 | { |
e25f3727 | 5183 | scm_t_inum in = SCM_I_INUM (n); |
7f848242 KR |
5184 | |
5185 | /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to | |
d77ad560 | 5186 | SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */ |
857ae6af | 5187 | in = SCM_SRS (in, min (istart, SCM_I_FIXNUM_BIT-1)); |
ac0c002c | 5188 | |
0aacf84e MD |
5189 | if (in < 0 && bits >= SCM_I_FIXNUM_BIT) |
5190 | { | |
5191 | /* Since we emulate two's complement encoded numbers, this | |
5192 | * special case requires us to produce a result that has | |
7f848242 | 5193 | * more bits than can be stored in a fixnum. |
0aacf84e | 5194 | */ |
e25f3727 | 5195 | SCM result = scm_i_inum2big (in); |
7f848242 KR |
5196 | mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), |
5197 | bits); | |
5198 | return result; | |
0aacf84e | 5199 | } |
ac0c002c | 5200 | |
7f848242 | 5201 | /* mask down to requisite bits */ |
857ae6af | 5202 | bits = min (bits, SCM_I_FIXNUM_BIT); |
d956fa6f | 5203 | return SCM_I_MAKINUM (in & ((1L << bits) - 1)); |
0aacf84e MD |
5204 | } |
5205 | else if (SCM_BIGP (n)) | |
ac0c002c | 5206 | { |
7f848242 KR |
5207 | SCM result; |
5208 | if (bits == 1) | |
5209 | { | |
d956fa6f | 5210 | result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart)); |
7f848242 KR |
5211 | } |
5212 | else | |
5213 | { | |
5214 | /* ENHANCE-ME: It'd be nice not to allocate a new bignum when | |
5215 | bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get | |
5216 | such bits into a ulong. */ | |
5217 | result = scm_i_mkbig (); | |
5218 | mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart); | |
5219 | mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits); | |
5220 | result = scm_i_normbig (result); | |
5221 | } | |
5222 | scm_remember_upto_here_1 (n); | |
5223 | return result; | |
ac0c002c | 5224 | } |
0aacf84e | 5225 | else |
78166ad5 | 5226 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); |
0f2d19dd | 5227 | } |
1bbd0b84 | 5228 | #undef FUNC_NAME |
0f2d19dd | 5229 | |
7f848242 | 5230 | |
e4755e5c JB |
5231 | static const char scm_logtab[] = { |
5232 | 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 | |
5233 | }; | |
1cc91f1b | 5234 | |
a1ec6916 | 5235 | SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0, |
1bbd0b84 | 5236 | (SCM n), |
1e6808ea MG |
5237 | "Return the number of bits in integer @var{n}. If integer is\n" |
5238 | "positive, the 1-bits in its binary representation are counted.\n" | |
5239 | "If negative, the 0-bits in its two's-complement binary\n" | |
5240 | "representation are counted. If 0, 0 is returned.\n" | |
5241 | "\n" | |
b380b885 MD |
5242 | "@lisp\n" |
5243 | "(logcount #b10101010)\n" | |
ca46fb90 RB |
5244 | " @result{} 4\n" |
5245 | "(logcount 0)\n" | |
5246 | " @result{} 0\n" | |
5247 | "(logcount -2)\n" | |
5248 | " @result{} 1\n" | |
5249 | "@end lisp") | |
5250 | #define FUNC_NAME s_scm_logcount | |
5251 | { | |
e11e83f3 | 5252 | if (SCM_I_INUMP (n)) |
f872b822 | 5253 | { |
e25f3727 AW |
5254 | unsigned long c = 0; |
5255 | scm_t_inum nn = SCM_I_INUM (n); | |
ca46fb90 RB |
5256 | if (nn < 0) |
5257 | nn = -1 - nn; | |
5258 | while (nn) | |
5259 | { | |
5260 | c += scm_logtab[15 & nn]; | |
5261 | nn >>= 4; | |
5262 | } | |
d956fa6f | 5263 | return SCM_I_MAKINUM (c); |
f872b822 | 5264 | } |
ca46fb90 | 5265 | else if (SCM_BIGP (n)) |
f872b822 | 5266 | { |
ca46fb90 | 5267 | unsigned long count; |
713a4259 KR |
5268 | if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0) |
5269 | count = mpz_popcount (SCM_I_BIG_MPZ (n)); | |
ca46fb90 | 5270 | else |
713a4259 KR |
5271 | count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one); |
5272 | scm_remember_upto_here_1 (n); | |
d956fa6f | 5273 | return SCM_I_MAKINUM (count); |
f872b822 | 5274 | } |
ca46fb90 RB |
5275 | else |
5276 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); | |
0f2d19dd | 5277 | } |
ca46fb90 | 5278 | #undef FUNC_NAME |
0f2d19dd JB |
5279 | |
5280 | ||
ca46fb90 RB |
5281 | static const char scm_ilentab[] = { |
5282 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 | |
5283 | }; | |
5284 | ||
0f2d19dd | 5285 | |
ca46fb90 RB |
5286 | SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0, |
5287 | (SCM n), | |
5288 | "Return the number of bits necessary to represent @var{n}.\n" | |
5289 | "\n" | |
5290 | "@lisp\n" | |
5291 | "(integer-length #b10101010)\n" | |
5292 | " @result{} 8\n" | |
5293 | "(integer-length 0)\n" | |
5294 | " @result{} 0\n" | |
5295 | "(integer-length #b1111)\n" | |
5296 | " @result{} 4\n" | |
5297 | "@end lisp") | |
5298 | #define FUNC_NAME s_scm_integer_length | |
5299 | { | |
e11e83f3 | 5300 | if (SCM_I_INUMP (n)) |
0aacf84e | 5301 | { |
e25f3727 | 5302 | unsigned long c = 0; |
0aacf84e | 5303 | unsigned int l = 4; |
e25f3727 | 5304 | scm_t_inum nn = SCM_I_INUM (n); |
0aacf84e MD |
5305 | if (nn < 0) |
5306 | nn = -1 - nn; | |
5307 | while (nn) | |
5308 | { | |
5309 | c += 4; | |
5310 | l = scm_ilentab [15 & nn]; | |
5311 | nn >>= 4; | |
5312 | } | |
d956fa6f | 5313 | return SCM_I_MAKINUM (c - 4 + l); |
0aacf84e MD |
5314 | } |
5315 | else if (SCM_BIGP (n)) | |
5316 | { | |
5317 | /* mpz_sizeinbase looks at the absolute value of negatives, whereas we | |
5318 | want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is | |
5319 | 1 too big, so check for that and adjust. */ | |
5320 | size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2); | |
5321 | if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0 | |
5322 | && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */ | |
5323 | mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX) | |
5324 | size--; | |
5325 | scm_remember_upto_here_1 (n); | |
d956fa6f | 5326 | return SCM_I_MAKINUM (size); |
0aacf84e MD |
5327 | } |
5328 | else | |
ca46fb90 | 5329 | SCM_WRONG_TYPE_ARG (SCM_ARG1, n); |
ca46fb90 RB |
5330 | } |
5331 | #undef FUNC_NAME | |
0f2d19dd JB |
5332 | |
5333 | /*** NUMBERS -> STRINGS ***/ | |
0b799eea MV |
5334 | #define SCM_MAX_DBL_RADIX 36 |
5335 | ||
0b799eea | 5336 | /* use this array as a way to generate a single digit */ |
9b5fcde6 | 5337 | static const char number_chars[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
0f2d19dd | 5338 | |
1ea37620 MW |
5339 | static mpz_t dbl_minimum_normal_mantissa; |
5340 | ||
1be6b49c | 5341 | static size_t |
1ea37620 | 5342 | idbl2str (double dbl, char *a, int radix) |
0f2d19dd | 5343 | { |
1ea37620 | 5344 | int ch = 0; |
0b799eea | 5345 | |
1ea37620 MW |
5346 | if (radix < 2 || radix > SCM_MAX_DBL_RADIX) |
5347 | /* revert to existing behavior */ | |
5348 | radix = 10; | |
0f2d19dd | 5349 | |
1ea37620 | 5350 | if (isinf (dbl)) |
abb7e44d | 5351 | { |
1ea37620 MW |
5352 | strcpy (a, (dbl > 0.0) ? "+inf.0" : "-inf.0"); |
5353 | return 6; | |
abb7e44d | 5354 | } |
1ea37620 MW |
5355 | else if (dbl > 0.0) |
5356 | ; | |
5357 | else if (dbl < 0.0) | |
7351e207 | 5358 | { |
1ea37620 MW |
5359 | dbl = -dbl; |
5360 | a[ch++] = '-'; | |
7351e207 | 5361 | } |
1ea37620 | 5362 | else if (dbl == 0.0) |
7351e207 | 5363 | { |
e1592f8a | 5364 | if (copysign (1.0, dbl) < 0.0) |
1ea37620 MW |
5365 | a[ch++] = '-'; |
5366 | strcpy (a + ch, "0.0"); | |
5367 | return ch + 3; | |
7351e207 | 5368 | } |
1ea37620 | 5369 | else if (isnan (dbl)) |
f872b822 | 5370 | { |
1ea37620 MW |
5371 | strcpy (a, "+nan.0"); |
5372 | return 6; | |
f872b822 | 5373 | } |
7351e207 | 5374 | |
1ea37620 MW |
5375 | /* Algorithm taken from "Printing Floating-Point Numbers Quickly and |
5376 | Accurately" by Robert G. Burger and R. Kent Dybvig */ | |
5377 | { | |
5378 | int e, k; | |
5379 | mpz_t f, r, s, mplus, mminus, hi, digit; | |
5380 | int f_is_even, f_is_odd; | |
8150dfa1 | 5381 | int expon; |
1ea37620 MW |
5382 | int show_exp = 0; |
5383 | ||
5384 | mpz_inits (f, r, s, mplus, mminus, hi, digit, NULL); | |
5385 | mpz_set_d (f, ldexp (frexp (dbl, &e), DBL_MANT_DIG)); | |
5386 | if (e < DBL_MIN_EXP) | |
5387 | { | |
5388 | mpz_tdiv_q_2exp (f, f, DBL_MIN_EXP - e); | |
5389 | e = DBL_MIN_EXP; | |
5390 | } | |
5391 | e -= DBL_MANT_DIG; | |
0b799eea | 5392 | |
1ea37620 MW |
5393 | f_is_even = !mpz_odd_p (f); |
5394 | f_is_odd = !f_is_even; | |
0b799eea | 5395 | |
1ea37620 MW |
5396 | /* Initialize r, s, mplus, and mminus according |
5397 | to Table 1 from the paper. */ | |
5398 | if (e < 0) | |
5399 | { | |
5400 | mpz_set_ui (mminus, 1); | |
5401 | if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0 | |
5402 | || e == DBL_MIN_EXP - DBL_MANT_DIG) | |
5403 | { | |
5404 | mpz_set_ui (mplus, 1); | |
5405 | mpz_mul_2exp (r, f, 1); | |
5406 | mpz_mul_2exp (s, mminus, 1 - e); | |
5407 | } | |
5408 | else | |
5409 | { | |
5410 | mpz_set_ui (mplus, 2); | |
5411 | mpz_mul_2exp (r, f, 2); | |
5412 | mpz_mul_2exp (s, mminus, 2 - e); | |
5413 | } | |
5414 | } | |
5415 | else | |
5416 | { | |
5417 | mpz_set_ui (mminus, 1); | |
5418 | mpz_mul_2exp (mminus, mminus, e); | |
5419 | if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0) | |
5420 | { | |
5421 | mpz_set (mplus, mminus); | |
5422 | mpz_mul_2exp (r, f, 1 + e); | |
5423 | mpz_set_ui (s, 2); | |
5424 | } | |
5425 | else | |
5426 | { | |
5427 | mpz_mul_2exp (mplus, mminus, 1); | |
5428 | mpz_mul_2exp (r, f, 2 + e); | |
5429 | mpz_set_ui (s, 4); | |
5430 | } | |
5431 | } | |
0b799eea | 5432 | |
1ea37620 MW |
5433 | /* Find the smallest k such that: |
5434 | (r + mplus) / s < radix^k (if f is even) | |
5435 | (r + mplus) / s <= radix^k (if f is odd) */ | |
f872b822 | 5436 | { |
1ea37620 MW |
5437 | /* IMPROVE-ME: Make an initial guess to speed this up */ |
5438 | mpz_add (hi, r, mplus); | |
5439 | k = 0; | |
5440 | while (mpz_cmp (hi, s) >= f_is_odd) | |
5441 | { | |
5442 | mpz_mul_ui (s, s, radix); | |
5443 | k++; | |
5444 | } | |
5445 | if (k == 0) | |
5446 | { | |
5447 | mpz_mul_ui (hi, hi, radix); | |
5448 | while (mpz_cmp (hi, s) < f_is_odd) | |
5449 | { | |
5450 | mpz_mul_ui (r, r, radix); | |
5451 | mpz_mul_ui (mplus, mplus, radix); | |
5452 | mpz_mul_ui (mminus, mminus, radix); | |
5453 | mpz_mul_ui (hi, hi, radix); | |
5454 | k--; | |
5455 | } | |
5456 | } | |
cda139a7 | 5457 | } |
f872b822 | 5458 | |
8150dfa1 MW |
5459 | expon = k - 1; |
5460 | if (k <= 0) | |
1ea37620 | 5461 | { |
8150dfa1 MW |
5462 | if (k <= -3) |
5463 | { | |
5464 | /* Use scientific notation */ | |
5465 | show_exp = 1; | |
5466 | k = 1; | |
5467 | } | |
5468 | else | |
5469 | { | |
5470 | int i; | |
0f2d19dd | 5471 | |
8150dfa1 MW |
5472 | /* Print leading zeroes */ |
5473 | a[ch++] = '0'; | |
5474 | a[ch++] = '.'; | |
5475 | for (i = 0; i > k; i--) | |
5476 | a[ch++] = '0'; | |
5477 | } | |
1ea37620 MW |
5478 | } |
5479 | ||
5480 | for (;;) | |
5481 | { | |
5482 | int end_1_p, end_2_p; | |
5483 | int d; | |
5484 | ||
5485 | mpz_mul_ui (mplus, mplus, radix); | |
5486 | mpz_mul_ui (mminus, mminus, radix); | |
5487 | mpz_mul_ui (r, r, radix); | |
5488 | mpz_fdiv_qr (digit, r, r, s); | |
5489 | d = mpz_get_ui (digit); | |
5490 | ||
5491 | mpz_add (hi, r, mplus); | |
5492 | end_1_p = (mpz_cmp (r, mminus) < f_is_even); | |
5493 | end_2_p = (mpz_cmp (s, hi) < f_is_even); | |
5494 | if (end_1_p || end_2_p) | |
5495 | { | |
5496 | mpz_mul_2exp (r, r, 1); | |
5497 | if (!end_2_p) | |
5498 | ; | |
5499 | else if (!end_1_p) | |
5500 | d++; | |
5501 | else if (mpz_cmp (r, s) >= !(d & 1)) | |
5502 | d++; | |
5503 | a[ch++] = number_chars[d]; | |
5504 | if (--k == 0) | |
5505 | a[ch++] = '.'; | |
5506 | break; | |
5507 | } | |
5508 | else | |
5509 | { | |
5510 | a[ch++] = number_chars[d]; | |
5511 | if (--k == 0) | |
5512 | a[ch++] = '.'; | |
5513 | } | |
5514 | } | |
5515 | ||
5516 | if (k > 0) | |
5517 | { | |
8150dfa1 MW |
5518 | if (expon >= 7 && k >= 4 && expon >= k) |
5519 | { | |
5520 | /* Here we would have to print more than three zeroes | |
5521 | followed by a decimal point and another zero. It | |
5522 | makes more sense to use scientific notation. */ | |
5523 | ||
5524 | /* Adjust k to what it would have been if we had chosen | |
5525 | scientific notation from the beginning. */ | |
5526 | k -= expon; | |
5527 | ||
5528 | /* k will now be <= 0, with magnitude equal to the number of | |
5529 | digits that we printed which should now be put after the | |
5530 | decimal point. */ | |
5531 | ||
5532 | /* Insert a decimal point */ | |
5533 | memmove (a + ch + k + 1, a + ch + k, -k); | |
5534 | a[ch + k] = '.'; | |
5535 | ch++; | |
5536 | ||
5537 | show_exp = 1; | |
5538 | } | |
5539 | else | |
5540 | { | |
5541 | for (; k > 0; k--) | |
5542 | a[ch++] = '0'; | |
5543 | a[ch++] = '.'; | |
5544 | } | |
1ea37620 MW |
5545 | } |
5546 | ||
5547 | if (k == 0) | |
5548 | a[ch++] = '0'; | |
5549 | ||
5550 | if (show_exp) | |
5551 | { | |
5552 | a[ch++] = 'e'; | |
8150dfa1 | 5553 | ch += scm_iint2str (expon, radix, a + ch); |
1ea37620 MW |
5554 | } |
5555 | ||
5556 | mpz_clears (f, r, s, mplus, mminus, hi, digit, NULL); | |
5557 | } | |
0f2d19dd JB |
5558 | return ch; |
5559 | } | |
5560 | ||
7a1aba42 MV |
5561 | |
5562 | static size_t | |
5563 | icmplx2str (double real, double imag, char *str, int radix) | |
5564 | { | |
5565 | size_t i; | |
c7218482 | 5566 | double sgn; |
7a1aba42 MV |
5567 | |
5568 | i = idbl2str (real, str, radix); | |
c7218482 MW |
5569 | #ifdef HAVE_COPYSIGN |
5570 | sgn = copysign (1.0, imag); | |
5571 | #else | |
5572 | sgn = imag; | |
5573 | #endif | |
5574 | /* Don't output a '+' for negative numbers or for Inf and | |
5575 | NaN. They will provide their own sign. */ | |
19374ad2 | 5576 | if (sgn >= 0 && isfinite (imag)) |
c7218482 MW |
5577 | str[i++] = '+'; |
5578 | i += idbl2str (imag, &str[i], radix); | |
5579 | str[i++] = 'i'; | |
7a1aba42 MV |
5580 | return i; |
5581 | } | |
5582 | ||
1be6b49c | 5583 | static size_t |
0b799eea | 5584 | iflo2str (SCM flt, char *str, int radix) |
0f2d19dd | 5585 | { |
1be6b49c | 5586 | size_t i; |
3c9a524f | 5587 | if (SCM_REALP (flt)) |
0b799eea | 5588 | i = idbl2str (SCM_REAL_VALUE (flt), str, radix); |
0f2d19dd | 5589 | else |
7a1aba42 MV |
5590 | i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt), |
5591 | str, radix); | |
0f2d19dd JB |
5592 | return i; |
5593 | } | |
0f2d19dd | 5594 | |
2881e77b | 5595 | /* convert a scm_t_intmax to a string (unterminated). returns the number of |
1bbd0b84 GB |
5596 | characters in the result. |
5597 | rad is output base | |
5598 | p is destination: worst case (base 2) is SCM_INTBUFLEN */ | |
1be6b49c | 5599 | size_t |
2881e77b MV |
5600 | scm_iint2str (scm_t_intmax num, int rad, char *p) |
5601 | { | |
5602 | if (num < 0) | |
5603 | { | |
5604 | *p++ = '-'; | |
5605 | return scm_iuint2str (-num, rad, p) + 1; | |
5606 | } | |
5607 | else | |
5608 | return scm_iuint2str (num, rad, p); | |
5609 | } | |
5610 | ||
5611 | /* convert a scm_t_intmax to a string (unterminated). returns the number of | |
5612 | characters in the result. | |
5613 | rad is output base | |
5614 | p is destination: worst case (base 2) is SCM_INTBUFLEN */ | |
5615 | size_t | |
5616 | scm_iuint2str (scm_t_uintmax num, int rad, char *p) | |
0f2d19dd | 5617 | { |
1be6b49c ML |
5618 | size_t j = 1; |
5619 | size_t i; | |
2881e77b | 5620 | scm_t_uintmax n = num; |
5c11cc9d | 5621 | |
a6f3af16 AW |
5622 | if (rad < 2 || rad > 36) |
5623 | scm_out_of_range ("scm_iuint2str", scm_from_int (rad)); | |
5624 | ||
f872b822 | 5625 | for (n /= rad; n > 0; n /= rad) |
5c11cc9d GH |
5626 | j++; |
5627 | ||
5628 | i = j; | |
2881e77b | 5629 | n = num; |
f872b822 MD |
5630 | while (i--) |
5631 | { | |
5c11cc9d GH |
5632 | int d = n % rad; |
5633 | ||
f872b822 | 5634 | n /= rad; |
a6f3af16 | 5635 | p[i] = number_chars[d]; |
f872b822 | 5636 | } |
0f2d19dd JB |
5637 | return j; |
5638 | } | |
5639 | ||
a1ec6916 | 5640 | SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0, |
bb628794 DH |
5641 | (SCM n, SCM radix), |
5642 | "Return a string holding the external representation of the\n" | |
942e5b91 MG |
5643 | "number @var{n} in the given @var{radix}. If @var{n} is\n" |
5644 | "inexact, a radix of 10 will be used.") | |
1bbd0b84 | 5645 | #define FUNC_NAME s_scm_number_to_string |
0f2d19dd | 5646 | { |
1bbd0b84 | 5647 | int base; |
98cb6e75 | 5648 | |
0aacf84e | 5649 | if (SCM_UNBNDP (radix)) |
98cb6e75 | 5650 | base = 10; |
0aacf84e | 5651 | else |
5efd3c7d | 5652 | base = scm_to_signed_integer (radix, 2, 36); |
98cb6e75 | 5653 | |
e11e83f3 | 5654 | if (SCM_I_INUMP (n)) |
0aacf84e MD |
5655 | { |
5656 | char num_buf [SCM_INTBUFLEN]; | |
e11e83f3 | 5657 | size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf); |
cc95e00a | 5658 | return scm_from_locale_stringn (num_buf, length); |
0aacf84e MD |
5659 | } |
5660 | else if (SCM_BIGP (n)) | |
5661 | { | |
5662 | char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n)); | |
d88f5323 AW |
5663 | size_t len = strlen (str); |
5664 | void (*freefunc) (void *, size_t); | |
5665 | SCM ret; | |
5666 | mp_get_memory_functions (NULL, NULL, &freefunc); | |
0aacf84e | 5667 | scm_remember_upto_here_1 (n); |
d88f5323 AW |
5668 | ret = scm_from_latin1_stringn (str, len); |
5669 | freefunc (str, len + 1); | |
5670 | return ret; | |
0aacf84e | 5671 | } |
f92e85f7 MV |
5672 | else if (SCM_FRACTIONP (n)) |
5673 | { | |
f92e85f7 | 5674 | return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix), |
cc95e00a | 5675 | scm_from_locale_string ("/"), |
f92e85f7 MV |
5676 | scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix))); |
5677 | } | |
0aacf84e MD |
5678 | else if (SCM_INEXACTP (n)) |
5679 | { | |
5680 | char num_buf [FLOBUFLEN]; | |
cc95e00a | 5681 | return scm_from_locale_stringn (num_buf, iflo2str (n, num_buf, base)); |
0aacf84e MD |
5682 | } |
5683 | else | |
bb628794 | 5684 | SCM_WRONG_TYPE_ARG (1, n); |
0f2d19dd | 5685 | } |
1bbd0b84 | 5686 | #undef FUNC_NAME |
0f2d19dd JB |
5687 | |
5688 | ||
ca46fb90 RB |
5689 | /* These print routines used to be stubbed here so that scm_repl.c |
5690 | wouldn't need SCM_BIGDIG conditionals (pre GMP) */ | |
1cc91f1b | 5691 | |
0f2d19dd | 5692 | int |
e81d98ec | 5693 | scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED) |
0f2d19dd | 5694 | { |
56e55ac7 | 5695 | char num_buf[FLOBUFLEN]; |
0b799eea | 5696 | scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port); |
0f2d19dd JB |
5697 | return !0; |
5698 | } | |
5699 | ||
b479fe9a MV |
5700 | void |
5701 | scm_i_print_double (double val, SCM port) | |
5702 | { | |
5703 | char num_buf[FLOBUFLEN]; | |
5704 | scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port); | |
5705 | } | |
5706 | ||
f3ae5d60 | 5707 | int |
e81d98ec | 5708 | scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED) |
f92e85f7 | 5709 | |
f3ae5d60 | 5710 | { |
56e55ac7 | 5711 | char num_buf[FLOBUFLEN]; |
0b799eea | 5712 | scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port); |
f3ae5d60 MD |
5713 | return !0; |
5714 | } | |
1cc91f1b | 5715 | |
7a1aba42 MV |
5716 | void |
5717 | scm_i_print_complex (double real, double imag, SCM port) | |
5718 | { | |
5719 | char num_buf[FLOBUFLEN]; | |
5720 | scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port); | |
5721 | } | |
5722 | ||
f92e85f7 MV |
5723 | int |
5724 | scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED) | |
5725 | { | |
5726 | SCM str; | |
f92e85f7 | 5727 | str = scm_number_to_string (sexp, SCM_UNDEFINED); |
a9178715 | 5728 | scm_display (str, port); |
f92e85f7 MV |
5729 | scm_remember_upto_here_1 (str); |
5730 | return !0; | |
5731 | } | |
5732 | ||
0f2d19dd | 5733 | int |
e81d98ec | 5734 | scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) |
0f2d19dd | 5735 | { |
ca46fb90 | 5736 | char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp)); |
b57bf272 AW |
5737 | size_t len = strlen (str); |
5738 | void (*freefunc) (void *, size_t); | |
5739 | mp_get_memory_functions (NULL, NULL, &freefunc); | |
ca46fb90 | 5740 | scm_remember_upto_here_1 (exp); |
b57bf272 AW |
5741 | scm_lfwrite (str, len, port); |
5742 | freefunc (str, len + 1); | |
0f2d19dd JB |
5743 | return !0; |
5744 | } | |
5745 | /*** END nums->strs ***/ | |
5746 | ||
3c9a524f | 5747 | |
0f2d19dd | 5748 | /*** STRINGS -> NUMBERS ***/ |
2a8fecee | 5749 | |
3c9a524f DH |
5750 | /* The following functions implement the conversion from strings to numbers. |
5751 | * The implementation somehow follows the grammar for numbers as it is given | |
5752 | * in R5RS. Thus, the functions resemble syntactic units (<ureal R>, | |
5753 | * <uinteger R>, ...) that are used to build up numbers in the grammar. Some | |
5754 | * points should be noted about the implementation: | |
bc3d34f5 | 5755 | * |
3c9a524f DH |
5756 | * * Each function keeps a local index variable 'idx' that points at the |
5757 | * current position within the parsed string. The global index is only | |
5758 | * updated if the function could parse the corresponding syntactic unit | |
5759 | * successfully. | |
bc3d34f5 | 5760 | * |
3c9a524f | 5761 | * * Similarly, the functions keep track of indicators of inexactness ('#', |
bc3d34f5 MW |
5762 | * '.' or exponents) using local variables ('hash_seen', 'x'). |
5763 | * | |
3c9a524f DH |
5764 | * * Sequences of digits are parsed into temporary variables holding fixnums. |
5765 | * Only if these fixnums would overflow, the result variables are updated | |
5766 | * using the standard functions scm_add, scm_product, scm_divide etc. Then, | |
5767 | * the temporary variables holding the fixnums are cleared, and the process | |
5768 | * starts over again. If for example fixnums were able to store five decimal | |
5769 | * digits, a number 1234567890 would be parsed in two parts 12345 and 67890, | |
5770 | * and the result was computed as 12345 * 100000 + 67890. In other words, | |
5771 | * only every five digits two bignum operations were performed. | |
bc3d34f5 MW |
5772 | * |
5773 | * Notes on the handling of exactness specifiers: | |
5774 | * | |
5775 | * When parsing non-real complex numbers, we apply exactness specifiers on | |
5776 | * per-component basis, as is done in PLT Scheme. For complex numbers | |
5777 | * written in rectangular form, exactness specifiers are applied to the | |
5778 | * real and imaginary parts before calling scm_make_rectangular. For | |
5779 | * complex numbers written in polar form, exactness specifiers are applied | |
5780 | * to the magnitude and angle before calling scm_make_polar. | |
5781 | * | |
5782 | * There are two kinds of exactness specifiers: forced and implicit. A | |
5783 | * forced exactness specifier is a "#e" or "#i" prefix at the beginning of | |
5784 | * the entire number, and applies to both components of a complex number. | |
5785 | * "#e" causes each component to be made exact, and "#i" causes each | |
5786 | * component to be made inexact. If no forced exactness specifier is | |
5787 | * present, then the exactness of each component is determined | |
5788 | * independently by the presence or absence of a decimal point or hash mark | |
5789 | * within that component. If a decimal point or hash mark is present, the | |
5790 | * component is made inexact, otherwise it is made exact. | |
5791 | * | |
5792 | * After the exactness specifiers have been applied to each component, they | |
5793 | * are passed to either scm_make_rectangular or scm_make_polar to produce | |
5794 | * the final result. Note that this will result in a real number if the | |
5795 | * imaginary part, magnitude, or angle is an exact 0. | |
5796 | * | |
5797 | * For example, (string->number "#i5.0+0i") does the equivalent of: | |
5798 | * | |
5799 | * (make-rectangular (exact->inexact 5) (exact->inexact 0)) | |
3c9a524f DH |
5800 | */ |
5801 | ||
5802 | enum t_exactness {NO_EXACTNESS, INEXACT, EXACT}; | |
5803 | ||
5804 | /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */ | |
5805 | ||
a6f3af16 AW |
5806 | /* Caller is responsible for checking that the return value is in range |
5807 | for the given radix, which should be <= 36. */ | |
5808 | static unsigned int | |
5809 | char_decimal_value (scm_t_uint32 c) | |
5810 | { | |
5811 | /* uc_decimal_value returns -1 on error. When cast to an unsigned int, | |
5812 | that's certainly above any valid decimal, so we take advantage of | |
5813 | that to elide some tests. */ | |
5814 | unsigned int d = (unsigned int) uc_decimal_value (c); | |
5815 | ||
5816 | /* If that failed, try extended hexadecimals, then. Only accept ascii | |
5817 | hexadecimals. */ | |
5818 | if (d >= 10U) | |
5819 | { | |
5820 | c = uc_tolower (c); | |
5821 | if (c >= (scm_t_uint32) 'a') | |
5822 | d = c - (scm_t_uint32)'a' + 10U; | |
5823 | } | |
5824 | return d; | |
5825 | } | |
3c9a524f | 5826 | |
91db4a37 LC |
5827 | /* Parse the substring of MEM starting at *P_IDX for an unsigned integer |
5828 | in base RADIX. Upon success, return the unsigned integer and update | |
5829 | *P_IDX and *P_EXACTNESS accordingly. Return #f on failure. */ | |
2a8fecee | 5830 | static SCM |
3f47e526 | 5831 | mem2uinteger (SCM mem, unsigned int *p_idx, |
3c9a524f | 5832 | unsigned int radix, enum t_exactness *p_exactness) |
2a8fecee | 5833 | { |
3c9a524f DH |
5834 | unsigned int idx = *p_idx; |
5835 | unsigned int hash_seen = 0; | |
5836 | scm_t_bits shift = 1; | |
5837 | scm_t_bits add = 0; | |
5838 | unsigned int digit_value; | |
5839 | SCM result; | |
5840 | char c; | |
3f47e526 | 5841 | size_t len = scm_i_string_length (mem); |
3c9a524f DH |
5842 | |
5843 | if (idx == len) | |
5844 | return SCM_BOOL_F; | |
2a8fecee | 5845 | |
3f47e526 | 5846 | c = scm_i_string_ref (mem, idx); |
a6f3af16 | 5847 | digit_value = char_decimal_value (c); |
3c9a524f DH |
5848 | if (digit_value >= radix) |
5849 | return SCM_BOOL_F; | |
5850 | ||
5851 | idx++; | |
d956fa6f | 5852 | result = SCM_I_MAKINUM (digit_value); |
3c9a524f | 5853 | while (idx != len) |
f872b822 | 5854 | { |
3f47e526 | 5855 | scm_t_wchar c = scm_i_string_ref (mem, idx); |
a6f3af16 | 5856 | if (c == '#') |
3c9a524f DH |
5857 | { |
5858 | hash_seen = 1; | |
5859 | digit_value = 0; | |
5860 | } | |
a6f3af16 AW |
5861 | else if (hash_seen) |
5862 | break; | |
3c9a524f | 5863 | else |
a6f3af16 AW |
5864 | { |
5865 | digit_value = char_decimal_value (c); | |
5866 | /* This check catches non-decimals in addition to out-of-range | |
5867 | decimals. */ | |
5868 | if (digit_value >= radix) | |
5869 | break; | |
5870 | } | |
3c9a524f DH |
5871 | |
5872 | idx++; | |
5873 | if (SCM_MOST_POSITIVE_FIXNUM / radix < shift) | |
5874 | { | |
d956fa6f | 5875 | result = scm_product (result, SCM_I_MAKINUM (shift)); |
3c9a524f | 5876 | if (add > 0) |
d956fa6f | 5877 | result = scm_sum (result, SCM_I_MAKINUM (add)); |
3c9a524f DH |
5878 | |
5879 | shift = radix; | |
5880 | add = digit_value; | |
5881 | } | |
5882 | else | |
5883 | { | |
5884 | shift = shift * radix; | |
5885 | add = add * radix + digit_value; | |
5886 | } | |
5887 | }; | |
5888 | ||
5889 | if (shift > 1) | |
d956fa6f | 5890 | result = scm_product (result, SCM_I_MAKINUM (shift)); |
3c9a524f | 5891 | if (add > 0) |
d956fa6f | 5892 | result = scm_sum (result, SCM_I_MAKINUM (add)); |
3c9a524f DH |
5893 | |
5894 | *p_idx = idx; | |
5895 | if (hash_seen) | |
5896 | *p_exactness = INEXACT; | |
5897 | ||
5898 | return result; | |
2a8fecee JB |
5899 | } |
5900 | ||
5901 | ||
3c9a524f DH |
5902 | /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only |
5903 | * covers the parts of the rules that start at a potential point. The value | |
5904 | * of the digits up to the point have been parsed by the caller and are given | |
79d34f68 DH |
5905 | * in variable result. The content of *p_exactness indicates, whether a hash |
5906 | * has already been seen in the digits before the point. | |
3c9a524f | 5907 | */ |
1cc91f1b | 5908 | |
3f47e526 | 5909 | #define DIGIT2UINT(d) (uc_numeric_value(d).numerator) |
3c9a524f DH |
5910 | |
5911 | static SCM | |
3f47e526 | 5912 | mem2decimal_from_point (SCM result, SCM mem, |
3c9a524f | 5913 | unsigned int *p_idx, enum t_exactness *p_exactness) |
0f2d19dd | 5914 | { |
3c9a524f DH |
5915 | unsigned int idx = *p_idx; |
5916 | enum t_exactness x = *p_exactness; | |
3f47e526 | 5917 | size_t len = scm_i_string_length (mem); |
3c9a524f DH |
5918 | |
5919 | if (idx == len) | |
79d34f68 | 5920 | return result; |
3c9a524f | 5921 | |
3f47e526 | 5922 | if (scm_i_string_ref (mem, idx) == '.') |
3c9a524f DH |
5923 | { |
5924 | scm_t_bits shift = 1; | |
5925 | scm_t_bits add = 0; | |
5926 | unsigned int digit_value; | |
cff5fa33 | 5927 | SCM big_shift = SCM_INUM1; |
3c9a524f DH |
5928 | |
5929 | idx++; | |
5930 | while (idx != len) | |
5931 | { | |
3f47e526 MG |
5932 | scm_t_wchar c = scm_i_string_ref (mem, idx); |
5933 | if (uc_is_property_decimal_digit ((scm_t_uint32) c)) | |
3c9a524f DH |
5934 | { |
5935 | if (x == INEXACT) | |
5936 | return SCM_BOOL_F; | |
5937 | else | |
5938 | digit_value = DIGIT2UINT (c); | |
5939 | } | |
5940 | else if (c == '#') | |
5941 | { | |
5942 | x = INEXACT; | |
5943 | digit_value = 0; | |
5944 | } | |
5945 | else | |
5946 | break; | |
5947 | ||
5948 | idx++; | |
5949 | if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift) | |
5950 | { | |
d956fa6f MV |
5951 | big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift)); |
5952 | result = scm_product (result, SCM_I_MAKINUM (shift)); | |
3c9a524f | 5953 | if (add > 0) |
d956fa6f | 5954 | result = scm_sum (result, SCM_I_MAKINUM (add)); |
3c9a524f DH |
5955 | |
5956 | shift = 10; | |
5957 | add = digit_value; | |
5958 | } | |
5959 | else | |
5960 | { | |
5961 | shift = shift * 10; | |
5962 | add = add * 10 + digit_value; | |
5963 | } | |
5964 | }; | |
5965 | ||
5966 | if (add > 0) | |
5967 | { | |
d956fa6f MV |
5968 | big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift)); |
5969 | result = scm_product (result, SCM_I_MAKINUM (shift)); | |
5970 | result = scm_sum (result, SCM_I_MAKINUM (add)); | |
3c9a524f DH |
5971 | } |
5972 | ||
d8592269 | 5973 | result = scm_divide (result, big_shift); |
79d34f68 | 5974 | |
3c9a524f DH |
5975 | /* We've seen a decimal point, thus the value is implicitly inexact. */ |
5976 | x = INEXACT; | |
f872b822 | 5977 | } |
3c9a524f | 5978 | |
3c9a524f | 5979 | if (idx != len) |
f872b822 | 5980 | { |
3c9a524f DH |
5981 | int sign = 1; |
5982 | unsigned int start; | |
3f47e526 | 5983 | scm_t_wchar c; |
3c9a524f DH |
5984 | int exponent; |
5985 | SCM e; | |
5986 | ||
5987 | /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */ | |
5988 | ||
3f47e526 | 5989 | switch (scm_i_string_ref (mem, idx)) |
f872b822 | 5990 | { |
3c9a524f DH |
5991 | case 'd': case 'D': |
5992 | case 'e': case 'E': | |
5993 | case 'f': case 'F': | |
5994 | case 'l': case 'L': | |
5995 | case 's': case 'S': | |
5996 | idx++; | |
ee0ddd21 AW |
5997 | if (idx == len) |
5998 | return SCM_BOOL_F; | |
5999 | ||
3c9a524f | 6000 | start = idx; |
3f47e526 | 6001 | c = scm_i_string_ref (mem, idx); |
3c9a524f DH |
6002 | if (c == '-') |
6003 | { | |
6004 | idx++; | |
ee0ddd21 AW |
6005 | if (idx == len) |
6006 | return SCM_BOOL_F; | |
6007 | ||
3c9a524f | 6008 | sign = -1; |
3f47e526 | 6009 | c = scm_i_string_ref (mem, idx); |
3c9a524f DH |
6010 | } |
6011 | else if (c == '+') | |
6012 | { | |
6013 | idx++; | |
ee0ddd21 AW |
6014 | if (idx == len) |
6015 | return SCM_BOOL_F; | |
6016 | ||
3c9a524f | 6017 | sign = 1; |
3f47e526 | 6018 | c = scm_i_string_ref (mem, idx); |
3c9a524f DH |
6019 | } |
6020 | else | |
6021 | sign = 1; | |
6022 | ||
3f47e526 | 6023 | if (!uc_is_property_decimal_digit ((scm_t_uint32) c)) |
3c9a524f DH |
6024 | return SCM_BOOL_F; |
6025 | ||
6026 | idx++; | |
6027 | exponent = DIGIT2UINT (c); | |
6028 | while (idx != len) | |
f872b822 | 6029 | { |
3f47e526 MG |
6030 | scm_t_wchar c = scm_i_string_ref (mem, idx); |
6031 | if (uc_is_property_decimal_digit ((scm_t_uint32) c)) | |
3c9a524f DH |
6032 | { |
6033 | idx++; | |
6034 | if (exponent <= SCM_MAXEXP) | |
6035 | exponent = exponent * 10 + DIGIT2UINT (c); | |
6036 | } | |
6037 | else | |
6038 | break; | |
f872b822 | 6039 | } |
3c9a524f | 6040 | |
1ea37620 | 6041 | if (exponent > ((sign == 1) ? SCM_MAXEXP : SCM_MAXEXP + DBL_DIG + 1)) |
f872b822 | 6042 | { |
3c9a524f | 6043 | size_t exp_len = idx - start; |
3f47e526 | 6044 | SCM exp_string = scm_i_substring_copy (mem, start, start + exp_len); |
3c9a524f DH |
6045 | SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED); |
6046 | scm_out_of_range ("string->number", exp_num); | |
f872b822 | 6047 | } |
3c9a524f | 6048 | |
d956fa6f | 6049 | e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent)); |
3c9a524f DH |
6050 | if (sign == 1) |
6051 | result = scm_product (result, e); | |
6052 | else | |
6ebecdeb | 6053 | result = scm_divide (result, e); |
3c9a524f DH |
6054 | |
6055 | /* We've seen an exponent, thus the value is implicitly inexact. */ | |
6056 | x = INEXACT; | |
6057 | ||
f872b822 | 6058 | break; |
3c9a524f | 6059 | |
f872b822 | 6060 | default: |
3c9a524f | 6061 | break; |
f872b822 | 6062 | } |
0f2d19dd | 6063 | } |
3c9a524f DH |
6064 | |
6065 | *p_idx = idx; | |
6066 | if (x == INEXACT) | |
6067 | *p_exactness = x; | |
6068 | ||
6069 | return result; | |
0f2d19dd | 6070 | } |
0f2d19dd | 6071 | |
3c9a524f DH |
6072 | |
6073 | /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */ | |
6074 | ||
6075 | static SCM | |
3f47e526 | 6076 | mem2ureal (SCM mem, unsigned int *p_idx, |
929d11b2 MW |
6077 | unsigned int radix, enum t_exactness forced_x, |
6078 | int allow_inf_or_nan) | |
0f2d19dd | 6079 | { |
3c9a524f | 6080 | unsigned int idx = *p_idx; |
164d2481 | 6081 | SCM result; |
3f47e526 | 6082 | size_t len = scm_i_string_length (mem); |
3c9a524f | 6083 | |
40f89215 NJ |
6084 | /* Start off believing that the number will be exact. This changes |
6085 | to INEXACT if we see a decimal point or a hash. */ | |
9d427b2c | 6086 | enum t_exactness implicit_x = EXACT; |
40f89215 | 6087 | |
3c9a524f DH |
6088 | if (idx == len) |
6089 | return SCM_BOOL_F; | |
6090 | ||
929d11b2 MW |
6091 | if (allow_inf_or_nan && forced_x != EXACT && idx+5 <= len) |
6092 | switch (scm_i_string_ref (mem, idx)) | |
6093 | { | |
6094 | case 'i': case 'I': | |
6095 | switch (scm_i_string_ref (mem, idx + 1)) | |
6096 | { | |
6097 | case 'n': case 'N': | |
6098 | switch (scm_i_string_ref (mem, idx + 2)) | |
6099 | { | |
6100 | case 'f': case 'F': | |
6101 | if (scm_i_string_ref (mem, idx + 3) == '.' | |
6102 | && scm_i_string_ref (mem, idx + 4) == '0') | |
6103 | { | |
6104 | *p_idx = idx+5; | |
6105 | return scm_inf (); | |
6106 | } | |
6107 | } | |
6108 | } | |
6109 | case 'n': case 'N': | |
6110 | switch (scm_i_string_ref (mem, idx + 1)) | |
6111 | { | |
6112 | case 'a': case 'A': | |
6113 | switch (scm_i_string_ref (mem, idx + 2)) | |
6114 | { | |
6115 | case 'n': case 'N': | |
6116 | if (scm_i_string_ref (mem, idx + 3) == '.') | |
6117 | { | |
6118 | /* Cobble up the fractional part. We might want to | |
6119 | set the NaN's mantissa from it. */ | |
6120 | idx += 4; | |
6121 | if (!scm_is_eq (mem2uinteger (mem, &idx, 10, &implicit_x), | |
6122 | SCM_INUM0)) | |
6123 | { | |
5f237d6e | 6124 | #if SCM_ENABLE_DEPRECATED == 1 |
929d11b2 MW |
6125 | scm_c_issue_deprecation_warning |
6126 | ("Non-zero suffixes to `+nan.' are deprecated. Use `+nan.0'."); | |
5f237d6e | 6127 | #else |
929d11b2 | 6128 | return SCM_BOOL_F; |
5f237d6e | 6129 | #endif |
929d11b2 | 6130 | } |
5f237d6e | 6131 | |
929d11b2 MW |
6132 | *p_idx = idx; |
6133 | return scm_nan (); | |
6134 | } | |
6135 | } | |
6136 | } | |
6137 | } | |
7351e207 | 6138 | |
3f47e526 | 6139 | if (scm_i_string_ref (mem, idx) == '.') |
3c9a524f DH |
6140 | { |
6141 | if (radix != 10) | |
6142 | return SCM_BOOL_F; | |
6143 | else if (idx + 1 == len) | |
6144 | return SCM_BOOL_F; | |
3f47e526 | 6145 | else if (!uc_is_property_decimal_digit ((scm_t_uint32) scm_i_string_ref (mem, idx+1))) |
3c9a524f DH |
6146 | return SCM_BOOL_F; |
6147 | else | |
cff5fa33 | 6148 | result = mem2decimal_from_point (SCM_INUM0, mem, |
9d427b2c | 6149 | p_idx, &implicit_x); |
f872b822 | 6150 | } |
3c9a524f DH |
6151 | else |
6152 | { | |
3c9a524f | 6153 | SCM uinteger; |
3c9a524f | 6154 | |
9d427b2c | 6155 | uinteger = mem2uinteger (mem, &idx, radix, &implicit_x); |
73e4de09 | 6156 | if (scm_is_false (uinteger)) |
3c9a524f DH |
6157 | return SCM_BOOL_F; |
6158 | ||
6159 | if (idx == len) | |
6160 | result = uinteger; | |
3f47e526 | 6161 | else if (scm_i_string_ref (mem, idx) == '/') |
f872b822 | 6162 | { |
3c9a524f DH |
6163 | SCM divisor; |
6164 | ||
6165 | idx++; | |
ee0ddd21 AW |
6166 | if (idx == len) |
6167 | return SCM_BOOL_F; | |
3c9a524f | 6168 | |
9d427b2c | 6169 | divisor = mem2uinteger (mem, &idx, radix, &implicit_x); |
929d11b2 | 6170 | if (scm_is_false (divisor) || scm_is_eq (divisor, SCM_INUM0)) |
3c9a524f DH |
6171 | return SCM_BOOL_F; |
6172 | ||
f92e85f7 | 6173 | /* both are int/big here, I assume */ |
cba42c93 | 6174 | result = scm_i_make_ratio (uinteger, divisor); |
f872b822 | 6175 | } |
3c9a524f DH |
6176 | else if (radix == 10) |
6177 | { | |
9d427b2c | 6178 | result = mem2decimal_from_point (uinteger, mem, &idx, &implicit_x); |
73e4de09 | 6179 | if (scm_is_false (result)) |
3c9a524f DH |
6180 | return SCM_BOOL_F; |
6181 | } | |
6182 | else | |
6183 | result = uinteger; | |
6184 | ||
6185 | *p_idx = idx; | |
f872b822 | 6186 | } |
164d2481 | 6187 | |
9d427b2c MW |
6188 | switch (forced_x) |
6189 | { | |
6190 | case EXACT: | |
6191 | if (SCM_INEXACTP (result)) | |
6192 | return scm_inexact_to_exact (result); | |
6193 | else | |
6194 | return result; | |
6195 | case INEXACT: | |
6196 | if (SCM_INEXACTP (result)) | |
6197 | return result; | |
6198 | else | |
6199 | return scm_exact_to_inexact (result); | |
6200 | case NO_EXACTNESS: | |
6201 | if (implicit_x == INEXACT) | |
6202 | { | |
6203 | if (SCM_INEXACTP (result)) | |
6204 | return result; | |
6205 | else | |
6206 | return scm_exact_to_inexact (result); | |
6207 | } | |
6208 | else | |
6209 | return result; | |
6210 | } | |
164d2481 | 6211 | |
9d427b2c | 6212 | /* We should never get here */ |
6f82b8f6 | 6213 | assert (0); |
3c9a524f | 6214 | } |
0f2d19dd | 6215 | |
0f2d19dd | 6216 | |
3c9a524f | 6217 | /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */ |
0f2d19dd | 6218 | |
3c9a524f | 6219 | static SCM |
3f47e526 | 6220 | mem2complex (SCM mem, unsigned int idx, |
9d427b2c | 6221 | unsigned int radix, enum t_exactness forced_x) |
3c9a524f | 6222 | { |
3f47e526 | 6223 | scm_t_wchar c; |
3c9a524f DH |
6224 | int sign = 0; |
6225 | SCM ureal; | |
3f47e526 | 6226 | size_t len = scm_i_string_length (mem); |
3c9a524f DH |
6227 | |
6228 | if (idx == len) | |
6229 | return SCM_BOOL_F; | |
6230 | ||
3f47e526 | 6231 | c = scm_i_string_ref (mem, idx); |
3c9a524f DH |
6232 | if (c == '+') |
6233 | { | |
6234 | idx++; | |
6235 | sign = 1; | |
6236 | } | |
6237 | else if (c == '-') | |
6238 | { | |
6239 | idx++; | |
6240 | sign = -1; | |
0f2d19dd | 6241 | } |
0f2d19dd | 6242 | |
3c9a524f DH |
6243 | if (idx == len) |
6244 | return SCM_BOOL_F; | |
6245 | ||
929d11b2 | 6246 | ureal = mem2ureal (mem, &idx, radix, forced_x, sign != 0); |
73e4de09 | 6247 | if (scm_is_false (ureal)) |
f872b822 | 6248 | { |
3c9a524f DH |
6249 | /* input must be either +i or -i */ |
6250 | ||
6251 | if (sign == 0) | |
6252 | return SCM_BOOL_F; | |
6253 | ||
3f47e526 MG |
6254 | if (scm_i_string_ref (mem, idx) == 'i' |
6255 | || scm_i_string_ref (mem, idx) == 'I') | |
f872b822 | 6256 | { |
3c9a524f DH |
6257 | idx++; |
6258 | if (idx != len) | |
6259 | return SCM_BOOL_F; | |
6260 | ||
cff5fa33 | 6261 | return scm_make_rectangular (SCM_INUM0, SCM_I_MAKINUM (sign)); |
f872b822 | 6262 | } |
3c9a524f DH |
6263 | else |
6264 | return SCM_BOOL_F; | |
0f2d19dd | 6265 | } |
3c9a524f DH |
6266 | else |
6267 | { | |
73e4de09 | 6268 | if (sign == -1 && scm_is_false (scm_nan_p (ureal))) |
3c9a524f | 6269 | ureal = scm_difference (ureal, SCM_UNDEFINED); |
f872b822 | 6270 | |
3c9a524f DH |
6271 | if (idx == len) |
6272 | return ureal; | |
6273 | ||
3f47e526 | 6274 | c = scm_i_string_ref (mem, idx); |
3c9a524f | 6275 | switch (c) |
f872b822 | 6276 | { |
3c9a524f DH |
6277 | case 'i': case 'I': |
6278 | /* either +<ureal>i or -<ureal>i */ | |
6279 | ||
6280 | idx++; | |
6281 | if (sign == 0) | |
6282 | return SCM_BOOL_F; | |
6283 | if (idx != len) | |
6284 | return SCM_BOOL_F; | |
cff5fa33 | 6285 | return scm_make_rectangular (SCM_INUM0, ureal); |
3c9a524f DH |
6286 | |
6287 | case '@': | |
6288 | /* polar input: <real>@<real>. */ | |
6289 | ||
6290 | idx++; | |
6291 | if (idx == len) | |
6292 | return SCM_BOOL_F; | |
6293 | else | |
f872b822 | 6294 | { |
3c9a524f DH |
6295 | int sign; |
6296 | SCM angle; | |
6297 | SCM result; | |
6298 | ||
3f47e526 | 6299 | c = scm_i_string_ref (mem, idx); |
3c9a524f DH |
6300 | if (c == '+') |
6301 | { | |
6302 | idx++; | |
ee0ddd21 AW |
6303 | if (idx == len) |
6304 | return SCM_BOOL_F; | |
3c9a524f DH |
6305 | sign = 1; |
6306 | } | |
6307 | else if (c == '-') | |
6308 | { | |
6309 | idx++; | |
ee0ddd21 AW |
6310 | if (idx == len) |
6311 | return SCM_BOOL_F; | |
3c9a524f DH |
6312 | sign = -1; |
6313 | } | |
6314 | else | |
929d11b2 | 6315 | sign = 0; |
3c9a524f | 6316 | |
929d11b2 | 6317 | angle = mem2ureal (mem, &idx, radix, forced_x, sign != 0); |
73e4de09 | 6318 | if (scm_is_false (angle)) |
3c9a524f DH |
6319 | return SCM_BOOL_F; |
6320 | if (idx != len) | |
6321 | return SCM_BOOL_F; | |
6322 | ||
73e4de09 | 6323 | if (sign == -1 && scm_is_false (scm_nan_p (ureal))) |
3c9a524f DH |
6324 | angle = scm_difference (angle, SCM_UNDEFINED); |
6325 | ||
6326 | result = scm_make_polar (ureal, angle); | |
6327 | return result; | |
f872b822 | 6328 | } |
3c9a524f DH |
6329 | case '+': |
6330 | case '-': | |
6331 | /* expecting input matching <real>[+-]<ureal>?i */ | |
0f2d19dd | 6332 | |
3c9a524f DH |
6333 | idx++; |
6334 | if (idx == len) | |
6335 | return SCM_BOOL_F; | |
6336 | else | |
6337 | { | |
6338 | int sign = (c == '+') ? 1 : -1; | |
929d11b2 | 6339 | SCM imag = mem2ureal (mem, &idx, radix, forced_x, sign != 0); |
0f2d19dd | 6340 | |
73e4de09 | 6341 | if (scm_is_false (imag)) |
d956fa6f | 6342 | imag = SCM_I_MAKINUM (sign); |
23295dc3 | 6343 | else if (sign == -1 && scm_is_false (scm_nan_p (imag))) |
1fe5e088 | 6344 | imag = scm_difference (imag, SCM_UNDEFINED); |
0f2d19dd | 6345 | |
3c9a524f DH |
6346 | if (idx == len) |
6347 | return SCM_BOOL_F; | |
3f47e526 MG |
6348 | if (scm_i_string_ref (mem, idx) != 'i' |
6349 | && scm_i_string_ref (mem, idx) != 'I') | |
3c9a524f | 6350 | return SCM_BOOL_F; |
0f2d19dd | 6351 | |
3c9a524f DH |
6352 | idx++; |
6353 | if (idx != len) | |
6354 | return SCM_BOOL_F; | |
0f2d19dd | 6355 | |
1fe5e088 | 6356 | return scm_make_rectangular (ureal, imag); |
3c9a524f DH |
6357 | } |
6358 | default: | |
6359 | return SCM_BOOL_F; | |
6360 | } | |
6361 | } | |
0f2d19dd | 6362 | } |
0f2d19dd JB |
6363 | |
6364 | ||
3c9a524f DH |
6365 | /* R5RS, section 7.1.1, lexical structure of numbers: <number> */ |
6366 | ||
6367 | enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16}; | |
1cc91f1b | 6368 | |
0f2d19dd | 6369 | SCM |
3f47e526 | 6370 | scm_i_string_to_number (SCM mem, unsigned int default_radix) |
0f2d19dd | 6371 | { |
3c9a524f DH |
6372 | unsigned int idx = 0; |
6373 | unsigned int radix = NO_RADIX; | |
6374 | enum t_exactness forced_x = NO_EXACTNESS; | |
3f47e526 | 6375 | size_t len = scm_i_string_length (mem); |
3c9a524f DH |
6376 | |
6377 | /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */ | |
3f47e526 | 6378 | while (idx + 2 < len && scm_i_string_ref (mem, idx) == '#') |
3c9a524f | 6379 | { |
3f47e526 | 6380 | switch (scm_i_string_ref (mem, idx + 1)) |
3c9a524f DH |
6381 | { |
6382 | case 'b': case 'B': | |
6383 | if (radix != NO_RADIX) | |
6384 | return SCM_BOOL_F; | |
6385 | radix = DUAL; | |
6386 | break; | |
6387 | case 'd': case 'D': | |
6388 | if (radix != NO_RADIX) | |
6389 | return SCM_BOOL_F; | |
6390 | radix = DEC; | |
6391 | break; | |
6392 | case 'i': case 'I': | |
6393 | if (forced_x != NO_EXACTNESS) | |
6394 | return SCM_BOOL_F; | |
6395 | forced_x = INEXACT; | |
6396 | break; | |
6397 | case 'e': case 'E': | |
6398 | if (forced_x != NO_EXACTNESS) | |
6399 | return SCM_BOOL_F; | |
6400 | forced_x = EXACT; | |
6401 | break; | |
6402 | case 'o': case 'O': | |
6403 | if (radix != NO_RADIX) | |
6404 | return SCM_BOOL_F; | |
6405 | radix = OCT; | |
6406 | break; | |
6407 | case 'x': case 'X': | |
6408 | if (radix != NO_RADIX) | |
6409 | return SCM_BOOL_F; | |
6410 | radix = HEX; | |
6411 | break; | |
6412 | default: | |
f872b822 | 6413 | return SCM_BOOL_F; |
3c9a524f DH |
6414 | } |
6415 | idx += 2; | |
6416 | } | |
6417 | ||
6418 | /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */ | |
6419 | if (radix == NO_RADIX) | |
9d427b2c | 6420 | radix = default_radix; |
f872b822 | 6421 | |
9d427b2c | 6422 | return mem2complex (mem, idx, radix, forced_x); |
0f2d19dd JB |
6423 | } |
6424 | ||
3f47e526 MG |
6425 | SCM |
6426 | scm_c_locale_stringn_to_number (const char* mem, size_t len, | |
6427 | unsigned int default_radix) | |
6428 | { | |
6429 | SCM str = scm_from_locale_stringn (mem, len); | |
6430 | ||
6431 | return scm_i_string_to_number (str, default_radix); | |
6432 | } | |
6433 | ||
0f2d19dd | 6434 | |
a1ec6916 | 6435 | SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0, |
bb628794 | 6436 | (SCM string, SCM radix), |
1e6808ea | 6437 | "Return a number of the maximally precise representation\n" |
942e5b91 | 6438 | "expressed by the given @var{string}. @var{radix} must be an\n" |
5352393c MG |
6439 | "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n" |
6440 | "is a default radix that may be overridden by an explicit radix\n" | |
6441 | "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n" | |
6442 | "supplied, then the default radix is 10. If string is not a\n" | |
6443 | "syntactically valid notation for a number, then\n" | |
6444 | "@code{string->number} returns @code{#f}.") | |
1bbd0b84 | 6445 | #define FUNC_NAME s_scm_string_to_number |
0f2d19dd JB |
6446 | { |
6447 | SCM answer; | |
5efd3c7d | 6448 | unsigned int base; |
a6d9e5ab | 6449 | SCM_VALIDATE_STRING (1, string); |
5efd3c7d MV |
6450 | |
6451 | if (SCM_UNBNDP (radix)) | |
6452 | base = 10; | |
6453 | else | |
6454 | base = scm_to_unsigned_integer (radix, 2, INT_MAX); | |
6455 | ||
3f47e526 | 6456 | answer = scm_i_string_to_number (string, base); |
8824ac88 MV |
6457 | scm_remember_upto_here_1 (string); |
6458 | return answer; | |
0f2d19dd | 6459 | } |
1bbd0b84 | 6460 | #undef FUNC_NAME |
3c9a524f DH |
6461 | |
6462 | ||
0f2d19dd JB |
6463 | /*** END strs->nums ***/ |
6464 | ||
5986c47d | 6465 | |
8507ec80 MV |
6466 | SCM_DEFINE (scm_number_p, "number?", 1, 0, 0, |
6467 | (SCM x), | |
6468 | "Return @code{#t} if @var{x} is a number, @code{#f}\n" | |
6469 | "otherwise.") | |
6470 | #define FUNC_NAME s_scm_number_p | |
6471 | { | |
6472 | return scm_from_bool (SCM_NUMBERP (x)); | |
6473 | } | |
6474 | #undef FUNC_NAME | |
6475 | ||
6476 | SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0, | |
1bbd0b84 | 6477 | (SCM x), |
942e5b91 | 6478 | "Return @code{#t} if @var{x} is a complex number, @code{#f}\n" |
bb2c02f2 | 6479 | "otherwise. Note that the sets of real, rational and integer\n" |
942e5b91 MG |
6480 | "values form subsets of the set of complex numbers, i. e. the\n" |
6481 | "predicate will also be fulfilled if @var{x} is a real,\n" | |
6482 | "rational or integer number.") | |
8507ec80 | 6483 | #define FUNC_NAME s_scm_complex_p |
0f2d19dd | 6484 | { |
8507ec80 MV |
6485 | /* all numbers are complex. */ |
6486 | return scm_number_p (x); | |
0f2d19dd | 6487 | } |
1bbd0b84 | 6488 | #undef FUNC_NAME |
0f2d19dd | 6489 | |
f92e85f7 MV |
6490 | SCM_DEFINE (scm_real_p, "real?", 1, 0, 0, |
6491 | (SCM x), | |
6492 | "Return @code{#t} if @var{x} is a real number, @code{#f}\n" | |
6493 | "otherwise. Note that the set of integer values forms a subset of\n" | |
6494 | "the set of real numbers, i. e. the predicate will also be\n" | |
6495 | "fulfilled if @var{x} is an integer number.") | |
6496 | #define FUNC_NAME s_scm_real_p | |
6497 | { | |
c960e556 MW |
6498 | return scm_from_bool |
6499 | (SCM_I_INUMP (x) || SCM_REALP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x)); | |
f92e85f7 MV |
6500 | } |
6501 | #undef FUNC_NAME | |
6502 | ||
6503 | SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0, | |
1bbd0b84 | 6504 | (SCM x), |
942e5b91 | 6505 | "Return @code{#t} if @var{x} is a rational number, @code{#f}\n" |
bb2c02f2 | 6506 | "otherwise. Note that the set of integer values forms a subset of\n" |
942e5b91 | 6507 | "the set of rational numbers, i. e. the predicate will also be\n" |
f92e85f7 MV |
6508 | "fulfilled if @var{x} is an integer number.") |
6509 | #define FUNC_NAME s_scm_rational_p | |
0f2d19dd | 6510 | { |
c960e556 | 6511 | if (SCM_I_INUMP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x)) |
f92e85f7 MV |
6512 | return SCM_BOOL_T; |
6513 | else if (SCM_REALP (x)) | |
c960e556 MW |
6514 | /* due to their limited precision, finite floating point numbers are |
6515 | rational as well. (finite means neither infinity nor a NaN) */ | |
19374ad2 | 6516 | return scm_from_bool (isfinite (SCM_REAL_VALUE (x))); |
0aacf84e | 6517 | else |
bb628794 | 6518 | return SCM_BOOL_F; |
0f2d19dd | 6519 | } |
1bbd0b84 | 6520 | #undef FUNC_NAME |
0f2d19dd | 6521 | |
a1ec6916 | 6522 | SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0, |
1bbd0b84 | 6523 | (SCM x), |
900a897c MW |
6524 | "Return @code{#t} if @var{x} is an integer number,\n" |
6525 | "else return @code{#f}.") | |
1bbd0b84 | 6526 | #define FUNC_NAME s_scm_integer_p |
0f2d19dd | 6527 | { |
c960e556 | 6528 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
f872b822 | 6529 | return SCM_BOOL_T; |
c960e556 MW |
6530 | else if (SCM_REALP (x)) |
6531 | { | |
6532 | double val = SCM_REAL_VALUE (x); | |
6533 | return scm_from_bool (!isinf (val) && (val == floor (val))); | |
6534 | } | |
6535 | else | |
8e43ed5d | 6536 | return SCM_BOOL_F; |
0f2d19dd | 6537 | } |
1bbd0b84 | 6538 | #undef FUNC_NAME |
0f2d19dd | 6539 | |
900a897c MW |
6540 | SCM_DEFINE (scm_exact_integer_p, "exact-integer?", 1, 0, 0, |
6541 | (SCM x), | |
6542 | "Return @code{#t} if @var{x} is an exact integer number,\n" | |
6543 | "else return @code{#f}.") | |
6544 | #define FUNC_NAME s_scm_exact_integer_p | |
6545 | { | |
6546 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) | |
6547 | return SCM_BOOL_T; | |
6548 | else | |
6549 | return SCM_BOOL_F; | |
6550 | } | |
6551 | #undef FUNC_NAME | |
6552 | ||
0f2d19dd | 6553 | |
8a1f4f98 AW |
6554 | SCM scm_i_num_eq_p (SCM, SCM, SCM); |
6555 | SCM_PRIMITIVE_GENERIC (scm_i_num_eq_p, "=", 0, 2, 1, | |
6556 | (SCM x, SCM y, SCM rest), | |
6557 | "Return @code{#t} if all parameters are numerically equal.") | |
6558 | #define FUNC_NAME s_scm_i_num_eq_p | |
6559 | { | |
6560 | if (SCM_UNBNDP (x) || SCM_UNBNDP (y)) | |
6561 | return SCM_BOOL_T; | |
6562 | while (!scm_is_null (rest)) | |
6563 | { | |
6564 | if (scm_is_false (scm_num_eq_p (x, y))) | |
6565 | return SCM_BOOL_F; | |
6566 | x = y; | |
6567 | y = scm_car (rest); | |
6568 | rest = scm_cdr (rest); | |
6569 | } | |
6570 | return scm_num_eq_p (x, y); | |
6571 | } | |
6572 | #undef FUNC_NAME | |
0f2d19dd | 6573 | SCM |
6e8d25a6 | 6574 | scm_num_eq_p (SCM x, SCM y) |
0f2d19dd | 6575 | { |
d8b95e27 | 6576 | again: |
e11e83f3 | 6577 | if (SCM_I_INUMP (x)) |
0aacf84e | 6578 | { |
e25f3727 | 6579 | scm_t_signed_bits xx = SCM_I_INUM (x); |
e11e83f3 | 6580 | if (SCM_I_INUMP (y)) |
0aacf84e | 6581 | { |
e25f3727 | 6582 | scm_t_signed_bits yy = SCM_I_INUM (y); |
73e4de09 | 6583 | return scm_from_bool (xx == yy); |
0aacf84e MD |
6584 | } |
6585 | else if (SCM_BIGP (y)) | |
6586 | return SCM_BOOL_F; | |
6587 | else if (SCM_REALP (y)) | |
e8c5b1f2 KR |
6588 | { |
6589 | /* On a 32-bit system an inum fits a double, we can cast the inum | |
6590 | to a double and compare. | |
6591 | ||
6592 | But on a 64-bit system an inum is bigger than a double and | |
01329288 MW |
6593 | casting it to a double (call that dxx) will round. |
6594 | Although dxx will not in general be equal to xx, dxx will | |
6595 | always be an integer and within a factor of 2 of xx, so if | |
6596 | dxx==yy, we know that yy is an integer and fits in | |
6597 | scm_t_signed_bits. So we cast yy to scm_t_signed_bits and | |
e8c5b1f2 KR |
6598 | compare with plain xx. |
6599 | ||
6600 | An alternative (for any size system actually) would be to check | |
6601 | yy is an integer (with floor) and is in range of an inum | |
6602 | (compare against appropriate powers of 2) then test | |
e25f3727 AW |
6603 | xx==(scm_t_signed_bits)yy. It's just a matter of which |
6604 | casts/comparisons might be fastest or easiest for the cpu. */ | |
e8c5b1f2 KR |
6605 | |
6606 | double yy = SCM_REAL_VALUE (y); | |
3a1b45fd MV |
6607 | return scm_from_bool ((double) xx == yy |
6608 | && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1 | |
e25f3727 | 6609 | || xx == (scm_t_signed_bits) yy)); |
e8c5b1f2 | 6610 | } |
0aacf84e | 6611 | else if (SCM_COMPLEXP (y)) |
01329288 MW |
6612 | { |
6613 | /* see comments with inum/real above */ | |
6614 | double ry = SCM_COMPLEX_REAL (y); | |
6615 | return scm_from_bool ((double) xx == ry | |
6616 | && 0.0 == SCM_COMPLEX_IMAG (y) | |
6617 | && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1 | |
6618 | || xx == (scm_t_signed_bits) ry)); | |
6619 | } | |
f92e85f7 MV |
6620 | else if (SCM_FRACTIONP (y)) |
6621 | return SCM_BOOL_F; | |
0aacf84e | 6622 | else |
8a1f4f98 | 6623 | SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p); |
f872b822 | 6624 | } |
0aacf84e MD |
6625 | else if (SCM_BIGP (x)) |
6626 | { | |
e11e83f3 | 6627 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
6628 | return SCM_BOOL_F; |
6629 | else if (SCM_BIGP (y)) | |
6630 | { | |
6631 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
6632 | scm_remember_upto_here_2 (x, y); | |
73e4de09 | 6633 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
6634 | } |
6635 | else if (SCM_REALP (y)) | |
6636 | { | |
6637 | int cmp; | |
2e65b52f | 6638 | if (isnan (SCM_REAL_VALUE (y))) |
0aacf84e MD |
6639 | return SCM_BOOL_F; |
6640 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y)); | |
6641 | scm_remember_upto_here_1 (x); | |
73e4de09 | 6642 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
6643 | } |
6644 | else if (SCM_COMPLEXP (y)) | |
6645 | { | |
6646 | int cmp; | |
6647 | if (0.0 != SCM_COMPLEX_IMAG (y)) | |
6648 | return SCM_BOOL_F; | |
2e65b52f | 6649 | if (isnan (SCM_COMPLEX_REAL (y))) |
0aacf84e MD |
6650 | return SCM_BOOL_F; |
6651 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y)); | |
6652 | scm_remember_upto_here_1 (x); | |
73e4de09 | 6653 | return scm_from_bool (0 == cmp); |
0aacf84e | 6654 | } |
f92e85f7 MV |
6655 | else if (SCM_FRACTIONP (y)) |
6656 | return SCM_BOOL_F; | |
0aacf84e | 6657 | else |
8a1f4f98 | 6658 | SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p); |
f4c627b3 | 6659 | } |
0aacf84e MD |
6660 | else if (SCM_REALP (x)) |
6661 | { | |
e8c5b1f2 | 6662 | double xx = SCM_REAL_VALUE (x); |
e11e83f3 | 6663 | if (SCM_I_INUMP (y)) |
e8c5b1f2 KR |
6664 | { |
6665 | /* see comments with inum/real above */ | |
e25f3727 | 6666 | scm_t_signed_bits yy = SCM_I_INUM (y); |
3a1b45fd MV |
6667 | return scm_from_bool (xx == (double) yy |
6668 | && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1 | |
e25f3727 | 6669 | || (scm_t_signed_bits) xx == yy)); |
e8c5b1f2 | 6670 | } |
0aacf84e MD |
6671 | else if (SCM_BIGP (y)) |
6672 | { | |
6673 | int cmp; | |
01329288 | 6674 | if (isnan (xx)) |
0aacf84e | 6675 | return SCM_BOOL_F; |
01329288 | 6676 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), xx); |
0aacf84e | 6677 | scm_remember_upto_here_1 (y); |
73e4de09 | 6678 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
6679 | } |
6680 | else if (SCM_REALP (y)) | |
01329288 | 6681 | return scm_from_bool (xx == SCM_REAL_VALUE (y)); |
0aacf84e | 6682 | else if (SCM_COMPLEXP (y)) |
01329288 MW |
6683 | return scm_from_bool ((xx == SCM_COMPLEX_REAL (y)) |
6684 | && (0.0 == SCM_COMPLEX_IMAG (y))); | |
f92e85f7 | 6685 | else if (SCM_FRACTIONP (y)) |
d8b95e27 | 6686 | { |
01329288 | 6687 | if (isnan (xx) || isinf (xx)) |
d8b95e27 | 6688 | return SCM_BOOL_F; |
d8b95e27 KR |
6689 | x = scm_inexact_to_exact (x); /* with x as frac or int */ |
6690 | goto again; | |
6691 | } | |
0aacf84e | 6692 | else |
8a1f4f98 | 6693 | SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p); |
f872b822 | 6694 | } |
0aacf84e MD |
6695 | else if (SCM_COMPLEXP (x)) |
6696 | { | |
e11e83f3 | 6697 | if (SCM_I_INUMP (y)) |
01329288 MW |
6698 | { |
6699 | /* see comments with inum/real above */ | |
6700 | double rx = SCM_COMPLEX_REAL (x); | |
6701 | scm_t_signed_bits yy = SCM_I_INUM (y); | |
6702 | return scm_from_bool (rx == (double) yy | |
6703 | && 0.0 == SCM_COMPLEX_IMAG (x) | |
6704 | && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1 | |
6705 | || (scm_t_signed_bits) rx == yy)); | |
6706 | } | |
0aacf84e MD |
6707 | else if (SCM_BIGP (y)) |
6708 | { | |
6709 | int cmp; | |
6710 | if (0.0 != SCM_COMPLEX_IMAG (x)) | |
6711 | return SCM_BOOL_F; | |
2e65b52f | 6712 | if (isnan (SCM_COMPLEX_REAL (x))) |
0aacf84e MD |
6713 | return SCM_BOOL_F; |
6714 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x)); | |
6715 | scm_remember_upto_here_1 (y); | |
73e4de09 | 6716 | return scm_from_bool (0 == cmp); |
0aacf84e MD |
6717 | } |
6718 | else if (SCM_REALP (y)) | |
73e4de09 | 6719 | return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y)) |
01329288 | 6720 | && (SCM_COMPLEX_IMAG (x) == 0.0)); |
0aacf84e | 6721 | else if (SCM_COMPLEXP (y)) |
73e4de09 | 6722 | return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)) |
01329288 | 6723 | && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y))); |
f92e85f7 | 6724 | else if (SCM_FRACTIONP (y)) |
d8b95e27 KR |
6725 | { |
6726 | double xx; | |
6727 | if (SCM_COMPLEX_IMAG (x) != 0.0) | |
6728 | return SCM_BOOL_F; | |
6729 | xx = SCM_COMPLEX_REAL (x); | |
01329288 | 6730 | if (isnan (xx) || isinf (xx)) |
d8b95e27 | 6731 | return SCM_BOOL_F; |
d8b95e27 KR |
6732 | x = scm_inexact_to_exact (x); /* with x as frac or int */ |
6733 | goto again; | |
6734 | } | |
f92e85f7 | 6735 | else |
8a1f4f98 | 6736 | SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p); |
f92e85f7 MV |
6737 | } |
6738 | else if (SCM_FRACTIONP (x)) | |
6739 | { | |
e11e83f3 | 6740 | if (SCM_I_INUMP (y)) |
f92e85f7 MV |
6741 | return SCM_BOOL_F; |
6742 | else if (SCM_BIGP (y)) | |
6743 | return SCM_BOOL_F; | |
6744 | else if (SCM_REALP (y)) | |
d8b95e27 KR |
6745 | { |
6746 | double yy = SCM_REAL_VALUE (y); | |
01329288 | 6747 | if (isnan (yy) || isinf (yy)) |
d8b95e27 | 6748 | return SCM_BOOL_F; |
d8b95e27 KR |
6749 | y = scm_inexact_to_exact (y); /* with y as frac or int */ |
6750 | goto again; | |
6751 | } | |
f92e85f7 | 6752 | else if (SCM_COMPLEXP (y)) |
d8b95e27 KR |
6753 | { |
6754 | double yy; | |
6755 | if (SCM_COMPLEX_IMAG (y) != 0.0) | |
6756 | return SCM_BOOL_F; | |
6757 | yy = SCM_COMPLEX_REAL (y); | |
01329288 | 6758 | if (isnan (yy) || isinf(yy)) |
d8b95e27 | 6759 | return SCM_BOOL_F; |
d8b95e27 KR |
6760 | y = scm_inexact_to_exact (y); /* with y as frac or int */ |
6761 | goto again; | |
6762 | } | |
f92e85f7 MV |
6763 | else if (SCM_FRACTIONP (y)) |
6764 | return scm_i_fraction_equalp (x, y); | |
0aacf84e | 6765 | else |
8a1f4f98 | 6766 | SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p); |
f4c627b3 | 6767 | } |
0aacf84e | 6768 | else |
8a1f4f98 | 6769 | SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARG1, s_scm_i_num_eq_p); |
0f2d19dd JB |
6770 | } |
6771 | ||
6772 | ||
a5f0b599 KR |
6773 | /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications |
6774 | done are good for inums, but for bignums an answer can almost always be | |
6775 | had by just examining a few high bits of the operands, as done by GMP in | |
6776 | mpq_cmp. flonum/frac compares likewise, but with the slight complication | |
6777 | of the float exponent to take into account. */ | |
6778 | ||
8c93b597 | 6779 | SCM_INTERNAL SCM scm_i_num_less_p (SCM, SCM, SCM); |
8a1f4f98 AW |
6780 | SCM_PRIMITIVE_GENERIC (scm_i_num_less_p, "<", 0, 2, 1, |
6781 | (SCM x, SCM y, SCM rest), | |
6782 | "Return @code{#t} if the list of parameters is monotonically\n" | |
6783 | "increasing.") | |
6784 | #define FUNC_NAME s_scm_i_num_less_p | |
6785 | { | |
6786 | if (SCM_UNBNDP (x) || SCM_UNBNDP (y)) | |
6787 | return SCM_BOOL_T; | |
6788 | while (!scm_is_null (rest)) | |
6789 | { | |
6790 | if (scm_is_false (scm_less_p (x, y))) | |
6791 | return SCM_BOOL_F; | |
6792 | x = y; | |
6793 | y = scm_car (rest); | |
6794 | rest = scm_cdr (rest); | |
6795 | } | |
6796 | return scm_less_p (x, y); | |
6797 | } | |
6798 | #undef FUNC_NAME | |
0f2d19dd | 6799 | SCM |
6e8d25a6 | 6800 | scm_less_p (SCM x, SCM y) |
0f2d19dd | 6801 | { |
a5f0b599 | 6802 | again: |
e11e83f3 | 6803 | if (SCM_I_INUMP (x)) |
0aacf84e | 6804 | { |
e25f3727 | 6805 | scm_t_inum xx = SCM_I_INUM (x); |
e11e83f3 | 6806 | if (SCM_I_INUMP (y)) |
0aacf84e | 6807 | { |
e25f3727 | 6808 | scm_t_inum yy = SCM_I_INUM (y); |
73e4de09 | 6809 | return scm_from_bool (xx < yy); |
0aacf84e MD |
6810 | } |
6811 | else if (SCM_BIGP (y)) | |
6812 | { | |
6813 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
6814 | scm_remember_upto_here_1 (y); | |
73e4de09 | 6815 | return scm_from_bool (sgn > 0); |
0aacf84e MD |
6816 | } |
6817 | else if (SCM_REALP (y)) | |
95ed2217 MW |
6818 | { |
6819 | /* We can safely take the ceiling of y without changing the | |
6820 | result of x<y, given that x is an integer. */ | |
6821 | double yy = ceil (SCM_REAL_VALUE (y)); | |
6822 | ||
6823 | /* In the following comparisons, it's important that the right | |
6824 | hand side always be a power of 2, so that it can be | |
6825 | losslessly converted to a double even on 64-bit | |
6826 | machines. */ | |
6827 | if (yy >= (double) (SCM_MOST_POSITIVE_FIXNUM+1)) | |
6828 | return SCM_BOOL_T; | |
6829 | else if (!(yy > (double) SCM_MOST_NEGATIVE_FIXNUM)) | |
6830 | /* The condition above is carefully written to include the | |
6831 | case where yy==NaN. */ | |
6832 | return SCM_BOOL_F; | |
6833 | else | |
6834 | /* yy is a finite integer that fits in an inum. */ | |
6835 | return scm_from_bool (xx < (scm_t_inum) yy); | |
6836 | } | |
f92e85f7 | 6837 | else if (SCM_FRACTIONP (y)) |
a5f0b599 KR |
6838 | { |
6839 | /* "x < a/b" becomes "x*b < a" */ | |
6840 | int_frac: | |
6841 | x = scm_product (x, SCM_FRACTION_DENOMINATOR (y)); | |
6842 | y = SCM_FRACTION_NUMERATOR (y); | |
6843 | goto again; | |
6844 | } | |
0aacf84e | 6845 | else |
8a1f4f98 | 6846 | SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p); |
f872b822 | 6847 | } |
0aacf84e MD |
6848 | else if (SCM_BIGP (x)) |
6849 | { | |
e11e83f3 | 6850 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
6851 | { |
6852 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
6853 | scm_remember_upto_here_1 (x); | |
73e4de09 | 6854 | return scm_from_bool (sgn < 0); |
0aacf84e MD |
6855 | } |
6856 | else if (SCM_BIGP (y)) | |
6857 | { | |
6858 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
6859 | scm_remember_upto_here_2 (x, y); | |
73e4de09 | 6860 | return scm_from_bool (cmp < 0); |
0aacf84e MD |
6861 | } |
6862 | else if (SCM_REALP (y)) | |
6863 | { | |
6864 | int cmp; | |
2e65b52f | 6865 | if (isnan (SCM_REAL_VALUE (y))) |
0aacf84e MD |
6866 | return SCM_BOOL_F; |
6867 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y)); | |
6868 | scm_remember_upto_here_1 (x); | |
73e4de09 | 6869 | return scm_from_bool (cmp < 0); |
0aacf84e | 6870 | } |
f92e85f7 | 6871 | else if (SCM_FRACTIONP (y)) |
a5f0b599 | 6872 | goto int_frac; |
0aacf84e | 6873 | else |
8a1f4f98 | 6874 | SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p); |
f4c627b3 | 6875 | } |
0aacf84e MD |
6876 | else if (SCM_REALP (x)) |
6877 | { | |
e11e83f3 | 6878 | if (SCM_I_INUMP (y)) |
95ed2217 MW |
6879 | { |
6880 | /* We can safely take the floor of x without changing the | |
6881 | result of x<y, given that y is an integer. */ | |
6882 | double xx = floor (SCM_REAL_VALUE (x)); | |
6883 | ||
6884 | /* In the following comparisons, it's important that the right | |
6885 | hand side always be a power of 2, so that it can be | |
6886 | losslessly converted to a double even on 64-bit | |
6887 | machines. */ | |
6888 | if (xx < (double) SCM_MOST_NEGATIVE_FIXNUM) | |
6889 | return SCM_BOOL_T; | |
6890 | else if (!(xx < (double) (SCM_MOST_POSITIVE_FIXNUM+1))) | |
6891 | /* The condition above is carefully written to include the | |
6892 | case where xx==NaN. */ | |
6893 | return SCM_BOOL_F; | |
6894 | else | |
6895 | /* xx is a finite integer that fits in an inum. */ | |
6896 | return scm_from_bool ((scm_t_inum) xx < SCM_I_INUM (y)); | |
6897 | } | |
0aacf84e MD |
6898 | else if (SCM_BIGP (y)) |
6899 | { | |
6900 | int cmp; | |
2e65b52f | 6901 | if (isnan (SCM_REAL_VALUE (x))) |
0aacf84e MD |
6902 | return SCM_BOOL_F; |
6903 | cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x)); | |
6904 | scm_remember_upto_here_1 (y); | |
73e4de09 | 6905 | return scm_from_bool (cmp > 0); |
0aacf84e MD |
6906 | } |
6907 | else if (SCM_REALP (y)) | |
73e4de09 | 6908 | return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y)); |
f92e85f7 | 6909 | else if (SCM_FRACTIONP (y)) |
a5f0b599 KR |
6910 | { |
6911 | double xx = SCM_REAL_VALUE (x); | |
2e65b52f | 6912 | if (isnan (xx)) |
a5f0b599 | 6913 | return SCM_BOOL_F; |
2e65b52f | 6914 | if (isinf (xx)) |
73e4de09 | 6915 | return scm_from_bool (xx < 0.0); |
a5f0b599 KR |
6916 | x = scm_inexact_to_exact (x); /* with x as frac or int */ |
6917 | goto again; | |
6918 | } | |
f92e85f7 | 6919 | else |
8a1f4f98 | 6920 | SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p); |
f92e85f7 MV |
6921 | } |
6922 | else if (SCM_FRACTIONP (x)) | |
6923 | { | |
e11e83f3 | 6924 | if (SCM_I_INUMP (y) || SCM_BIGP (y)) |
a5f0b599 KR |
6925 | { |
6926 | /* "a/b < y" becomes "a < y*b" */ | |
6927 | y = scm_product (y, SCM_FRACTION_DENOMINATOR (x)); | |
6928 | x = SCM_FRACTION_NUMERATOR (x); | |
6929 | goto again; | |
6930 | } | |
f92e85f7 | 6931 | else if (SCM_REALP (y)) |
a5f0b599 KR |
6932 | { |
6933 | double yy = SCM_REAL_VALUE (y); | |
2e65b52f | 6934 | if (isnan (yy)) |
a5f0b599 | 6935 | return SCM_BOOL_F; |
2e65b52f | 6936 | if (isinf (yy)) |
73e4de09 | 6937 | return scm_from_bool (0.0 < yy); |
a5f0b599 KR |
6938 | y = scm_inexact_to_exact (y); /* with y as frac or int */ |
6939 | goto again; | |
6940 | } | |
f92e85f7 | 6941 | else if (SCM_FRACTIONP (y)) |
a5f0b599 KR |
6942 | { |
6943 | /* "a/b < c/d" becomes "a*d < c*b" */ | |
6944 | SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x), | |
6945 | SCM_FRACTION_DENOMINATOR (y)); | |
6946 | SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y), | |
6947 | SCM_FRACTION_DENOMINATOR (x)); | |
6948 | x = new_x; | |
6949 | y = new_y; | |
6950 | goto again; | |
6951 | } | |
0aacf84e | 6952 | else |
8a1f4f98 | 6953 | SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p); |
f872b822 | 6954 | } |
0aacf84e | 6955 | else |
8a1f4f98 | 6956 | SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARG1, s_scm_i_num_less_p); |
0f2d19dd JB |
6957 | } |
6958 | ||
6959 | ||
8a1f4f98 AW |
6960 | SCM scm_i_num_gr_p (SCM, SCM, SCM); |
6961 | SCM_PRIMITIVE_GENERIC (scm_i_num_gr_p, ">", 0, 2, 1, | |
6962 | (SCM x, SCM y, SCM rest), | |
6963 | "Return @code{#t} if the list of parameters is monotonically\n" | |
6964 | "decreasing.") | |
6965 | #define FUNC_NAME s_scm_i_num_gr_p | |
6966 | { | |
6967 | if (SCM_UNBNDP (x) || SCM_UNBNDP (y)) | |
6968 | return SCM_BOOL_T; | |
6969 | while (!scm_is_null (rest)) | |
6970 | { | |
6971 | if (scm_is_false (scm_gr_p (x, y))) | |
6972 | return SCM_BOOL_F; | |
6973 | x = y; | |
6974 | y = scm_car (rest); | |
6975 | rest = scm_cdr (rest); | |
6976 | } | |
6977 | return scm_gr_p (x, y); | |
6978 | } | |
6979 | #undef FUNC_NAME | |
6980 | #define FUNC_NAME s_scm_i_num_gr_p | |
c76b1eaf MD |
6981 | SCM |
6982 | scm_gr_p (SCM x, SCM y) | |
0f2d19dd | 6983 | { |
c76b1eaf | 6984 | if (!SCM_NUMBERP (x)) |
8a1f4f98 | 6985 | SCM_WTA_DISPATCH_2 (g_scm_i_num_gr_p, x, y, SCM_ARG1, FUNC_NAME); |
c76b1eaf | 6986 | else if (!SCM_NUMBERP (y)) |
8a1f4f98 | 6987 | SCM_WTA_DISPATCH_2 (g_scm_i_num_gr_p, x, y, SCM_ARG2, FUNC_NAME); |
c76b1eaf MD |
6988 | else |
6989 | return scm_less_p (y, x); | |
0f2d19dd | 6990 | } |
1bbd0b84 | 6991 | #undef FUNC_NAME |
0f2d19dd JB |
6992 | |
6993 | ||
8a1f4f98 AW |
6994 | SCM scm_i_num_leq_p (SCM, SCM, SCM); |
6995 | SCM_PRIMITIVE_GENERIC (scm_i_num_leq_p, "<=", 0, 2, 1, | |
6996 | (SCM x, SCM y, SCM rest), | |
6997 | "Return @code{#t} if the list of parameters is monotonically\n" | |
6998 | "non-decreasing.") | |
6999 | #define FUNC_NAME s_scm_i_num_leq_p | |
7000 | { | |
7001 | if (SCM_UNBNDP (x) || SCM_UNBNDP (y)) | |
7002 | return SCM_BOOL_T; | |
7003 | while (!scm_is_null (rest)) | |
7004 | { | |
7005 | if (scm_is_false (scm_leq_p (x, y))) | |
7006 | return SCM_BOOL_F; | |
7007 | x = y; | |
7008 | y = scm_car (rest); | |
7009 | rest = scm_cdr (rest); | |
7010 | } | |
7011 | return scm_leq_p (x, y); | |
7012 | } | |
7013 | #undef FUNC_NAME | |
7014 | #define FUNC_NAME s_scm_i_num_leq_p | |
c76b1eaf MD |
7015 | SCM |
7016 | scm_leq_p (SCM x, SCM y) | |
0f2d19dd | 7017 | { |
c76b1eaf | 7018 | if (!SCM_NUMBERP (x)) |
8a1f4f98 | 7019 | SCM_WTA_DISPATCH_2 (g_scm_i_num_leq_p, x, y, SCM_ARG1, FUNC_NAME); |
c76b1eaf | 7020 | else if (!SCM_NUMBERP (y)) |
8a1f4f98 | 7021 | SCM_WTA_DISPATCH_2 (g_scm_i_num_leq_p, x, y, SCM_ARG2, FUNC_NAME); |
73e4de09 | 7022 | else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y))) |
fc194577 | 7023 | return SCM_BOOL_F; |
c76b1eaf | 7024 | else |
73e4de09 | 7025 | return scm_not (scm_less_p (y, x)); |
0f2d19dd | 7026 | } |
1bbd0b84 | 7027 | #undef FUNC_NAME |
0f2d19dd JB |
7028 | |
7029 | ||
8a1f4f98 AW |
7030 | SCM scm_i_num_geq_p (SCM, SCM, SCM); |
7031 | SCM_PRIMITIVE_GENERIC (scm_i_num_geq_p, ">=", 0, 2, 1, | |
7032 | (SCM x, SCM y, SCM rest), | |
7033 | "Return @code{#t} if the list of parameters is monotonically\n" | |
7034 | "non-increasing.") | |
7035 | #define FUNC_NAME s_scm_i_num_geq_p | |
7036 | { | |
7037 | if (SCM_UNBNDP (x) || SCM_UNBNDP (y)) | |
7038 | return SCM_BOOL_T; | |
7039 | while (!scm_is_null (rest)) | |
7040 | { | |
7041 | if (scm_is_false (scm_geq_p (x, y))) | |
7042 | return SCM_BOOL_F; | |
7043 | x = y; | |
7044 | y = scm_car (rest); | |
7045 | rest = scm_cdr (rest); | |
7046 | } | |
7047 | return scm_geq_p (x, y); | |
7048 | } | |
7049 | #undef FUNC_NAME | |
7050 | #define FUNC_NAME s_scm_i_num_geq_p | |
c76b1eaf MD |
7051 | SCM |
7052 | scm_geq_p (SCM x, SCM y) | |
0f2d19dd | 7053 | { |
c76b1eaf | 7054 | if (!SCM_NUMBERP (x)) |
8a1f4f98 | 7055 | SCM_WTA_DISPATCH_2 (g_scm_i_num_geq_p, x, y, SCM_ARG1, FUNC_NAME); |
c76b1eaf | 7056 | else if (!SCM_NUMBERP (y)) |
8a1f4f98 | 7057 | SCM_WTA_DISPATCH_2 (g_scm_i_num_geq_p, x, y, SCM_ARG2, FUNC_NAME); |
73e4de09 | 7058 | else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y))) |
fc194577 | 7059 | return SCM_BOOL_F; |
c76b1eaf | 7060 | else |
73e4de09 | 7061 | return scm_not (scm_less_p (x, y)); |
0f2d19dd | 7062 | } |
1bbd0b84 | 7063 | #undef FUNC_NAME |
0f2d19dd JB |
7064 | |
7065 | ||
2519490c MW |
7066 | SCM_PRIMITIVE_GENERIC (scm_zero_p, "zero?", 1, 0, 0, |
7067 | (SCM z), | |
7068 | "Return @code{#t} if @var{z} is an exact or inexact number equal to\n" | |
7069 | "zero.") | |
7070 | #define FUNC_NAME s_scm_zero_p | |
0f2d19dd | 7071 | { |
e11e83f3 | 7072 | if (SCM_I_INUMP (z)) |
bc36d050 | 7073 | return scm_from_bool (scm_is_eq (z, SCM_INUM0)); |
0aacf84e | 7074 | else if (SCM_BIGP (z)) |
c2ff8ab0 | 7075 | return SCM_BOOL_F; |
0aacf84e | 7076 | else if (SCM_REALP (z)) |
73e4de09 | 7077 | return scm_from_bool (SCM_REAL_VALUE (z) == 0.0); |
0aacf84e | 7078 | else if (SCM_COMPLEXP (z)) |
73e4de09 | 7079 | return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0 |
c2ff8ab0 | 7080 | && SCM_COMPLEX_IMAG (z) == 0.0); |
f92e85f7 MV |
7081 | else if (SCM_FRACTIONP (z)) |
7082 | return SCM_BOOL_F; | |
0aacf84e | 7083 | else |
2519490c | 7084 | SCM_WTA_DISPATCH_1 (g_scm_zero_p, z, SCM_ARG1, s_scm_zero_p); |
0f2d19dd | 7085 | } |
2519490c | 7086 | #undef FUNC_NAME |
0f2d19dd JB |
7087 | |
7088 | ||
2519490c MW |
7089 | SCM_PRIMITIVE_GENERIC (scm_positive_p, "positive?", 1, 0, 0, |
7090 | (SCM x), | |
7091 | "Return @code{#t} if @var{x} is an exact or inexact number greater than\n" | |
7092 | "zero.") | |
7093 | #define FUNC_NAME s_scm_positive_p | |
0f2d19dd | 7094 | { |
e11e83f3 MV |
7095 | if (SCM_I_INUMP (x)) |
7096 | return scm_from_bool (SCM_I_INUM (x) > 0); | |
0aacf84e MD |
7097 | else if (SCM_BIGP (x)) |
7098 | { | |
7099 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
7100 | scm_remember_upto_here_1 (x); | |
73e4de09 | 7101 | return scm_from_bool (sgn > 0); |
0aacf84e MD |
7102 | } |
7103 | else if (SCM_REALP (x)) | |
73e4de09 | 7104 | return scm_from_bool(SCM_REAL_VALUE (x) > 0.0); |
f92e85f7 MV |
7105 | else if (SCM_FRACTIONP (x)) |
7106 | return scm_positive_p (SCM_FRACTION_NUMERATOR (x)); | |
0aacf84e | 7107 | else |
2519490c | 7108 | SCM_WTA_DISPATCH_1 (g_scm_positive_p, x, SCM_ARG1, s_scm_positive_p); |
0f2d19dd | 7109 | } |
2519490c | 7110 | #undef FUNC_NAME |
0f2d19dd JB |
7111 | |
7112 | ||
2519490c MW |
7113 | SCM_PRIMITIVE_GENERIC (scm_negative_p, "negative?", 1, 0, 0, |
7114 | (SCM x), | |
7115 | "Return @code{#t} if @var{x} is an exact or inexact number less than\n" | |
7116 | "zero.") | |
7117 | #define FUNC_NAME s_scm_negative_p | |
0f2d19dd | 7118 | { |
e11e83f3 MV |
7119 | if (SCM_I_INUMP (x)) |
7120 | return scm_from_bool (SCM_I_INUM (x) < 0); | |
0aacf84e MD |
7121 | else if (SCM_BIGP (x)) |
7122 | { | |
7123 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
7124 | scm_remember_upto_here_1 (x); | |
73e4de09 | 7125 | return scm_from_bool (sgn < 0); |
0aacf84e MD |
7126 | } |
7127 | else if (SCM_REALP (x)) | |
73e4de09 | 7128 | return scm_from_bool(SCM_REAL_VALUE (x) < 0.0); |
f92e85f7 MV |
7129 | else if (SCM_FRACTIONP (x)) |
7130 | return scm_negative_p (SCM_FRACTION_NUMERATOR (x)); | |
0aacf84e | 7131 | else |
2519490c | 7132 | SCM_WTA_DISPATCH_1 (g_scm_negative_p, x, SCM_ARG1, s_scm_negative_p); |
0f2d19dd | 7133 | } |
2519490c | 7134 | #undef FUNC_NAME |
0f2d19dd JB |
7135 | |
7136 | ||
2a06f791 KR |
7137 | /* scm_min and scm_max return an inexact when either argument is inexact, as |
7138 | required by r5rs. On that basis, for exact/inexact combinations the | |
7139 | exact is converted to inexact to compare and possibly return. This is | |
7140 | unlike scm_less_p above which takes some trouble to preserve all bits in | |
7141 | its test, such trouble is not required for min and max. */ | |
7142 | ||
78d3deb1 AW |
7143 | SCM_PRIMITIVE_GENERIC (scm_i_max, "max", 0, 2, 1, |
7144 | (SCM x, SCM y, SCM rest), | |
7145 | "Return the maximum of all parameter values.") | |
7146 | #define FUNC_NAME s_scm_i_max | |
7147 | { | |
7148 | while (!scm_is_null (rest)) | |
7149 | { x = scm_max (x, y); | |
7150 | y = scm_car (rest); | |
7151 | rest = scm_cdr (rest); | |
7152 | } | |
7153 | return scm_max (x, y); | |
7154 | } | |
7155 | #undef FUNC_NAME | |
7156 | ||
7157 | #define s_max s_scm_i_max | |
7158 | #define g_max g_scm_i_max | |
7159 | ||
0f2d19dd | 7160 | SCM |
6e8d25a6 | 7161 | scm_max (SCM x, SCM y) |
0f2d19dd | 7162 | { |
0aacf84e MD |
7163 | if (SCM_UNBNDP (y)) |
7164 | { | |
7165 | if (SCM_UNBNDP (x)) | |
7166 | SCM_WTA_DISPATCH_0 (g_max, s_max); | |
e11e83f3 | 7167 | else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x)) |
0aacf84e MD |
7168 | return x; |
7169 | else | |
7170 | SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max); | |
f872b822 | 7171 | } |
f4c627b3 | 7172 | |
e11e83f3 | 7173 | if (SCM_I_INUMP (x)) |
0aacf84e | 7174 | { |
e25f3727 | 7175 | scm_t_inum xx = SCM_I_INUM (x); |
e11e83f3 | 7176 | if (SCM_I_INUMP (y)) |
0aacf84e | 7177 | { |
e25f3727 | 7178 | scm_t_inum yy = SCM_I_INUM (y); |
0aacf84e MD |
7179 | return (xx < yy) ? y : x; |
7180 | } | |
7181 | else if (SCM_BIGP (y)) | |
7182 | { | |
7183 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
7184 | scm_remember_upto_here_1 (y); | |
7185 | return (sgn < 0) ? x : y; | |
7186 | } | |
7187 | else if (SCM_REALP (y)) | |
7188 | { | |
2e274311 MW |
7189 | double xxd = xx; |
7190 | double yyd = SCM_REAL_VALUE (y); | |
7191 | ||
7192 | if (xxd > yyd) | |
00472a22 | 7193 | return scm_i_from_double (xxd); |
2e274311 MW |
7194 | /* If y is a NaN, then "==" is false and we return the NaN */ |
7195 | else if (SCM_LIKELY (!(xxd == yyd))) | |
7196 | return y; | |
7197 | /* Handle signed zeroes properly */ | |
7198 | else if (xx == 0) | |
7199 | return flo0; | |
7200 | else | |
7201 | return y; | |
0aacf84e | 7202 | } |
f92e85f7 MV |
7203 | else if (SCM_FRACTIONP (y)) |
7204 | { | |
e4bc5d6c | 7205 | use_less: |
73e4de09 | 7206 | return (scm_is_false (scm_less_p (x, y)) ? x : y); |
f92e85f7 | 7207 | } |
0aacf84e MD |
7208 | else |
7209 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
f872b822 | 7210 | } |
0aacf84e MD |
7211 | else if (SCM_BIGP (x)) |
7212 | { | |
e11e83f3 | 7213 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
7214 | { |
7215 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
7216 | scm_remember_upto_here_1 (x); | |
7217 | return (sgn < 0) ? y : x; | |
7218 | } | |
7219 | else if (SCM_BIGP (y)) | |
7220 | { | |
7221 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
7222 | scm_remember_upto_here_2 (x, y); | |
7223 | return (cmp > 0) ? x : y; | |
7224 | } | |
7225 | else if (SCM_REALP (y)) | |
7226 | { | |
2a06f791 KR |
7227 | /* if y==NaN then xx>yy is false, so we return the NaN y */ |
7228 | double xx, yy; | |
7229 | big_real: | |
7230 | xx = scm_i_big2dbl (x); | |
7231 | yy = SCM_REAL_VALUE (y); | |
00472a22 | 7232 | return (xx > yy ? scm_i_from_double (xx) : y); |
0aacf84e | 7233 | } |
f92e85f7 MV |
7234 | else if (SCM_FRACTIONP (y)) |
7235 | { | |
e4bc5d6c | 7236 | goto use_less; |
f92e85f7 | 7237 | } |
0aacf84e MD |
7238 | else |
7239 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
f4c627b3 | 7240 | } |
0aacf84e MD |
7241 | else if (SCM_REALP (x)) |
7242 | { | |
e11e83f3 | 7243 | if (SCM_I_INUMP (y)) |
0aacf84e | 7244 | { |
2e274311 MW |
7245 | scm_t_inum yy = SCM_I_INUM (y); |
7246 | double xxd = SCM_REAL_VALUE (x); | |
7247 | double yyd = yy; | |
7248 | ||
7249 | if (yyd > xxd) | |
00472a22 | 7250 | return scm_i_from_double (yyd); |
2e274311 MW |
7251 | /* If x is a NaN, then "==" is false and we return the NaN */ |
7252 | else if (SCM_LIKELY (!(xxd == yyd))) | |
7253 | return x; | |
7254 | /* Handle signed zeroes properly */ | |
7255 | else if (yy == 0) | |
7256 | return flo0; | |
7257 | else | |
7258 | return x; | |
0aacf84e MD |
7259 | } |
7260 | else if (SCM_BIGP (y)) | |
7261 | { | |
b6f8f763 | 7262 | SCM_SWAP (x, y); |
2a06f791 | 7263 | goto big_real; |
0aacf84e MD |
7264 | } |
7265 | else if (SCM_REALP (y)) | |
7266 | { | |
0aacf84e | 7267 | double xx = SCM_REAL_VALUE (x); |
2e274311 MW |
7268 | double yy = SCM_REAL_VALUE (y); |
7269 | ||
b4c55c9c MW |
7270 | /* For purposes of max: nan > +inf.0 > everything else, |
7271 | per the R6RS errata */ | |
2e274311 MW |
7272 | if (xx > yy) |
7273 | return x; | |
7274 | else if (SCM_LIKELY (xx < yy)) | |
7275 | return y; | |
7276 | /* If neither (xx > yy) nor (xx < yy), then | |
7277 | either they're equal or one is a NaN */ | |
b4c55c9c MW |
7278 | else if (SCM_UNLIKELY (xx != yy)) |
7279 | return (xx != xx) ? x : y; /* Return the NaN */ | |
2e274311 | 7280 | /* xx == yy, but handle signed zeroes properly */ |
e1592f8a | 7281 | else if (copysign (1.0, yy) < 0.0) |
2e274311 | 7282 | return x; |
e1592f8a MW |
7283 | else |
7284 | return y; | |
0aacf84e | 7285 | } |
f92e85f7 MV |
7286 | else if (SCM_FRACTIONP (y)) |
7287 | { | |
7288 | double yy = scm_i_fraction2double (y); | |
7289 | double xx = SCM_REAL_VALUE (x); | |
00472a22 | 7290 | return (xx < yy) ? scm_i_from_double (yy) : x; |
f92e85f7 MV |
7291 | } |
7292 | else | |
7293 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
7294 | } | |
7295 | else if (SCM_FRACTIONP (x)) | |
7296 | { | |
e11e83f3 | 7297 | if (SCM_I_INUMP (y)) |
f92e85f7 | 7298 | { |
e4bc5d6c | 7299 | goto use_less; |
f92e85f7 MV |
7300 | } |
7301 | else if (SCM_BIGP (y)) | |
7302 | { | |
e4bc5d6c | 7303 | goto use_less; |
f92e85f7 MV |
7304 | } |
7305 | else if (SCM_REALP (y)) | |
7306 | { | |
7307 | double xx = scm_i_fraction2double (x); | |
2e274311 | 7308 | /* if y==NaN then ">" is false, so we return the NaN y */ |
00472a22 | 7309 | return (xx > SCM_REAL_VALUE (y)) ? scm_i_from_double (xx) : y; |
f92e85f7 MV |
7310 | } |
7311 | else if (SCM_FRACTIONP (y)) | |
7312 | { | |
e4bc5d6c | 7313 | goto use_less; |
f92e85f7 | 7314 | } |
0aacf84e MD |
7315 | else |
7316 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max); | |
f872b822 | 7317 | } |
0aacf84e | 7318 | else |
f4c627b3 | 7319 | SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max); |
0f2d19dd JB |
7320 | } |
7321 | ||
7322 | ||
78d3deb1 AW |
7323 | SCM_PRIMITIVE_GENERIC (scm_i_min, "min", 0, 2, 1, |
7324 | (SCM x, SCM y, SCM rest), | |
7325 | "Return the minimum of all parameter values.") | |
7326 | #define FUNC_NAME s_scm_i_min | |
7327 | { | |
7328 | while (!scm_is_null (rest)) | |
7329 | { x = scm_min (x, y); | |
7330 | y = scm_car (rest); | |
7331 | rest = scm_cdr (rest); | |
7332 | } | |
7333 | return scm_min (x, y); | |
7334 | } | |
7335 | #undef FUNC_NAME | |
7336 | ||
7337 | #define s_min s_scm_i_min | |
7338 | #define g_min g_scm_i_min | |
7339 | ||
0f2d19dd | 7340 | SCM |
6e8d25a6 | 7341 | scm_min (SCM x, SCM y) |
0f2d19dd | 7342 | { |
0aacf84e MD |
7343 | if (SCM_UNBNDP (y)) |
7344 | { | |
7345 | if (SCM_UNBNDP (x)) | |
7346 | SCM_WTA_DISPATCH_0 (g_min, s_min); | |
e11e83f3 | 7347 | else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x)) |
0aacf84e MD |
7348 | return x; |
7349 | else | |
7350 | SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min); | |
f872b822 | 7351 | } |
f4c627b3 | 7352 | |
e11e83f3 | 7353 | if (SCM_I_INUMP (x)) |
0aacf84e | 7354 | { |
e25f3727 | 7355 | scm_t_inum xx = SCM_I_INUM (x); |
e11e83f3 | 7356 | if (SCM_I_INUMP (y)) |
0aacf84e | 7357 | { |
e25f3727 | 7358 | scm_t_inum yy = SCM_I_INUM (y); |
0aacf84e MD |
7359 | return (xx < yy) ? x : y; |
7360 | } | |
7361 | else if (SCM_BIGP (y)) | |
7362 | { | |
7363 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
7364 | scm_remember_upto_here_1 (y); | |
7365 | return (sgn < 0) ? y : x; | |
7366 | } | |
7367 | else if (SCM_REALP (y)) | |
7368 | { | |
7369 | double z = xx; | |
7370 | /* if y==NaN then "<" is false and we return NaN */ | |
00472a22 | 7371 | return (z < SCM_REAL_VALUE (y)) ? scm_i_from_double (z) : y; |
0aacf84e | 7372 | } |
f92e85f7 MV |
7373 | else if (SCM_FRACTIONP (y)) |
7374 | { | |
e4bc5d6c | 7375 | use_less: |
73e4de09 | 7376 | return (scm_is_false (scm_less_p (x, y)) ? y : x); |
f92e85f7 | 7377 | } |
0aacf84e MD |
7378 | else |
7379 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min); | |
f872b822 | 7380 | } |
0aacf84e MD |
7381 | else if (SCM_BIGP (x)) |
7382 | { | |
e11e83f3 | 7383 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
7384 | { |
7385 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
7386 | scm_remember_upto_here_1 (x); | |
7387 | return (sgn < 0) ? x : y; | |
7388 | } | |
7389 | else if (SCM_BIGP (y)) | |
7390 | { | |
7391 | int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y)); | |
7392 | scm_remember_upto_here_2 (x, y); | |
7393 | return (cmp > 0) ? y : x; | |
7394 | } | |
7395 | else if (SCM_REALP (y)) | |
7396 | { | |
2a06f791 KR |
7397 | /* if y==NaN then xx<yy is false, so we return the NaN y */ |
7398 | double xx, yy; | |
7399 | big_real: | |
7400 | xx = scm_i_big2dbl (x); | |
7401 | yy = SCM_REAL_VALUE (y); | |
00472a22 | 7402 | return (xx < yy ? scm_i_from_double (xx) : y); |
0aacf84e | 7403 | } |
f92e85f7 MV |
7404 | else if (SCM_FRACTIONP (y)) |
7405 | { | |
e4bc5d6c | 7406 | goto use_less; |
f92e85f7 | 7407 | } |
0aacf84e MD |
7408 | else |
7409 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min); | |
f4c627b3 | 7410 | } |
0aacf84e MD |
7411 | else if (SCM_REALP (x)) |
7412 | { | |
e11e83f3 | 7413 | if (SCM_I_INUMP (y)) |
0aacf84e | 7414 | { |
e11e83f3 | 7415 | double z = SCM_I_INUM (y); |
0aacf84e | 7416 | /* if x==NaN then "<" is false and we return NaN */ |
00472a22 | 7417 | return (z < SCM_REAL_VALUE (x)) ? scm_i_from_double (z) : x; |
0aacf84e MD |
7418 | } |
7419 | else if (SCM_BIGP (y)) | |
7420 | { | |
b6f8f763 | 7421 | SCM_SWAP (x, y); |
2a06f791 | 7422 | goto big_real; |
0aacf84e MD |
7423 | } |
7424 | else if (SCM_REALP (y)) | |
7425 | { | |
0aacf84e | 7426 | double xx = SCM_REAL_VALUE (x); |
2e274311 MW |
7427 | double yy = SCM_REAL_VALUE (y); |
7428 | ||
b4c55c9c MW |
7429 | /* For purposes of min: nan < -inf.0 < everything else, |
7430 | per the R6RS errata */ | |
2e274311 MW |
7431 | if (xx < yy) |
7432 | return x; | |
7433 | else if (SCM_LIKELY (xx > yy)) | |
7434 | return y; | |
7435 | /* If neither (xx < yy) nor (xx > yy), then | |
7436 | either they're equal or one is a NaN */ | |
b4c55c9c MW |
7437 | else if (SCM_UNLIKELY (xx != yy)) |
7438 | return (xx != xx) ? x : y; /* Return the NaN */ | |
2e274311 | 7439 | /* xx == yy, but handle signed zeroes properly */ |
e1592f8a | 7440 | else if (copysign (1.0, xx) < 0.0) |
2e274311 | 7441 | return x; |
e1592f8a MW |
7442 | else |
7443 | return y; | |
0aacf84e | 7444 | } |
f92e85f7 MV |
7445 | else if (SCM_FRACTIONP (y)) |
7446 | { | |
7447 | double yy = scm_i_fraction2double (y); | |
7448 | double xx = SCM_REAL_VALUE (x); | |
00472a22 | 7449 | return (yy < xx) ? scm_i_from_double (yy) : x; |
f92e85f7 | 7450 | } |
0aacf84e MD |
7451 | else |
7452 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min); | |
f872b822 | 7453 | } |
f92e85f7 MV |
7454 | else if (SCM_FRACTIONP (x)) |
7455 | { | |
e11e83f3 | 7456 | if (SCM_I_INUMP (y)) |
f92e85f7 | 7457 | { |
e4bc5d6c | 7458 | goto use_less; |
f92e85f7 MV |
7459 | } |
7460 | else if (SCM_BIGP (y)) | |
7461 | { | |
e4bc5d6c | 7462 | goto use_less; |
f92e85f7 MV |
7463 | } |
7464 | else if (SCM_REALP (y)) | |
7465 | { | |
7466 | double xx = scm_i_fraction2double (x); | |
2e274311 | 7467 | /* if y==NaN then "<" is false, so we return the NaN y */ |
00472a22 | 7468 | return (xx < SCM_REAL_VALUE (y)) ? scm_i_from_double (xx) : y; |
f92e85f7 MV |
7469 | } |
7470 | else if (SCM_FRACTIONP (y)) | |
7471 | { | |
e4bc5d6c | 7472 | goto use_less; |
f92e85f7 MV |
7473 | } |
7474 | else | |
78d3deb1 | 7475 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min); |
f92e85f7 | 7476 | } |
0aacf84e | 7477 | else |
f4c627b3 | 7478 | SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min); |
0f2d19dd JB |
7479 | } |
7480 | ||
7481 | ||
8ccd24f7 AW |
7482 | SCM_PRIMITIVE_GENERIC (scm_i_sum, "+", 0, 2, 1, |
7483 | (SCM x, SCM y, SCM rest), | |
7484 | "Return the sum of all parameter values. Return 0 if called without\n" | |
7485 | "any parameters." ) | |
7486 | #define FUNC_NAME s_scm_i_sum | |
7487 | { | |
7488 | while (!scm_is_null (rest)) | |
7489 | { x = scm_sum (x, y); | |
7490 | y = scm_car (rest); | |
7491 | rest = scm_cdr (rest); | |
7492 | } | |
7493 | return scm_sum (x, y); | |
7494 | } | |
7495 | #undef FUNC_NAME | |
7496 | ||
7497 | #define s_sum s_scm_i_sum | |
7498 | #define g_sum g_scm_i_sum | |
7499 | ||
0f2d19dd | 7500 | SCM |
6e8d25a6 | 7501 | scm_sum (SCM x, SCM y) |
0f2d19dd | 7502 | { |
9cc37597 | 7503 | if (SCM_UNLIKELY (SCM_UNBNDP (y))) |
ca46fb90 RB |
7504 | { |
7505 | if (SCM_NUMBERP (x)) return x; | |
7506 | if (SCM_UNBNDP (x)) return SCM_INUM0; | |
98cb6e75 | 7507 | SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum); |
f872b822 | 7508 | } |
c209c88e | 7509 | |
9cc37597 | 7510 | if (SCM_LIKELY (SCM_I_INUMP (x))) |
ca46fb90 | 7511 | { |
9cc37597 | 7512 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
ca46fb90 | 7513 | { |
e25f3727 AW |
7514 | scm_t_inum xx = SCM_I_INUM (x); |
7515 | scm_t_inum yy = SCM_I_INUM (y); | |
7516 | scm_t_inum z = xx + yy; | |
7517 | return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_inum2big (z); | |
ca46fb90 RB |
7518 | } |
7519 | else if (SCM_BIGP (y)) | |
7520 | { | |
7521 | SCM_SWAP (x, y); | |
7522 | goto add_big_inum; | |
7523 | } | |
7524 | else if (SCM_REALP (y)) | |
7525 | { | |
e25f3727 | 7526 | scm_t_inum xx = SCM_I_INUM (x); |
00472a22 | 7527 | return scm_i_from_double (xx + SCM_REAL_VALUE (y)); |
ca46fb90 RB |
7528 | } |
7529 | else if (SCM_COMPLEXP (y)) | |
7530 | { | |
e25f3727 | 7531 | scm_t_inum xx = SCM_I_INUM (x); |
8507ec80 | 7532 | return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y), |
ca46fb90 RB |
7533 | SCM_COMPLEX_IMAG (y)); |
7534 | } | |
f92e85f7 | 7535 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 7536 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y), |
f92e85f7 MV |
7537 | scm_product (x, SCM_FRACTION_DENOMINATOR (y))), |
7538 | SCM_FRACTION_DENOMINATOR (y)); | |
ca46fb90 RB |
7539 | else |
7540 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
0aacf84e MD |
7541 | } else if (SCM_BIGP (x)) |
7542 | { | |
e11e83f3 | 7543 | if (SCM_I_INUMP (y)) |
0aacf84e | 7544 | { |
e25f3727 | 7545 | scm_t_inum inum; |
0aacf84e MD |
7546 | int bigsgn; |
7547 | add_big_inum: | |
e11e83f3 | 7548 | inum = SCM_I_INUM (y); |
0aacf84e MD |
7549 | if (inum == 0) |
7550 | return x; | |
7551 | bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
7552 | if (inum < 0) | |
7553 | { | |
7554 | SCM result = scm_i_mkbig (); | |
7555 | mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum); | |
7556 | scm_remember_upto_here_1 (x); | |
7557 | /* we know the result will have to be a bignum */ | |
7558 | if (bigsgn == -1) | |
7559 | return result; | |
7560 | return scm_i_normbig (result); | |
7561 | } | |
7562 | else | |
7563 | { | |
7564 | SCM result = scm_i_mkbig (); | |
7565 | mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum); | |
7566 | scm_remember_upto_here_1 (x); | |
7567 | /* we know the result will have to be a bignum */ | |
7568 | if (bigsgn == 1) | |
7569 | return result; | |
7570 | return scm_i_normbig (result); | |
7571 | } | |
7572 | } | |
7573 | else if (SCM_BIGP (y)) | |
7574 | { | |
7575 | SCM result = scm_i_mkbig (); | |
7576 | int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
7577 | int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
7578 | mpz_add (SCM_I_BIG_MPZ (result), | |
7579 | SCM_I_BIG_MPZ (x), | |
7580 | SCM_I_BIG_MPZ (y)); | |
7581 | scm_remember_upto_here_2 (x, y); | |
7582 | /* we know the result will have to be a bignum */ | |
7583 | if (sgn_x == sgn_y) | |
7584 | return result; | |
7585 | return scm_i_normbig (result); | |
7586 | } | |
7587 | else if (SCM_REALP (y)) | |
7588 | { | |
7589 | double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y); | |
7590 | scm_remember_upto_here_1 (x); | |
00472a22 | 7591 | return scm_i_from_double (result); |
0aacf84e MD |
7592 | } |
7593 | else if (SCM_COMPLEXP (y)) | |
7594 | { | |
7595 | double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x)) | |
7596 | + SCM_COMPLEX_REAL (y)); | |
7597 | scm_remember_upto_here_1 (x); | |
8507ec80 | 7598 | return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y)); |
0aacf84e | 7599 | } |
f92e85f7 | 7600 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 7601 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y), |
f92e85f7 MV |
7602 | scm_product (x, SCM_FRACTION_DENOMINATOR (y))), |
7603 | SCM_FRACTION_DENOMINATOR (y)); | |
0aacf84e MD |
7604 | else |
7605 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
0f2d19dd | 7606 | } |
0aacf84e MD |
7607 | else if (SCM_REALP (x)) |
7608 | { | |
e11e83f3 | 7609 | if (SCM_I_INUMP (y)) |
00472a22 | 7610 | return scm_i_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y)); |
0aacf84e MD |
7611 | else if (SCM_BIGP (y)) |
7612 | { | |
7613 | double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x); | |
7614 | scm_remember_upto_here_1 (y); | |
00472a22 | 7615 | return scm_i_from_double (result); |
0aacf84e MD |
7616 | } |
7617 | else if (SCM_REALP (y)) | |
00472a22 | 7618 | return scm_i_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y)); |
0aacf84e | 7619 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 7620 | return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y), |
0aacf84e | 7621 | SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 7622 | else if (SCM_FRACTIONP (y)) |
00472a22 | 7623 | return scm_i_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y)); |
0aacf84e MD |
7624 | else |
7625 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
f872b822 | 7626 | } |
0aacf84e MD |
7627 | else if (SCM_COMPLEXP (x)) |
7628 | { | |
e11e83f3 | 7629 | if (SCM_I_INUMP (y)) |
8507ec80 | 7630 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y), |
0aacf84e MD |
7631 | SCM_COMPLEX_IMAG (x)); |
7632 | else if (SCM_BIGP (y)) | |
7633 | { | |
7634 | double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y)) | |
7635 | + SCM_COMPLEX_REAL (x)); | |
7636 | scm_remember_upto_here_1 (y); | |
8507ec80 | 7637 | return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x)); |
0aacf84e MD |
7638 | } |
7639 | else if (SCM_REALP (y)) | |
8507ec80 | 7640 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y), |
0aacf84e MD |
7641 | SCM_COMPLEX_IMAG (x)); |
7642 | else if (SCM_COMPLEXP (y)) | |
8507ec80 | 7643 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y), |
0aacf84e | 7644 | SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 7645 | else if (SCM_FRACTIONP (y)) |
8507ec80 | 7646 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y), |
f92e85f7 MV |
7647 | SCM_COMPLEX_IMAG (x)); |
7648 | else | |
7649 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
7650 | } | |
7651 | else if (SCM_FRACTIONP (x)) | |
7652 | { | |
e11e83f3 | 7653 | if (SCM_I_INUMP (y)) |
cba42c93 | 7654 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
7655 | scm_product (y, SCM_FRACTION_DENOMINATOR (x))), |
7656 | SCM_FRACTION_DENOMINATOR (x)); | |
7657 | else if (SCM_BIGP (y)) | |
cba42c93 | 7658 | return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
7659 | scm_product (y, SCM_FRACTION_DENOMINATOR (x))), |
7660 | SCM_FRACTION_DENOMINATOR (x)); | |
7661 | else if (SCM_REALP (y)) | |
00472a22 | 7662 | return scm_i_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x)); |
f92e85f7 | 7663 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 7664 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x), |
f92e85f7 MV |
7665 | SCM_COMPLEX_IMAG (y)); |
7666 | else if (SCM_FRACTIONP (y)) | |
7667 | /* a/b + c/d = (ad + bc) / bd */ | |
cba42c93 | 7668 | return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
7669 | scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))), |
7670 | scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y))); | |
0aacf84e MD |
7671 | else |
7672 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum); | |
98cb6e75 | 7673 | } |
0aacf84e | 7674 | else |
98cb6e75 | 7675 | SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum); |
0f2d19dd JB |
7676 | } |
7677 | ||
7678 | ||
40882e3d KR |
7679 | SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0, |
7680 | (SCM x), | |
7681 | "Return @math{@var{x}+1}.") | |
7682 | #define FUNC_NAME s_scm_oneplus | |
7683 | { | |
cff5fa33 | 7684 | return scm_sum (x, SCM_INUM1); |
40882e3d KR |
7685 | } |
7686 | #undef FUNC_NAME | |
7687 | ||
7688 | ||
78d3deb1 AW |
7689 | SCM_PRIMITIVE_GENERIC (scm_i_difference, "-", 0, 2, 1, |
7690 | (SCM x, SCM y, SCM rest), | |
7691 | "If called with one argument @var{z1}, -@var{z1} returned. Otherwise\n" | |
7692 | "the sum of all but the first argument are subtracted from the first\n" | |
7693 | "argument.") | |
7694 | #define FUNC_NAME s_scm_i_difference | |
7695 | { | |
7696 | while (!scm_is_null (rest)) | |
7697 | { x = scm_difference (x, y); | |
7698 | y = scm_car (rest); | |
7699 | rest = scm_cdr (rest); | |
7700 | } | |
7701 | return scm_difference (x, y); | |
7702 | } | |
7703 | #undef FUNC_NAME | |
7704 | ||
7705 | #define s_difference s_scm_i_difference | |
7706 | #define g_difference g_scm_i_difference | |
7707 | ||
0f2d19dd | 7708 | SCM |
6e8d25a6 | 7709 | scm_difference (SCM x, SCM y) |
78d3deb1 | 7710 | #define FUNC_NAME s_difference |
0f2d19dd | 7711 | { |
9cc37597 | 7712 | if (SCM_UNLIKELY (SCM_UNBNDP (y))) |
ca46fb90 RB |
7713 | { |
7714 | if (SCM_UNBNDP (x)) | |
7715 | SCM_WTA_DISPATCH_0 (g_difference, s_difference); | |
7716 | else | |
e11e83f3 | 7717 | if (SCM_I_INUMP (x)) |
ca46fb90 | 7718 | { |
e25f3727 | 7719 | scm_t_inum xx = -SCM_I_INUM (x); |
ca46fb90 | 7720 | if (SCM_FIXABLE (xx)) |
d956fa6f | 7721 | return SCM_I_MAKINUM (xx); |
ca46fb90 | 7722 | else |
e25f3727 | 7723 | return scm_i_inum2big (xx); |
ca46fb90 RB |
7724 | } |
7725 | else if (SCM_BIGP (x)) | |
a9ad4847 KR |
7726 | /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a |
7727 | bignum, but negating that gives a fixnum. */ | |
ca46fb90 RB |
7728 | return scm_i_normbig (scm_i_clonebig (x, 0)); |
7729 | else if (SCM_REALP (x)) | |
00472a22 | 7730 | return scm_i_from_double (-SCM_REAL_VALUE (x)); |
ca46fb90 | 7731 | else if (SCM_COMPLEXP (x)) |
8507ec80 | 7732 | return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x), |
ca46fb90 | 7733 | -SCM_COMPLEX_IMAG (x)); |
f92e85f7 | 7734 | else if (SCM_FRACTIONP (x)) |
a285b18c MW |
7735 | return scm_i_make_ratio_already_reduced |
7736 | (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED), | |
7737 | SCM_FRACTION_DENOMINATOR (x)); | |
ca46fb90 RB |
7738 | else |
7739 | SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference); | |
f872b822 | 7740 | } |
ca46fb90 | 7741 | |
9cc37597 | 7742 | if (SCM_LIKELY (SCM_I_INUMP (x))) |
0aacf84e | 7743 | { |
9cc37597 | 7744 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
0aacf84e | 7745 | { |
e25f3727 AW |
7746 | scm_t_inum xx = SCM_I_INUM (x); |
7747 | scm_t_inum yy = SCM_I_INUM (y); | |
7748 | scm_t_inum z = xx - yy; | |
0aacf84e | 7749 | if (SCM_FIXABLE (z)) |
d956fa6f | 7750 | return SCM_I_MAKINUM (z); |
0aacf84e | 7751 | else |
e25f3727 | 7752 | return scm_i_inum2big (z); |
0aacf84e MD |
7753 | } |
7754 | else if (SCM_BIGP (y)) | |
7755 | { | |
7756 | /* inum-x - big-y */ | |
e25f3727 | 7757 | scm_t_inum xx = SCM_I_INUM (x); |
ca46fb90 | 7758 | |
0aacf84e | 7759 | if (xx == 0) |
b5c40589 MW |
7760 | { |
7761 | /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a | |
7762 | bignum, but negating that gives a fixnum. */ | |
7763 | return scm_i_normbig (scm_i_clonebig (y, 0)); | |
7764 | } | |
0aacf84e MD |
7765 | else |
7766 | { | |
7767 | int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
7768 | SCM result = scm_i_mkbig (); | |
ca46fb90 | 7769 | |
0aacf84e MD |
7770 | if (xx >= 0) |
7771 | mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y)); | |
7772 | else | |
7773 | { | |
7774 | /* x - y == -(y + -x) */ | |
7775 | mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx); | |
7776 | mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result)); | |
7777 | } | |
7778 | scm_remember_upto_here_1 (y); | |
ca46fb90 | 7779 | |
0aacf84e MD |
7780 | if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0)) |
7781 | /* we know the result will have to be a bignum */ | |
7782 | return result; | |
7783 | else | |
7784 | return scm_i_normbig (result); | |
7785 | } | |
7786 | } | |
7787 | else if (SCM_REALP (y)) | |
7788 | { | |
e25f3727 | 7789 | scm_t_inum xx = SCM_I_INUM (x); |
9b9ef10c MW |
7790 | |
7791 | /* | |
7792 | * We need to handle x == exact 0 | |
7793 | * specially because R6RS states that: | |
7794 | * (- 0.0) ==> -0.0 and | |
7795 | * (- 0.0 0.0) ==> 0.0 | |
7796 | * and the scheme compiler changes | |
7797 | * (- 0.0) into (- 0 0.0) | |
7798 | * So we need to treat (- 0 0.0) like (- 0.0). | |
7799 | * At the C level, (-x) is different than (0.0 - x). | |
7800 | * (0.0 - 0.0) ==> 0.0, but (- 0.0) ==> -0.0. | |
7801 | */ | |
7802 | if (xx == 0) | |
00472a22 | 7803 | return scm_i_from_double (- SCM_REAL_VALUE (y)); |
9b9ef10c | 7804 | else |
00472a22 | 7805 | return scm_i_from_double (xx - SCM_REAL_VALUE (y)); |
0aacf84e MD |
7806 | } |
7807 | else if (SCM_COMPLEXP (y)) | |
7808 | { | |
e25f3727 | 7809 | scm_t_inum xx = SCM_I_INUM (x); |
9b9ef10c MW |
7810 | |
7811 | /* We need to handle x == exact 0 specially. | |
7812 | See the comment above (for SCM_REALP (y)) */ | |
7813 | if (xx == 0) | |
7814 | return scm_c_make_rectangular (- SCM_COMPLEX_REAL (y), | |
7815 | - SCM_COMPLEX_IMAG (y)); | |
7816 | else | |
7817 | return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y), | |
7818 | - SCM_COMPLEX_IMAG (y)); | |
0aacf84e | 7819 | } |
f92e85f7 MV |
7820 | else if (SCM_FRACTIONP (y)) |
7821 | /* a - b/c = (ac - b) / c */ | |
cba42c93 | 7822 | return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
7823 | SCM_FRACTION_NUMERATOR (y)), |
7824 | SCM_FRACTION_DENOMINATOR (y)); | |
0aacf84e MD |
7825 | else |
7826 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
f872b822 | 7827 | } |
0aacf84e MD |
7828 | else if (SCM_BIGP (x)) |
7829 | { | |
e11e83f3 | 7830 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
7831 | { |
7832 | /* big-x - inum-y */ | |
e25f3727 | 7833 | scm_t_inum yy = SCM_I_INUM (y); |
0aacf84e | 7834 | int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x)); |
ca46fb90 | 7835 | |
0aacf84e MD |
7836 | scm_remember_upto_here_1 (x); |
7837 | if (sgn_x == 0) | |
c71b0706 | 7838 | return (SCM_FIXABLE (-yy) ? |
e25f3727 | 7839 | SCM_I_MAKINUM (-yy) : scm_from_inum (-yy)); |
0aacf84e MD |
7840 | else |
7841 | { | |
7842 | SCM result = scm_i_mkbig (); | |
ca46fb90 | 7843 | |
708f22c6 KR |
7844 | if (yy >= 0) |
7845 | mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy); | |
7846 | else | |
7847 | mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy); | |
0aacf84e | 7848 | scm_remember_upto_here_1 (x); |
ca46fb90 | 7849 | |
0aacf84e MD |
7850 | if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0)) |
7851 | /* we know the result will have to be a bignum */ | |
7852 | return result; | |
7853 | else | |
7854 | return scm_i_normbig (result); | |
7855 | } | |
7856 | } | |
7857 | else if (SCM_BIGP (y)) | |
7858 | { | |
7859 | int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x)); | |
7860 | int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y)); | |
7861 | SCM result = scm_i_mkbig (); | |
7862 | mpz_sub (SCM_I_BIG_MPZ (result), | |
7863 | SCM_I_BIG_MPZ (x), | |
7864 | SCM_I_BIG_MPZ (y)); | |
7865 | scm_remember_upto_here_2 (x, y); | |
7866 | /* we know the result will have to be a bignum */ | |
7867 | if ((sgn_x == 1) && (sgn_y == -1)) | |
7868 | return result; | |
7869 | if ((sgn_x == -1) && (sgn_y == 1)) | |
7870 | return result; | |
7871 | return scm_i_normbig (result); | |
7872 | } | |
7873 | else if (SCM_REALP (y)) | |
7874 | { | |
7875 | double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y); | |
7876 | scm_remember_upto_here_1 (x); | |
00472a22 | 7877 | return scm_i_from_double (result); |
0aacf84e MD |
7878 | } |
7879 | else if (SCM_COMPLEXP (y)) | |
7880 | { | |
7881 | double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x)) | |
7882 | - SCM_COMPLEX_REAL (y)); | |
7883 | scm_remember_upto_here_1 (x); | |
8507ec80 | 7884 | return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y)); |
0aacf84e | 7885 | } |
f92e85f7 | 7886 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 7887 | return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
7888 | SCM_FRACTION_NUMERATOR (y)), |
7889 | SCM_FRACTION_DENOMINATOR (y)); | |
0aacf84e | 7890 | else SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); |
ca46fb90 | 7891 | } |
0aacf84e MD |
7892 | else if (SCM_REALP (x)) |
7893 | { | |
e11e83f3 | 7894 | if (SCM_I_INUMP (y)) |
00472a22 | 7895 | return scm_i_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y)); |
0aacf84e MD |
7896 | else if (SCM_BIGP (y)) |
7897 | { | |
7898 | double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y)); | |
7899 | scm_remember_upto_here_1 (x); | |
00472a22 | 7900 | return scm_i_from_double (result); |
0aacf84e MD |
7901 | } |
7902 | else if (SCM_REALP (y)) | |
00472a22 | 7903 | return scm_i_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y)); |
0aacf84e | 7904 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 7905 | return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y), |
0aacf84e | 7906 | -SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 7907 | else if (SCM_FRACTIONP (y)) |
00472a22 | 7908 | return scm_i_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y)); |
0aacf84e MD |
7909 | else |
7910 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
98cb6e75 | 7911 | } |
0aacf84e MD |
7912 | else if (SCM_COMPLEXP (x)) |
7913 | { | |
e11e83f3 | 7914 | if (SCM_I_INUMP (y)) |
8507ec80 | 7915 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y), |
0aacf84e MD |
7916 | SCM_COMPLEX_IMAG (x)); |
7917 | else if (SCM_BIGP (y)) | |
7918 | { | |
7919 | double real_part = (SCM_COMPLEX_REAL (x) | |
7920 | - mpz_get_d (SCM_I_BIG_MPZ (y))); | |
7921 | scm_remember_upto_here_1 (x); | |
8507ec80 | 7922 | return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y)); |
0aacf84e MD |
7923 | } |
7924 | else if (SCM_REALP (y)) | |
8507ec80 | 7925 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y), |
0aacf84e MD |
7926 | SCM_COMPLEX_IMAG (x)); |
7927 | else if (SCM_COMPLEXP (y)) | |
8507ec80 | 7928 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y), |
0aacf84e | 7929 | SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 7930 | else if (SCM_FRACTIONP (y)) |
8507ec80 | 7931 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y), |
f92e85f7 MV |
7932 | SCM_COMPLEX_IMAG (x)); |
7933 | else | |
7934 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
7935 | } | |
7936 | else if (SCM_FRACTIONP (x)) | |
7937 | { | |
e11e83f3 | 7938 | if (SCM_I_INUMP (y)) |
f92e85f7 | 7939 | /* a/b - c = (a - cb) / b */ |
cba42c93 | 7940 | return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
7941 | scm_product(y, SCM_FRACTION_DENOMINATOR (x))), |
7942 | SCM_FRACTION_DENOMINATOR (x)); | |
7943 | else if (SCM_BIGP (y)) | |
cba42c93 | 7944 | return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
7945 | scm_product(y, SCM_FRACTION_DENOMINATOR (x))), |
7946 | SCM_FRACTION_DENOMINATOR (x)); | |
7947 | else if (SCM_REALP (y)) | |
00472a22 | 7948 | return scm_i_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y)); |
f92e85f7 | 7949 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 7950 | return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y), |
f92e85f7 MV |
7951 | -SCM_COMPLEX_IMAG (y)); |
7952 | else if (SCM_FRACTIONP (y)) | |
7953 | /* a/b - c/d = (ad - bc) / bd */ | |
cba42c93 | 7954 | return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)), |
f92e85f7 MV |
7955 | scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))), |
7956 | scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y))); | |
0aacf84e MD |
7957 | else |
7958 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference); | |
98cb6e75 | 7959 | } |
0aacf84e | 7960 | else |
98cb6e75 | 7961 | SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference); |
0f2d19dd | 7962 | } |
c05e97b7 | 7963 | #undef FUNC_NAME |
0f2d19dd | 7964 | |
ca46fb90 | 7965 | |
40882e3d KR |
7966 | SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0, |
7967 | (SCM x), | |
7968 | "Return @math{@var{x}-1}.") | |
7969 | #define FUNC_NAME s_scm_oneminus | |
7970 | { | |
cff5fa33 | 7971 | return scm_difference (x, SCM_INUM1); |
40882e3d KR |
7972 | } |
7973 | #undef FUNC_NAME | |
7974 | ||
7975 | ||
78d3deb1 AW |
7976 | SCM_PRIMITIVE_GENERIC (scm_i_product, "*", 0, 2, 1, |
7977 | (SCM x, SCM y, SCM rest), | |
7978 | "Return the product of all arguments. If called without arguments,\n" | |
7979 | "1 is returned.") | |
7980 | #define FUNC_NAME s_scm_i_product | |
7981 | { | |
7982 | while (!scm_is_null (rest)) | |
7983 | { x = scm_product (x, y); | |
7984 | y = scm_car (rest); | |
7985 | rest = scm_cdr (rest); | |
7986 | } | |
7987 | return scm_product (x, y); | |
7988 | } | |
7989 | #undef FUNC_NAME | |
7990 | ||
7991 | #define s_product s_scm_i_product | |
7992 | #define g_product g_scm_i_product | |
7993 | ||
0f2d19dd | 7994 | SCM |
6e8d25a6 | 7995 | scm_product (SCM x, SCM y) |
0f2d19dd | 7996 | { |
9cc37597 | 7997 | if (SCM_UNLIKELY (SCM_UNBNDP (y))) |
0aacf84e MD |
7998 | { |
7999 | if (SCM_UNBNDP (x)) | |
d956fa6f | 8000 | return SCM_I_MAKINUM (1L); |
0aacf84e MD |
8001 | else if (SCM_NUMBERP (x)) |
8002 | return x; | |
8003 | else | |
8004 | SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product); | |
f872b822 | 8005 | } |
ca46fb90 | 8006 | |
9cc37597 | 8007 | if (SCM_LIKELY (SCM_I_INUMP (x))) |
0aacf84e | 8008 | { |
e25f3727 | 8009 | scm_t_inum xx; |
f4c627b3 | 8010 | |
5e791807 | 8011 | xinum: |
e11e83f3 | 8012 | xx = SCM_I_INUM (x); |
f4c627b3 | 8013 | |
0aacf84e MD |
8014 | switch (xx) |
8015 | { | |
5e791807 MW |
8016 | case 1: |
8017 | /* exact1 is the universal multiplicative identity */ | |
8018 | return y; | |
8019 | break; | |
8020 | case 0: | |
8021 | /* exact0 times a fixnum is exact0: optimize this case */ | |
8022 | if (SCM_LIKELY (SCM_I_INUMP (y))) | |
8023 | return SCM_INUM0; | |
8024 | /* if the other argument is inexact, the result is inexact, | |
8025 | and we must do the multiplication in order to handle | |
8026 | infinities and NaNs properly. */ | |
8027 | else if (SCM_REALP (y)) | |
00472a22 | 8028 | return scm_i_from_double (0.0 * SCM_REAL_VALUE (y)); |
5e791807 MW |
8029 | else if (SCM_COMPLEXP (y)) |
8030 | return scm_c_make_rectangular (0.0 * SCM_COMPLEX_REAL (y), | |
8031 | 0.0 * SCM_COMPLEX_IMAG (y)); | |
8032 | /* we've already handled inexact numbers, | |
8033 | so y must be exact, and we return exact0 */ | |
8034 | else if (SCM_NUMP (y)) | |
8035 | return SCM_INUM0; | |
8036 | else | |
8037 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
8038 | break; | |
8039 | case -1: | |
b5c40589 | 8040 | /* |
5e791807 MW |
8041 | * This case is important for more than just optimization. |
8042 | * It handles the case of negating | |
b5c40589 MW |
8043 | * (+ 1 most-positive-fixnum) aka (- most-negative-fixnum), |
8044 | * which is a bignum that must be changed back into a fixnum. | |
8045 | * Failure to do so will cause the following to return #f: | |
8046 | * (= most-negative-fixnum (* -1 (- most-negative-fixnum))) | |
8047 | */ | |
b5c40589 MW |
8048 | return scm_difference(y, SCM_UNDEFINED); |
8049 | break; | |
0aacf84e | 8050 | } |
f4c627b3 | 8051 | |
9cc37597 | 8052 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
0aacf84e | 8053 | { |
e25f3727 | 8054 | scm_t_inum yy = SCM_I_INUM (y); |
2355f017 MW |
8055 | #if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64 |
8056 | scm_t_int64 kk = xx * (scm_t_int64) yy; | |
8057 | if (SCM_FIXABLE (kk)) | |
8058 | return SCM_I_MAKINUM (kk); | |
8059 | #else | |
8060 | scm_t_inum axx = (xx > 0) ? xx : -xx; | |
8061 | scm_t_inum ayy = (yy > 0) ? yy : -yy; | |
8062 | if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy) | |
8063 | return SCM_I_MAKINUM (xx * yy); | |
8064 | #endif | |
0aacf84e MD |
8065 | else |
8066 | { | |
e25f3727 | 8067 | SCM result = scm_i_inum2big (xx); |
0aacf84e MD |
8068 | mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy); |
8069 | return scm_i_normbig (result); | |
8070 | } | |
8071 | } | |
8072 | else if (SCM_BIGP (y)) | |
8073 | { | |
8074 | SCM result = scm_i_mkbig (); | |
8075 | mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx); | |
8076 | scm_remember_upto_here_1 (y); | |
8077 | return result; | |
8078 | } | |
8079 | else if (SCM_REALP (y)) | |
00472a22 | 8080 | return scm_i_from_double (xx * SCM_REAL_VALUE (y)); |
0aacf84e | 8081 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 8082 | return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y), |
0aacf84e | 8083 | xx * SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 8084 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 8085 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)), |
f92e85f7 | 8086 | SCM_FRACTION_DENOMINATOR (y)); |
0aacf84e MD |
8087 | else |
8088 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 8089 | } |
0aacf84e MD |
8090 | else if (SCM_BIGP (x)) |
8091 | { | |
e11e83f3 | 8092 | if (SCM_I_INUMP (y)) |
0aacf84e MD |
8093 | { |
8094 | SCM_SWAP (x, y); | |
5e791807 | 8095 | goto xinum; |
0aacf84e MD |
8096 | } |
8097 | else if (SCM_BIGP (y)) | |
8098 | { | |
8099 | SCM result = scm_i_mkbig (); | |
8100 | mpz_mul (SCM_I_BIG_MPZ (result), | |
8101 | SCM_I_BIG_MPZ (x), | |
8102 | SCM_I_BIG_MPZ (y)); | |
8103 | scm_remember_upto_here_2 (x, y); | |
8104 | return result; | |
8105 | } | |
8106 | else if (SCM_REALP (y)) | |
8107 | { | |
8108 | double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y); | |
8109 | scm_remember_upto_here_1 (x); | |
00472a22 | 8110 | return scm_i_from_double (result); |
0aacf84e MD |
8111 | } |
8112 | else if (SCM_COMPLEXP (y)) | |
8113 | { | |
8114 | double z = mpz_get_d (SCM_I_BIG_MPZ (x)); | |
8115 | scm_remember_upto_here_1 (x); | |
8507ec80 | 8116 | return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y), |
0aacf84e MD |
8117 | z * SCM_COMPLEX_IMAG (y)); |
8118 | } | |
f92e85f7 | 8119 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 8120 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)), |
f92e85f7 | 8121 | SCM_FRACTION_DENOMINATOR (y)); |
0aacf84e MD |
8122 | else |
8123 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 8124 | } |
0aacf84e MD |
8125 | else if (SCM_REALP (x)) |
8126 | { | |
e11e83f3 | 8127 | if (SCM_I_INUMP (y)) |
5e791807 MW |
8128 | { |
8129 | SCM_SWAP (x, y); | |
8130 | goto xinum; | |
8131 | } | |
0aacf84e MD |
8132 | else if (SCM_BIGP (y)) |
8133 | { | |
8134 | double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x); | |
8135 | scm_remember_upto_here_1 (y); | |
00472a22 | 8136 | return scm_i_from_double (result); |
0aacf84e MD |
8137 | } |
8138 | else if (SCM_REALP (y)) | |
00472a22 | 8139 | return scm_i_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y)); |
0aacf84e | 8140 | else if (SCM_COMPLEXP (y)) |
8507ec80 | 8141 | return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y), |
0aacf84e | 8142 | SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y)); |
f92e85f7 | 8143 | else if (SCM_FRACTIONP (y)) |
00472a22 | 8144 | return scm_i_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y)); |
0aacf84e MD |
8145 | else |
8146 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 8147 | } |
0aacf84e MD |
8148 | else if (SCM_COMPLEXP (x)) |
8149 | { | |
e11e83f3 | 8150 | if (SCM_I_INUMP (y)) |
5e791807 MW |
8151 | { |
8152 | SCM_SWAP (x, y); | |
8153 | goto xinum; | |
8154 | } | |
0aacf84e MD |
8155 | else if (SCM_BIGP (y)) |
8156 | { | |
8157 | double z = mpz_get_d (SCM_I_BIG_MPZ (y)); | |
8158 | scm_remember_upto_here_1 (y); | |
8507ec80 | 8159 | return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x), |
76506335 | 8160 | z * SCM_COMPLEX_IMAG (x)); |
0aacf84e MD |
8161 | } |
8162 | else if (SCM_REALP (y)) | |
8507ec80 | 8163 | return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x), |
0aacf84e MD |
8164 | SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x)); |
8165 | else if (SCM_COMPLEXP (y)) | |
8166 | { | |
8507ec80 | 8167 | return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y) |
0aacf84e MD |
8168 | - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y), |
8169 | SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y) | |
8170 | + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y)); | |
8171 | } | |
f92e85f7 MV |
8172 | else if (SCM_FRACTIONP (y)) |
8173 | { | |
8174 | double yy = scm_i_fraction2double (y); | |
8507ec80 | 8175 | return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x), |
f92e85f7 MV |
8176 | yy * SCM_COMPLEX_IMAG (x)); |
8177 | } | |
8178 | else | |
8179 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
8180 | } | |
8181 | else if (SCM_FRACTIONP (x)) | |
8182 | { | |
e11e83f3 | 8183 | if (SCM_I_INUMP (y)) |
cba42c93 | 8184 | return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)), |
f92e85f7 MV |
8185 | SCM_FRACTION_DENOMINATOR (x)); |
8186 | else if (SCM_BIGP (y)) | |
cba42c93 | 8187 | return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)), |
f92e85f7 MV |
8188 | SCM_FRACTION_DENOMINATOR (x)); |
8189 | else if (SCM_REALP (y)) | |
00472a22 | 8190 | return scm_i_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y)); |
f92e85f7 MV |
8191 | else if (SCM_COMPLEXP (y)) |
8192 | { | |
8193 | double xx = scm_i_fraction2double (x); | |
8507ec80 | 8194 | return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y), |
f92e85f7 MV |
8195 | xx * SCM_COMPLEX_IMAG (y)); |
8196 | } | |
8197 | else if (SCM_FRACTIONP (y)) | |
8198 | /* a/b * c/d = ac / bd */ | |
cba42c93 | 8199 | return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), |
f92e85f7 MV |
8200 | SCM_FRACTION_NUMERATOR (y)), |
8201 | scm_product (SCM_FRACTION_DENOMINATOR (x), | |
8202 | SCM_FRACTION_DENOMINATOR (y))); | |
0aacf84e MD |
8203 | else |
8204 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product); | |
f4c627b3 | 8205 | } |
0aacf84e | 8206 | else |
f4c627b3 | 8207 | SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product); |
0f2d19dd JB |
8208 | } |
8209 | ||
7351e207 MV |
8210 | #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \ |
8211 | || (defined (HAVE_FINITE) && defined (HAVE_ISNAN))) | |
8212 | #define ALLOW_DIVIDE_BY_ZERO | |
8213 | /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */ | |
8214 | #endif | |
0f2d19dd | 8215 | |
ba74ef4e MV |
8216 | /* The code below for complex division is adapted from the GNU |
8217 | libstdc++, which adapted it from f2c's libF77, and is subject to | |
8218 | this copyright: */ | |
8219 | ||
8220 | /**************************************************************** | |
8221 | Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore. | |
8222 | ||
8223 | Permission to use, copy, modify, and distribute this software | |
8224 | and its documentation for any purpose and without fee is hereby | |
8225 | granted, provided that the above copyright notice appear in all | |
8226 | copies and that both that the copyright notice and this | |
8227 | permission notice and warranty disclaimer appear in supporting | |
8228 | documentation, and that the names of AT&T Bell Laboratories or | |
8229 | Bellcore or any of their entities not be used in advertising or | |
8230 | publicity pertaining to distribution of the software without | |
8231 | specific, written prior permission. | |
8232 | ||
8233 | AT&T and Bellcore disclaim all warranties with regard to this | |
8234 | software, including all implied warranties of merchantability | |
8235 | and fitness. In no event shall AT&T or Bellcore be liable for | |
8236 | any special, indirect or consequential damages or any damages | |
8237 | whatsoever resulting from loss of use, data or profits, whether | |
8238 | in an action of contract, negligence or other tortious action, | |
8239 | arising out of or in connection with the use or performance of | |
8240 | this software. | |
8241 | ****************************************************************/ | |
8242 | ||
78d3deb1 AW |
8243 | SCM_PRIMITIVE_GENERIC (scm_i_divide, "/", 0, 2, 1, |
8244 | (SCM x, SCM y, SCM rest), | |
8245 | "Divide the first argument by the product of the remaining\n" | |
8246 | "arguments. If called with one argument @var{z1}, 1/@var{z1} is\n" | |
8247 | "returned.") | |
8248 | #define FUNC_NAME s_scm_i_divide | |
8249 | { | |
8250 | while (!scm_is_null (rest)) | |
8251 | { x = scm_divide (x, y); | |
8252 | y = scm_car (rest); | |
8253 | rest = scm_cdr (rest); | |
8254 | } | |
8255 | return scm_divide (x, y); | |
8256 | } | |
8257 | #undef FUNC_NAME | |
8258 | ||
8259 | #define s_divide s_scm_i_divide | |
8260 | #define g_divide g_scm_i_divide | |
8261 | ||
98237784 MW |
8262 | SCM |
8263 | scm_divide (SCM x, SCM y) | |
78d3deb1 | 8264 | #define FUNC_NAME s_divide |
0f2d19dd | 8265 | { |
f8de44c1 DH |
8266 | double a; |
8267 | ||
9cc37597 | 8268 | if (SCM_UNLIKELY (SCM_UNBNDP (y))) |
0aacf84e MD |
8269 | { |
8270 | if (SCM_UNBNDP (x)) | |
8271 | SCM_WTA_DISPATCH_0 (g_divide, s_divide); | |
e11e83f3 | 8272 | else if (SCM_I_INUMP (x)) |
0aacf84e | 8273 | { |
e25f3727 | 8274 | scm_t_inum xx = SCM_I_INUM (x); |
0aacf84e MD |
8275 | if (xx == 1 || xx == -1) |
8276 | return x; | |
7351e207 | 8277 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e MD |
8278 | else if (xx == 0) |
8279 | scm_num_overflow (s_divide); | |
7351e207 | 8280 | #endif |
0aacf84e | 8281 | else |
98237784 | 8282 | return scm_i_make_ratio_already_reduced (SCM_INUM1, x); |
0aacf84e MD |
8283 | } |
8284 | else if (SCM_BIGP (x)) | |
98237784 | 8285 | return scm_i_make_ratio_already_reduced (SCM_INUM1, x); |
0aacf84e MD |
8286 | else if (SCM_REALP (x)) |
8287 | { | |
8288 | double xx = SCM_REAL_VALUE (x); | |
7351e207 | 8289 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
8290 | if (xx == 0.0) |
8291 | scm_num_overflow (s_divide); | |
8292 | else | |
7351e207 | 8293 | #endif |
00472a22 | 8294 | return scm_i_from_double (1.0 / xx); |
0aacf84e MD |
8295 | } |
8296 | else if (SCM_COMPLEXP (x)) | |
8297 | { | |
8298 | double r = SCM_COMPLEX_REAL (x); | |
8299 | double i = SCM_COMPLEX_IMAG (x); | |
4c6e36a6 | 8300 | if (fabs(r) <= fabs(i)) |
0aacf84e MD |
8301 | { |
8302 | double t = r / i; | |
8303 | double d = i * (1.0 + t * t); | |
8507ec80 | 8304 | return scm_c_make_rectangular (t / d, -1.0 / d); |
0aacf84e MD |
8305 | } |
8306 | else | |
8307 | { | |
8308 | double t = i / r; | |
8309 | double d = r * (1.0 + t * t); | |
8507ec80 | 8310 | return scm_c_make_rectangular (1.0 / d, -t / d); |
0aacf84e MD |
8311 | } |
8312 | } | |
f92e85f7 | 8313 | else if (SCM_FRACTIONP (x)) |
a285b18c MW |
8314 | return scm_i_make_ratio_already_reduced (SCM_FRACTION_DENOMINATOR (x), |
8315 | SCM_FRACTION_NUMERATOR (x)); | |
0aacf84e MD |
8316 | else |
8317 | SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide); | |
f8de44c1 | 8318 | } |
f8de44c1 | 8319 | |
9cc37597 | 8320 | if (SCM_LIKELY (SCM_I_INUMP (x))) |
0aacf84e | 8321 | { |
e25f3727 | 8322 | scm_t_inum xx = SCM_I_INUM (x); |
9cc37597 | 8323 | if (SCM_LIKELY (SCM_I_INUMP (y))) |
0aacf84e | 8324 | { |
e25f3727 | 8325 | scm_t_inum yy = SCM_I_INUM (y); |
0aacf84e MD |
8326 | if (yy == 0) |
8327 | { | |
7351e207 | 8328 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e | 8329 | scm_num_overflow (s_divide); |
7351e207 | 8330 | #else |
00472a22 | 8331 | return scm_i_from_double ((double) xx / (double) yy); |
7351e207 | 8332 | #endif |
0aacf84e MD |
8333 | } |
8334 | else if (xx % yy != 0) | |
98237784 | 8335 | return scm_i_make_ratio (x, y); |
0aacf84e MD |
8336 | else |
8337 | { | |
e25f3727 | 8338 | scm_t_inum z = xx / yy; |
0aacf84e | 8339 | if (SCM_FIXABLE (z)) |
d956fa6f | 8340 | return SCM_I_MAKINUM (z); |
0aacf84e | 8341 | else |
e25f3727 | 8342 | return scm_i_inum2big (z); |
0aacf84e | 8343 | } |
f872b822 | 8344 | } |
0aacf84e | 8345 | else if (SCM_BIGP (y)) |
98237784 | 8346 | return scm_i_make_ratio (x, y); |
0aacf84e MD |
8347 | else if (SCM_REALP (y)) |
8348 | { | |
8349 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 8350 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
8351 | if (yy == 0.0) |
8352 | scm_num_overflow (s_divide); | |
8353 | else | |
7351e207 | 8354 | #endif |
98237784 MW |
8355 | /* FIXME: Precision may be lost here due to: |
8356 | (1) The cast from 'scm_t_inum' to 'double' | |
8357 | (2) Double rounding */ | |
00472a22 | 8358 | return scm_i_from_double ((double) xx / yy); |
ba74ef4e | 8359 | } |
0aacf84e MD |
8360 | else if (SCM_COMPLEXP (y)) |
8361 | { | |
8362 | a = xx; | |
8363 | complex_div: /* y _must_ be a complex number */ | |
8364 | { | |
8365 | double r = SCM_COMPLEX_REAL (y); | |
8366 | double i = SCM_COMPLEX_IMAG (y); | |
4c6e36a6 | 8367 | if (fabs(r) <= fabs(i)) |
0aacf84e MD |
8368 | { |
8369 | double t = r / i; | |
8370 | double d = i * (1.0 + t * t); | |
8507ec80 | 8371 | return scm_c_make_rectangular ((a * t) / d, -a / d); |
0aacf84e MD |
8372 | } |
8373 | else | |
8374 | { | |
8375 | double t = i / r; | |
8376 | double d = r * (1.0 + t * t); | |
8507ec80 | 8377 | return scm_c_make_rectangular (a / d, -(a * t) / d); |
0aacf84e MD |
8378 | } |
8379 | } | |
8380 | } | |
f92e85f7 MV |
8381 | else if (SCM_FRACTIONP (y)) |
8382 | /* a / b/c = ac / b */ | |
cba42c93 | 8383 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
98237784 | 8384 | SCM_FRACTION_NUMERATOR (y)); |
0aacf84e MD |
8385 | else |
8386 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f8de44c1 | 8387 | } |
0aacf84e MD |
8388 | else if (SCM_BIGP (x)) |
8389 | { | |
e11e83f3 | 8390 | if (SCM_I_INUMP (y)) |
0aacf84e | 8391 | { |
e25f3727 | 8392 | scm_t_inum yy = SCM_I_INUM (y); |
0aacf84e MD |
8393 | if (yy == 0) |
8394 | { | |
7351e207 | 8395 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e | 8396 | scm_num_overflow (s_divide); |
7351e207 | 8397 | #else |
0aacf84e MD |
8398 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (x)); |
8399 | scm_remember_upto_here_1 (x); | |
8400 | return (sgn == 0) ? scm_nan () : scm_inf (); | |
7351e207 | 8401 | #endif |
0aacf84e MD |
8402 | } |
8403 | else if (yy == 1) | |
8404 | return x; | |
8405 | else | |
8406 | { | |
8407 | /* FIXME: HMM, what are the relative performance issues here? | |
8408 | We need to test. Is it faster on average to test | |
8409 | divisible_p, then perform whichever operation, or is it | |
8410 | faster to perform the integer div opportunistically and | |
8411 | switch to real if there's a remainder? For now we take the | |
8412 | middle ground: test, then if divisible, use the faster div | |
8413 | func. */ | |
8414 | ||
e25f3727 | 8415 | scm_t_inum abs_yy = yy < 0 ? -yy : yy; |
0aacf84e MD |
8416 | int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy); |
8417 | ||
8418 | if (divisible_p) | |
8419 | { | |
8420 | SCM result = scm_i_mkbig (); | |
8421 | mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy); | |
8422 | scm_remember_upto_here_1 (x); | |
8423 | if (yy < 0) | |
8424 | mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result)); | |
8425 | return scm_i_normbig (result); | |
8426 | } | |
8427 | else | |
98237784 | 8428 | return scm_i_make_ratio (x, y); |
0aacf84e MD |
8429 | } |
8430 | } | |
8431 | else if (SCM_BIGP (y)) | |
8432 | { | |
98237784 MW |
8433 | int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x), |
8434 | SCM_I_BIG_MPZ (y)); | |
8435 | if (divisible_p) | |
8436 | { | |
8437 | SCM result = scm_i_mkbig (); | |
8438 | mpz_divexact (SCM_I_BIG_MPZ (result), | |
8439 | SCM_I_BIG_MPZ (x), | |
8440 | SCM_I_BIG_MPZ (y)); | |
8441 | scm_remember_upto_here_2 (x, y); | |
8442 | return scm_i_normbig (result); | |
8443 | } | |
8444 | else | |
8445 | return scm_i_make_ratio (x, y); | |
0aacf84e MD |
8446 | } |
8447 | else if (SCM_REALP (y)) | |
8448 | { | |
8449 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 8450 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
8451 | if (yy == 0.0) |
8452 | scm_num_overflow (s_divide); | |
8453 | else | |
7351e207 | 8454 | #endif |
98237784 MW |
8455 | /* FIXME: Precision may be lost here due to: |
8456 | (1) scm_i_big2dbl (2) Double rounding */ | |
00472a22 | 8457 | return scm_i_from_double (scm_i_big2dbl (x) / yy); |
0aacf84e MD |
8458 | } |
8459 | else if (SCM_COMPLEXP (y)) | |
8460 | { | |
8461 | a = scm_i_big2dbl (x); | |
8462 | goto complex_div; | |
8463 | } | |
f92e85f7 | 8464 | else if (SCM_FRACTIONP (y)) |
cba42c93 | 8465 | return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)), |
98237784 | 8466 | SCM_FRACTION_NUMERATOR (y)); |
0aacf84e MD |
8467 | else |
8468 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f872b822 | 8469 | } |
0aacf84e MD |
8470 | else if (SCM_REALP (x)) |
8471 | { | |
8472 | double rx = SCM_REAL_VALUE (x); | |
e11e83f3 | 8473 | if (SCM_I_INUMP (y)) |
0aacf84e | 8474 | { |
e25f3727 | 8475 | scm_t_inum yy = SCM_I_INUM (y); |
7351e207 | 8476 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e MD |
8477 | if (yy == 0) |
8478 | scm_num_overflow (s_divide); | |
8479 | else | |
7351e207 | 8480 | #endif |
98237784 MW |
8481 | /* FIXME: Precision may be lost here due to: |
8482 | (1) The cast from 'scm_t_inum' to 'double' | |
8483 | (2) Double rounding */ | |
00472a22 | 8484 | return scm_i_from_double (rx / (double) yy); |
0aacf84e MD |
8485 | } |
8486 | else if (SCM_BIGP (y)) | |
8487 | { | |
98237784 MW |
8488 | /* FIXME: Precision may be lost here due to: |
8489 | (1) The conversion from bignum to double | |
8490 | (2) Double rounding */ | |
0aacf84e MD |
8491 | double dby = mpz_get_d (SCM_I_BIG_MPZ (y)); |
8492 | scm_remember_upto_here_1 (y); | |
00472a22 | 8493 | return scm_i_from_double (rx / dby); |
0aacf84e MD |
8494 | } |
8495 | else if (SCM_REALP (y)) | |
8496 | { | |
8497 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 8498 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
8499 | if (yy == 0.0) |
8500 | scm_num_overflow (s_divide); | |
8501 | else | |
7351e207 | 8502 | #endif |
00472a22 | 8503 | return scm_i_from_double (rx / yy); |
0aacf84e MD |
8504 | } |
8505 | else if (SCM_COMPLEXP (y)) | |
8506 | { | |
8507 | a = rx; | |
8508 | goto complex_div; | |
8509 | } | |
f92e85f7 | 8510 | else if (SCM_FRACTIONP (y)) |
00472a22 | 8511 | return scm_i_from_double (rx / scm_i_fraction2double (y)); |
0aacf84e MD |
8512 | else |
8513 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f872b822 | 8514 | } |
0aacf84e MD |
8515 | else if (SCM_COMPLEXP (x)) |
8516 | { | |
8517 | double rx = SCM_COMPLEX_REAL (x); | |
8518 | double ix = SCM_COMPLEX_IMAG (x); | |
e11e83f3 | 8519 | if (SCM_I_INUMP (y)) |
0aacf84e | 8520 | { |
e25f3727 | 8521 | scm_t_inum yy = SCM_I_INUM (y); |
7351e207 | 8522 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
0aacf84e MD |
8523 | if (yy == 0) |
8524 | scm_num_overflow (s_divide); | |
8525 | else | |
7351e207 | 8526 | #endif |
0aacf84e | 8527 | { |
98237784 MW |
8528 | /* FIXME: Precision may be lost here due to: |
8529 | (1) The conversion from 'scm_t_inum' to double | |
8530 | (2) Double rounding */ | |
0aacf84e | 8531 | double d = yy; |
8507ec80 | 8532 | return scm_c_make_rectangular (rx / d, ix / d); |
0aacf84e MD |
8533 | } |
8534 | } | |
8535 | else if (SCM_BIGP (y)) | |
8536 | { | |
98237784 MW |
8537 | /* FIXME: Precision may be lost here due to: |
8538 | (1) The conversion from bignum to double | |
8539 | (2) Double rounding */ | |
0aacf84e MD |
8540 | double dby = mpz_get_d (SCM_I_BIG_MPZ (y)); |
8541 | scm_remember_upto_here_1 (y); | |
8507ec80 | 8542 | return scm_c_make_rectangular (rx / dby, ix / dby); |
0aacf84e MD |
8543 | } |
8544 | else if (SCM_REALP (y)) | |
8545 | { | |
8546 | double yy = SCM_REAL_VALUE (y); | |
7351e207 | 8547 | #ifndef ALLOW_DIVIDE_BY_ZERO |
0aacf84e MD |
8548 | if (yy == 0.0) |
8549 | scm_num_overflow (s_divide); | |
8550 | else | |
7351e207 | 8551 | #endif |
8507ec80 | 8552 | return scm_c_make_rectangular (rx / yy, ix / yy); |
0aacf84e MD |
8553 | } |
8554 | else if (SCM_COMPLEXP (y)) | |
8555 | { | |
8556 | double ry = SCM_COMPLEX_REAL (y); | |
8557 | double iy = SCM_COMPLEX_IMAG (y); | |
4c6e36a6 | 8558 | if (fabs(ry) <= fabs(iy)) |
0aacf84e MD |
8559 | { |
8560 | double t = ry / iy; | |
8561 | double d = iy * (1.0 + t * t); | |
8507ec80 | 8562 | return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d); |
0aacf84e MD |
8563 | } |
8564 | else | |
8565 | { | |
8566 | double t = iy / ry; | |
8567 | double d = ry * (1.0 + t * t); | |
8507ec80 | 8568 | return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d); |
0aacf84e MD |
8569 | } |
8570 | } | |
f92e85f7 MV |
8571 | else if (SCM_FRACTIONP (y)) |
8572 | { | |
98237784 MW |
8573 | /* FIXME: Precision may be lost here due to: |
8574 | (1) The conversion from fraction to double | |
8575 | (2) Double rounding */ | |
f92e85f7 | 8576 | double yy = scm_i_fraction2double (y); |
8507ec80 | 8577 | return scm_c_make_rectangular (rx / yy, ix / yy); |
f92e85f7 | 8578 | } |
0aacf84e MD |
8579 | else |
8580 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
f8de44c1 | 8581 | } |
f92e85f7 MV |
8582 | else if (SCM_FRACTIONP (x)) |
8583 | { | |
e11e83f3 | 8584 | if (SCM_I_INUMP (y)) |
f92e85f7 | 8585 | { |
e25f3727 | 8586 | scm_t_inum yy = SCM_I_INUM (y); |
f92e85f7 MV |
8587 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
8588 | if (yy == 0) | |
8589 | scm_num_overflow (s_divide); | |
8590 | else | |
8591 | #endif | |
cba42c93 | 8592 | return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x), |
98237784 | 8593 | scm_product (SCM_FRACTION_DENOMINATOR (x), y)); |
f92e85f7 MV |
8594 | } |
8595 | else if (SCM_BIGP (y)) | |
8596 | { | |
cba42c93 | 8597 | return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x), |
98237784 | 8598 | scm_product (SCM_FRACTION_DENOMINATOR (x), y)); |
f92e85f7 MV |
8599 | } |
8600 | else if (SCM_REALP (y)) | |
8601 | { | |
8602 | double yy = SCM_REAL_VALUE (y); | |
8603 | #ifndef ALLOW_DIVIDE_BY_ZERO | |
8604 | if (yy == 0.0) | |
8605 | scm_num_overflow (s_divide); | |
8606 | else | |
8607 | #endif | |
98237784 MW |
8608 | /* FIXME: Precision may be lost here due to: |
8609 | (1) The conversion from fraction to double | |
8610 | (2) Double rounding */ | |
00472a22 | 8611 | return scm_i_from_double (scm_i_fraction2double (x) / yy); |
f92e85f7 MV |
8612 | } |
8613 | else if (SCM_COMPLEXP (y)) | |
8614 | { | |
98237784 MW |
8615 | /* FIXME: Precision may be lost here due to: |
8616 | (1) The conversion from fraction to double | |
8617 | (2) Double rounding */ | |
f92e85f7 MV |
8618 | a = scm_i_fraction2double (x); |
8619 | goto complex_div; | |
8620 | } | |
8621 | else if (SCM_FRACTIONP (y)) | |
cba42c93 | 8622 | return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)), |
98237784 | 8623 | scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))); |
f92e85f7 MV |
8624 | else |
8625 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide); | |
8626 | } | |
0aacf84e | 8627 | else |
f8de44c1 | 8628 | SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide); |
0f2d19dd | 8629 | } |
c05e97b7 | 8630 | #undef FUNC_NAME |
0f2d19dd | 8631 | |
fa605590 | 8632 | |
0f2d19dd | 8633 | double |
3101f40f | 8634 | scm_c_truncate (double x) |
0f2d19dd | 8635 | { |
fa605590 | 8636 | return trunc (x); |
0f2d19dd | 8637 | } |
0f2d19dd | 8638 | |
3101f40f MV |
8639 | /* scm_c_round is done using floor(x+0.5) to round to nearest and with |
8640 | half-way case (ie. when x is an integer plus 0.5) going upwards. | |
8641 | Then half-way cases are identified and adjusted down if the | |
8642 | round-upwards didn't give the desired even integer. | |
6187f48b KR |
8643 | |
8644 | "plus_half == result" identifies a half-way case. If plus_half, which is | |
8645 | x + 0.5, is an integer then x must be an integer plus 0.5. | |
8646 | ||
8647 | An odd "result" value is identified with result/2 != floor(result/2). | |
8648 | This is done with plus_half, since that value is ready for use sooner in | |
8649 | a pipelined cpu, and we're already requiring plus_half == result. | |
8650 | ||
8651 | Note however that we need to be careful when x is big and already an | |
8652 | integer. In that case "x+0.5" may round to an adjacent integer, causing | |
8653 | us to return such a value, incorrectly. For instance if the hardware is | |
8654 | in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF | |
8655 | (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value | |
8656 | returned. Or if the hardware is in round-upwards mode, then other bigger | |
8657 | values like say x == 2^128 will see x+0.5 rounding up to the next higher | |
8658 | representable value, 2^128+2^76 (or whatever), again incorrect. | |
8659 | ||
8660 | These bad roundings of x+0.5 are avoided by testing at the start whether | |
8661 | x is already an integer. If it is then clearly that's the desired result | |
8662 | already. And if it's not then the exponent must be small enough to allow | |
8663 | an 0.5 to be represented, and hence added without a bad rounding. */ | |
8664 | ||
0f2d19dd | 8665 | double |
3101f40f | 8666 | scm_c_round (double x) |
0f2d19dd | 8667 | { |
6187f48b KR |
8668 | double plus_half, result; |
8669 | ||
8670 | if (x == floor (x)) | |
8671 | return x; | |
8672 | ||
8673 | plus_half = x + 0.5; | |
8674 | result = floor (plus_half); | |
3101f40f | 8675 | /* Adjust so that the rounding is towards even. */ |
0aacf84e MD |
8676 | return ((plus_half == result && plus_half / 2 != floor (plus_half / 2)) |
8677 | ? result - 1 | |
8678 | : result); | |
0f2d19dd JB |
8679 | } |
8680 | ||
8b56bcec MW |
8681 | SCM_PRIMITIVE_GENERIC (scm_truncate_number, "truncate", 1, 0, 0, |
8682 | (SCM x), | |
8683 | "Round the number @var{x} towards zero.") | |
f92e85f7 MV |
8684 | #define FUNC_NAME s_scm_truncate_number |
8685 | { | |
8b56bcec MW |
8686 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
8687 | return x; | |
8688 | else if (SCM_REALP (x)) | |
00472a22 | 8689 | return scm_i_from_double (trunc (SCM_REAL_VALUE (x))); |
8b56bcec MW |
8690 | else if (SCM_FRACTIONP (x)) |
8691 | return scm_truncate_quotient (SCM_FRACTION_NUMERATOR (x), | |
8692 | SCM_FRACTION_DENOMINATOR (x)); | |
f92e85f7 | 8693 | else |
8b56bcec MW |
8694 | SCM_WTA_DISPATCH_1 (g_scm_truncate_number, x, SCM_ARG1, |
8695 | s_scm_truncate_number); | |
f92e85f7 MV |
8696 | } |
8697 | #undef FUNC_NAME | |
8698 | ||
8b56bcec MW |
8699 | SCM_PRIMITIVE_GENERIC (scm_round_number, "round", 1, 0, 0, |
8700 | (SCM x), | |
8701 | "Round the number @var{x} towards the nearest integer. " | |
8702 | "When it is exactly halfway between two integers, " | |
8703 | "round towards the even one.") | |
f92e85f7 MV |
8704 | #define FUNC_NAME s_scm_round_number |
8705 | { | |
e11e83f3 | 8706 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
bae30667 KR |
8707 | return x; |
8708 | else if (SCM_REALP (x)) | |
00472a22 | 8709 | return scm_i_from_double (scm_c_round (SCM_REAL_VALUE (x))); |
8b56bcec MW |
8710 | else if (SCM_FRACTIONP (x)) |
8711 | return scm_round_quotient (SCM_FRACTION_NUMERATOR (x), | |
8712 | SCM_FRACTION_DENOMINATOR (x)); | |
f92e85f7 | 8713 | else |
8b56bcec MW |
8714 | SCM_WTA_DISPATCH_1 (g_scm_round_number, x, SCM_ARG1, |
8715 | s_scm_round_number); | |
f92e85f7 MV |
8716 | } |
8717 | #undef FUNC_NAME | |
8718 | ||
8719 | SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0, | |
8720 | (SCM x), | |
8721 | "Round the number @var{x} towards minus infinity.") | |
8722 | #define FUNC_NAME s_scm_floor | |
8723 | { | |
e11e83f3 | 8724 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
f92e85f7 MV |
8725 | return x; |
8726 | else if (SCM_REALP (x)) | |
00472a22 | 8727 | return scm_i_from_double (floor (SCM_REAL_VALUE (x))); |
f92e85f7 | 8728 | else if (SCM_FRACTIONP (x)) |
8b56bcec MW |
8729 | return scm_floor_quotient (SCM_FRACTION_NUMERATOR (x), |
8730 | SCM_FRACTION_DENOMINATOR (x)); | |
f92e85f7 MV |
8731 | else |
8732 | SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor); | |
8733 | } | |
8734 | #undef FUNC_NAME | |
8735 | ||
8736 | SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0, | |
8737 | (SCM x), | |
8738 | "Round the number @var{x} towards infinity.") | |
8739 | #define FUNC_NAME s_scm_ceiling | |
8740 | { | |
e11e83f3 | 8741 | if (SCM_I_INUMP (x) || SCM_BIGP (x)) |
f92e85f7 MV |
8742 | return x; |
8743 | else if (SCM_REALP (x)) | |
00472a22 | 8744 | return scm_i_from_double (ceil (SCM_REAL_VALUE (x))); |
f92e85f7 | 8745 | else if (SCM_FRACTIONP (x)) |
8b56bcec MW |
8746 | return scm_ceiling_quotient (SCM_FRACTION_NUMERATOR (x), |
8747 | SCM_FRACTION_DENOMINATOR (x)); | |
f92e85f7 MV |
8748 | else |
8749 | SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling); | |
8750 | } | |
8751 | #undef FUNC_NAME | |
0f2d19dd | 8752 | |
2519490c MW |
8753 | SCM_PRIMITIVE_GENERIC (scm_expt, "expt", 2, 0, 0, |
8754 | (SCM x, SCM y), | |
8755 | "Return @var{x} raised to the power of @var{y}.") | |
6fc4d012 | 8756 | #define FUNC_NAME s_scm_expt |
0f2d19dd | 8757 | { |
01c7284a MW |
8758 | if (scm_is_integer (y)) |
8759 | { | |
8760 | if (scm_is_true (scm_exact_p (y))) | |
8761 | return scm_integer_expt (x, y); | |
8762 | else | |
8763 | { | |
8764 | /* Here we handle the case where the exponent is an inexact | |
8765 | integer. We make the exponent exact in order to use | |
8766 | scm_integer_expt, and thus avoid the spurious imaginary | |
8767 | parts that may result from round-off errors in the general | |
8768 | e^(y log x) method below (for example when squaring a large | |
8769 | negative number). In this case, we must return an inexact | |
8770 | result for correctness. We also make the base inexact so | |
8771 | that scm_integer_expt will use fast inexact arithmetic | |
8772 | internally. Note that making the base inexact is not | |
8773 | sufficient to guarantee an inexact result, because | |
8774 | scm_integer_expt will return an exact 1 when the exponent | |
8775 | is 0, even if the base is inexact. */ | |
8776 | return scm_exact_to_inexact | |
8777 | (scm_integer_expt (scm_exact_to_inexact (x), | |
8778 | scm_inexact_to_exact (y))); | |
8779 | } | |
8780 | } | |
6fc4d012 AW |
8781 | else if (scm_is_real (x) && scm_is_real (y) && scm_to_double (x) >= 0.0) |
8782 | { | |
00472a22 | 8783 | return scm_i_from_double (pow (scm_to_double (x), scm_to_double (y))); |
6fc4d012 | 8784 | } |
2519490c | 8785 | else if (scm_is_complex (x) && scm_is_complex (y)) |
6fc4d012 | 8786 | return scm_exp (scm_product (scm_log (x), y)); |
2519490c MW |
8787 | else if (scm_is_complex (x)) |
8788 | SCM_WTA_DISPATCH_2 (g_scm_expt, x, y, SCM_ARG2, s_scm_expt); | |
8789 | else | |
8790 | SCM_WTA_DISPATCH_2 (g_scm_expt, x, y, SCM_ARG1, s_scm_expt); | |
0f2d19dd | 8791 | } |
1bbd0b84 | 8792 | #undef FUNC_NAME |
0f2d19dd | 8793 | |
7f41099e MW |
8794 | /* sin/cos/tan/asin/acos/atan |
8795 | sinh/cosh/tanh/asinh/acosh/atanh | |
8796 | Derived from "Transcen.scm", Complex trancendental functions for SCM. | |
8797 | Written by Jerry D. Hedden, (C) FSF. | |
8798 | See the file `COPYING' for terms applying to this program. */ | |
8799 | ||
ad79736c AW |
8800 | SCM_PRIMITIVE_GENERIC (scm_sin, "sin", 1, 0, 0, |
8801 | (SCM z), | |
8802 | "Compute the sine of @var{z}.") | |
8803 | #define FUNC_NAME s_scm_sin | |
8804 | { | |
8deddc94 MW |
8805 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
8806 | return z; /* sin(exact0) = exact0 */ | |
8807 | else if (scm_is_real (z)) | |
00472a22 | 8808 | return scm_i_from_double (sin (scm_to_double (z))); |
ad79736c AW |
8809 | else if (SCM_COMPLEXP (z)) |
8810 | { double x, y; | |
8811 | x = SCM_COMPLEX_REAL (z); | |
8812 | y = SCM_COMPLEX_IMAG (z); | |
8813 | return scm_c_make_rectangular (sin (x) * cosh (y), | |
8814 | cos (x) * sinh (y)); | |
8815 | } | |
8816 | else | |
8817 | SCM_WTA_DISPATCH_1 (g_scm_sin, z, 1, s_scm_sin); | |
8818 | } | |
8819 | #undef FUNC_NAME | |
0f2d19dd | 8820 | |
ad79736c AW |
8821 | SCM_PRIMITIVE_GENERIC (scm_cos, "cos", 1, 0, 0, |
8822 | (SCM z), | |
8823 | "Compute the cosine of @var{z}.") | |
8824 | #define FUNC_NAME s_scm_cos | |
8825 | { | |
8deddc94 MW |
8826 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
8827 | return SCM_INUM1; /* cos(exact0) = exact1 */ | |
8828 | else if (scm_is_real (z)) | |
00472a22 | 8829 | return scm_i_from_double (cos (scm_to_double (z))); |
ad79736c AW |
8830 | else if (SCM_COMPLEXP (z)) |
8831 | { double x, y; | |
8832 | x = SCM_COMPLEX_REAL (z); | |
8833 | y = SCM_COMPLEX_IMAG (z); | |
8834 | return scm_c_make_rectangular (cos (x) * cosh (y), | |
8835 | -sin (x) * sinh (y)); | |
8836 | } | |
8837 | else | |
8838 | SCM_WTA_DISPATCH_1 (g_scm_cos, z, 1, s_scm_cos); | |
8839 | } | |
8840 | #undef FUNC_NAME | |
8841 | ||
8842 | SCM_PRIMITIVE_GENERIC (scm_tan, "tan", 1, 0, 0, | |
8843 | (SCM z), | |
8844 | "Compute the tangent of @var{z}.") | |
8845 | #define FUNC_NAME s_scm_tan | |
0f2d19dd | 8846 | { |
8deddc94 MW |
8847 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
8848 | return z; /* tan(exact0) = exact0 */ | |
8849 | else if (scm_is_real (z)) | |
00472a22 | 8850 | return scm_i_from_double (tan (scm_to_double (z))); |
ad79736c AW |
8851 | else if (SCM_COMPLEXP (z)) |
8852 | { double x, y, w; | |
8853 | x = 2.0 * SCM_COMPLEX_REAL (z); | |
8854 | y = 2.0 * SCM_COMPLEX_IMAG (z); | |
8855 | w = cos (x) + cosh (y); | |
8856 | #ifndef ALLOW_DIVIDE_BY_ZERO | |
8857 | if (w == 0.0) | |
8858 | scm_num_overflow (s_scm_tan); | |
8859 | #endif | |
8860 | return scm_c_make_rectangular (sin (x) / w, sinh (y) / w); | |
8861 | } | |
8862 | else | |
8863 | SCM_WTA_DISPATCH_1 (g_scm_tan, z, 1, s_scm_tan); | |
8864 | } | |
8865 | #undef FUNC_NAME | |
8866 | ||
8867 | SCM_PRIMITIVE_GENERIC (scm_sinh, "sinh", 1, 0, 0, | |
8868 | (SCM z), | |
8869 | "Compute the hyperbolic sine of @var{z}.") | |
8870 | #define FUNC_NAME s_scm_sinh | |
8871 | { | |
8deddc94 MW |
8872 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
8873 | return z; /* sinh(exact0) = exact0 */ | |
8874 | else if (scm_is_real (z)) | |
00472a22 | 8875 | return scm_i_from_double (sinh (scm_to_double (z))); |
ad79736c AW |
8876 | else if (SCM_COMPLEXP (z)) |
8877 | { double x, y; | |
8878 | x = SCM_COMPLEX_REAL (z); | |
8879 | y = SCM_COMPLEX_IMAG (z); | |
8880 | return scm_c_make_rectangular (sinh (x) * cos (y), | |
8881 | cosh (x) * sin (y)); | |
8882 | } | |
8883 | else | |
8884 | SCM_WTA_DISPATCH_1 (g_scm_sinh, z, 1, s_scm_sinh); | |
8885 | } | |
8886 | #undef FUNC_NAME | |
8887 | ||
8888 | SCM_PRIMITIVE_GENERIC (scm_cosh, "cosh", 1, 0, 0, | |
8889 | (SCM z), | |
8890 | "Compute the hyperbolic cosine of @var{z}.") | |
8891 | #define FUNC_NAME s_scm_cosh | |
8892 | { | |
8deddc94 MW |
8893 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
8894 | return SCM_INUM1; /* cosh(exact0) = exact1 */ | |
8895 | else if (scm_is_real (z)) | |
00472a22 | 8896 | return scm_i_from_double (cosh (scm_to_double (z))); |
ad79736c AW |
8897 | else if (SCM_COMPLEXP (z)) |
8898 | { double x, y; | |
8899 | x = SCM_COMPLEX_REAL (z); | |
8900 | y = SCM_COMPLEX_IMAG (z); | |
8901 | return scm_c_make_rectangular (cosh (x) * cos (y), | |
8902 | sinh (x) * sin (y)); | |
8903 | } | |
8904 | else | |
8905 | SCM_WTA_DISPATCH_1 (g_scm_cosh, z, 1, s_scm_cosh); | |
8906 | } | |
8907 | #undef FUNC_NAME | |
8908 | ||
8909 | SCM_PRIMITIVE_GENERIC (scm_tanh, "tanh", 1, 0, 0, | |
8910 | (SCM z), | |
8911 | "Compute the hyperbolic tangent of @var{z}.") | |
8912 | #define FUNC_NAME s_scm_tanh | |
8913 | { | |
8deddc94 MW |
8914 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
8915 | return z; /* tanh(exact0) = exact0 */ | |
8916 | else if (scm_is_real (z)) | |
00472a22 | 8917 | return scm_i_from_double (tanh (scm_to_double (z))); |
ad79736c AW |
8918 | else if (SCM_COMPLEXP (z)) |
8919 | { double x, y, w; | |
8920 | x = 2.0 * SCM_COMPLEX_REAL (z); | |
8921 | y = 2.0 * SCM_COMPLEX_IMAG (z); | |
8922 | w = cosh (x) + cos (y); | |
8923 | #ifndef ALLOW_DIVIDE_BY_ZERO | |
8924 | if (w == 0.0) | |
8925 | scm_num_overflow (s_scm_tanh); | |
8926 | #endif | |
8927 | return scm_c_make_rectangular (sinh (x) / w, sin (y) / w); | |
8928 | } | |
8929 | else | |
8930 | SCM_WTA_DISPATCH_1 (g_scm_tanh, z, 1, s_scm_tanh); | |
8931 | } | |
8932 | #undef FUNC_NAME | |
8933 | ||
8934 | SCM_PRIMITIVE_GENERIC (scm_asin, "asin", 1, 0, 0, | |
8935 | (SCM z), | |
8936 | "Compute the arc sine of @var{z}.") | |
8937 | #define FUNC_NAME s_scm_asin | |
8938 | { | |
8deddc94 MW |
8939 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
8940 | return z; /* asin(exact0) = exact0 */ | |
8941 | else if (scm_is_real (z)) | |
ad79736c AW |
8942 | { |
8943 | double w = scm_to_double (z); | |
8944 | if (w >= -1.0 && w <= 1.0) | |
00472a22 | 8945 | return scm_i_from_double (asin (w)); |
ad79736c AW |
8946 | else |
8947 | return scm_product (scm_c_make_rectangular (0, -1), | |
8948 | scm_sys_asinh (scm_c_make_rectangular (0, w))); | |
8949 | } | |
8950 | else if (SCM_COMPLEXP (z)) | |
8951 | { double x, y; | |
8952 | x = SCM_COMPLEX_REAL (z); | |
8953 | y = SCM_COMPLEX_IMAG (z); | |
8954 | return scm_product (scm_c_make_rectangular (0, -1), | |
8955 | scm_sys_asinh (scm_c_make_rectangular (-y, x))); | |
8956 | } | |
8957 | else | |
8958 | SCM_WTA_DISPATCH_1 (g_scm_asin, z, 1, s_scm_asin); | |
8959 | } | |
8960 | #undef FUNC_NAME | |
8961 | ||
8962 | SCM_PRIMITIVE_GENERIC (scm_acos, "acos", 1, 0, 0, | |
8963 | (SCM z), | |
8964 | "Compute the arc cosine of @var{z}.") | |
8965 | #define FUNC_NAME s_scm_acos | |
8966 | { | |
8deddc94 MW |
8967 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1))) |
8968 | return SCM_INUM0; /* acos(exact1) = exact0 */ | |
8969 | else if (scm_is_real (z)) | |
ad79736c AW |
8970 | { |
8971 | double w = scm_to_double (z); | |
8972 | if (w >= -1.0 && w <= 1.0) | |
00472a22 | 8973 | return scm_i_from_double (acos (w)); |
ad79736c | 8974 | else |
00472a22 | 8975 | return scm_sum (scm_i_from_double (acos (0.0)), |
ad79736c AW |
8976 | scm_product (scm_c_make_rectangular (0, 1), |
8977 | scm_sys_asinh (scm_c_make_rectangular (0, w)))); | |
8978 | } | |
8979 | else if (SCM_COMPLEXP (z)) | |
8980 | { double x, y; | |
8981 | x = SCM_COMPLEX_REAL (z); | |
8982 | y = SCM_COMPLEX_IMAG (z); | |
00472a22 | 8983 | return scm_sum (scm_i_from_double (acos (0.0)), |
ad79736c AW |
8984 | scm_product (scm_c_make_rectangular (0, 1), |
8985 | scm_sys_asinh (scm_c_make_rectangular (-y, x)))); | |
8986 | } | |
8987 | else | |
8988 | SCM_WTA_DISPATCH_1 (g_scm_acos, z, 1, s_scm_acos); | |
8989 | } | |
8990 | #undef FUNC_NAME | |
8991 | ||
8992 | SCM_PRIMITIVE_GENERIC (scm_atan, "atan", 1, 1, 0, | |
8993 | (SCM z, SCM y), | |
8994 | "With one argument, compute the arc tangent of @var{z}.\n" | |
8995 | "If @var{y} is present, compute the arc tangent of @var{z}/@var{y},\n" | |
8996 | "using the sign of @var{z} and @var{y} to determine the quadrant.") | |
8997 | #define FUNC_NAME s_scm_atan | |
8998 | { | |
8999 | if (SCM_UNBNDP (y)) | |
9000 | { | |
8deddc94 MW |
9001 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
9002 | return z; /* atan(exact0) = exact0 */ | |
9003 | else if (scm_is_real (z)) | |
00472a22 | 9004 | return scm_i_from_double (atan (scm_to_double (z))); |
ad79736c AW |
9005 | else if (SCM_COMPLEXP (z)) |
9006 | { | |
9007 | double v, w; | |
9008 | v = SCM_COMPLEX_REAL (z); | |
9009 | w = SCM_COMPLEX_IMAG (z); | |
9010 | return scm_divide (scm_log (scm_divide (scm_c_make_rectangular (v, w - 1.0), | |
9011 | scm_c_make_rectangular (v, w + 1.0))), | |
9012 | scm_c_make_rectangular (0, 2)); | |
9013 | } | |
9014 | else | |
18104cac | 9015 | SCM_WTA_DISPATCH_1 (g_scm_atan, z, SCM_ARG1, s_scm_atan); |
ad79736c AW |
9016 | } |
9017 | else if (scm_is_real (z)) | |
9018 | { | |
9019 | if (scm_is_real (y)) | |
00472a22 | 9020 | return scm_i_from_double (atan2 (scm_to_double (z), scm_to_double (y))); |
ad79736c AW |
9021 | else |
9022 | SCM_WTA_DISPATCH_2 (g_scm_atan, z, y, SCM_ARG2, s_scm_atan); | |
9023 | } | |
9024 | else | |
9025 | SCM_WTA_DISPATCH_2 (g_scm_atan, z, y, SCM_ARG1, s_scm_atan); | |
9026 | } | |
9027 | #undef FUNC_NAME | |
9028 | ||
9029 | SCM_PRIMITIVE_GENERIC (scm_sys_asinh, "asinh", 1, 0, 0, | |
9030 | (SCM z), | |
9031 | "Compute the inverse hyperbolic sine of @var{z}.") | |
9032 | #define FUNC_NAME s_scm_sys_asinh | |
9033 | { | |
8deddc94 MW |
9034 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
9035 | return z; /* asinh(exact0) = exact0 */ | |
9036 | else if (scm_is_real (z)) | |
00472a22 | 9037 | return scm_i_from_double (asinh (scm_to_double (z))); |
ad79736c AW |
9038 | else if (scm_is_number (z)) |
9039 | return scm_log (scm_sum (z, | |
9040 | scm_sqrt (scm_sum (scm_product (z, z), | |
cff5fa33 | 9041 | SCM_INUM1)))); |
ad79736c AW |
9042 | else |
9043 | SCM_WTA_DISPATCH_1 (g_scm_sys_asinh, z, 1, s_scm_sys_asinh); | |
9044 | } | |
9045 | #undef FUNC_NAME | |
9046 | ||
9047 | SCM_PRIMITIVE_GENERIC (scm_sys_acosh, "acosh", 1, 0, 0, | |
9048 | (SCM z), | |
9049 | "Compute the inverse hyperbolic cosine of @var{z}.") | |
9050 | #define FUNC_NAME s_scm_sys_acosh | |
9051 | { | |
8deddc94 MW |
9052 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1))) |
9053 | return SCM_INUM0; /* acosh(exact1) = exact0 */ | |
9054 | else if (scm_is_real (z) && scm_to_double (z) >= 1.0) | |
00472a22 | 9055 | return scm_i_from_double (acosh (scm_to_double (z))); |
ad79736c AW |
9056 | else if (scm_is_number (z)) |
9057 | return scm_log (scm_sum (z, | |
9058 | scm_sqrt (scm_difference (scm_product (z, z), | |
cff5fa33 | 9059 | SCM_INUM1)))); |
ad79736c AW |
9060 | else |
9061 | SCM_WTA_DISPATCH_1 (g_scm_sys_acosh, z, 1, s_scm_sys_acosh); | |
9062 | } | |
9063 | #undef FUNC_NAME | |
9064 | ||
9065 | SCM_PRIMITIVE_GENERIC (scm_sys_atanh, "atanh", 1, 0, 0, | |
9066 | (SCM z), | |
9067 | "Compute the inverse hyperbolic tangent of @var{z}.") | |
9068 | #define FUNC_NAME s_scm_sys_atanh | |
9069 | { | |
8deddc94 MW |
9070 | if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0))) |
9071 | return z; /* atanh(exact0) = exact0 */ | |
9072 | else if (scm_is_real (z) && scm_to_double (z) >= -1.0 && scm_to_double (z) <= 1.0) | |
00472a22 | 9073 | return scm_i_from_double (atanh (scm_to_double (z))); |
ad79736c | 9074 | else if (scm_is_number (z)) |
cff5fa33 MW |
9075 | return scm_divide (scm_log (scm_divide (scm_sum (SCM_INUM1, z), |
9076 | scm_difference (SCM_INUM1, z))), | |
ad79736c AW |
9077 | SCM_I_MAKINUM (2)); |
9078 | else | |
9079 | SCM_WTA_DISPATCH_1 (g_scm_sys_atanh, z, 1, s_scm_sys_atanh); | |
0f2d19dd | 9080 | } |
1bbd0b84 | 9081 | #undef FUNC_NAME |
0f2d19dd | 9082 | |
8507ec80 MV |
9083 | SCM |
9084 | scm_c_make_rectangular (double re, double im) | |
9085 | { | |
c7218482 | 9086 | SCM z; |
03604fcf | 9087 | |
c7218482 MW |
9088 | z = PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_complex), |
9089 | "complex")); | |
9090 | SCM_SET_CELL_TYPE (z, scm_tc16_complex); | |
9091 | SCM_COMPLEX_REAL (z) = re; | |
9092 | SCM_COMPLEX_IMAG (z) = im; | |
9093 | return z; | |
8507ec80 | 9094 | } |
0f2d19dd | 9095 | |
a1ec6916 | 9096 | SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0, |
a2c25234 | 9097 | (SCM real_part, SCM imaginary_part), |
b7e64f8b BT |
9098 | "Return a complex number constructed of the given @var{real_part} " |
9099 | "and @var{imaginary_part} parts.") | |
1bbd0b84 | 9100 | #define FUNC_NAME s_scm_make_rectangular |
0f2d19dd | 9101 | { |
ad79736c AW |
9102 | SCM_ASSERT_TYPE (scm_is_real (real_part), real_part, |
9103 | SCM_ARG1, FUNC_NAME, "real"); | |
9104 | SCM_ASSERT_TYPE (scm_is_real (imaginary_part), imaginary_part, | |
9105 | SCM_ARG2, FUNC_NAME, "real"); | |
c7218482 MW |
9106 | |
9107 | /* Return a real if and only if the imaginary_part is an _exact_ 0 */ | |
9108 | if (scm_is_eq (imaginary_part, SCM_INUM0)) | |
9109 | return real_part; | |
9110 | else | |
9111 | return scm_c_make_rectangular (scm_to_double (real_part), | |
9112 | scm_to_double (imaginary_part)); | |
0f2d19dd | 9113 | } |
1bbd0b84 | 9114 | #undef FUNC_NAME |
0f2d19dd | 9115 | |
8507ec80 MV |
9116 | SCM |
9117 | scm_c_make_polar (double mag, double ang) | |
9118 | { | |
9119 | double s, c; | |
5e647d08 LC |
9120 | |
9121 | /* The sincos(3) function is undocumented an broken on Tru64. Thus we only | |
9122 | use it on Glibc-based systems that have it (it's a GNU extension). See | |
9123 | http://lists.gnu.org/archive/html/guile-user/2009-04/msg00033.html for | |
9124 | details. */ | |
9125 | #if (defined HAVE_SINCOS) && (defined __GLIBC__) && (defined _GNU_SOURCE) | |
8507ec80 MV |
9126 | sincos (ang, &s, &c); |
9127 | #else | |
9128 | s = sin (ang); | |
9129 | c = cos (ang); | |
9130 | #endif | |
9d427b2c MW |
9131 | |
9132 | /* If s and c are NaNs, this indicates that the angle is a NaN, | |
9133 | infinite, or perhaps simply too large to determine its value | |
9134 | mod 2*pi. However, we know something that the floating-point | |
9135 | implementation doesn't know: We know that s and c are finite. | |
9136 | Therefore, if the magnitude is zero, return a complex zero. | |
9137 | ||
9138 | The reason we check for the NaNs instead of using this case | |
9139 | whenever mag == 0.0 is because when the angle is known, we'd | |
9140 | like to return the correct kind of non-real complex zero: | |
9141 | +0.0+0.0i, -0.0+0.0i, -0.0-0.0i, or +0.0-0.0i, depending | |
9142 | on which quadrant the angle is in. | |
9143 | */ | |
9144 | if (SCM_UNLIKELY (isnan(s)) && isnan(c) && (mag == 0.0)) | |
9145 | return scm_c_make_rectangular (0.0, 0.0); | |
9146 | else | |
9147 | return scm_c_make_rectangular (mag * c, mag * s); | |
8507ec80 | 9148 | } |
0f2d19dd | 9149 | |
a1ec6916 | 9150 | SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0, |
c7218482 MW |
9151 | (SCM mag, SCM ang), |
9152 | "Return the complex number @var{mag} * e^(i * @var{ang}).") | |
1bbd0b84 | 9153 | #define FUNC_NAME s_scm_make_polar |
0f2d19dd | 9154 | { |
c7218482 MW |
9155 | SCM_ASSERT_TYPE (scm_is_real (mag), mag, SCM_ARG1, FUNC_NAME, "real"); |
9156 | SCM_ASSERT_TYPE (scm_is_real (ang), ang, SCM_ARG2, FUNC_NAME, "real"); | |
9157 | ||
9158 | /* If mag is exact0, return exact0 */ | |
9159 | if (scm_is_eq (mag, SCM_INUM0)) | |
9160 | return SCM_INUM0; | |
9161 | /* Return a real if ang is exact0 */ | |
9162 | else if (scm_is_eq (ang, SCM_INUM0)) | |
9163 | return mag; | |
9164 | else | |
9165 | return scm_c_make_polar (scm_to_double (mag), scm_to_double (ang)); | |
0f2d19dd | 9166 | } |
1bbd0b84 | 9167 | #undef FUNC_NAME |
0f2d19dd JB |
9168 | |
9169 | ||
2519490c MW |
9170 | SCM_PRIMITIVE_GENERIC (scm_real_part, "real-part", 1, 0, 0, |
9171 | (SCM z), | |
9172 | "Return the real part of the number @var{z}.") | |
9173 | #define FUNC_NAME s_scm_real_part | |
0f2d19dd | 9174 | { |
2519490c | 9175 | if (SCM_COMPLEXP (z)) |
00472a22 | 9176 | return scm_i_from_double (SCM_COMPLEX_REAL (z)); |
2519490c | 9177 | else if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_REALP (z) || SCM_FRACTIONP (z)) |
2fa2d879 | 9178 | return z; |
0aacf84e | 9179 | else |
2519490c | 9180 | SCM_WTA_DISPATCH_1 (g_scm_real_part, z, SCM_ARG1, s_scm_real_part); |
0f2d19dd | 9181 | } |
2519490c | 9182 | #undef FUNC_NAME |
0f2d19dd JB |
9183 | |
9184 | ||
2519490c MW |
9185 | SCM_PRIMITIVE_GENERIC (scm_imag_part, "imag-part", 1, 0, 0, |
9186 | (SCM z), | |
9187 | "Return the imaginary part of the number @var{z}.") | |
9188 | #define FUNC_NAME s_scm_imag_part | |
0f2d19dd | 9189 | { |
2519490c | 9190 | if (SCM_COMPLEXP (z)) |
00472a22 | 9191 | return scm_i_from_double (SCM_COMPLEX_IMAG (z)); |
c7218482 | 9192 | else if (SCM_I_INUMP (z) || SCM_REALP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z)) |
f92e85f7 | 9193 | return SCM_INUM0; |
0aacf84e | 9194 | else |
2519490c | 9195 | SCM_WTA_DISPATCH_1 (g_scm_imag_part, z, SCM_ARG1, s_scm_imag_part); |
0f2d19dd | 9196 | } |
2519490c | 9197 | #undef FUNC_NAME |
0f2d19dd | 9198 | |
2519490c MW |
9199 | SCM_PRIMITIVE_GENERIC (scm_numerator, "numerator", 1, 0, 0, |
9200 | (SCM z), | |
9201 | "Return the numerator of the number @var{z}.") | |
9202 | #define FUNC_NAME s_scm_numerator | |
f92e85f7 | 9203 | { |
2519490c | 9204 | if (SCM_I_INUMP (z) || SCM_BIGP (z)) |
f92e85f7 MV |
9205 | return z; |
9206 | else if (SCM_FRACTIONP (z)) | |
e2bf3b19 | 9207 | return SCM_FRACTION_NUMERATOR (z); |
f92e85f7 | 9208 | else if (SCM_REALP (z)) |
fa102e73 MW |
9209 | { |
9210 | double zz = SCM_REAL_VALUE (z); | |
9211 | if (zz == floor (zz)) | |
9212 | /* Handle -0.0 and infinities in accordance with R6RS | |
9213 | flnumerator, and optimize handling of integers. */ | |
9214 | return z; | |
9215 | else | |
9216 | return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z))); | |
9217 | } | |
f92e85f7 | 9218 | else |
2519490c | 9219 | SCM_WTA_DISPATCH_1 (g_scm_numerator, z, SCM_ARG1, s_scm_numerator); |
f92e85f7 | 9220 | } |
2519490c | 9221 | #undef FUNC_NAME |
f92e85f7 MV |
9222 | |
9223 | ||
2519490c MW |
9224 | SCM_PRIMITIVE_GENERIC (scm_denominator, "denominator", 1, 0, 0, |
9225 | (SCM z), | |
9226 | "Return the denominator of the number @var{z}.") | |
9227 | #define FUNC_NAME s_scm_denominator | |
f92e85f7 | 9228 | { |
2519490c | 9229 | if (SCM_I_INUMP (z) || SCM_BIGP (z)) |
cff5fa33 | 9230 | return SCM_INUM1; |
f92e85f7 | 9231 | else if (SCM_FRACTIONP (z)) |
e2bf3b19 | 9232 | return SCM_FRACTION_DENOMINATOR (z); |
f92e85f7 | 9233 | else if (SCM_REALP (z)) |
fa102e73 MW |
9234 | { |
9235 | double zz = SCM_REAL_VALUE (z); | |
9236 | if (zz == floor (zz)) | |
9237 | /* Handle infinities in accordance with R6RS fldenominator, and | |
9238 | optimize handling of integers. */ | |
9239 | return scm_i_from_double (1.0); | |
9240 | else | |
9241 | return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z))); | |
9242 | } | |
f92e85f7 | 9243 | else |
2519490c | 9244 | SCM_WTA_DISPATCH_1 (g_scm_denominator, z, SCM_ARG1, s_scm_denominator); |
f92e85f7 | 9245 | } |
2519490c | 9246 | #undef FUNC_NAME |
0f2d19dd | 9247 | |
2519490c MW |
9248 | |
9249 | SCM_PRIMITIVE_GENERIC (scm_magnitude, "magnitude", 1, 0, 0, | |
9250 | (SCM z), | |
9251 | "Return the magnitude of the number @var{z}. This is the same as\n" | |
9252 | "@code{abs} for real arguments, but also allows complex numbers.") | |
9253 | #define FUNC_NAME s_scm_magnitude | |
0f2d19dd | 9254 | { |
e11e83f3 | 9255 | if (SCM_I_INUMP (z)) |
0aacf84e | 9256 | { |
e25f3727 | 9257 | scm_t_inum zz = SCM_I_INUM (z); |
0aacf84e MD |
9258 | if (zz >= 0) |
9259 | return z; | |
9260 | else if (SCM_POSFIXABLE (-zz)) | |
d956fa6f | 9261 | return SCM_I_MAKINUM (-zz); |
0aacf84e | 9262 | else |
e25f3727 | 9263 | return scm_i_inum2big (-zz); |
5986c47d | 9264 | } |
0aacf84e MD |
9265 | else if (SCM_BIGP (z)) |
9266 | { | |
9267 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (z)); | |
9268 | scm_remember_upto_here_1 (z); | |
9269 | if (sgn < 0) | |
9270 | return scm_i_clonebig (z, 0); | |
9271 | else | |
9272 | return z; | |
5986c47d | 9273 | } |
0aacf84e | 9274 | else if (SCM_REALP (z)) |
00472a22 | 9275 | return scm_i_from_double (fabs (SCM_REAL_VALUE (z))); |
0aacf84e | 9276 | else if (SCM_COMPLEXP (z)) |
00472a22 | 9277 | return scm_i_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z))); |
f92e85f7 MV |
9278 | else if (SCM_FRACTIONP (z)) |
9279 | { | |
73e4de09 | 9280 | if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z)))) |
f92e85f7 | 9281 | return z; |
a285b18c MW |
9282 | return scm_i_make_ratio_already_reduced |
9283 | (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED), | |
9284 | SCM_FRACTION_DENOMINATOR (z)); | |
f92e85f7 | 9285 | } |
0aacf84e | 9286 | else |
2519490c | 9287 | SCM_WTA_DISPATCH_1 (g_scm_magnitude, z, SCM_ARG1, s_scm_magnitude); |
0f2d19dd | 9288 | } |
2519490c | 9289 | #undef FUNC_NAME |
0f2d19dd JB |
9290 | |
9291 | ||
2519490c MW |
9292 | SCM_PRIMITIVE_GENERIC (scm_angle, "angle", 1, 0, 0, |
9293 | (SCM z), | |
9294 | "Return the angle of the complex number @var{z}.") | |
9295 | #define FUNC_NAME s_scm_angle | |
0f2d19dd | 9296 | { |
c8ae173e | 9297 | /* atan(0,-1) is pi and it'd be possible to have that as a constant like |
00472a22 | 9298 | flo0 to save allocating a new flonum with scm_i_from_double each time. |
c8ae173e KR |
9299 | But if atan2 follows the floating point rounding mode, then the value |
9300 | is not a constant. Maybe it'd be close enough though. */ | |
e11e83f3 | 9301 | if (SCM_I_INUMP (z)) |
0aacf84e | 9302 | { |
e11e83f3 | 9303 | if (SCM_I_INUM (z) >= 0) |
e7efe8e7 | 9304 | return flo0; |
0aacf84e | 9305 | else |
00472a22 | 9306 | return scm_i_from_double (atan2 (0.0, -1.0)); |
f872b822 | 9307 | } |
0aacf84e MD |
9308 | else if (SCM_BIGP (z)) |
9309 | { | |
9310 | int sgn = mpz_sgn (SCM_I_BIG_MPZ (z)); | |
9311 | scm_remember_upto_here_1 (z); | |
9312 | if (sgn < 0) | |
00472a22 | 9313 | return scm_i_from_double (atan2 (0.0, -1.0)); |
0aacf84e | 9314 | else |
e7efe8e7 | 9315 | return flo0; |
0f2d19dd | 9316 | } |
0aacf84e | 9317 | else if (SCM_REALP (z)) |
c8ae173e | 9318 | { |
10a97755 | 9319 | double x = SCM_REAL_VALUE (z); |
e1592f8a | 9320 | if (copysign (1.0, x) > 0.0) |
e7efe8e7 | 9321 | return flo0; |
c8ae173e | 9322 | else |
00472a22 | 9323 | return scm_i_from_double (atan2 (0.0, -1.0)); |
c8ae173e | 9324 | } |
0aacf84e | 9325 | else if (SCM_COMPLEXP (z)) |
00472a22 | 9326 | return scm_i_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z))); |
f92e85f7 MV |
9327 | else if (SCM_FRACTIONP (z)) |
9328 | { | |
73e4de09 | 9329 | if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z)))) |
e7efe8e7 | 9330 | return flo0; |
00472a22 | 9331 | else return scm_i_from_double (atan2 (0.0, -1.0)); |
f92e85f7 | 9332 | } |
0aacf84e | 9333 | else |
2519490c | 9334 | SCM_WTA_DISPATCH_1 (g_scm_angle, z, SCM_ARG1, s_scm_angle); |
0f2d19dd | 9335 | } |
2519490c | 9336 | #undef FUNC_NAME |
0f2d19dd JB |
9337 | |
9338 | ||
2519490c MW |
9339 | SCM_PRIMITIVE_GENERIC (scm_exact_to_inexact, "exact->inexact", 1, 0, 0, |
9340 | (SCM z), | |
9341 | "Convert the number @var{z} to its inexact representation.\n") | |
9342 | #define FUNC_NAME s_scm_exact_to_inexact | |
3c9a524f | 9343 | { |
e11e83f3 | 9344 | if (SCM_I_INUMP (z)) |
00472a22 | 9345 | return scm_i_from_double ((double) SCM_I_INUM (z)); |
3c9a524f | 9346 | else if (SCM_BIGP (z)) |
00472a22 | 9347 | return scm_i_from_double (scm_i_big2dbl (z)); |
f92e85f7 | 9348 | else if (SCM_FRACTIONP (z)) |
00472a22 | 9349 | return scm_i_from_double (scm_i_fraction2double (z)); |
3c9a524f DH |
9350 | else if (SCM_INEXACTP (z)) |
9351 | return z; | |
9352 | else | |
2519490c | 9353 | SCM_WTA_DISPATCH_1 (g_scm_exact_to_inexact, z, 1, s_scm_exact_to_inexact); |
3c9a524f | 9354 | } |
2519490c | 9355 | #undef FUNC_NAME |
3c9a524f DH |
9356 | |
9357 | ||
2519490c MW |
9358 | SCM_PRIMITIVE_GENERIC (scm_inexact_to_exact, "inexact->exact", 1, 0, 0, |
9359 | (SCM z), | |
9360 | "Return an exact number that is numerically closest to @var{z}.") | |
1bbd0b84 | 9361 | #define FUNC_NAME s_scm_inexact_to_exact |
0f2d19dd | 9362 | { |
c7218482 | 9363 | if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z)) |
f872b822 | 9364 | return z; |
c7218482 | 9365 | else |
0aacf84e | 9366 | { |
c7218482 MW |
9367 | double val; |
9368 | ||
9369 | if (SCM_REALP (z)) | |
9370 | val = SCM_REAL_VALUE (z); | |
9371 | else if (SCM_COMPLEXP (z) && SCM_COMPLEX_IMAG (z) == 0.0) | |
9372 | val = SCM_COMPLEX_REAL (z); | |
9373 | else | |
9374 | SCM_WTA_DISPATCH_1 (g_scm_inexact_to_exact, z, 1, s_scm_inexact_to_exact); | |
9375 | ||
19374ad2 | 9376 | if (!SCM_LIKELY (isfinite (val))) |
f92e85f7 | 9377 | SCM_OUT_OF_RANGE (1, z); |
24475b86 MW |
9378 | else if (val == 0.0) |
9379 | return SCM_INUM0; | |
2be24db4 | 9380 | else |
f92e85f7 | 9381 | { |
24475b86 MW |
9382 | int expon; |
9383 | SCM numerator; | |
9384 | ||
9385 | numerator = scm_i_dbl2big (ldexp (frexp (val, &expon), | |
9386 | DBL_MANT_DIG)); | |
9387 | expon -= DBL_MANT_DIG; | |
9388 | if (expon < 0) | |
9389 | { | |
9390 | int shift = mpz_scan1 (SCM_I_BIG_MPZ (numerator), 0); | |
9391 | ||
9392 | if (shift > -expon) | |
9393 | shift = -expon; | |
9394 | mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (numerator), | |
9395 | SCM_I_BIG_MPZ (numerator), | |
9396 | shift); | |
9397 | expon += shift; | |
9398 | } | |
9399 | numerator = scm_i_normbig (numerator); | |
9400 | if (expon < 0) | |
9401 | return scm_i_make_ratio_already_reduced | |
9402 | (numerator, left_shift_exact_integer (SCM_INUM1, -expon)); | |
9403 | else if (expon > 0) | |
9404 | return left_shift_exact_integer (numerator, expon); | |
9405 | else | |
9406 | return numerator; | |
f92e85f7 | 9407 | } |
c2ff8ab0 | 9408 | } |
0f2d19dd | 9409 | } |
1bbd0b84 | 9410 | #undef FUNC_NAME |
0f2d19dd | 9411 | |
f92e85f7 | 9412 | SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0, |
76dae881 NJ |
9413 | (SCM x, SCM eps), |
9414 | "Returns the @emph{simplest} rational number differing\n" | |
9415 | "from @var{x} by no more than @var{eps}.\n" | |
9416 | "\n" | |
9417 | "As required by @acronym{R5RS}, @code{rationalize} only returns an\n" | |
9418 | "exact result when both its arguments are exact. Thus, you might need\n" | |
9419 | "to use @code{inexact->exact} on the arguments.\n" | |
9420 | "\n" | |
9421 | "@lisp\n" | |
9422 | "(rationalize (inexact->exact 1.2) 1/100)\n" | |
9423 | "@result{} 6/5\n" | |
9424 | "@end lisp") | |
f92e85f7 MV |
9425 | #define FUNC_NAME s_scm_rationalize |
9426 | { | |
605f6980 MW |
9427 | SCM_ASSERT_TYPE (scm_is_real (x), x, SCM_ARG1, FUNC_NAME, "real"); |
9428 | SCM_ASSERT_TYPE (scm_is_real (eps), eps, SCM_ARG2, FUNC_NAME, "real"); | |
620c13e8 MW |
9429 | |
9430 | if (SCM_UNLIKELY (!scm_is_exact (eps) || !scm_is_exact (x))) | |
605f6980 | 9431 | { |
620c13e8 MW |
9432 | if (SCM_UNLIKELY (scm_is_false (scm_finite_p (eps)))) |
9433 | { | |
9434 | if (scm_is_false (scm_nan_p (eps)) && scm_is_true (scm_finite_p (x))) | |
9435 | return flo0; | |
9436 | else | |
9437 | return scm_nan (); | |
9438 | } | |
9439 | else if (SCM_UNLIKELY (scm_is_false (scm_finite_p (x)))) | |
9440 | return x; | |
605f6980 | 9441 | else |
620c13e8 MW |
9442 | return scm_exact_to_inexact |
9443 | (scm_rationalize (scm_inexact_to_exact (x), | |
9444 | scm_inexact_to_exact (eps))); | |
605f6980 MW |
9445 | } |
9446 | else | |
f92e85f7 | 9447 | { |
620c13e8 MW |
9448 | /* X and EPS are exact rationals. |
9449 | ||
9450 | The code that follows is equivalent to the following Scheme code: | |
9451 | ||
9452 | (define (exact-rationalize x eps) | |
9453 | (let ((n1 (if (negative? x) -1 1)) | |
9454 | (x (abs x)) | |
9455 | (eps (abs eps))) | |
9456 | (let ((lo (- x eps)) | |
9457 | (hi (+ x eps))) | |
9458 | (if (<= lo 0) | |
9459 | 0 | |
9460 | (let loop ((nlo (numerator lo)) (dlo (denominator lo)) | |
9461 | (nhi (numerator hi)) (dhi (denominator hi)) | |
9462 | (n1 n1) (d1 0) (n2 0) (d2 1)) | |
9463 | (let-values (((qlo rlo) (floor/ nlo dlo)) | |
9464 | ((qhi rhi) (floor/ nhi dhi))) | |
9465 | (let ((n0 (+ n2 (* n1 qlo))) | |
9466 | (d0 (+ d2 (* d1 qlo)))) | |
9467 | (cond ((zero? rlo) (/ n0 d0)) | |
9468 | ((< qlo qhi) (/ (+ n0 n1) (+ d0 d1))) | |
9469 | (else (loop dhi rhi dlo rlo n0 d0 n1 d1)))))))))) | |
f92e85f7 MV |
9470 | */ |
9471 | ||
620c13e8 MW |
9472 | int n1_init = 1; |
9473 | SCM lo, hi; | |
f92e85f7 | 9474 | |
620c13e8 MW |
9475 | eps = scm_abs (eps); |
9476 | if (scm_is_true (scm_negative_p (x))) | |
9477 | { | |
9478 | n1_init = -1; | |
9479 | x = scm_difference (x, SCM_UNDEFINED); | |
9480 | } | |
f92e85f7 | 9481 | |
620c13e8 | 9482 | /* X and EPS are non-negative exact rationals. */ |
f92e85f7 | 9483 | |
620c13e8 MW |
9484 | lo = scm_difference (x, eps); |
9485 | hi = scm_sum (x, eps); | |
9486 | ||
9487 | if (scm_is_false (scm_positive_p (lo))) | |
9488 | /* If zero is included in the interval, return it. | |
9489 | It is the simplest rational of all. */ | |
9490 | return SCM_INUM0; | |
9491 | else | |
9492 | { | |
9493 | SCM result; | |
9494 | mpz_t n0, d0, n1, d1, n2, d2; | |
9495 | mpz_t nlo, dlo, nhi, dhi; | |
9496 | mpz_t qlo, rlo, qhi, rhi; | |
9497 | ||
9498 | /* LO and HI are positive exact rationals. */ | |
9499 | ||
9500 | /* Our approach here follows the method described by Alan | |
9501 | Bawden in a message entitled "(rationalize x y)" on the | |
9502 | rrrs-authors mailing list, dated 16 Feb 1988 14:08:28 EST: | |
9503 | ||
9504 | http://groups.csail.mit.edu/mac/ftpdir/scheme-mail/HTML/rrrs-1988/msg00063.html | |
9505 | ||
9506 | In brief, we compute the continued fractions of the two | |
9507 | endpoints of the interval (LO and HI). The continued | |
9508 | fraction of the result consists of the common prefix of the | |
9509 | continued fractions of LO and HI, plus one final term. The | |
9510 | final term of the result is the smallest integer contained | |
9511 | in the interval between the remainders of LO and HI after | |
9512 | the common prefix has been removed. | |
9513 | ||
9514 | The following code lazily computes the continued fraction | |
9515 | representations of LO and HI, and simultaneously converts | |
9516 | the continued fraction of the result into a rational | |
9517 | number. We use MPZ functions directly to avoid type | |
9518 | dispatch and GC allocation during the loop. */ | |
9519 | ||
9520 | mpz_inits (n0, d0, n1, d1, n2, d2, | |
9521 | nlo, dlo, nhi, dhi, | |
9522 | qlo, rlo, qhi, rhi, | |
9523 | NULL); | |
9524 | ||
9525 | /* The variables N1, D1, N2 and D2 are used to compute the | |
9526 | resulting rational from its continued fraction. At each | |
9527 | step, N2/D2 and N1/D1 are the last two convergents. They | |
9528 | are normally initialized to 0/1 and 1/0, respectively. | |
9529 | However, if we negated X then we must negate the result as | |
9530 | well, and we do that by initializing N1/D1 to -1/0. */ | |
9531 | mpz_set_si (n1, n1_init); | |
9532 | mpz_set_ui (d1, 0); | |
9533 | mpz_set_ui (n2, 0); | |
9534 | mpz_set_ui (d2, 1); | |
9535 | ||
9536 | /* The variables NLO, DLO, NHI, and DHI are used to lazily | |
9537 | compute the continued fraction representations of LO and HI | |
9538 | using Euclid's algorithm. Initially, NLO/DLO == LO and | |
9539 | NHI/DHI == HI. */ | |
9540 | scm_to_mpz (scm_numerator (lo), nlo); | |
9541 | scm_to_mpz (scm_denominator (lo), dlo); | |
9542 | scm_to_mpz (scm_numerator (hi), nhi); | |
9543 | scm_to_mpz (scm_denominator (hi), dhi); | |
9544 | ||
9545 | /* As long as we're using exact arithmetic, the following loop | |
9546 | is guaranteed to terminate. */ | |
9547 | for (;;) | |
9548 | { | |
9549 | /* Compute the next terms (QLO and QHI) of the continued | |
9550 | fractions of LO and HI. */ | |
9551 | mpz_fdiv_qr (qlo, rlo, nlo, dlo); /* QLO <-- floor (NLO/DLO), RLO <-- NLO - QLO * DLO */ | |
9552 | mpz_fdiv_qr (qhi, rhi, nhi, dhi); /* QHI <-- floor (NHI/DHI), RHI <-- NHI - QHI * DHI */ | |
9553 | ||
9554 | /* The next term of the result will be either QLO or | |
9555 | QLO+1. Here we compute the next convergent of the | |
9556 | result based on the assumption that QLO is the next | |
9557 | term. If that turns out to be wrong, we'll adjust | |
9558 | these later by adding N1 to N0 and D1 to D0. */ | |
9559 | mpz_set (n0, n2); mpz_addmul (n0, n1, qlo); /* N0 <-- N2 + (QLO * N1) */ | |
9560 | mpz_set (d0, d2); mpz_addmul (d0, d1, qlo); /* D0 <-- D2 + (QLO * D1) */ | |
9561 | ||
9562 | /* We stop iterating when an integer is contained in the | |
9563 | interval between the remainders NLO/DLO and NHI/DHI. | |
9564 | There are two cases to consider: either NLO/DLO == QLO | |
9565 | is an integer (indicated by RLO == 0), or QLO < QHI. */ | |
d9e7774f MW |
9566 | if (mpz_sgn (rlo) == 0 || mpz_cmp (qlo, qhi) != 0) |
9567 | break; | |
620c13e8 MW |
9568 | |
9569 | /* Efficiently shuffle variables around for the next | |
9570 | iteration. First we shift the recent convergents. */ | |
9571 | mpz_swap (n2, n1); mpz_swap (n1, n0); /* N2 <-- N1 <-- N0 */ | |
9572 | mpz_swap (d2, d1); mpz_swap (d1, d0); /* D2 <-- D1 <-- D0 */ | |
9573 | ||
9574 | /* The following shuffling is a bit confusing, so some | |
9575 | explanation is in order. Conceptually, we're doing a | |
9576 | couple of things here. After substracting the floor of | |
9577 | NLO/DLO, the remainder is RLO/DLO. The rest of the | |
9578 | continued fraction will represent the remainder's | |
9579 | reciprocal DLO/RLO. Similarly for the HI endpoint. | |
9580 | So in the next iteration, the new endpoints will be | |
9581 | DLO/RLO and DHI/RHI. However, when we take the | |
9582 | reciprocals of these endpoints, their order is | |
9583 | switched. So in summary, we want NLO/DLO <-- DHI/RHI | |
9584 | and NHI/DHI <-- DLO/RLO. */ | |
9585 | mpz_swap (nlo, dhi); mpz_swap (dhi, rlo); /* NLO <-- DHI <-- RLO */ | |
9586 | mpz_swap (nhi, dlo); mpz_swap (dlo, rhi); /* NHI <-- DLO <-- RHI */ | |
9587 | } | |
9588 | ||
9589 | /* There is now an integer in the interval [NLO/DLO NHI/DHI]. | |
9590 | The last term of the result will be the smallest integer in | |
9591 | that interval, which is ceiling(NLO/DLO). We have already | |
9592 | computed floor(NLO/DLO) in QLO, so now we adjust QLO to be | |
9593 | equal to the ceiling. */ | |
9594 | if (mpz_sgn (rlo) != 0) | |
9595 | { | |
9596 | /* If RLO is non-zero, then NLO/DLO is not an integer and | |
9597 | the next term will be QLO+1. QLO was used in the | |
9598 | computation of N0 and D0 above. Here we adjust N0 and | |
9599 | D0 to be based on QLO+1 instead of QLO. */ | |
9600 | mpz_add (n0, n0, n1); /* N0 <-- N0 + N1 */ | |
9601 | mpz_add (d0, d0, d1); /* D0 <-- D0 + D1 */ | |
9602 | } | |
9603 | ||
9604 | /* The simplest rational in the interval is N0/D0 */ | |
9605 | result = scm_i_make_ratio_already_reduced (scm_from_mpz (n0), | |
9606 | scm_from_mpz (d0)); | |
9607 | mpz_clears (n0, d0, n1, d1, n2, d2, | |
9608 | nlo, dlo, nhi, dhi, | |
9609 | qlo, rlo, qhi, rhi, | |
9610 | NULL); | |
9611 | return result; | |
9612 | } | |
f92e85f7 | 9613 | } |
f92e85f7 MV |
9614 | } |
9615 | #undef FUNC_NAME | |
9616 | ||
73e4de09 MV |
9617 | /* conversion functions */ |
9618 | ||
9619 | int | |
9620 | scm_is_integer (SCM val) | |
9621 | { | |
9622 | return scm_is_true (scm_integer_p (val)); | |
9623 | } | |
9624 | ||
900a897c MW |
9625 | int |
9626 | scm_is_exact_integer (SCM val) | |
9627 | { | |
9628 | return scm_is_true (scm_exact_integer_p (val)); | |
9629 | } | |
9630 | ||
73e4de09 MV |
9631 | int |
9632 | scm_is_signed_integer (SCM val, scm_t_intmax min, scm_t_intmax max) | |
9633 | { | |
e11e83f3 | 9634 | if (SCM_I_INUMP (val)) |
73e4de09 | 9635 | { |
e11e83f3 | 9636 | scm_t_signed_bits n = SCM_I_INUM (val); |
73e4de09 MV |
9637 | return n >= min && n <= max; |
9638 | } | |
9639 | else if (SCM_BIGP (val)) | |
9640 | { | |
9641 | if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM) | |
9642 | return 0; | |
9643 | else if (min >= LONG_MIN && max <= LONG_MAX) | |
d956fa6f MV |
9644 | { |
9645 | if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val))) | |
9646 | { | |
9647 | long n = mpz_get_si (SCM_I_BIG_MPZ (val)); | |
9648 | return n >= min && n <= max; | |
9649 | } | |
9650 | else | |
9651 | return 0; | |
9652 | } | |
73e4de09 MV |
9653 | else |
9654 | { | |
d956fa6f MV |
9655 | scm_t_intmax n; |
9656 | size_t count; | |
73e4de09 | 9657 | |
d956fa6f MV |
9658 | if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2) |
9659 | > CHAR_BIT*sizeof (scm_t_uintmax)) | |
9660 | return 0; | |
9661 | ||
9662 | mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0, | |
9663 | SCM_I_BIG_MPZ (val)); | |
73e4de09 | 9664 | |
d956fa6f | 9665 | if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0) |
73e4de09 | 9666 | { |
d956fa6f MV |
9667 | if (n < 0) |
9668 | return 0; | |
73e4de09 | 9669 | } |
73e4de09 MV |
9670 | else |
9671 | { | |
d956fa6f MV |
9672 | n = -n; |
9673 | if (n >= 0) | |
9674 | return 0; | |
73e4de09 | 9675 | } |
d956fa6f MV |
9676 | |
9677 | return n >= min && n <= max; | |
73e4de09 MV |
9678 | } |
9679 | } | |
73e4de09 MV |
9680 | else |
9681 | return 0; | |
9682 | } | |
9683 | ||
9684 | int | |
9685 | scm_is_unsigned_integer (SCM val, scm_t_uintmax min, scm_t_uintmax max) | |
9686 | { | |
e11e83f3 | 9687 | if (SCM_I_INUMP (val)) |
73e4de09 | 9688 | { |
e11e83f3 | 9689 | scm_t_signed_bits n = SCM_I_INUM (val); |
73e4de09 MV |
9690 | return n >= 0 && ((scm_t_uintmax)n) >= min && ((scm_t_uintmax)n) <= max; |
9691 | } | |
9692 | else if (SCM_BIGP (val)) | |
9693 | { | |
9694 | if (max <= SCM_MOST_POSITIVE_FIXNUM) | |
9695 | return 0; | |
9696 | else if (max <= ULONG_MAX) | |
d956fa6f MV |
9697 | { |
9698 | if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val))) | |
9699 | { | |
9700 | unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val)); | |
9701 | return n >= min && n <= max; | |
9702 | } | |
9703 | else | |
9704 | return 0; | |
9705 | } | |
73e4de09 MV |
9706 | else |
9707 | { | |
d956fa6f MV |
9708 | scm_t_uintmax n; |
9709 | size_t count; | |
73e4de09 | 9710 | |
d956fa6f MV |
9711 | if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0) |
9712 | return 0; | |
73e4de09 | 9713 | |
d956fa6f MV |
9714 | if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2) |
9715 | > CHAR_BIT*sizeof (scm_t_uintmax)) | |
73e4de09 | 9716 | return 0; |
d956fa6f MV |
9717 | |
9718 | mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0, | |
9719 | SCM_I_BIG_MPZ (val)); | |
73e4de09 | 9720 | |
d956fa6f | 9721 | return n >= min && n <= max; |
73e4de09 MV |
9722 | } |
9723 | } | |
73e4de09 MV |
9724 | else |
9725 | return 0; | |
9726 | } | |
9727 | ||
1713d319 MV |
9728 | static void |
9729 | scm_i_range_error (SCM bad_val, SCM min, SCM max) | |
9730 | { | |
9731 | scm_error (scm_out_of_range_key, | |
9732 | NULL, | |
9733 | "Value out of range ~S to ~S: ~S", | |
9734 | scm_list_3 (min, max, bad_val), | |
9735 | scm_list_1 (bad_val)); | |
9736 | } | |
9737 | ||
bfd7932e MV |
9738 | #define TYPE scm_t_intmax |
9739 | #define TYPE_MIN min | |
9740 | #define TYPE_MAX max | |
9741 | #define SIZEOF_TYPE 0 | |
9742 | #define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, scm_t_intmax min, scm_t_intmax max) | |
9743 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg) | |
9744 | #include "libguile/conv-integer.i.c" | |
9745 | ||
9746 | #define TYPE scm_t_uintmax | |
9747 | #define TYPE_MIN min | |
9748 | #define TYPE_MAX max | |
9749 | #define SIZEOF_TYPE 0 | |
9750 | #define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, scm_t_uintmax min, scm_t_uintmax max) | |
9751 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg) | |
9752 | #include "libguile/conv-uinteger.i.c" | |
9753 | ||
9754 | #define TYPE scm_t_int8 | |
9755 | #define TYPE_MIN SCM_T_INT8_MIN | |
9756 | #define TYPE_MAX SCM_T_INT8_MAX | |
9757 | #define SIZEOF_TYPE 1 | |
9758 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg) | |
9759 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg) | |
9760 | #include "libguile/conv-integer.i.c" | |
9761 | ||
9762 | #define TYPE scm_t_uint8 | |
9763 | #define TYPE_MIN 0 | |
9764 | #define TYPE_MAX SCM_T_UINT8_MAX | |
9765 | #define SIZEOF_TYPE 1 | |
9766 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg) | |
9767 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg) | |
9768 | #include "libguile/conv-uinteger.i.c" | |
9769 | ||
9770 | #define TYPE scm_t_int16 | |
9771 | #define TYPE_MIN SCM_T_INT16_MIN | |
9772 | #define TYPE_MAX SCM_T_INT16_MAX | |
9773 | #define SIZEOF_TYPE 2 | |
9774 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg) | |
9775 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg) | |
9776 | #include "libguile/conv-integer.i.c" | |
9777 | ||
9778 | #define TYPE scm_t_uint16 | |
9779 | #define TYPE_MIN 0 | |
9780 | #define TYPE_MAX SCM_T_UINT16_MAX | |
9781 | #define SIZEOF_TYPE 2 | |
9782 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg) | |
9783 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg) | |
9784 | #include "libguile/conv-uinteger.i.c" | |
9785 | ||
9786 | #define TYPE scm_t_int32 | |
9787 | #define TYPE_MIN SCM_T_INT32_MIN | |
9788 | #define TYPE_MAX SCM_T_INT32_MAX | |
9789 | #define SIZEOF_TYPE 4 | |
9790 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg) | |
9791 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg) | |
9792 | #include "libguile/conv-integer.i.c" | |
9793 | ||
9794 | #define TYPE scm_t_uint32 | |
9795 | #define TYPE_MIN 0 | |
9796 | #define TYPE_MAX SCM_T_UINT32_MAX | |
9797 | #define SIZEOF_TYPE 4 | |
9798 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg) | |
9799 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg) | |
9800 | #include "libguile/conv-uinteger.i.c" | |
9801 | ||
904a78f1 MG |
9802 | #define TYPE scm_t_wchar |
9803 | #define TYPE_MIN (scm_t_int32)-1 | |
9804 | #define TYPE_MAX (scm_t_int32)0x10ffff | |
9805 | #define SIZEOF_TYPE 4 | |
9806 | #define SCM_TO_TYPE_PROTO(arg) scm_to_wchar (arg) | |
9807 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_wchar (arg) | |
9808 | #include "libguile/conv-integer.i.c" | |
9809 | ||
bfd7932e MV |
9810 | #define TYPE scm_t_int64 |
9811 | #define TYPE_MIN SCM_T_INT64_MIN | |
9812 | #define TYPE_MAX SCM_T_INT64_MAX | |
9813 | #define SIZEOF_TYPE 8 | |
9814 | #define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg) | |
9815 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg) | |
9816 | #include "libguile/conv-integer.i.c" | |
9817 | ||
9818 | #define TYPE scm_t_uint64 | |
9819 | #define TYPE_MIN 0 | |
9820 | #define TYPE_MAX SCM_T_UINT64_MAX | |
9821 | #define SIZEOF_TYPE 8 | |
9822 | #define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg) | |
9823 | #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg) | |
9824 | #include "libguile/conv-uinteger.i.c" | |
73e4de09 | 9825 | |
cd036260 MV |
9826 | void |
9827 | scm_to_mpz (SCM val, mpz_t rop) | |
9828 | { | |
9829 | if (SCM_I_INUMP (val)) | |
9830 | mpz_set_si (rop, SCM_I_INUM (val)); | |
9831 | else if (SCM_BIGP (val)) | |
9832 | mpz_set (rop, SCM_I_BIG_MPZ (val)); | |
9833 | else | |
9834 | scm_wrong_type_arg_msg (NULL, 0, val, "exact integer"); | |
9835 | } | |
9836 | ||
9837 | SCM | |
9838 | scm_from_mpz (mpz_t val) | |
9839 | { | |
9840 | return scm_i_mpz2num (val); | |
9841 | } | |
9842 | ||
73e4de09 MV |
9843 | int |
9844 | scm_is_real (SCM val) | |
9845 | { | |
9846 | return scm_is_true (scm_real_p (val)); | |
9847 | } | |
9848 | ||
55f26379 MV |
9849 | int |
9850 | scm_is_rational (SCM val) | |
9851 | { | |
9852 | return scm_is_true (scm_rational_p (val)); | |
9853 | } | |
9854 | ||
73e4de09 MV |
9855 | double |
9856 | scm_to_double (SCM val) | |
9857 | { | |
55f26379 MV |
9858 | if (SCM_I_INUMP (val)) |
9859 | return SCM_I_INUM (val); | |
9860 | else if (SCM_BIGP (val)) | |
9861 | return scm_i_big2dbl (val); | |
9862 | else if (SCM_FRACTIONP (val)) | |
9863 | return scm_i_fraction2double (val); | |
9864 | else if (SCM_REALP (val)) | |
9865 | return SCM_REAL_VALUE (val); | |
9866 | else | |
7a1aba42 | 9867 | scm_wrong_type_arg_msg (NULL, 0, val, "real number"); |
73e4de09 MV |
9868 | } |
9869 | ||
9870 | SCM | |
9871 | scm_from_double (double val) | |
9872 | { | |
00472a22 | 9873 | return scm_i_from_double (val); |
73e4de09 MV |
9874 | } |
9875 | ||
220058a8 | 9876 | #if SCM_ENABLE_DEPRECATED == 1 |
55f26379 MV |
9877 | |
9878 | float | |
e25f3727 | 9879 | scm_num2float (SCM num, unsigned long pos, const char *s_caller) |
55f26379 | 9880 | { |
220058a8 AW |
9881 | scm_c_issue_deprecation_warning |
9882 | ("`scm_num2float' is deprecated. Use scm_to_double instead."); | |
9883 | ||
55f26379 MV |
9884 | if (SCM_BIGP (num)) |
9885 | { | |
9886 | float res = mpz_get_d (SCM_I_BIG_MPZ (num)); | |
2e65b52f | 9887 | if (!isinf (res)) |
55f26379 MV |
9888 | return res; |
9889 | else | |
9890 | scm_out_of_range (NULL, num); | |
9891 | } | |
9892 | else | |
9893 | return scm_to_double (num); | |
9894 | } | |
9895 | ||
9896 | double | |
e25f3727 | 9897 | scm_num2double (SCM num, unsigned long pos, const char *s_caller) |
55f26379 | 9898 | { |
220058a8 AW |
9899 | scm_c_issue_deprecation_warning |
9900 | ("`scm_num2double' is deprecated. Use scm_to_double instead."); | |
9901 | ||
55f26379 MV |
9902 | if (SCM_BIGP (num)) |
9903 | { | |
9904 | double res = mpz_get_d (SCM_I_BIG_MPZ (num)); | |
2e65b52f | 9905 | if (!isinf (res)) |
55f26379 MV |
9906 | return res; |
9907 | else | |
9908 | scm_out_of_range (NULL, num); | |
9909 | } | |
9910 | else | |
9911 | return scm_to_double (num); | |
9912 | } | |
9913 | ||
9914 | #endif | |
9915 | ||
8507ec80 MV |
9916 | int |
9917 | scm_is_complex (SCM val) | |
9918 | { | |
9919 | return scm_is_true (scm_complex_p (val)); | |
9920 | } | |
9921 | ||
9922 | double | |
9923 | scm_c_real_part (SCM z) | |
9924 | { | |
9925 | if (SCM_COMPLEXP (z)) | |
9926 | return SCM_COMPLEX_REAL (z); | |
9927 | else | |
9928 | { | |
9929 | /* Use the scm_real_part to get proper error checking and | |
9930 | dispatching. | |
9931 | */ | |
9932 | return scm_to_double (scm_real_part (z)); | |
9933 | } | |
9934 | } | |
9935 | ||
9936 | double | |
9937 | scm_c_imag_part (SCM z) | |
9938 | { | |
9939 | if (SCM_COMPLEXP (z)) | |
9940 | return SCM_COMPLEX_IMAG (z); | |
9941 | else | |
9942 | { | |
9943 | /* Use the scm_imag_part to get proper error checking and | |
9944 | dispatching. The result will almost always be 0.0, but not | |
9945 | always. | |
9946 | */ | |
9947 | return scm_to_double (scm_imag_part (z)); | |
9948 | } | |
9949 | } | |
9950 | ||
9951 | double | |
9952 | scm_c_magnitude (SCM z) | |
9953 | { | |
9954 | return scm_to_double (scm_magnitude (z)); | |
9955 | } | |
9956 | ||
9957 | double | |
9958 | scm_c_angle (SCM z) | |
9959 | { | |
9960 | return scm_to_double (scm_angle (z)); | |
9961 | } | |
9962 | ||
9963 | int | |
9964 | scm_is_number (SCM z) | |
9965 | { | |
9966 | return scm_is_true (scm_number_p (z)); | |
9967 | } | |
9968 | ||
8ab3d8a0 | 9969 | |
a5f6b751 MW |
9970 | /* Returns log(x * 2^shift) */ |
9971 | static SCM | |
9972 | log_of_shifted_double (double x, long shift) | |
9973 | { | |
9974 | double ans = log (fabs (x)) + shift * M_LN2; | |
9975 | ||
e1592f8a | 9976 | if (copysign (1.0, x) > 0.0) |
00472a22 | 9977 | return scm_i_from_double (ans); |
a5f6b751 MW |
9978 | else |
9979 | return scm_c_make_rectangular (ans, M_PI); | |
9980 | } | |
9981 | ||
85bdb6ac | 9982 | /* Returns log(n), for exact integer n */ |
a5f6b751 MW |
9983 | static SCM |
9984 | log_of_exact_integer (SCM n) | |
9985 | { | |
7f34acd8 MW |
9986 | if (SCM_I_INUMP (n)) |
9987 | return log_of_shifted_double (SCM_I_INUM (n), 0); | |
9988 | else if (SCM_BIGP (n)) | |
9989 | { | |
9990 | long expon; | |
9991 | double signif = scm_i_big2dbl_2exp (n, &expon); | |
9992 | return log_of_shifted_double (signif, expon); | |
9993 | } | |
9994 | else | |
9995 | scm_wrong_type_arg ("log_of_exact_integer", SCM_ARG1, n); | |
a5f6b751 MW |
9996 | } |
9997 | ||
9998 | /* Returns log(n/d), for exact non-zero integers n and d */ | |
9999 | static SCM | |
10000 | log_of_fraction (SCM n, SCM d) | |
10001 | { | |
10002 | long n_size = scm_to_long (scm_integer_length (n)); | |
10003 | long d_size = scm_to_long (scm_integer_length (d)); | |
10004 | ||
10005 | if (abs (n_size - d_size) > 1) | |
7f34acd8 MW |
10006 | return (scm_difference (log_of_exact_integer (n), |
10007 | log_of_exact_integer (d))); | |
a5f6b751 | 10008 | else if (scm_is_false (scm_negative_p (n))) |
00472a22 | 10009 | return scm_i_from_double |
98237784 | 10010 | (log1p (scm_i_divide2double (scm_difference (n, d), d))); |
a5f6b751 MW |
10011 | else |
10012 | return scm_c_make_rectangular | |
98237784 MW |
10013 | (log1p (scm_i_divide2double (scm_difference (scm_abs (n), d), |
10014 | d)), | |
a5f6b751 MW |
10015 | M_PI); |
10016 | } | |
10017 | ||
10018 | ||
8ab3d8a0 KR |
10019 | /* In the following functions we dispatch to the real-arg funcs like log() |
10020 | when we know the arg is real, instead of just handing everything to | |
10021 | clog() for instance. This is in case clog() doesn't optimize for a | |
10022 | real-only case, and because we have to test SCM_COMPLEXP anyway so may as | |
10023 | well use it to go straight to the applicable C func. */ | |
10024 | ||
2519490c MW |
10025 | SCM_PRIMITIVE_GENERIC (scm_log, "log", 1, 0, 0, |
10026 | (SCM z), | |
10027 | "Return the natural logarithm of @var{z}.") | |
8ab3d8a0 KR |
10028 | #define FUNC_NAME s_scm_log |
10029 | { | |
10030 | if (SCM_COMPLEXP (z)) | |
10031 | { | |
03976fee AW |
10032 | #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG \ |
10033 | && defined (SCM_COMPLEX_VALUE) | |
8ab3d8a0 KR |
10034 | return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z))); |
10035 | #else | |
10036 | double re = SCM_COMPLEX_REAL (z); | |
10037 | double im = SCM_COMPLEX_IMAG (z); | |
10038 | return scm_c_make_rectangular (log (hypot (re, im)), | |
10039 | atan2 (im, re)); | |
10040 | #endif | |
10041 | } | |
a5f6b751 MW |
10042 | else if (SCM_REALP (z)) |
10043 | return log_of_shifted_double (SCM_REAL_VALUE (z), 0); | |
10044 | else if (SCM_I_INUMP (z)) | |
8ab3d8a0 | 10045 | { |
a5f6b751 MW |
10046 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
10047 | if (scm_is_eq (z, SCM_INUM0)) | |
10048 | scm_num_overflow (s_scm_log); | |
10049 | #endif | |
10050 | return log_of_shifted_double (SCM_I_INUM (z), 0); | |
8ab3d8a0 | 10051 | } |
a5f6b751 MW |
10052 | else if (SCM_BIGP (z)) |
10053 | return log_of_exact_integer (z); | |
10054 | else if (SCM_FRACTIONP (z)) | |
10055 | return log_of_fraction (SCM_FRACTION_NUMERATOR (z), | |
10056 | SCM_FRACTION_DENOMINATOR (z)); | |
2519490c MW |
10057 | else |
10058 | SCM_WTA_DISPATCH_1 (g_scm_log, z, 1, s_scm_log); | |
8ab3d8a0 KR |
10059 | } |
10060 | #undef FUNC_NAME | |
10061 | ||
10062 | ||
2519490c MW |
10063 | SCM_PRIMITIVE_GENERIC (scm_log10, "log10", 1, 0, 0, |
10064 | (SCM z), | |
10065 | "Return the base 10 logarithm of @var{z}.") | |
8ab3d8a0 KR |
10066 | #define FUNC_NAME s_scm_log10 |
10067 | { | |
10068 | if (SCM_COMPLEXP (z)) | |
10069 | { | |
10070 | /* Mingw has clog() but not clog10(). (Maybe it'd be worth using | |
10071 | clog() and a multiply by M_LOG10E, rather than the fallback | |
10072 | log10+hypot+atan2.) */ | |
f328f862 LC |
10073 | #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG10 \ |
10074 | && defined SCM_COMPLEX_VALUE | |
8ab3d8a0 KR |
10075 | return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z))); |
10076 | #else | |
10077 | double re = SCM_COMPLEX_REAL (z); | |
10078 | double im = SCM_COMPLEX_IMAG (z); | |
10079 | return scm_c_make_rectangular (log10 (hypot (re, im)), | |
10080 | M_LOG10E * atan2 (im, re)); | |
10081 | #endif | |
10082 | } | |
a5f6b751 | 10083 | else if (SCM_REALP (z) || SCM_I_INUMP (z)) |
8ab3d8a0 | 10084 | { |
a5f6b751 MW |
10085 | #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO |
10086 | if (scm_is_eq (z, SCM_INUM0)) | |
10087 | scm_num_overflow (s_scm_log10); | |
10088 | #endif | |
10089 | { | |
10090 | double re = scm_to_double (z); | |
10091 | double l = log10 (fabs (re)); | |
e1592f8a | 10092 | if (copysign (1.0, re) > 0.0) |
00472a22 | 10093 | return scm_i_from_double (l); |
a5f6b751 MW |
10094 | else |
10095 | return scm_c_make_rectangular (l, M_LOG10E * M_PI); | |
10096 | } | |
8ab3d8a0 | 10097 | } |
a5f6b751 MW |
10098 | else if (SCM_BIGP (z)) |
10099 | return scm_product (flo_log10e, log_of_exact_integer (z)); | |
10100 | else if (SCM_FRACTIONP (z)) | |
10101 | return scm_product (flo_log10e, | |
10102 | log_of_fraction (SCM_FRACTION_NUMERATOR (z), | |
10103 | SCM_FRACTION_DENOMINATOR (z))); | |
2519490c MW |
10104 | else |
10105 | SCM_WTA_DISPATCH_1 (g_scm_log10, z, 1, s_scm_log10); | |
8ab3d8a0 KR |
10106 | } |
10107 | #undef FUNC_NAME | |
10108 | ||
10109 | ||
2519490c MW |
10110 | SCM_PRIMITIVE_GENERIC (scm_exp, "exp", 1, 0, 0, |
10111 | (SCM z), | |
10112 | "Return @math{e} to the power of @var{z}, where @math{e} is the\n" | |
10113 | "base of natural logarithms (2.71828@dots{}).") | |
8ab3d8a0 KR |
10114 | #define FUNC_NAME s_scm_exp |
10115 | { | |
10116 | if (SCM_COMPLEXP (z)) | |
10117 | { | |
93723f3d MW |
10118 | #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CEXP \ |
10119 | && defined (SCM_COMPLEX_VALUE) | |
10120 | return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z))); | |
10121 | #else | |
8ab3d8a0 KR |
10122 | return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)), |
10123 | SCM_COMPLEX_IMAG (z)); | |
93723f3d | 10124 | #endif |
8ab3d8a0 | 10125 | } |
2519490c | 10126 | else if (SCM_NUMBERP (z)) |
8ab3d8a0 KR |
10127 | { |
10128 | /* When z is a negative bignum the conversion to double overflows, | |
10129 | giving -infinity, but that's ok, the exp is still 0.0. */ | |
00472a22 | 10130 | return scm_i_from_double (exp (scm_to_double (z))); |
8ab3d8a0 | 10131 | } |
2519490c MW |
10132 | else |
10133 | SCM_WTA_DISPATCH_1 (g_scm_exp, z, 1, s_scm_exp); | |
8ab3d8a0 KR |
10134 | } |
10135 | #undef FUNC_NAME | |
10136 | ||
10137 | ||
882c8963 MW |
10138 | SCM_DEFINE (scm_i_exact_integer_sqrt, "exact-integer-sqrt", 1, 0, 0, |
10139 | (SCM k), | |
10140 | "Return two exact non-negative integers @var{s} and @var{r}\n" | |
10141 | "such that @math{@var{k} = @var{s}^2 + @var{r}} and\n" | |
10142 | "@math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.\n" | |
10143 | "An error is raised if @var{k} is not an exact non-negative integer.\n" | |
10144 | "\n" | |
10145 | "@lisp\n" | |
10146 | "(exact-integer-sqrt 10) @result{} 3 and 1\n" | |
10147 | "@end lisp") | |
10148 | #define FUNC_NAME s_scm_i_exact_integer_sqrt | |
10149 | { | |
10150 | SCM s, r; | |
10151 | ||
10152 | scm_exact_integer_sqrt (k, &s, &r); | |
10153 | return scm_values (scm_list_2 (s, r)); | |
10154 | } | |
10155 | #undef FUNC_NAME | |
10156 | ||
10157 | void | |
10158 | scm_exact_integer_sqrt (SCM k, SCM *sp, SCM *rp) | |
10159 | { | |
10160 | if (SCM_LIKELY (SCM_I_INUMP (k))) | |
10161 | { | |
687a87bf | 10162 | mpz_t kk, ss, rr; |
882c8963 | 10163 | |
687a87bf | 10164 | if (SCM_I_INUM (k) < 0) |
882c8963 MW |
10165 | scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k, |
10166 | "exact non-negative integer"); | |
687a87bf MW |
10167 | mpz_init_set_ui (kk, SCM_I_INUM (k)); |
10168 | mpz_inits (ss, rr, NULL); | |
10169 | mpz_sqrtrem (ss, rr, kk); | |
10170 | *sp = SCM_I_MAKINUM (mpz_get_ui (ss)); | |
10171 | *rp = SCM_I_MAKINUM (mpz_get_ui (rr)); | |
10172 | mpz_clears (kk, ss, rr, NULL); | |
882c8963 MW |
10173 | } |
10174 | else if (SCM_LIKELY (SCM_BIGP (k))) | |
10175 | { | |
10176 | SCM s, r; | |
10177 | ||
10178 | if (mpz_sgn (SCM_I_BIG_MPZ (k)) < 0) | |
10179 | scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k, | |
10180 | "exact non-negative integer"); | |
10181 | s = scm_i_mkbig (); | |
10182 | r = scm_i_mkbig (); | |
10183 | mpz_sqrtrem (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (k)); | |
10184 | scm_remember_upto_here_1 (k); | |
10185 | *sp = scm_i_normbig (s); | |
10186 | *rp = scm_i_normbig (r); | |
10187 | } | |
10188 | else | |
10189 | scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k, | |
10190 | "exact non-negative integer"); | |
10191 | } | |
10192 | ||
ddb71742 MW |
10193 | /* Return true iff K is a perfect square. |
10194 | K must be an exact integer. */ | |
10195 | static int | |
10196 | exact_integer_is_perfect_square (SCM k) | |
10197 | { | |
10198 | int result; | |
10199 | ||
10200 | if (SCM_LIKELY (SCM_I_INUMP (k))) | |
10201 | { | |
10202 | mpz_t kk; | |
10203 | ||
10204 | mpz_init_set_si (kk, SCM_I_INUM (k)); | |
10205 | result = mpz_perfect_square_p (kk); | |
10206 | mpz_clear (kk); | |
10207 | } | |
10208 | else | |
10209 | { | |
10210 | result = mpz_perfect_square_p (SCM_I_BIG_MPZ (k)); | |
10211 | scm_remember_upto_here_1 (k); | |
10212 | } | |
10213 | return result; | |
10214 | } | |
10215 | ||
10216 | /* Return the floor of the square root of K. | |
10217 | K must be an exact integer. */ | |
10218 | static SCM | |
10219 | exact_integer_floor_square_root (SCM k) | |
10220 | { | |
10221 | if (SCM_LIKELY (SCM_I_INUMP (k))) | |
10222 | { | |
10223 | mpz_t kk; | |
10224 | scm_t_inum ss; | |
10225 | ||
10226 | mpz_init_set_ui (kk, SCM_I_INUM (k)); | |
10227 | mpz_sqrt (kk, kk); | |
10228 | ss = mpz_get_ui (kk); | |
10229 | mpz_clear (kk); | |
10230 | return SCM_I_MAKINUM (ss); | |
10231 | } | |
10232 | else | |
10233 | { | |
10234 | SCM s; | |
10235 | ||
10236 | s = scm_i_mkbig (); | |
10237 | mpz_sqrt (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (k)); | |
10238 | scm_remember_upto_here_1 (k); | |
10239 | return scm_i_normbig (s); | |
10240 | } | |
10241 | } | |
10242 | ||
882c8963 | 10243 | |
2519490c MW |
10244 | SCM_PRIMITIVE_GENERIC (scm_sqrt, "sqrt", 1, 0, 0, |
10245 | (SCM z), | |
10246 | "Return the square root of @var{z}. Of the two possible roots\n" | |
ffb62a43 | 10247 | "(positive and negative), the one with positive real part\n" |
2519490c MW |
10248 | "is returned, or if that's zero then a positive imaginary part.\n" |
10249 | "Thus,\n" | |
10250 | "\n" | |
10251 | "@example\n" | |
10252 | "(sqrt 9.0) @result{} 3.0\n" | |
10253 | "(sqrt -9.0) @result{} 0.0+3.0i\n" | |
10254 | "(sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i\n" | |
10255 | "(sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i\n" | |
10256 | "@end example") | |
8ab3d8a0 KR |
10257 | #define FUNC_NAME s_scm_sqrt |
10258 | { | |
2519490c | 10259 | if (SCM_COMPLEXP (z)) |
8ab3d8a0 | 10260 | { |
f328f862 LC |
10261 | #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_USABLE_CSQRT \ |
10262 | && defined SCM_COMPLEX_VALUE | |
2519490c | 10263 | return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (z))); |
8ab3d8a0 | 10264 | #else |
2519490c MW |
10265 | double re = SCM_COMPLEX_REAL (z); |
10266 | double im = SCM_COMPLEX_IMAG (z); | |
8ab3d8a0 KR |
10267 | return scm_c_make_polar (sqrt (hypot (re, im)), |
10268 | 0.5 * atan2 (im, re)); | |
10269 | #endif | |
10270 | } | |
2519490c | 10271 | else if (SCM_NUMBERP (z)) |
8ab3d8a0 | 10272 | { |
44002664 MW |
10273 | if (SCM_I_INUMP (z)) |
10274 | { | |
ddb71742 MW |
10275 | scm_t_inum x = SCM_I_INUM (z); |
10276 | ||
10277 | if (SCM_LIKELY (x >= 0)) | |
44002664 | 10278 | { |
ddb71742 MW |
10279 | if (SCM_LIKELY (SCM_I_FIXNUM_BIT < DBL_MANT_DIG |
10280 | || x < (1L << (DBL_MANT_DIG - 1)))) | |
44002664 | 10281 | { |
ddb71742 | 10282 | double root = sqrt (x); |
44002664 MW |
10283 | |
10284 | /* If 0 <= x < 2^(DBL_MANT_DIG-1) and sqrt(x) is an | |
10285 | integer, then the result is exact. */ | |
10286 | if (root == floor (root)) | |
10287 | return SCM_I_MAKINUM ((scm_t_inum) root); | |
10288 | else | |
00472a22 | 10289 | return scm_i_from_double (root); |
44002664 MW |
10290 | } |
10291 | else | |
10292 | { | |
ddb71742 | 10293 | mpz_t xx; |
44002664 MW |
10294 | scm_t_inum root; |
10295 | ||
ddb71742 MW |
10296 | mpz_init_set_ui (xx, x); |
10297 | if (mpz_perfect_square_p (xx)) | |
44002664 | 10298 | { |
ddb71742 MW |
10299 | mpz_sqrt (xx, xx); |
10300 | root = mpz_get_ui (xx); | |
10301 | mpz_clear (xx); | |
44002664 MW |
10302 | return SCM_I_MAKINUM (root); |
10303 | } | |
10304 | else | |
ddb71742 | 10305 | mpz_clear (xx); |
44002664 MW |
10306 | } |
10307 | } | |
10308 | } | |
10309 | else if (SCM_BIGP (z)) | |
10310 | { | |
ddb71742 | 10311 | if (mpz_perfect_square_p (SCM_I_BIG_MPZ (z))) |
44002664 MW |
10312 | { |
10313 | SCM root = scm_i_mkbig (); | |
10314 | ||
10315 | mpz_sqrt (SCM_I_BIG_MPZ (root), SCM_I_BIG_MPZ (z)); | |
10316 | scm_remember_upto_here_1 (z); | |
10317 | return scm_i_normbig (root); | |
10318 | } | |
ddb71742 MW |
10319 | else |
10320 | { | |
10321 | long expon; | |
10322 | double signif = scm_i_big2dbl_2exp (z, &expon); | |
10323 | ||
10324 | if (expon & 1) | |
10325 | { | |
10326 | signif *= 2; | |
10327 | expon--; | |
10328 | } | |
10329 | if (signif < 0) | |
10330 | return scm_c_make_rectangular | |
10331 | (0.0, ldexp (sqrt (-signif), expon / 2)); | |
10332 | else | |
00472a22 | 10333 | return scm_i_from_double (ldexp (sqrt (signif), expon / 2)); |
ddb71742 | 10334 | } |
44002664 MW |
10335 | } |
10336 | else if (SCM_FRACTIONP (z)) | |
ddb71742 MW |
10337 | { |
10338 | SCM n = SCM_FRACTION_NUMERATOR (z); | |
10339 | SCM d = SCM_FRACTION_DENOMINATOR (z); | |
10340 | ||
10341 | if (exact_integer_is_perfect_square (n) | |
10342 | && exact_integer_is_perfect_square (d)) | |
10343 | return scm_i_make_ratio_already_reduced | |
10344 | (exact_integer_floor_square_root (n), | |
10345 | exact_integer_floor_square_root (d)); | |
10346 | else | |
10347 | { | |
10348 | double xx = scm_i_divide2double (n, d); | |
10349 | double abs_xx = fabs (xx); | |
10350 | long shift = 0; | |
10351 | ||
10352 | if (SCM_UNLIKELY (abs_xx > DBL_MAX || abs_xx < DBL_MIN)) | |
10353 | { | |
10354 | shift = (scm_to_long (scm_integer_length (n)) | |
10355 | - scm_to_long (scm_integer_length (d))) / 2; | |
10356 | if (shift > 0) | |
10357 | d = left_shift_exact_integer (d, 2 * shift); | |
10358 | else | |
10359 | n = left_shift_exact_integer (n, -2 * shift); | |
10360 | xx = scm_i_divide2double (n, d); | |
10361 | } | |
10362 | ||
10363 | if (xx < 0) | |
10364 | return scm_c_make_rectangular (0.0, ldexp (sqrt (-xx), shift)); | |
10365 | else | |
00472a22 | 10366 | return scm_i_from_double (ldexp (sqrt (xx), shift)); |
ddb71742 MW |
10367 | } |
10368 | } | |
44002664 MW |
10369 | |
10370 | /* Fallback method, when the cases above do not apply. */ | |
10371 | { | |
10372 | double xx = scm_to_double (z); | |
10373 | if (xx < 0) | |
10374 | return scm_c_make_rectangular (0.0, sqrt (-xx)); | |
10375 | else | |
00472a22 | 10376 | return scm_i_from_double (sqrt (xx)); |
44002664 | 10377 | } |
8ab3d8a0 | 10378 | } |
2519490c MW |
10379 | else |
10380 | SCM_WTA_DISPATCH_1 (g_scm_sqrt, z, 1, s_scm_sqrt); | |
8ab3d8a0 KR |
10381 | } |
10382 | #undef FUNC_NAME | |
10383 | ||
10384 | ||
10385 | ||
0f2d19dd JB |
10386 | void |
10387 | scm_init_numbers () | |
0f2d19dd | 10388 | { |
b57bf272 AW |
10389 | if (scm_install_gmp_memory_functions) |
10390 | mp_set_memory_functions (custom_gmp_malloc, | |
10391 | custom_gmp_realloc, | |
10392 | custom_gmp_free); | |
10393 | ||
713a4259 KR |
10394 | mpz_init_set_si (z_negative_one, -1); |
10395 | ||
a261c0e9 DH |
10396 | /* It may be possible to tune the performance of some algorithms by using |
10397 | * the following constants to avoid the creation of bignums. Please, before | |
10398 | * using these values, remember the two rules of program optimization: | |
10399 | * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */ | |
86d31dfe | 10400 | scm_c_define ("most-positive-fixnum", |
d956fa6f | 10401 | SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM)); |
86d31dfe | 10402 | scm_c_define ("most-negative-fixnum", |
d956fa6f | 10403 | SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM)); |
a261c0e9 | 10404 | |
f3ae5d60 MD |
10405 | scm_add_feature ("complex"); |
10406 | scm_add_feature ("inexact"); | |
00472a22 MW |
10407 | flo0 = scm_i_from_double (0.0); |
10408 | flo_log10e = scm_i_from_double (M_LOG10E); | |
0b799eea | 10409 | |
cff5fa33 | 10410 | exactly_one_half = scm_divide (SCM_INUM1, SCM_I_MAKINUM (2)); |
98237784 MW |
10411 | |
10412 | { | |
10413 | /* Set scm_i_divide2double_lo2b to (2 b^p - 1) */ | |
10414 | mpz_init_set_ui (scm_i_divide2double_lo2b, 1); | |
10415 | mpz_mul_2exp (scm_i_divide2double_lo2b, | |
10416 | scm_i_divide2double_lo2b, | |
10417 | DBL_MANT_DIG + 1); /* 2 b^p */ | |
10418 | mpz_sub_ui (scm_i_divide2double_lo2b, scm_i_divide2double_lo2b, 1); | |
10419 | } | |
10420 | ||
1ea37620 MW |
10421 | { |
10422 | /* Set dbl_minimum_normal_mantissa to b^{p-1} */ | |
10423 | mpz_init_set_ui (dbl_minimum_normal_mantissa, 1); | |
10424 | mpz_mul_2exp (dbl_minimum_normal_mantissa, | |
10425 | dbl_minimum_normal_mantissa, | |
10426 | DBL_MANT_DIG - 1); | |
10427 | } | |
10428 | ||
a0599745 | 10429 | #include "libguile/numbers.x" |
0f2d19dd | 10430 | } |
89e00824 ML |
10431 | |
10432 | /* | |
10433 | Local Variables: | |
10434 | c-file-style: "gnu" | |
10435 | End: | |
10436 | */ |