min and max: NaNs beat infinities, per R6RS errata.
[bpt/guile.git] / libguile / numbers.c
CommitLineData
07b390d5
LC
1/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
3 * 2013 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
LC
50#include <verify.h>
51
0f2d19dd 52#include <math.h>
fc194577 53#include <string.h>
3f47e526
MG
54#include <unicase.h>
55#include <unictype.h>
f92e85f7 56
8ab3d8a0
KR
57#if HAVE_COMPLEX_H
58#include <complex.h>
59#endif
60
07b390d5
LC
61#include <stdarg.h>
62
a0599745 63#include "libguile/_scm.h"
a0599745
MD
64#include "libguile/feature.h"
65#include "libguile/ports.h"
66#include "libguile/root.h"
67#include "libguile/smob.h"
68#include "libguile/strings.h"
864e7d42 69#include "libguile/bdw-gc.h"
a0599745
MD
70
71#include "libguile/validate.h"
72#include "libguile/numbers.h"
1be6b49c 73#include "libguile/deprecation.h"
f4c627b3 74
f92e85f7
MV
75#include "libguile/eq.h"
76
8ab3d8a0
KR
77/* values per glibc, if not already defined */
78#ifndef M_LOG10E
79#define M_LOG10E 0.43429448190325182765
80#endif
85bdb6ac
MW
81#ifndef M_LN2
82#define M_LN2 0.69314718055994530942
83#endif
8ab3d8a0
KR
84#ifndef M_PI
85#define M_PI 3.14159265358979323846
86#endif
87
cba521fe
MW
88/* FIXME: We assume that FLT_RADIX is 2 */
89verify (FLT_RADIX == 2);
90
e25f3727
AW
91typedef scm_t_signed_bits scm_t_inum;
92#define scm_from_inum(x) (scm_from_signed_integer (x))
93
7112615f
MW
94/* Tests to see if a C double is neither infinite nor a NaN.
95 TODO: if it's available, use C99's isfinite(x) instead */
96#define DOUBLE_IS_FINITE(x) (!isinf(x) && !isnan(x))
97
041fccf6
MW
98/* On some platforms, isinf(x) returns 0, 1 or -1, indicating the sign
99 of the infinity, but other platforms return a boolean only. */
100#define DOUBLE_IS_POSITIVE_INFINITY(x) (isinf(x) && ((x) > 0))
101#define DOUBLE_IS_NEGATIVE_INFINITY(x) (isinf(x) && ((x) < 0))
102
4cc2e41c
MW
103/* Test an inum to see if it can be converted to a double without loss
104 of precision. Note that this will sometimes return 0 even when 1
105 could have been returned, e.g. for large powers of 2. It is designed
106 to be a fast check to optimize common cases. */
107#define INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE(n) \
108 (SCM_I_FIXNUM_BIT-1 <= DBL_MANT_DIG \
109 || ((n) ^ ((n) >> (SCM_I_FIXNUM_BIT-1))) < (1L << DBL_MANT_DIG))
07b390d5
LC
110
111#if ! HAVE_DECL_MPZ_INITS
112
113/* GMP < 5.0.0 lacks `mpz_inits' and `mpz_clears'. Provide them. */
114
115#define VARARG_MPZ_ITERATOR(func) \
116 static void \
117 func ## s (mpz_t x, ...) \
118 { \
119 va_list ap; \
120 \
121 va_start (ap, x); \
122 while (x != NULL) \
123 { \
124 func (x); \
125 x = va_arg (ap, mpz_ptr); \
126 } \
127 va_end (ap); \
128 }
129
130VARARG_MPZ_ITERATOR (mpz_init)
131VARARG_MPZ_ITERATOR (mpz_clear)
132
133#endif
134
0f2d19dd 135\f
f4c627b3 136
ca46fb90
RB
137/*
138 Wonder if this might be faster for some of our code? A switch on
139 the numtag would jump directly to the right case, and the
140 SCM_I_NUMTAG code might be faster than repeated SCM_FOOP tests...
141
142 #define SCM_I_NUMTAG_NOTNUM 0
143 #define SCM_I_NUMTAG_INUM 1
144 #define SCM_I_NUMTAG_BIG scm_tc16_big
145 #define SCM_I_NUMTAG_REAL scm_tc16_real
146 #define SCM_I_NUMTAG_COMPLEX scm_tc16_complex
147 #define SCM_I_NUMTAG(x) \
e11e83f3 148 (SCM_I_INUMP(x) ? SCM_I_NUMTAG_INUM \
ca46fb90 149 : (SCM_IMP(x) ? SCM_I_NUMTAG_NOTNUM \
534c55a9 150 : (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) ? SCM_TYP16(x) \
ca46fb90
RB
151 : SCM_I_NUMTAG_NOTNUM)))
152*/
f92e85f7 153/* the macro above will not work as is with fractions */
f4c627b3
DH
154
155
b57bf272
AW
156/* Default to 1, because as we used to hard-code `free' as the
157 deallocator, we know that overriding these functions with
158 instrumented `malloc' / `free' is OK. */
159int scm_install_gmp_memory_functions = 1;
e7efe8e7 160static SCM flo0;
ff62c168 161static SCM exactly_one_half;
a5f6b751 162static SCM flo_log10e;
e7efe8e7 163
34d19ef6 164#define SCM_SWAP(x, y) do { SCM __t = x; x = y; y = __t; } while (0)
09fb7599 165
56e55ac7 166/* FLOBUFLEN is the maximum number of characters neccessary for the
3a9809df
DH
167 * printed or scm_string representation of an inexact number.
168 */
0b799eea 169#define FLOBUFLEN (40+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
3a9809df 170
b127c712 171
ad79736c
AW
172#if !defined (HAVE_ASINH)
173static double asinh (double x) { return log (x + sqrt (x * x + 1)); }
174#endif
175#if !defined (HAVE_ACOSH)
176static double acosh (double x) { return log (x + sqrt (x * x - 1)); }
177#endif
178#if !defined (HAVE_ATANH)
179static double atanh (double x) { return 0.5 * log ((1 + x) / (1 - x)); }
180#endif
181
18d78c5e
MW
182/* mpz_cmp_d in GMP before 4.2 didn't recognise infinities, so
183 xmpz_cmp_d uses an explicit check. Starting with GMP 4.2 (released
184 in March 2006), mpz_cmp_d now handles infinities properly. */
f8a8200b 185#if 1
b127c712 186#define xmpz_cmp_d(z, d) \
2e65b52f 187 (isinf (d) ? (d < 0.0 ? 1 : -1) : mpz_cmp_d (z, d))
b127c712
KR
188#else
189#define xmpz_cmp_d(z, d) mpz_cmp_d (z, d)
190#endif
191
f92e85f7 192
4b26c03e 193#if defined (GUILE_I)
03976fee 194#if defined HAVE_COMPLEX_DOUBLE
8ab3d8a0
KR
195
196/* For an SCM object Z which is a complex number (ie. satisfies
197 SCM_COMPLEXP), return its value as a C level "complex double". */
198#define SCM_COMPLEX_VALUE(z) \
4b26c03e 199 (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z))
8ab3d8a0 200
7a35784c 201static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED;
8ab3d8a0
KR
202
203/* Convert a C "complex double" to an SCM value. */
7a35784c 204static inline SCM
8ab3d8a0
KR
205scm_from_complex_double (complex double z)
206{
207 return scm_c_make_rectangular (creal (z), cimag (z));
208}
bca69a9f 209
8ab3d8a0 210#endif /* HAVE_COMPLEX_DOUBLE */
bca69a9f 211#endif /* GUILE_I */
8ab3d8a0 212
0f2d19dd
JB
213\f
214
713a4259 215static mpz_t z_negative_one;
ac0c002c
DH
216
217\f
b57bf272 218
864e7d42
LC
219/* Clear the `mpz_t' embedded in bignum PTR. */
220static void
6922d92f 221finalize_bignum (void *ptr, void *data)
864e7d42
LC
222{
223 SCM bignum;
224
225 bignum = PTR2SCM (ptr);
226 mpz_clear (SCM_I_BIG_MPZ (bignum));
227}
228
b57bf272
AW
229/* The next three functions (custom_libgmp_*) are passed to
230 mp_set_memory_functions (in GMP) so that memory used by the digits
231 themselves is known to the garbage collector. This is needed so
232 that GC will be run at appropriate times. Otherwise, a program which
233 creates many large bignums would malloc a huge amount of memory
234 before the GC runs. */
235static void *
236custom_gmp_malloc (size_t alloc_size)
237{
238 return scm_malloc (alloc_size);
239}
240
241static void *
242custom_gmp_realloc (void *old_ptr, size_t old_size, size_t new_size)
243{
244 return scm_realloc (old_ptr, new_size);
245}
246
247static void
248custom_gmp_free (void *ptr, size_t size)
249{
250 free (ptr);
251}
252
253
d017fcdf
LC
254/* Return a new uninitialized bignum. */
255static inline SCM
256make_bignum (void)
257{
258 scm_t_bits *p;
259
260 /* Allocate one word for the type tag and enough room for an `mpz_t'. */
261 p = scm_gc_malloc_pointerless (sizeof (scm_t_bits) + sizeof (mpz_t),
262 "bignum");
263 p[0] = scm_tc16_big;
264
75ba64d6 265 scm_i_set_finalizer (p, finalize_bignum, NULL);
864e7d42 266
d017fcdf
LC
267 return SCM_PACK (p);
268}
ac0c002c 269
864e7d42 270
189171c5 271SCM
ca46fb90
RB
272scm_i_mkbig ()
273{
274 /* Return a newly created bignum. */
d017fcdf 275 SCM z = make_bignum ();
ca46fb90
RB
276 mpz_init (SCM_I_BIG_MPZ (z));
277 return z;
278}
279
e25f3727
AW
280static SCM
281scm_i_inum2big (scm_t_inum x)
282{
283 /* Return a newly created bignum initialized to X. */
284 SCM z = make_bignum ();
285#if SIZEOF_VOID_P == SIZEOF_LONG
286 mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
287#else
288 /* Note that in this case, you'll also have to check all mpz_*_ui and
289 mpz_*_si invocations in Guile. */
290#error creation of mpz not implemented for this inum size
291#endif
292 return z;
293}
294
189171c5 295SCM
c71b0706
MV
296scm_i_long2big (long x)
297{
298 /* Return a newly created bignum initialized to X. */
d017fcdf 299 SCM z = make_bignum ();
c71b0706
MV
300 mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
301 return z;
302}
303
189171c5 304SCM
c71b0706
MV
305scm_i_ulong2big (unsigned long x)
306{
307 /* Return a newly created bignum initialized to X. */
d017fcdf 308 SCM z = make_bignum ();
c71b0706
MV
309 mpz_init_set_ui (SCM_I_BIG_MPZ (z), x);
310 return z;
311}
312
189171c5 313SCM
ca46fb90
RB
314scm_i_clonebig (SCM src_big, int same_sign_p)
315{
316 /* Copy src_big's value, negate it if same_sign_p is false, and return. */
d017fcdf 317 SCM z = make_bignum ();
ca46fb90 318 mpz_init_set (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (src_big));
0aacf84e
MD
319 if (!same_sign_p)
320 mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z));
ca46fb90
RB
321 return z;
322}
323
189171c5 324int
ca46fb90
RB
325scm_i_bigcmp (SCM x, SCM y)
326{
327 /* Return neg if x < y, pos if x > y, and 0 if x == y */
328 /* presume we already know x and y are bignums */
329 int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
330 scm_remember_upto_here_2 (x, y);
331 return result;
332}
333
189171c5 334SCM
ca46fb90
RB
335scm_i_dbl2big (double d)
336{
337 /* results are only defined if d is an integer */
d017fcdf 338 SCM z = make_bignum ();
ca46fb90
RB
339 mpz_init_set_d (SCM_I_BIG_MPZ (z), d);
340 return z;
341}
342
f92e85f7
MV
343/* Convert a integer in double representation to a SCM number. */
344
189171c5 345SCM
f92e85f7
MV
346scm_i_dbl2num (double u)
347{
348 /* SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM are both
349 powers of 2, so there's no rounding when making "double" values
350 from them. If plain SCM_MOST_POSITIVE_FIXNUM was used it could
351 get rounded on a 64-bit machine, hence the "+1".
352
353 The use of floor() to force to an integer value ensures we get a
354 "numerically closest" value without depending on how a
355 double->long cast or how mpz_set_d will round. For reference,
356 double->long probably follows the hardware rounding mode,
357 mpz_set_d truncates towards zero. */
358
359 /* XXX - what happens when SCM_MOST_POSITIVE_FIXNUM etc is not
360 representable as a double? */
361
362 if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1)
363 && u >= (double) SCM_MOST_NEGATIVE_FIXNUM)
e25f3727 364 return SCM_I_MAKINUM ((scm_t_inum) u);
f92e85f7
MV
365 else
366 return scm_i_dbl2big (u);
367}
368
1eb6a33a
MW
369static SCM round_right_shift_exact_integer (SCM n, long count);
370
371/* scm_i_big2dbl_2exp() is like frexp for bignums: it converts the
372 bignum b into a normalized significand and exponent such that
373 b = significand * 2^exponent and 1/2 <= abs(significand) < 1.
374 The return value is the significand rounded to the closest
375 representable double, and the exponent is placed into *expon_p.
376 If b is zero, then the returned exponent and significand are both
377 zero. */
378
379static double
380scm_i_big2dbl_2exp (SCM b, long *expon_p)
ca46fb90 381{
1eb6a33a
MW
382 size_t bits = mpz_sizeinbase (SCM_I_BIG_MPZ (b), 2);
383 size_t shift = 0;
089c9a59
KR
384
385 if (bits > DBL_MANT_DIG)
386 {
1eb6a33a
MW
387 shift = bits - DBL_MANT_DIG;
388 b = round_right_shift_exact_integer (b, shift);
389 if (SCM_I_INUMP (b))
089c9a59 390 {
1eb6a33a
MW
391 int expon;
392 double signif = frexp (SCM_I_INUM (b), &expon);
393 *expon_p = expon + shift;
394 return signif;
089c9a59
KR
395 }
396 }
397
1eb6a33a
MW
398 {
399 long expon;
400 double signif = mpz_get_d_2exp (&expon, SCM_I_BIG_MPZ (b));
401 scm_remember_upto_here_1 (b);
402 *expon_p = expon + shift;
403 return signif;
404 }
405}
406
407/* scm_i_big2dbl() rounds to the closest representable double,
408 in accordance with R5RS exact->inexact. */
409double
410scm_i_big2dbl (SCM b)
411{
412 long expon;
413 double signif = scm_i_big2dbl_2exp (b, &expon);
414 return ldexp (signif, expon);
ca46fb90
RB
415}
416
189171c5 417SCM
ca46fb90
RB
418scm_i_normbig (SCM b)
419{
420 /* convert a big back to a fixnum if it'll fit */
421 /* presume b is a bignum */
422 if (mpz_fits_slong_p (SCM_I_BIG_MPZ (b)))
423 {
e25f3727 424 scm_t_inum val = mpz_get_si (SCM_I_BIG_MPZ (b));
ca46fb90 425 if (SCM_FIXABLE (val))
d956fa6f 426 b = SCM_I_MAKINUM (val);
ca46fb90
RB
427 }
428 return b;
429}
f872b822 430
f92e85f7
MV
431static SCM_C_INLINE_KEYWORD SCM
432scm_i_mpz2num (mpz_t b)
433{
434 /* convert a mpz number to a SCM number. */
435 if (mpz_fits_slong_p (b))
436 {
e25f3727 437 scm_t_inum val = mpz_get_si (b);
f92e85f7 438 if (SCM_FIXABLE (val))
d956fa6f 439 return SCM_I_MAKINUM (val);
f92e85f7
MV
440 }
441
442 {
d017fcdf 443 SCM z = make_bignum ();
f92e85f7
MV
444 mpz_init_set (SCM_I_BIG_MPZ (z), b);
445 return z;
446 }
447}
448
a285b18c
MW
449/* Make the ratio NUMERATOR/DENOMINATOR, where:
450 1. NUMERATOR and DENOMINATOR are exact integers
451 2. NUMERATOR and DENOMINATOR are reduced to lowest terms: gcd(n,d) == 1 */
cba42c93 452static SCM
a285b18c 453scm_i_make_ratio_already_reduced (SCM numerator, SCM denominator)
f92e85f7 454{
a285b18c
MW
455 /* Flip signs so that the denominator is positive. */
456 if (scm_is_false (scm_positive_p (denominator)))
f92e85f7 457 {
a285b18c 458 if (SCM_UNLIKELY (scm_is_eq (denominator, SCM_INUM0)))
f92e85f7 459 scm_num_overflow ("make-ratio");
a285b18c 460 else
f92e85f7 461 {
a285b18c
MW
462 numerator = scm_difference (numerator, SCM_UNDEFINED);
463 denominator = scm_difference (denominator, SCM_UNDEFINED);
f92e85f7
MV
464 }
465 }
a285b18c
MW
466
467 /* Check for the integer case */
468 if (scm_is_eq (denominator, SCM_INUM1))
469 return numerator;
470
471 return scm_double_cell (scm_tc16_fraction,
472 SCM_UNPACK (numerator),
473 SCM_UNPACK (denominator), 0);
474}
475
476static SCM scm_exact_integer_quotient (SCM x, SCM y);
477
478/* Make the ratio NUMERATOR/DENOMINATOR */
479static SCM
480scm_i_make_ratio (SCM numerator, SCM denominator)
481#define FUNC_NAME "make-ratio"
482{
483 /* Make sure the arguments are proper */
484 if (!SCM_LIKELY (SCM_I_INUMP (numerator) || SCM_BIGP (numerator)))
485 SCM_WRONG_TYPE_ARG (1, numerator);
486 else if (!SCM_LIKELY (SCM_I_INUMP (denominator) || SCM_BIGP (denominator)))
487 SCM_WRONG_TYPE_ARG (2, denominator);
488 else
f92e85f7 489 {
a285b18c
MW
490 SCM the_gcd = scm_gcd (numerator, denominator);
491 if (!(scm_is_eq (the_gcd, SCM_INUM1)))
c60e130c 492 {
a285b18c
MW
493 /* Reduce to lowest terms */
494 numerator = scm_exact_integer_quotient (numerator, the_gcd);
495 denominator = scm_exact_integer_quotient (denominator, the_gcd);
f92e85f7 496 }
a285b18c 497 return scm_i_make_ratio_already_reduced (numerator, denominator);
f92e85f7 498 }
f92e85f7 499}
c60e130c 500#undef FUNC_NAME
f92e85f7 501
98237784
MW
502static mpz_t scm_i_divide2double_lo2b;
503
504/* Return the double that is closest to the exact rational N/D, with
505 ties rounded toward even mantissas. N and D must be exact
506 integers. */
507static double
508scm_i_divide2double (SCM n, SCM d)
509{
510 int neg;
511 mpz_t nn, dd, lo, hi, x;
512 ssize_t e;
513
c8248c8e 514 if (SCM_LIKELY (SCM_I_INUMP (d)))
98237784 515 {
4cc2e41c
MW
516 if (SCM_LIKELY
517 (SCM_I_INUMP (n)
518 && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (n))
519 && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (d))))
c8248c8e
MW
520 /* If both N and D can be losslessly converted to doubles, then
521 we can rely on IEEE floating point to do proper rounding much
522 faster than we can. */
523 return ((double) SCM_I_INUM (n)) / ((double) SCM_I_INUM (d));
524
98237784
MW
525 if (SCM_UNLIKELY (scm_is_eq (d, SCM_INUM0)))
526 {
527 if (scm_is_true (scm_positive_p (n)))
528 return 1.0 / 0.0;
529 else if (scm_is_true (scm_negative_p (n)))
530 return -1.0 / 0.0;
531 else
532 return 0.0 / 0.0;
533 }
c8248c8e 534
98237784
MW
535 mpz_init_set_si (dd, SCM_I_INUM (d));
536 }
537 else
538 mpz_init_set (dd, SCM_I_BIG_MPZ (d));
539
540 if (SCM_I_INUMP (n))
541 mpz_init_set_si (nn, SCM_I_INUM (n));
542 else
543 mpz_init_set (nn, SCM_I_BIG_MPZ (n));
544
545 neg = (mpz_sgn (nn) < 0) ^ (mpz_sgn (dd) < 0);
546 mpz_abs (nn, nn);
547 mpz_abs (dd, dd);
548
549 /* Now we need to find the value of e such that:
550
551 For e <= 0:
552 b^{p-1} - 1/2b <= b^-e n / d < b^p - 1/2 [1A]
553 (2 b^p - 1) <= 2 b b^-e n / d < (2 b^p - 1) b [2A]
554 (2 b^p - 1) d <= 2 b b^-e n < (2 b^p - 1) d b [3A]
555
556 For e >= 0:
557 b^{p-1} - 1/2b <= n / b^e d < b^p - 1/2 [1B]
558 (2 b^p - 1) <= 2 b n / b^e d < (2 b^p - 1) b [2B]
559 (2 b^p - 1) d b^e <= 2 b n < (2 b^p - 1) d b b^e [3B]
560
561 where: p = DBL_MANT_DIG
562 b = FLT_RADIX (here assumed to be 2)
563
564 After rounding, the mantissa must be an integer between b^{p-1} and
565 (b^p - 1), except for subnormal numbers. In the inequations [1A]
566 and [1B], the middle expression represents the mantissa *before*
567 rounding, and therefore is bounded by the range of values that will
568 round to a floating-point number with the exponent e. The upper
569 bound is (b^p - 1 + 1/2) = (b^p - 1/2), and is exclusive because
570 ties will round up to the next power of b. The lower bound is
571 (b^{p-1} - 1/2b), and is inclusive because ties will round toward
572 this power of b. Here we subtract 1/2b instead of 1/2 because it
573 is in the range of the next smaller exponent, where the
574 representable numbers are closer together by a factor of b.
575
576 Inequations [2A] and [2B] are derived from [1A] and [1B] by
577 multiplying by 2b, and in [3A] and [3B] we multiply by the
578 denominator of the middle value to obtain integer expressions.
579
580 In the code below, we refer to the three expressions in [3A] or
581 [3B] as lo, x, and hi. If the number is normalizable, we will
582 achieve the goal: lo <= x < hi */
583
584 /* Make an initial guess for e */
585 e = mpz_sizeinbase (nn, 2) - mpz_sizeinbase (dd, 2) - (DBL_MANT_DIG-1);
586 if (e < DBL_MIN_EXP - DBL_MANT_DIG)
587 e = DBL_MIN_EXP - DBL_MANT_DIG;
588
589 /* Compute the initial values of lo, x, and hi
590 based on the initial guess of e */
591 mpz_inits (lo, hi, x, NULL);
592 mpz_mul_2exp (x, nn, 2 + ((e < 0) ? -e : 0));
593 mpz_mul (lo, dd, scm_i_divide2double_lo2b);
594 if (e > 0)
595 mpz_mul_2exp (lo, lo, e);
596 mpz_mul_2exp (hi, lo, 1);
597
598 /* Adjust e as needed to satisfy the inequality lo <= x < hi,
599 (but without making e less then the minimum exponent) */
600 while (mpz_cmp (x, lo) < 0 && e > DBL_MIN_EXP - DBL_MANT_DIG)
601 {
602 mpz_mul_2exp (x, x, 1);
603 e--;
604 }
605 while (mpz_cmp (x, hi) >= 0)
606 {
607 /* If we ever used lo's value again,
608 we would need to double lo here. */
609 mpz_mul_2exp (hi, hi, 1);
610 e++;
611 }
612
613 /* Now compute the rounded mantissa:
614 n / b^e d (if e >= 0)
615 n b^-e / d (if e <= 0) */
616 {
617 int cmp;
618 double result;
619
620 if (e < 0)
621 mpz_mul_2exp (nn, nn, -e);
622 else
623 mpz_mul_2exp (dd, dd, e);
624
625 /* mpz does not directly support rounded right
626 shifts, so we have to do it the hard way.
627 For efficiency, we reuse lo and hi.
628 hi == quotient, lo == remainder */
629 mpz_fdiv_qr (hi, lo, nn, dd);
630
631 /* The fractional part of the unrounded mantissa would be
632 remainder/dividend, i.e. lo/dd. So we have a tie if
633 lo/dd = 1/2. Multiplying both sides by 2*dd yields the
634 integer expression 2*lo = dd. Here we do that comparison
635 to decide whether to round up or down. */
636 mpz_mul_2exp (lo, lo, 1);
637 cmp = mpz_cmp (lo, dd);
638 if (cmp > 0 || (cmp == 0 && mpz_odd_p (hi)))
639 mpz_add_ui (hi, hi, 1);
640
641 result = ldexp (mpz_get_d (hi), e);
642 if (neg)
643 result = -result;
644
645 mpz_clears (nn, dd, lo, hi, x, NULL);
646 return result;
647 }
648}
649
f92e85f7
MV
650double
651scm_i_fraction2double (SCM z)
652{
98237784
MW
653 return scm_i_divide2double (SCM_FRACTION_NUMERATOR (z),
654 SCM_FRACTION_DENOMINATOR (z));
f92e85f7
MV
655}
656
2e274311
MW
657static int
658double_is_non_negative_zero (double x)
659{
660 static double zero = 0.0;
661
662 return !memcmp (&x, &zero, sizeof(double));
663}
664
2519490c
MW
665SCM_PRIMITIVE_GENERIC (scm_exact_p, "exact?", 1, 0, 0,
666 (SCM x),
942e5b91
MG
667 "Return @code{#t} if @var{x} is an exact number, @code{#f}\n"
668 "otherwise.")
1bbd0b84 669#define FUNC_NAME s_scm_exact_p
0f2d19dd 670{
41df63cf
MW
671 if (SCM_INEXACTP (x))
672 return SCM_BOOL_F;
673 else if (SCM_NUMBERP (x))
0aacf84e 674 return SCM_BOOL_T;
41df63cf 675 else
2519490c 676 SCM_WTA_DISPATCH_1 (g_scm_exact_p, x, 1, s_scm_exact_p);
41df63cf
MW
677}
678#undef FUNC_NAME
679
022dda69
MG
680int
681scm_is_exact (SCM val)
682{
683 return scm_is_true (scm_exact_p (val));
684}
41df63cf 685
2519490c 686SCM_PRIMITIVE_GENERIC (scm_inexact_p, "inexact?", 1, 0, 0,
41df63cf
MW
687 (SCM x),
688 "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
689 "else.")
690#define FUNC_NAME s_scm_inexact_p
691{
692 if (SCM_INEXACTP (x))
f92e85f7 693 return SCM_BOOL_T;
41df63cf 694 else if (SCM_NUMBERP (x))
eb927cb9 695 return SCM_BOOL_F;
41df63cf 696 else
2519490c 697 SCM_WTA_DISPATCH_1 (g_scm_inexact_p, x, 1, s_scm_inexact_p);
0f2d19dd 698}
1bbd0b84 699#undef FUNC_NAME
0f2d19dd 700
022dda69
MG
701int
702scm_is_inexact (SCM val)
703{
704 return scm_is_true (scm_inexact_p (val));
705}
4219f20d 706
2519490c 707SCM_PRIMITIVE_GENERIC (scm_odd_p, "odd?", 1, 0, 0,
1bbd0b84 708 (SCM n),
942e5b91
MG
709 "Return @code{#t} if @var{n} is an odd number, @code{#f}\n"
710 "otherwise.")
1bbd0b84 711#define FUNC_NAME s_scm_odd_p
0f2d19dd 712{
e11e83f3 713 if (SCM_I_INUMP (n))
0aacf84e 714 {
e25f3727 715 scm_t_inum val = SCM_I_INUM (n);
73e4de09 716 return scm_from_bool ((val & 1L) != 0);
0aacf84e
MD
717 }
718 else if (SCM_BIGP (n))
719 {
720 int odd_p = mpz_odd_p (SCM_I_BIG_MPZ (n));
721 scm_remember_upto_here_1 (n);
73e4de09 722 return scm_from_bool (odd_p);
0aacf84e 723 }
f92e85f7
MV
724 else if (SCM_REALP (n))
725 {
2519490c
MW
726 double val = SCM_REAL_VALUE (n);
727 if (DOUBLE_IS_FINITE (val))
728 {
729 double rem = fabs (fmod (val, 2.0));
730 if (rem == 1.0)
731 return SCM_BOOL_T;
732 else if (rem == 0.0)
733 return SCM_BOOL_F;
734 }
f92e85f7 735 }
2519490c 736 SCM_WTA_DISPATCH_1 (g_scm_odd_p, n, 1, s_scm_odd_p);
0f2d19dd 737}
1bbd0b84 738#undef FUNC_NAME
0f2d19dd 739
4219f20d 740
2519490c 741SCM_PRIMITIVE_GENERIC (scm_even_p, "even?", 1, 0, 0,
1bbd0b84 742 (SCM n),
942e5b91
MG
743 "Return @code{#t} if @var{n} is an even number, @code{#f}\n"
744 "otherwise.")
1bbd0b84 745#define FUNC_NAME s_scm_even_p
0f2d19dd 746{
e11e83f3 747 if (SCM_I_INUMP (n))
0aacf84e 748 {
e25f3727 749 scm_t_inum val = SCM_I_INUM (n);
73e4de09 750 return scm_from_bool ((val & 1L) == 0);
0aacf84e
MD
751 }
752 else if (SCM_BIGP (n))
753 {
754 int even_p = mpz_even_p (SCM_I_BIG_MPZ (n));
755 scm_remember_upto_here_1 (n);
73e4de09 756 return scm_from_bool (even_p);
0aacf84e 757 }
f92e85f7
MV
758 else if (SCM_REALP (n))
759 {
2519490c
MW
760 double val = SCM_REAL_VALUE (n);
761 if (DOUBLE_IS_FINITE (val))
762 {
763 double rem = fabs (fmod (val, 2.0));
764 if (rem == 1.0)
765 return SCM_BOOL_F;
766 else if (rem == 0.0)
767 return SCM_BOOL_T;
768 }
f92e85f7 769 }
2519490c 770 SCM_WTA_DISPATCH_1 (g_scm_even_p, n, 1, s_scm_even_p);
0f2d19dd 771}
1bbd0b84 772#undef FUNC_NAME
0f2d19dd 773
2519490c
MW
774SCM_PRIMITIVE_GENERIC (scm_finite_p, "finite?", 1, 0, 0,
775 (SCM x),
10391e06
AW
776 "Return @code{#t} if the real number @var{x} is neither\n"
777 "infinite nor a NaN, @code{#f} otherwise.")
7112615f
MW
778#define FUNC_NAME s_scm_finite_p
779{
780 if (SCM_REALP (x))
781 return scm_from_bool (DOUBLE_IS_FINITE (SCM_REAL_VALUE (x)));
10391e06 782 else if (scm_is_real (x))
7112615f
MW
783 return SCM_BOOL_T;
784 else
2519490c 785 SCM_WTA_DISPATCH_1 (g_scm_finite_p, x, 1, s_scm_finite_p);
7112615f
MW
786}
787#undef FUNC_NAME
788
2519490c
MW
789SCM_PRIMITIVE_GENERIC (scm_inf_p, "inf?", 1, 0, 0,
790 (SCM x),
791 "Return @code{#t} if the real number @var{x} is @samp{+inf.0} or\n"
792 "@samp{-inf.0}. Otherwise return @code{#f}.")
7351e207
MV
793#define FUNC_NAME s_scm_inf_p
794{
b1092b3a 795 if (SCM_REALP (x))
2e65b52f 796 return scm_from_bool (isinf (SCM_REAL_VALUE (x)));
10391e06 797 else if (scm_is_real (x))
7351e207 798 return SCM_BOOL_F;
10391e06 799 else
2519490c 800 SCM_WTA_DISPATCH_1 (g_scm_inf_p, x, 1, s_scm_inf_p);
7351e207
MV
801}
802#undef FUNC_NAME
803
2519490c
MW
804SCM_PRIMITIVE_GENERIC (scm_nan_p, "nan?", 1, 0, 0,
805 (SCM x),
10391e06
AW
806 "Return @code{#t} if the real number @var{x} is a NaN,\n"
807 "or @code{#f} otherwise.")
7351e207
MV
808#define FUNC_NAME s_scm_nan_p
809{
10391e06
AW
810 if (SCM_REALP (x))
811 return scm_from_bool (isnan (SCM_REAL_VALUE (x)));
812 else if (scm_is_real (x))
7351e207 813 return SCM_BOOL_F;
10391e06 814 else
2519490c 815 SCM_WTA_DISPATCH_1 (g_scm_nan_p, x, 1, s_scm_nan_p);
7351e207
MV
816}
817#undef FUNC_NAME
818
819/* Guile's idea of infinity. */
820static double guile_Inf;
821
822/* Guile's idea of not a number. */
823static double guile_NaN;
824
825static void
826guile_ieee_init (void)
827{
7351e207
MV
828/* Some version of gcc on some old version of Linux used to crash when
829 trying to make Inf and NaN. */
830
240a27d2
KR
831#ifdef INFINITY
832 /* C99 INFINITY, when available.
833 FIXME: The standard allows for INFINITY to be something that overflows
834 at compile time. We ought to have a configure test to check for that
835 before trying to use it. (But in practice we believe this is not a
836 problem on any system guile is likely to target.) */
837 guile_Inf = INFINITY;
56a3dcd4 838#elif defined HAVE_DINFINITY
240a27d2 839 /* OSF */
7351e207 840 extern unsigned int DINFINITY[2];
eaa94eaa 841 guile_Inf = (*((double *) (DINFINITY)));
7351e207
MV
842#else
843 double tmp = 1e+10;
844 guile_Inf = tmp;
845 for (;;)
846 {
847 guile_Inf *= 1e+10;
848 if (guile_Inf == tmp)
849 break;
850 tmp = guile_Inf;
851 }
852#endif
853
240a27d2
KR
854#ifdef NAN
855 /* C99 NAN, when available */
856 guile_NaN = NAN;
56a3dcd4 857#elif defined HAVE_DQNAN
eaa94eaa
LC
858 {
859 /* OSF */
860 extern unsigned int DQNAN[2];
861 guile_NaN = (*((double *)(DQNAN)));
862 }
7351e207
MV
863#else
864 guile_NaN = guile_Inf / guile_Inf;
865#endif
7351e207
MV
866}
867
868SCM_DEFINE (scm_inf, "inf", 0, 0, 0,
869 (void),
870 "Return Inf.")
871#define FUNC_NAME s_scm_inf
872{
873 static int initialized = 0;
874 if (! initialized)
875 {
876 guile_ieee_init ();
877 initialized = 1;
878 }
55f26379 879 return scm_from_double (guile_Inf);
7351e207
MV
880}
881#undef FUNC_NAME
882
883SCM_DEFINE (scm_nan, "nan", 0, 0, 0,
884 (void),
885 "Return NaN.")
886#define FUNC_NAME s_scm_nan
887{
888 static int initialized = 0;
0aacf84e 889 if (!initialized)
7351e207
MV
890 {
891 guile_ieee_init ();
892 initialized = 1;
893 }
55f26379 894 return scm_from_double (guile_NaN);
7351e207
MV
895}
896#undef FUNC_NAME
897
4219f20d 898
a48d60b1
MD
899SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0,
900 (SCM x),
901 "Return the absolute value of @var{x}.")
2519490c 902#define FUNC_NAME s_scm_abs
0f2d19dd 903{
e11e83f3 904 if (SCM_I_INUMP (x))
0aacf84e 905 {
e25f3727 906 scm_t_inum xx = SCM_I_INUM (x);
0aacf84e
MD
907 if (xx >= 0)
908 return x;
909 else if (SCM_POSFIXABLE (-xx))
d956fa6f 910 return SCM_I_MAKINUM (-xx);
0aacf84e 911 else
e25f3727 912 return scm_i_inum2big (-xx);
4219f20d 913 }
9b9ef10c
MW
914 else if (SCM_LIKELY (SCM_REALP (x)))
915 {
916 double xx = SCM_REAL_VALUE (x);
917 /* If x is a NaN then xx<0 is false so we return x unchanged */
918 if (xx < 0.0)
919 return scm_from_double (-xx);
920 /* Handle signed zeroes properly */
921 else if (SCM_UNLIKELY (xx == 0.0))
922 return flo0;
923 else
924 return x;
925 }
0aacf84e
MD
926 else if (SCM_BIGP (x))
927 {
928 const int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
929 if (sgn < 0)
930 return scm_i_clonebig (x, 0);
931 else
932 return x;
4219f20d 933 }
f92e85f7
MV
934 else if (SCM_FRACTIONP (x))
935 {
73e4de09 936 if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x))))
f92e85f7 937 return x;
a285b18c
MW
938 return scm_i_make_ratio_already_reduced
939 (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
940 SCM_FRACTION_DENOMINATOR (x));
f92e85f7 941 }
0aacf84e 942 else
a48d60b1 943 SCM_WTA_DISPATCH_1 (g_scm_abs, x, 1, s_scm_abs);
0f2d19dd 944}
a48d60b1 945#undef FUNC_NAME
0f2d19dd 946
4219f20d 947
2519490c
MW
948SCM_PRIMITIVE_GENERIC (scm_quotient, "quotient", 2, 0, 0,
949 (SCM x, SCM y),
950 "Return the quotient of the numbers @var{x} and @var{y}.")
951#define FUNC_NAME s_scm_quotient
0f2d19dd 952{
495a39c4 953 if (SCM_LIKELY (scm_is_integer (x)))
0aacf84e 954 {
495a39c4 955 if (SCM_LIKELY (scm_is_integer (y)))
a8da6d93 956 return scm_truncate_quotient (x, y);
0aacf84e 957 else
2519490c 958 SCM_WTA_DISPATCH_2 (g_scm_quotient, x, y, SCM_ARG2, s_scm_quotient);
f872b822 959 }
0aacf84e 960 else
2519490c 961 SCM_WTA_DISPATCH_2 (g_scm_quotient, x, y, SCM_ARG1, s_scm_quotient);
0f2d19dd 962}
2519490c 963#undef FUNC_NAME
0f2d19dd 964
2519490c
MW
965SCM_PRIMITIVE_GENERIC (scm_remainder, "remainder", 2, 0, 0,
966 (SCM x, SCM y),
967 "Return the remainder of the numbers @var{x} and @var{y}.\n"
968 "@lisp\n"
969 "(remainder 13 4) @result{} 1\n"
970 "(remainder -13 4) @result{} -1\n"
971 "@end lisp")
972#define FUNC_NAME s_scm_remainder
0f2d19dd 973{
495a39c4 974 if (SCM_LIKELY (scm_is_integer (x)))
0aacf84e 975 {
495a39c4 976 if (SCM_LIKELY (scm_is_integer (y)))
a8da6d93 977 return scm_truncate_remainder (x, y);
0aacf84e 978 else
2519490c 979 SCM_WTA_DISPATCH_2 (g_scm_remainder, x, y, SCM_ARG2, s_scm_remainder);
f872b822 980 }
0aacf84e 981 else
2519490c 982 SCM_WTA_DISPATCH_2 (g_scm_remainder, x, y, SCM_ARG1, s_scm_remainder);
0f2d19dd 983}
2519490c 984#undef FUNC_NAME
0f2d19dd 985
89a7e495 986
2519490c
MW
987SCM_PRIMITIVE_GENERIC (scm_modulo, "modulo", 2, 0, 0,
988 (SCM x, SCM y),
989 "Return the modulo of the numbers @var{x} and @var{y}.\n"
990 "@lisp\n"
991 "(modulo 13 4) @result{} 1\n"
992 "(modulo -13 4) @result{} 3\n"
993 "@end lisp")
994#define FUNC_NAME s_scm_modulo
0f2d19dd 995{
495a39c4 996 if (SCM_LIKELY (scm_is_integer (x)))
0aacf84e 997 {
495a39c4 998 if (SCM_LIKELY (scm_is_integer (y)))
a8da6d93 999 return scm_floor_remainder (x, y);
0aacf84e 1000 else
2519490c 1001 SCM_WTA_DISPATCH_2 (g_scm_modulo, x, y, SCM_ARG2, s_scm_modulo);
828865c3 1002 }
0aacf84e 1003 else
2519490c 1004 SCM_WTA_DISPATCH_2 (g_scm_modulo, x, y, SCM_ARG1, s_scm_modulo);
0f2d19dd 1005}
2519490c 1006#undef FUNC_NAME
0f2d19dd 1007
a285b18c
MW
1008/* Return the exact integer q such that n = q*d, for exact integers n
1009 and d, where d is known in advance to divide n evenly (with zero
1010 remainder). For large integers, this can be computed more
1011 efficiently than when the remainder is unknown. */
1012static SCM
1013scm_exact_integer_quotient (SCM n, SCM d)
1014#define FUNC_NAME "exact-integer-quotient"
1015{
1016 if (SCM_LIKELY (SCM_I_INUMP (n)))
1017 {
1018 scm_t_inum nn = SCM_I_INUM (n);
1019 if (SCM_LIKELY (SCM_I_INUMP (d)))
1020 {
1021 scm_t_inum dd = SCM_I_INUM (d);
1022 if (SCM_UNLIKELY (dd == 0))
1023 scm_num_overflow ("exact-integer-quotient");
1024 else
1025 {
1026 scm_t_inum qq = nn / dd;
1027 if (SCM_LIKELY (SCM_FIXABLE (qq)))
1028 return SCM_I_MAKINUM (qq);
1029 else
1030 return scm_i_inum2big (qq);
1031 }
1032 }
1033 else if (SCM_LIKELY (SCM_BIGP (d)))
1034 {
1035 /* n is an inum and d is a bignum. Given that d is known to
1036 divide n evenly, there are only two possibilities: n is 0,
1037 or else n is fixnum-min and d is abs(fixnum-min). */
1038 if (nn == 0)
1039 return SCM_INUM0;
1040 else
1041 return SCM_I_MAKINUM (-1);
1042 }
1043 else
1044 SCM_WRONG_TYPE_ARG (2, d);
1045 }
1046 else if (SCM_LIKELY (SCM_BIGP (n)))
1047 {
1048 if (SCM_LIKELY (SCM_I_INUMP (d)))
1049 {
1050 scm_t_inum dd = SCM_I_INUM (d);
1051 if (SCM_UNLIKELY (dd == 0))
1052 scm_num_overflow ("exact-integer-quotient");
1053 else if (SCM_UNLIKELY (dd == 1))
1054 return n;
1055 else
1056 {
1057 SCM q = scm_i_mkbig ();
1058 if (dd > 0)
1059 mpz_divexact_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), dd);
1060 else
1061 {
1062 mpz_divexact_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), -dd);
1063 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
1064 }
1065 scm_remember_upto_here_1 (n);
1066 return scm_i_normbig (q);
1067 }
1068 }
1069 else if (SCM_LIKELY (SCM_BIGP (d)))
1070 {
1071 SCM q = scm_i_mkbig ();
1072 mpz_divexact (SCM_I_BIG_MPZ (q),
1073 SCM_I_BIG_MPZ (n),
1074 SCM_I_BIG_MPZ (d));
1075 scm_remember_upto_here_2 (n, d);
1076 return scm_i_normbig (q);
1077 }
1078 else
1079 SCM_WRONG_TYPE_ARG (2, d);
1080 }
1081 else
1082 SCM_WRONG_TYPE_ARG (1, n);
1083}
1084#undef FUNC_NAME
1085
5fbf680b
MW
1086/* two_valued_wta_dispatch_2 is a version of SCM_WTA_DISPATCH_2 for
1087 two-valued functions. It is called from primitive generics that take
1088 two arguments and return two values, when the core procedure is
1089 unable to handle the given argument types. If there are GOOPS
1090 methods for this primitive generic, it dispatches to GOOPS and, if
1091 successful, expects two values to be returned, which are placed in
1092 *rp1 and *rp2. If there are no GOOPS methods, it throws a
1093 wrong-type-arg exception.
1094
1095 FIXME: This obviously belongs somewhere else, but until we decide on
1096 the right API, it is here as a static function, because it is needed
1097 by the *_divide functions below.
1098*/
1099static void
1100two_valued_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos,
1101 const char *subr, SCM *rp1, SCM *rp2)
1102{
1103 if (SCM_UNPACK (gf))
1104 scm_i_extract_values_2 (scm_call_generic_2 (gf, a1, a2), rp1, rp2);
1105 else
1106 scm_wrong_type_arg (subr, pos, (pos == SCM_ARG1) ? a1 : a2);
1107}
1108
a8da6d93
MW
1109SCM_DEFINE (scm_euclidean_quotient, "euclidean-quotient", 2, 0, 0,
1110 (SCM x, SCM y),
1111 "Return the integer @var{q} such that\n"
1112 "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
1113 "where @math{0 <= @var{r} < abs(@var{y})}.\n"
1114 "@lisp\n"
1115 "(euclidean-quotient 123 10) @result{} 12\n"
1116 "(euclidean-quotient 123 -10) @result{} -12\n"
1117 "(euclidean-quotient -123 10) @result{} -13\n"
1118 "(euclidean-quotient -123 -10) @result{} 13\n"
1119 "(euclidean-quotient -123.2 -63.5) @result{} 2.0\n"
1120 "(euclidean-quotient 16/3 -10/7) @result{} -3\n"
1121 "@end lisp")
ff62c168
MW
1122#define FUNC_NAME s_scm_euclidean_quotient
1123{
a8da6d93
MW
1124 if (scm_is_false (scm_negative_p (y)))
1125 return scm_floor_quotient (x, y);
ff62c168 1126 else
a8da6d93 1127 return scm_ceiling_quotient (x, y);
ff62c168
MW
1128}
1129#undef FUNC_NAME
1130
a8da6d93
MW
1131SCM_DEFINE (scm_euclidean_remainder, "euclidean-remainder", 2, 0, 0,
1132 (SCM x, SCM y),
1133 "Return the real number @var{r} such that\n"
1134 "@math{0 <= @var{r} < abs(@var{y})} and\n"
1135 "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
1136 "for some integer @var{q}.\n"
1137 "@lisp\n"
1138 "(euclidean-remainder 123 10) @result{} 3\n"
1139 "(euclidean-remainder 123 -10) @result{} 3\n"
1140 "(euclidean-remainder -123 10) @result{} 7\n"
1141 "(euclidean-remainder -123 -10) @result{} 7\n"
1142 "(euclidean-remainder -123.2 -63.5) @result{} 3.8\n"
1143 "(euclidean-remainder 16/3 -10/7) @result{} 22/21\n"
1144 "@end lisp")
ff62c168
MW
1145#define FUNC_NAME s_scm_euclidean_remainder
1146{
a8da6d93
MW
1147 if (scm_is_false (scm_negative_p (y)))
1148 return scm_floor_remainder (x, y);
ff62c168 1149 else
a8da6d93 1150 return scm_ceiling_remainder (x, y);
ff62c168
MW
1151}
1152#undef FUNC_NAME
1153
a8da6d93
MW
1154SCM_DEFINE (scm_i_euclidean_divide, "euclidean/", 2, 0, 0,
1155 (SCM x, SCM y),
1156 "Return the integer @var{q} and the real number @var{r}\n"
1157 "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
1158 "and @math{0 <= @var{r} < abs(@var{y})}.\n"
1159 "@lisp\n"
1160 "(euclidean/ 123 10) @result{} 12 and 3\n"
1161 "(euclidean/ 123 -10) @result{} -12 and 3\n"
1162 "(euclidean/ -123 10) @result{} -13 and 7\n"
1163 "(euclidean/ -123 -10) @result{} 13 and 7\n"
1164 "(euclidean/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
1165 "(euclidean/ 16/3 -10/7) @result{} -3 and 22/21\n"
1166 "@end lisp")
5fbf680b
MW
1167#define FUNC_NAME s_scm_i_euclidean_divide
1168{
a8da6d93
MW
1169 if (scm_is_false (scm_negative_p (y)))
1170 return scm_i_floor_divide (x, y);
1171 else
1172 return scm_i_ceiling_divide (x, y);
5fbf680b
MW
1173}
1174#undef FUNC_NAME
1175
5fbf680b
MW
1176void
1177scm_euclidean_divide (SCM x, SCM y, SCM *qp, SCM *rp)
ff62c168 1178{
a8da6d93
MW
1179 if (scm_is_false (scm_negative_p (y)))
1180 return scm_floor_divide (x, y, qp, rp);
ff62c168 1181 else
a8da6d93 1182 return scm_ceiling_divide (x, y, qp, rp);
ff62c168
MW
1183}
1184
8f9da340
MW
1185static SCM scm_i_inexact_floor_quotient (double x, double y);
1186static SCM scm_i_exact_rational_floor_quotient (SCM x, SCM y);
1187
1188SCM_PRIMITIVE_GENERIC (scm_floor_quotient, "floor-quotient", 2, 0, 0,
1189 (SCM x, SCM y),
1190 "Return the floor of @math{@var{x} / @var{y}}.\n"
1191 "@lisp\n"
1192 "(floor-quotient 123 10) @result{} 12\n"
1193 "(floor-quotient 123 -10) @result{} -13\n"
1194 "(floor-quotient -123 10) @result{} -13\n"
1195 "(floor-quotient -123 -10) @result{} 12\n"
1196 "(floor-quotient -123.2 -63.5) @result{} 1.0\n"
1197 "(floor-quotient 16/3 -10/7) @result{} -4\n"
1198 "@end lisp")
1199#define FUNC_NAME s_scm_floor_quotient
1200{
1201 if (SCM_LIKELY (SCM_I_INUMP (x)))
1202 {
1203 scm_t_inum xx = SCM_I_INUM (x);
1204 if (SCM_LIKELY (SCM_I_INUMP (y)))
1205 {
1206 scm_t_inum yy = SCM_I_INUM (y);
1207 scm_t_inum xx1 = xx;
1208 scm_t_inum qq;
1209 if (SCM_LIKELY (yy > 0))
1210 {
1211 if (SCM_UNLIKELY (xx < 0))
1212 xx1 = xx - yy + 1;
1213 }
1214 else if (SCM_UNLIKELY (yy == 0))
1215 scm_num_overflow (s_scm_floor_quotient);
1216 else if (xx > 0)
1217 xx1 = xx - yy - 1;
1218 qq = xx1 / yy;
1219 if (SCM_LIKELY (SCM_FIXABLE (qq)))
1220 return SCM_I_MAKINUM (qq);
1221 else
1222 return scm_i_inum2big (qq);
1223 }
1224 else if (SCM_BIGP (y))
1225 {
1226 int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
1227 scm_remember_upto_here_1 (y);
1228 if (sign > 0)
1229 return SCM_I_MAKINUM ((xx < 0) ? -1 : 0);
1230 else
1231 return SCM_I_MAKINUM ((xx > 0) ? -1 : 0);
1232 }
1233 else if (SCM_REALP (y))
1234 return scm_i_inexact_floor_quotient (xx, SCM_REAL_VALUE (y));
1235 else if (SCM_FRACTIONP (y))
1236 return scm_i_exact_rational_floor_quotient (x, y);
1237 else
1238 SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
1239 s_scm_floor_quotient);
1240 }
1241 else if (SCM_BIGP (x))
1242 {
1243 if (SCM_LIKELY (SCM_I_INUMP (y)))
1244 {
1245 scm_t_inum yy = SCM_I_INUM (y);
1246 if (SCM_UNLIKELY (yy == 0))
1247 scm_num_overflow (s_scm_floor_quotient);
1248 else if (SCM_UNLIKELY (yy == 1))
1249 return x;
1250 else
1251 {
1252 SCM q = scm_i_mkbig ();
1253 if (yy > 0)
1254 mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy);
1255 else
1256 {
1257 mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy);
1258 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
1259 }
1260 scm_remember_upto_here_1 (x);
1261 return scm_i_normbig (q);
1262 }
1263 }
1264 else if (SCM_BIGP (y))
1265 {
1266 SCM q = scm_i_mkbig ();
1267 mpz_fdiv_q (SCM_I_BIG_MPZ (q),
1268 SCM_I_BIG_MPZ (x),
1269 SCM_I_BIG_MPZ (y));
1270 scm_remember_upto_here_2 (x, y);
1271 return scm_i_normbig (q);
1272 }
1273 else if (SCM_REALP (y))
1274 return scm_i_inexact_floor_quotient
1275 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
1276 else if (SCM_FRACTIONP (y))
1277 return scm_i_exact_rational_floor_quotient (x, y);
1278 else
1279 SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
1280 s_scm_floor_quotient);
1281 }
1282 else if (SCM_REALP (x))
1283 {
1284 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
1285 SCM_BIGP (y) || SCM_FRACTIONP (y))
1286 return scm_i_inexact_floor_quotient
1287 (SCM_REAL_VALUE (x), scm_to_double (y));
1288 else
1289 SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
1290 s_scm_floor_quotient);
1291 }
1292 else if (SCM_FRACTIONP (x))
1293 {
1294 if (SCM_REALP (y))
1295 return scm_i_inexact_floor_quotient
1296 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
1297 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
1298 return scm_i_exact_rational_floor_quotient (x, y);
1299 else
1300 SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
1301 s_scm_floor_quotient);
1302 }
1303 else
1304 SCM_WTA_DISPATCH_2 (g_scm_floor_quotient, x, y, SCM_ARG1,
1305 s_scm_floor_quotient);
1306}
1307#undef FUNC_NAME
1308
1309static SCM
1310scm_i_inexact_floor_quotient (double x, double y)
1311{
1312 if (SCM_UNLIKELY (y == 0))
1313 scm_num_overflow (s_scm_floor_quotient); /* or return a NaN? */
1314 else
1315 return scm_from_double (floor (x / y));
1316}
1317
1318static SCM
1319scm_i_exact_rational_floor_quotient (SCM x, SCM y)
1320{
1321 return scm_floor_quotient
1322 (scm_product (scm_numerator (x), scm_denominator (y)),
1323 scm_product (scm_numerator (y), scm_denominator (x)));
1324}
1325
1326static SCM scm_i_inexact_floor_remainder (double x, double y);
1327static SCM scm_i_exact_rational_floor_remainder (SCM x, SCM y);
1328
1329SCM_PRIMITIVE_GENERIC (scm_floor_remainder, "floor-remainder", 2, 0, 0,
1330 (SCM x, SCM y),
1331 "Return the real number @var{r} such that\n"
1332 "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
1333 "where @math{@var{q} = floor(@var{x} / @var{y})}.\n"
1334 "@lisp\n"
1335 "(floor-remainder 123 10) @result{} 3\n"
1336 "(floor-remainder 123 -10) @result{} -7\n"
1337 "(floor-remainder -123 10) @result{} 7\n"
1338 "(floor-remainder -123 -10) @result{} -3\n"
1339 "(floor-remainder -123.2 -63.5) @result{} -59.7\n"
1340 "(floor-remainder 16/3 -10/7) @result{} -8/21\n"
1341 "@end lisp")
1342#define FUNC_NAME s_scm_floor_remainder
1343{
1344 if (SCM_LIKELY (SCM_I_INUMP (x)))
1345 {
1346 scm_t_inum xx = SCM_I_INUM (x);
1347 if (SCM_LIKELY (SCM_I_INUMP (y)))
1348 {
1349 scm_t_inum yy = SCM_I_INUM (y);
1350 if (SCM_UNLIKELY (yy == 0))
1351 scm_num_overflow (s_scm_floor_remainder);
1352 else
1353 {
1354 scm_t_inum rr = xx % yy;
1355 int needs_adjustment;
1356
1357 if (SCM_LIKELY (yy > 0))
1358 needs_adjustment = (rr < 0);
1359 else
1360 needs_adjustment = (rr > 0);
1361
1362 if (needs_adjustment)
1363 rr += yy;
1364 return SCM_I_MAKINUM (rr);
1365 }
1366 }
1367 else if (SCM_BIGP (y))
1368 {
1369 int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
1370 scm_remember_upto_here_1 (y);
1371 if (sign > 0)
1372 {
1373 if (xx < 0)
1374 {
1375 SCM r = scm_i_mkbig ();
1376 mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
1377 scm_remember_upto_here_1 (y);
1378 return scm_i_normbig (r);
1379 }
1380 else
1381 return x;
1382 }
1383 else if (xx <= 0)
1384 return x;
1385 else
1386 {
1387 SCM r = scm_i_mkbig ();
1388 mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
1389 scm_remember_upto_here_1 (y);
1390 return scm_i_normbig (r);
1391 }
1392 }
1393 else if (SCM_REALP (y))
1394 return scm_i_inexact_floor_remainder (xx, SCM_REAL_VALUE (y));
1395 else if (SCM_FRACTIONP (y))
1396 return scm_i_exact_rational_floor_remainder (x, y);
1397 else
1398 SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
1399 s_scm_floor_remainder);
1400 }
1401 else if (SCM_BIGP (x))
1402 {
1403 if (SCM_LIKELY (SCM_I_INUMP (y)))
1404 {
1405 scm_t_inum yy = SCM_I_INUM (y);
1406 if (SCM_UNLIKELY (yy == 0))
1407 scm_num_overflow (s_scm_floor_remainder);
1408 else
1409 {
1410 scm_t_inum rr;
1411 if (yy > 0)
1412 rr = mpz_fdiv_ui (SCM_I_BIG_MPZ (x), yy);
1413 else
1414 rr = -mpz_cdiv_ui (SCM_I_BIG_MPZ (x), -yy);
1415 scm_remember_upto_here_1 (x);
1416 return SCM_I_MAKINUM (rr);
1417 }
1418 }
1419 else if (SCM_BIGP (y))
1420 {
1421 SCM r = scm_i_mkbig ();
1422 mpz_fdiv_r (SCM_I_BIG_MPZ (r),
1423 SCM_I_BIG_MPZ (x),
1424 SCM_I_BIG_MPZ (y));
1425 scm_remember_upto_here_2 (x, y);
1426 return scm_i_normbig (r);
1427 }
1428 else if (SCM_REALP (y))
1429 return scm_i_inexact_floor_remainder
1430 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
1431 else if (SCM_FRACTIONP (y))
1432 return scm_i_exact_rational_floor_remainder (x, y);
1433 else
1434 SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
1435 s_scm_floor_remainder);
1436 }
1437 else if (SCM_REALP (x))
1438 {
1439 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
1440 SCM_BIGP (y) || SCM_FRACTIONP (y))
1441 return scm_i_inexact_floor_remainder
1442 (SCM_REAL_VALUE (x), scm_to_double (y));
1443 else
1444 SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
1445 s_scm_floor_remainder);
1446 }
1447 else if (SCM_FRACTIONP (x))
1448 {
1449 if (SCM_REALP (y))
1450 return scm_i_inexact_floor_remainder
1451 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
1452 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
1453 return scm_i_exact_rational_floor_remainder (x, y);
1454 else
1455 SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
1456 s_scm_floor_remainder);
1457 }
1458 else
1459 SCM_WTA_DISPATCH_2 (g_scm_floor_remainder, x, y, SCM_ARG1,
1460 s_scm_floor_remainder);
1461}
1462#undef FUNC_NAME
1463
1464static SCM
1465scm_i_inexact_floor_remainder (double x, double y)
1466{
1467 /* Although it would be more efficient to use fmod here, we can't
1468 because it would in some cases produce results inconsistent with
1469 scm_i_inexact_floor_quotient, such that x != q * y + r (not even
1470 close). In particular, when x is very close to a multiple of y,
1471 then r might be either 0.0 or y, but those two cases must
1472 correspond to different choices of q. If r = 0.0 then q must be
1473 x/y, and if r = y then q must be x/y-1. If quotient chooses one
1474 and remainder chooses the other, it would be bad. */
1475 if (SCM_UNLIKELY (y == 0))
1476 scm_num_overflow (s_scm_floor_remainder); /* or return a NaN? */
1477 else
1478 return scm_from_double (x - y * floor (x / y));
1479}
1480
1481static SCM
1482scm_i_exact_rational_floor_remainder (SCM x, SCM y)
1483{
1484 SCM xd = scm_denominator (x);
1485 SCM yd = scm_denominator (y);
1486 SCM r1 = scm_floor_remainder (scm_product (scm_numerator (x), yd),
1487 scm_product (scm_numerator (y), xd));
1488 return scm_divide (r1, scm_product (xd, yd));
1489}
1490
1491
1492static void scm_i_inexact_floor_divide (double x, double y,
1493 SCM *qp, SCM *rp);
1494static void scm_i_exact_rational_floor_divide (SCM x, SCM y,
1495 SCM *qp, SCM *rp);
1496
1497SCM_PRIMITIVE_GENERIC (scm_i_floor_divide, "floor/", 2, 0, 0,
1498 (SCM x, SCM y),
1499 "Return the integer @var{q} and the real number @var{r}\n"
1500 "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
1501 "and @math{@var{q} = floor(@var{x} / @var{y})}.\n"
1502 "@lisp\n"
1503 "(floor/ 123 10) @result{} 12 and 3\n"
1504 "(floor/ 123 -10) @result{} -13 and -7\n"
1505 "(floor/ -123 10) @result{} -13 and 7\n"
1506 "(floor/ -123 -10) @result{} 12 and -3\n"
1507 "(floor/ -123.2 -63.5) @result{} 1.0 and -59.7\n"
1508 "(floor/ 16/3 -10/7) @result{} -4 and -8/21\n"
1509 "@end lisp")
1510#define FUNC_NAME s_scm_i_floor_divide
1511{
1512 SCM q, r;
1513
1514 scm_floor_divide(x, y, &q, &r);
1515 return scm_values (scm_list_2 (q, r));
1516}
1517#undef FUNC_NAME
1518
1519#define s_scm_floor_divide s_scm_i_floor_divide
1520#define g_scm_floor_divide g_scm_i_floor_divide
1521
1522void
1523scm_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp)
1524{
1525 if (SCM_LIKELY (SCM_I_INUMP (x)))
1526 {
1527 scm_t_inum xx = SCM_I_INUM (x);
1528 if (SCM_LIKELY (SCM_I_INUMP (y)))
1529 {
1530 scm_t_inum yy = SCM_I_INUM (y);
1531 if (SCM_UNLIKELY (yy == 0))
1532 scm_num_overflow (s_scm_floor_divide);
1533 else
1534 {
1535 scm_t_inum qq = xx / yy;
1536 scm_t_inum rr = xx % yy;
1537 int needs_adjustment;
1538
1539 if (SCM_LIKELY (yy > 0))
1540 needs_adjustment = (rr < 0);
1541 else
1542 needs_adjustment = (rr > 0);
1543
1544 if (needs_adjustment)
1545 {
1546 rr += yy;
1547 qq--;
1548 }
1549
1550 if (SCM_LIKELY (SCM_FIXABLE (qq)))
1551 *qp = SCM_I_MAKINUM (qq);
1552 else
1553 *qp = scm_i_inum2big (qq);
1554 *rp = SCM_I_MAKINUM (rr);
1555 }
1556 return;
1557 }
1558 else if (SCM_BIGP (y))
1559 {
1560 int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
1561 scm_remember_upto_here_1 (y);
1562 if (sign > 0)
1563 {
1564 if (xx < 0)
1565 {
1566 SCM r = scm_i_mkbig ();
1567 mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
1568 scm_remember_upto_here_1 (y);
1569 *qp = SCM_I_MAKINUM (-1);
1570 *rp = scm_i_normbig (r);
1571 }
1572 else
1573 {
1574 *qp = SCM_INUM0;
1575 *rp = x;
1576 }
1577 }
1578 else if (xx <= 0)
1579 {
1580 *qp = SCM_INUM0;
1581 *rp = x;
1582 }
1583 else
1584 {
1585 SCM r = scm_i_mkbig ();
1586 mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
1587 scm_remember_upto_here_1 (y);
1588 *qp = SCM_I_MAKINUM (-1);
1589 *rp = scm_i_normbig (r);
1590 }
1591 return;
1592 }
1593 else if (SCM_REALP (y))
1594 return scm_i_inexact_floor_divide (xx, SCM_REAL_VALUE (y), qp, rp);
1595 else if (SCM_FRACTIONP (y))
1596 return scm_i_exact_rational_floor_divide (x, y, qp, rp);
1597 else
1598 return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
1599 s_scm_floor_divide, qp, rp);
1600 }
1601 else if (SCM_BIGP (x))
1602 {
1603 if (SCM_LIKELY (SCM_I_INUMP (y)))
1604 {
1605 scm_t_inum yy = SCM_I_INUM (y);
1606 if (SCM_UNLIKELY (yy == 0))
1607 scm_num_overflow (s_scm_floor_divide);
1608 else
1609 {
1610 SCM q = scm_i_mkbig ();
1611 SCM r = scm_i_mkbig ();
1612 if (yy > 0)
1613 mpz_fdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
1614 SCM_I_BIG_MPZ (x), yy);
1615 else
1616 {
1617 mpz_cdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
1618 SCM_I_BIG_MPZ (x), -yy);
1619 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
1620 }
1621 scm_remember_upto_here_1 (x);
1622 *qp = scm_i_normbig (q);
1623 *rp = scm_i_normbig (r);
1624 }
1625 return;
1626 }
1627 else if (SCM_BIGP (y))
1628 {
1629 SCM q = scm_i_mkbig ();
1630 SCM r = scm_i_mkbig ();
1631 mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
1632 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
1633 scm_remember_upto_here_2 (x, y);
1634 *qp = scm_i_normbig (q);
1635 *rp = scm_i_normbig (r);
1636 return;
1637 }
1638 else if (SCM_REALP (y))
1639 return scm_i_inexact_floor_divide
1640 (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp);
1641 else if (SCM_FRACTIONP (y))
1642 return scm_i_exact_rational_floor_divide (x, y, qp, rp);
1643 else
1644 return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
1645 s_scm_floor_divide, qp, rp);
1646 }
1647 else if (SCM_REALP (x))
1648 {
1649 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
1650 SCM_BIGP (y) || SCM_FRACTIONP (y))
1651 return scm_i_inexact_floor_divide
1652 (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp);
1653 else
1654 return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
1655 s_scm_floor_divide, qp, rp);
1656 }
1657 else if (SCM_FRACTIONP (x))
1658 {
1659 if (SCM_REALP (y))
1660 return scm_i_inexact_floor_divide
1661 (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
1662 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
1663 return scm_i_exact_rational_floor_divide (x, y, qp, rp);
1664 else
1665 return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
1666 s_scm_floor_divide, qp, rp);
1667 }
1668 else
1669 return two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG1,
1670 s_scm_floor_divide, qp, rp);
1671}
1672
1673static void
1674scm_i_inexact_floor_divide (double x, double y, SCM *qp, SCM *rp)
1675{
1676 if (SCM_UNLIKELY (y == 0))
1677 scm_num_overflow (s_scm_floor_divide); /* or return a NaN? */
1678 else
1679 {
1680 double q = floor (x / y);
1681 double r = x - q * y;
1682 *qp = scm_from_double (q);
1683 *rp = scm_from_double (r);
1684 }
1685}
1686
1687static void
1688scm_i_exact_rational_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp)
1689{
1690 SCM r1;
1691 SCM xd = scm_denominator (x);
1692 SCM yd = scm_denominator (y);
1693
1694 scm_floor_divide (scm_product (scm_numerator (x), yd),
1695 scm_product (scm_numerator (y), xd),
1696 qp, &r1);
1697 *rp = scm_divide (r1, scm_product (xd, yd));
1698}
1699
1700static SCM scm_i_inexact_ceiling_quotient (double x, double y);
1701static SCM scm_i_exact_rational_ceiling_quotient (SCM x, SCM y);
1702
1703SCM_PRIMITIVE_GENERIC (scm_ceiling_quotient, "ceiling-quotient", 2, 0, 0,
1704 (SCM x, SCM y),
1705 "Return the ceiling of @math{@var{x} / @var{y}}.\n"
1706 "@lisp\n"
1707 "(ceiling-quotient 123 10) @result{} 13\n"
1708 "(ceiling-quotient 123 -10) @result{} -12\n"
1709 "(ceiling-quotient -123 10) @result{} -12\n"
1710 "(ceiling-quotient -123 -10) @result{} 13\n"
1711 "(ceiling-quotient -123.2 -63.5) @result{} 2.0\n"
1712 "(ceiling-quotient 16/3 -10/7) @result{} -3\n"
1713 "@end lisp")
1714#define FUNC_NAME s_scm_ceiling_quotient
1715{
1716 if (SCM_LIKELY (SCM_I_INUMP (x)))
1717 {
1718 scm_t_inum xx = SCM_I_INUM (x);
1719 if (SCM_LIKELY (SCM_I_INUMP (y)))
1720 {
1721 scm_t_inum yy = SCM_I_INUM (y);
1722 if (SCM_UNLIKELY (yy == 0))
1723 scm_num_overflow (s_scm_ceiling_quotient);
1724 else
1725 {
1726 scm_t_inum xx1 = xx;
1727 scm_t_inum qq;
1728 if (SCM_LIKELY (yy > 0))
1729 {
1730 if (SCM_LIKELY (xx >= 0))
1731 xx1 = xx + yy - 1;
1732 }
8f9da340
MW
1733 else if (xx < 0)
1734 xx1 = xx + yy + 1;
1735 qq = xx1 / yy;
1736 if (SCM_LIKELY (SCM_FIXABLE (qq)))
1737 return SCM_I_MAKINUM (qq);
1738 else
1739 return scm_i_inum2big (qq);
1740 }
1741 }
1742 else if (SCM_BIGP (y))
1743 {
1744 int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
1745 scm_remember_upto_here_1 (y);
1746 if (SCM_LIKELY (sign > 0))
1747 {
1748 if (SCM_LIKELY (xx > 0))
1749 return SCM_INUM1;
1750 else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
1751 && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
1752 - SCM_MOST_NEGATIVE_FIXNUM) == 0))
1753 {
1754 /* Special case: x == fixnum-min && y == abs (fixnum-min) */
1755 scm_remember_upto_here_1 (y);
1756 return SCM_I_MAKINUM (-1);
1757 }
1758 else
1759 return SCM_INUM0;
1760 }
1761 else if (xx >= 0)
1762 return SCM_INUM0;
1763 else
1764 return SCM_INUM1;
1765 }
1766 else if (SCM_REALP (y))
1767 return scm_i_inexact_ceiling_quotient (xx, SCM_REAL_VALUE (y));
1768 else if (SCM_FRACTIONP (y))
1769 return scm_i_exact_rational_ceiling_quotient (x, y);
1770 else
1771 SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
1772 s_scm_ceiling_quotient);
1773 }
1774 else if (SCM_BIGP (x))
1775 {
1776 if (SCM_LIKELY (SCM_I_INUMP (y)))
1777 {
1778 scm_t_inum yy = SCM_I_INUM (y);
1779 if (SCM_UNLIKELY (yy == 0))
1780 scm_num_overflow (s_scm_ceiling_quotient);
1781 else if (SCM_UNLIKELY (yy == 1))
1782 return x;
1783 else
1784 {
1785 SCM q = scm_i_mkbig ();
1786 if (yy > 0)
1787 mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy);
1788 else
1789 {
1790 mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy);
1791 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
1792 }
1793 scm_remember_upto_here_1 (x);
1794 return scm_i_normbig (q);
1795 }
1796 }
1797 else if (SCM_BIGP (y))
1798 {
1799 SCM q = scm_i_mkbig ();
1800 mpz_cdiv_q (SCM_I_BIG_MPZ (q),
1801 SCM_I_BIG_MPZ (x),
1802 SCM_I_BIG_MPZ (y));
1803 scm_remember_upto_here_2 (x, y);
1804 return scm_i_normbig (q);
1805 }
1806 else if (SCM_REALP (y))
1807 return scm_i_inexact_ceiling_quotient
1808 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
1809 else if (SCM_FRACTIONP (y))
1810 return scm_i_exact_rational_ceiling_quotient (x, y);
1811 else
1812 SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
1813 s_scm_ceiling_quotient);
1814 }
1815 else if (SCM_REALP (x))
1816 {
1817 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
1818 SCM_BIGP (y) || SCM_FRACTIONP (y))
1819 return scm_i_inexact_ceiling_quotient
1820 (SCM_REAL_VALUE (x), scm_to_double (y));
1821 else
1822 SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
1823 s_scm_ceiling_quotient);
1824 }
1825 else if (SCM_FRACTIONP (x))
1826 {
1827 if (SCM_REALP (y))
1828 return scm_i_inexact_ceiling_quotient
1829 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
1830 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
1831 return scm_i_exact_rational_ceiling_quotient (x, y);
1832 else
1833 SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
1834 s_scm_ceiling_quotient);
1835 }
1836 else
1837 SCM_WTA_DISPATCH_2 (g_scm_ceiling_quotient, x, y, SCM_ARG1,
1838 s_scm_ceiling_quotient);
1839}
1840#undef FUNC_NAME
1841
1842static SCM
1843scm_i_inexact_ceiling_quotient (double x, double y)
1844{
1845 if (SCM_UNLIKELY (y == 0))
1846 scm_num_overflow (s_scm_ceiling_quotient); /* or return a NaN? */
1847 else
1848 return scm_from_double (ceil (x / y));
1849}
1850
1851static SCM
1852scm_i_exact_rational_ceiling_quotient (SCM x, SCM y)
1853{
1854 return scm_ceiling_quotient
1855 (scm_product (scm_numerator (x), scm_denominator (y)),
1856 scm_product (scm_numerator (y), scm_denominator (x)));
1857}
1858
1859static SCM scm_i_inexact_ceiling_remainder (double x, double y);
1860static SCM scm_i_exact_rational_ceiling_remainder (SCM x, SCM y);
1861
1862SCM_PRIMITIVE_GENERIC (scm_ceiling_remainder, "ceiling-remainder", 2, 0, 0,
1863 (SCM x, SCM y),
1864 "Return the real number @var{r} such that\n"
1865 "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
1866 "where @math{@var{q} = ceiling(@var{x} / @var{y})}.\n"
1867 "@lisp\n"
1868 "(ceiling-remainder 123 10) @result{} -7\n"
1869 "(ceiling-remainder 123 -10) @result{} 3\n"
1870 "(ceiling-remainder -123 10) @result{} -3\n"
1871 "(ceiling-remainder -123 -10) @result{} 7\n"
1872 "(ceiling-remainder -123.2 -63.5) @result{} 3.8\n"
1873 "(ceiling-remainder 16/3 -10/7) @result{} 22/21\n"
1874 "@end lisp")
1875#define FUNC_NAME s_scm_ceiling_remainder
1876{
1877 if (SCM_LIKELY (SCM_I_INUMP (x)))
1878 {
1879 scm_t_inum xx = SCM_I_INUM (x);
1880 if (SCM_LIKELY (SCM_I_INUMP (y)))
1881 {
1882 scm_t_inum yy = SCM_I_INUM (y);
1883 if (SCM_UNLIKELY (yy == 0))
1884 scm_num_overflow (s_scm_ceiling_remainder);
1885 else
1886 {
1887 scm_t_inum rr = xx % yy;
1888 int needs_adjustment;
1889
1890 if (SCM_LIKELY (yy > 0))
1891 needs_adjustment = (rr > 0);
1892 else
1893 needs_adjustment = (rr < 0);
1894
1895 if (needs_adjustment)
1896 rr -= yy;
1897 return SCM_I_MAKINUM (rr);
1898 }
1899 }
1900 else if (SCM_BIGP (y))
1901 {
1902 int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
1903 scm_remember_upto_here_1 (y);
1904 if (SCM_LIKELY (sign > 0))
1905 {
1906 if (SCM_LIKELY (xx > 0))
1907 {
1908 SCM r = scm_i_mkbig ();
1909 mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
1910 scm_remember_upto_here_1 (y);
1911 mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
1912 return scm_i_normbig (r);
1913 }
1914 else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
1915 && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
1916 - SCM_MOST_NEGATIVE_FIXNUM) == 0))
1917 {
1918 /* Special case: x == fixnum-min && y == abs (fixnum-min) */
1919 scm_remember_upto_here_1 (y);
1920 return SCM_INUM0;
1921 }
1922 else
1923 return x;
1924 }
1925 else if (xx >= 0)
1926 return x;
1927 else
1928 {
1929 SCM r = scm_i_mkbig ();
1930 mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
1931 scm_remember_upto_here_1 (y);
1932 mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
1933 return scm_i_normbig (r);
1934 }
1935 }
1936 else if (SCM_REALP (y))
1937 return scm_i_inexact_ceiling_remainder (xx, SCM_REAL_VALUE (y));
1938 else if (SCM_FRACTIONP (y))
1939 return scm_i_exact_rational_ceiling_remainder (x, y);
1940 else
1941 SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
1942 s_scm_ceiling_remainder);
1943 }
1944 else if (SCM_BIGP (x))
1945 {
1946 if (SCM_LIKELY (SCM_I_INUMP (y)))
1947 {
1948 scm_t_inum yy = SCM_I_INUM (y);
1949 if (SCM_UNLIKELY (yy == 0))
1950 scm_num_overflow (s_scm_ceiling_remainder);
1951 else
1952 {
1953 scm_t_inum rr;
1954 if (yy > 0)
1955 rr = -mpz_cdiv_ui (SCM_I_BIG_MPZ (x), yy);
1956 else
1957 rr = mpz_fdiv_ui (SCM_I_BIG_MPZ (x), -yy);
1958 scm_remember_upto_here_1 (x);
1959 return SCM_I_MAKINUM (rr);
1960 }
1961 }
1962 else if (SCM_BIGP (y))
1963 {
1964 SCM r = scm_i_mkbig ();
1965 mpz_cdiv_r (SCM_I_BIG_MPZ (r),
1966 SCM_I_BIG_MPZ (x),
1967 SCM_I_BIG_MPZ (y));
1968 scm_remember_upto_here_2 (x, y);
1969 return scm_i_normbig (r);
1970 }
1971 else if (SCM_REALP (y))
1972 return scm_i_inexact_ceiling_remainder
1973 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
1974 else if (SCM_FRACTIONP (y))
1975 return scm_i_exact_rational_ceiling_remainder (x, y);
1976 else
1977 SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
1978 s_scm_ceiling_remainder);
1979 }
1980 else if (SCM_REALP (x))
1981 {
1982 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
1983 SCM_BIGP (y) || SCM_FRACTIONP (y))
1984 return scm_i_inexact_ceiling_remainder
1985 (SCM_REAL_VALUE (x), scm_to_double (y));
1986 else
1987 SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
1988 s_scm_ceiling_remainder);
1989 }
1990 else if (SCM_FRACTIONP (x))
1991 {
1992 if (SCM_REALP (y))
1993 return scm_i_inexact_ceiling_remainder
1994 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
1995 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
1996 return scm_i_exact_rational_ceiling_remainder (x, y);
1997 else
1998 SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
1999 s_scm_ceiling_remainder);
2000 }
2001 else
2002 SCM_WTA_DISPATCH_2 (g_scm_ceiling_remainder, x, y, SCM_ARG1,
2003 s_scm_ceiling_remainder);
2004}
2005#undef FUNC_NAME
2006
2007static SCM
2008scm_i_inexact_ceiling_remainder (double x, double y)
2009{
2010 /* Although it would be more efficient to use fmod here, we can't
2011 because it would in some cases produce results inconsistent with
2012 scm_i_inexact_ceiling_quotient, such that x != q * y + r (not even
2013 close). In particular, when x is very close to a multiple of y,
2014 then r might be either 0.0 or -y, but those two cases must
2015 correspond to different choices of q. If r = 0.0 then q must be
2016 x/y, and if r = -y then q must be x/y+1. If quotient chooses one
2017 and remainder chooses the other, it would be bad. */
2018 if (SCM_UNLIKELY (y == 0))
2019 scm_num_overflow (s_scm_ceiling_remainder); /* or return a NaN? */
2020 else
2021 return scm_from_double (x - y * ceil (x / y));
2022}
2023
2024static SCM
2025scm_i_exact_rational_ceiling_remainder (SCM x, SCM y)
2026{
2027 SCM xd = scm_denominator (x);
2028 SCM yd = scm_denominator (y);
2029 SCM r1 = scm_ceiling_remainder (scm_product (scm_numerator (x), yd),
2030 scm_product (scm_numerator (y), xd));
2031 return scm_divide (r1, scm_product (xd, yd));
2032}
2033
2034static void scm_i_inexact_ceiling_divide (double x, double y,
2035 SCM *qp, SCM *rp);
2036static void scm_i_exact_rational_ceiling_divide (SCM x, SCM y,
2037 SCM *qp, SCM *rp);
2038
2039SCM_PRIMITIVE_GENERIC (scm_i_ceiling_divide, "ceiling/", 2, 0, 0,
2040 (SCM x, SCM y),
2041 "Return the integer @var{q} and the real number @var{r}\n"
2042 "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
2043 "and @math{@var{q} = ceiling(@var{x} / @var{y})}.\n"
2044 "@lisp\n"
2045 "(ceiling/ 123 10) @result{} 13 and -7\n"
2046 "(ceiling/ 123 -10) @result{} -12 and 3\n"
2047 "(ceiling/ -123 10) @result{} -12 and -3\n"
2048 "(ceiling/ -123 -10) @result{} 13 and 7\n"
2049 "(ceiling/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
2050 "(ceiling/ 16/3 -10/7) @result{} -3 and 22/21\n"
2051 "@end lisp")
2052#define FUNC_NAME s_scm_i_ceiling_divide
2053{
2054 SCM q, r;
2055
2056 scm_ceiling_divide(x, y, &q, &r);
2057 return scm_values (scm_list_2 (q, r));
2058}
2059#undef FUNC_NAME
2060
2061#define s_scm_ceiling_divide s_scm_i_ceiling_divide
2062#define g_scm_ceiling_divide g_scm_i_ceiling_divide
2063
2064void
2065scm_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp)
2066{
2067 if (SCM_LIKELY (SCM_I_INUMP (x)))
2068 {
2069 scm_t_inum xx = SCM_I_INUM (x);
2070 if (SCM_LIKELY (SCM_I_INUMP (y)))
2071 {
2072 scm_t_inum yy = SCM_I_INUM (y);
2073 if (SCM_UNLIKELY (yy == 0))
2074 scm_num_overflow (s_scm_ceiling_divide);
2075 else
2076 {
2077 scm_t_inum qq = xx / yy;
2078 scm_t_inum rr = xx % yy;
2079 int needs_adjustment;
2080
2081 if (SCM_LIKELY (yy > 0))
2082 needs_adjustment = (rr > 0);
2083 else
2084 needs_adjustment = (rr < 0);
2085
2086 if (needs_adjustment)
2087 {
2088 rr -= yy;
2089 qq++;
2090 }
2091 if (SCM_LIKELY (SCM_FIXABLE (qq)))
2092 *qp = SCM_I_MAKINUM (qq);
2093 else
2094 *qp = scm_i_inum2big (qq);
2095 *rp = SCM_I_MAKINUM (rr);
2096 }
2097 return;
2098 }
2099 else if (SCM_BIGP (y))
2100 {
2101 int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
2102 scm_remember_upto_here_1 (y);
2103 if (SCM_LIKELY (sign > 0))
2104 {
2105 if (SCM_LIKELY (xx > 0))
2106 {
2107 SCM r = scm_i_mkbig ();
2108 mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
2109 scm_remember_upto_here_1 (y);
2110 mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
2111 *qp = SCM_INUM1;
2112 *rp = scm_i_normbig (r);
2113 }
2114 else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
2115 && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
2116 - SCM_MOST_NEGATIVE_FIXNUM) == 0))
2117 {
2118 /* Special case: x == fixnum-min && y == abs (fixnum-min) */
2119 scm_remember_upto_here_1 (y);
2120 *qp = SCM_I_MAKINUM (-1);
2121 *rp = SCM_INUM0;
2122 }
2123 else
2124 {
2125 *qp = SCM_INUM0;
2126 *rp = x;
2127 }
2128 }
2129 else if (xx >= 0)
2130 {
2131 *qp = SCM_INUM0;
2132 *rp = x;
2133 }
2134 else
2135 {
2136 SCM r = scm_i_mkbig ();
2137 mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
2138 scm_remember_upto_here_1 (y);
2139 mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
2140 *qp = SCM_INUM1;
2141 *rp = scm_i_normbig (r);
2142 }
2143 return;
2144 }
2145 else if (SCM_REALP (y))
2146 return scm_i_inexact_ceiling_divide (xx, SCM_REAL_VALUE (y), qp, rp);
2147 else if (SCM_FRACTIONP (y))
2148 return scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
2149 else
2150 return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
2151 s_scm_ceiling_divide, qp, rp);
2152 }
2153 else if (SCM_BIGP (x))
2154 {
2155 if (SCM_LIKELY (SCM_I_INUMP (y)))
2156 {
2157 scm_t_inum yy = SCM_I_INUM (y);
2158 if (SCM_UNLIKELY (yy == 0))
2159 scm_num_overflow (s_scm_ceiling_divide);
2160 else
2161 {
2162 SCM q = scm_i_mkbig ();
2163 SCM r = scm_i_mkbig ();
2164 if (yy > 0)
2165 mpz_cdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
2166 SCM_I_BIG_MPZ (x), yy);
2167 else
2168 {
2169 mpz_fdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
2170 SCM_I_BIG_MPZ (x), -yy);
2171 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
2172 }
2173 scm_remember_upto_here_1 (x);
2174 *qp = scm_i_normbig (q);
2175 *rp = scm_i_normbig (r);
2176 }
2177 return;
2178 }
2179 else if (SCM_BIGP (y))
2180 {
2181 SCM q = scm_i_mkbig ();
2182 SCM r = scm_i_mkbig ();
2183 mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
2184 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
2185 scm_remember_upto_here_2 (x, y);
2186 *qp = scm_i_normbig (q);
2187 *rp = scm_i_normbig (r);
2188 return;
2189 }
2190 else if (SCM_REALP (y))
2191 return scm_i_inexact_ceiling_divide
2192 (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp);
2193 else if (SCM_FRACTIONP (y))
2194 return scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
2195 else
2196 return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
2197 s_scm_ceiling_divide, qp, rp);
2198 }
2199 else if (SCM_REALP (x))
2200 {
2201 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
2202 SCM_BIGP (y) || SCM_FRACTIONP (y))
2203 return scm_i_inexact_ceiling_divide
2204 (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp);
2205 else
2206 return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
2207 s_scm_ceiling_divide, qp, rp);
2208 }
2209 else if (SCM_FRACTIONP (x))
2210 {
2211 if (SCM_REALP (y))
2212 return scm_i_inexact_ceiling_divide
2213 (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
2214 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
2215 return scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
2216 else
2217 return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
2218 s_scm_ceiling_divide, qp, rp);
2219 }
2220 else
2221 return two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG1,
2222 s_scm_ceiling_divide, qp, rp);
2223}
2224
2225static void
2226scm_i_inexact_ceiling_divide (double x, double y, SCM *qp, SCM *rp)
2227{
2228 if (SCM_UNLIKELY (y == 0))
2229 scm_num_overflow (s_scm_ceiling_divide); /* or return a NaN? */
2230 else
2231 {
2232 double q = ceil (x / y);
2233 double r = x - q * y;
2234 *qp = scm_from_double (q);
2235 *rp = scm_from_double (r);
2236 }
2237}
2238
2239static void
2240scm_i_exact_rational_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp)
2241{
2242 SCM r1;
2243 SCM xd = scm_denominator (x);
2244 SCM yd = scm_denominator (y);
2245
2246 scm_ceiling_divide (scm_product (scm_numerator (x), yd),
2247 scm_product (scm_numerator (y), xd),
2248 qp, &r1);
2249 *rp = scm_divide (r1, scm_product (xd, yd));
2250}
2251
2252static SCM scm_i_inexact_truncate_quotient (double x, double y);
2253static SCM scm_i_exact_rational_truncate_quotient (SCM x, SCM y);
2254
2255SCM_PRIMITIVE_GENERIC (scm_truncate_quotient, "truncate-quotient", 2, 0, 0,
2256 (SCM x, SCM y),
2257 "Return @math{@var{x} / @var{y}} rounded toward zero.\n"
2258 "@lisp\n"
2259 "(truncate-quotient 123 10) @result{} 12\n"
2260 "(truncate-quotient 123 -10) @result{} -12\n"
2261 "(truncate-quotient -123 10) @result{} -12\n"
2262 "(truncate-quotient -123 -10) @result{} 12\n"
2263 "(truncate-quotient -123.2 -63.5) @result{} 1.0\n"
2264 "(truncate-quotient 16/3 -10/7) @result{} -3\n"
2265 "@end lisp")
2266#define FUNC_NAME s_scm_truncate_quotient
2267{
2268 if (SCM_LIKELY (SCM_I_INUMP (x)))
2269 {
2270 scm_t_inum xx = SCM_I_INUM (x);
2271 if (SCM_LIKELY (SCM_I_INUMP (y)))
2272 {
2273 scm_t_inum yy = SCM_I_INUM (y);
2274 if (SCM_UNLIKELY (yy == 0))
2275 scm_num_overflow (s_scm_truncate_quotient);
2276 else
2277 {
2278 scm_t_inum qq = xx / yy;
2279 if (SCM_LIKELY (SCM_FIXABLE (qq)))
2280 return SCM_I_MAKINUM (qq);
2281 else
2282 return scm_i_inum2big (qq);
2283 }
2284 }
2285 else if (SCM_BIGP (y))
2286 {
2287 if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
2288 && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
2289 - SCM_MOST_NEGATIVE_FIXNUM) == 0))
2290 {
2291 /* Special case: x == fixnum-min && y == abs (fixnum-min) */
2292 scm_remember_upto_here_1 (y);
2293 return SCM_I_MAKINUM (-1);
2294 }
2295 else
2296 return SCM_INUM0;
2297 }
2298 else if (SCM_REALP (y))
2299 return scm_i_inexact_truncate_quotient (xx, SCM_REAL_VALUE (y));
2300 else if (SCM_FRACTIONP (y))
2301 return scm_i_exact_rational_truncate_quotient (x, y);
2302 else
2303 SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
2304 s_scm_truncate_quotient);
2305 }
2306 else if (SCM_BIGP (x))
2307 {
2308 if (SCM_LIKELY (SCM_I_INUMP (y)))
2309 {
2310 scm_t_inum yy = SCM_I_INUM (y);
2311 if (SCM_UNLIKELY (yy == 0))
2312 scm_num_overflow (s_scm_truncate_quotient);
2313 else if (SCM_UNLIKELY (yy == 1))
2314 return x;
2315 else
2316 {
2317 SCM q = scm_i_mkbig ();
2318 if (yy > 0)
2319 mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy);
2320 else
2321 {
2322 mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy);
2323 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
2324 }
2325 scm_remember_upto_here_1 (x);
2326 return scm_i_normbig (q);
2327 }
2328 }
2329 else if (SCM_BIGP (y))
2330 {
2331 SCM q = scm_i_mkbig ();
2332 mpz_tdiv_q (SCM_I_BIG_MPZ (q),
2333 SCM_I_BIG_MPZ (x),
2334 SCM_I_BIG_MPZ (y));
2335 scm_remember_upto_here_2 (x, y);
2336 return scm_i_normbig (q);
2337 }
2338 else if (SCM_REALP (y))
2339 return scm_i_inexact_truncate_quotient
2340 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
2341 else if (SCM_FRACTIONP (y))
2342 return scm_i_exact_rational_truncate_quotient (x, y);
2343 else
2344 SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
2345 s_scm_truncate_quotient);
2346 }
2347 else if (SCM_REALP (x))
2348 {
2349 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
2350 SCM_BIGP (y) || SCM_FRACTIONP (y))
2351 return scm_i_inexact_truncate_quotient
2352 (SCM_REAL_VALUE (x), scm_to_double (y));
2353 else
2354 SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
2355 s_scm_truncate_quotient);
2356 }
2357 else if (SCM_FRACTIONP (x))
2358 {
2359 if (SCM_REALP (y))
2360 return scm_i_inexact_truncate_quotient
2361 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
2362 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
2363 return scm_i_exact_rational_truncate_quotient (x, y);
2364 else
2365 SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
2366 s_scm_truncate_quotient);
2367 }
2368 else
2369 SCM_WTA_DISPATCH_2 (g_scm_truncate_quotient, x, y, SCM_ARG1,
2370 s_scm_truncate_quotient);
2371}
2372#undef FUNC_NAME
2373
2374static SCM
2375scm_i_inexact_truncate_quotient (double x, double y)
2376{
2377 if (SCM_UNLIKELY (y == 0))
2378 scm_num_overflow (s_scm_truncate_quotient); /* or return a NaN? */
2379 else
c251ab63 2380 return scm_from_double (trunc (x / y));
8f9da340
MW
2381}
2382
2383static SCM
2384scm_i_exact_rational_truncate_quotient (SCM x, SCM y)
2385{
2386 return scm_truncate_quotient
2387 (scm_product (scm_numerator (x), scm_denominator (y)),
2388 scm_product (scm_numerator (y), scm_denominator (x)));
2389}
2390
2391static SCM scm_i_inexact_truncate_remainder (double x, double y);
2392static SCM scm_i_exact_rational_truncate_remainder (SCM x, SCM y);
2393
2394SCM_PRIMITIVE_GENERIC (scm_truncate_remainder, "truncate-remainder", 2, 0, 0,
2395 (SCM x, SCM y),
2396 "Return the real number @var{r} such that\n"
2397 "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
2398 "where @math{@var{q} = truncate(@var{x} / @var{y})}.\n"
2399 "@lisp\n"
2400 "(truncate-remainder 123 10) @result{} 3\n"
2401 "(truncate-remainder 123 -10) @result{} 3\n"
2402 "(truncate-remainder -123 10) @result{} -3\n"
2403 "(truncate-remainder -123 -10) @result{} -3\n"
2404 "(truncate-remainder -123.2 -63.5) @result{} -59.7\n"
2405 "(truncate-remainder 16/3 -10/7) @result{} 22/21\n"
2406 "@end lisp")
2407#define FUNC_NAME s_scm_truncate_remainder
2408{
2409 if (SCM_LIKELY (SCM_I_INUMP (x)))
2410 {
2411 scm_t_inum xx = SCM_I_INUM (x);
2412 if (SCM_LIKELY (SCM_I_INUMP (y)))
2413 {
2414 scm_t_inum yy = SCM_I_INUM (y);
2415 if (SCM_UNLIKELY (yy == 0))
2416 scm_num_overflow (s_scm_truncate_remainder);
2417 else
2418 return SCM_I_MAKINUM (xx % yy);
2419 }
2420 else if (SCM_BIGP (y))
2421 {
2422 if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
2423 && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
2424 - SCM_MOST_NEGATIVE_FIXNUM) == 0))
2425 {
2426 /* Special case: x == fixnum-min && y == abs (fixnum-min) */
2427 scm_remember_upto_here_1 (y);
2428 return SCM_INUM0;
2429 }
2430 else
2431 return x;
2432 }
2433 else if (SCM_REALP (y))
2434 return scm_i_inexact_truncate_remainder (xx, SCM_REAL_VALUE (y));
2435 else if (SCM_FRACTIONP (y))
2436 return scm_i_exact_rational_truncate_remainder (x, y);
2437 else
2438 SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
2439 s_scm_truncate_remainder);
2440 }
2441 else if (SCM_BIGP (x))
2442 {
2443 if (SCM_LIKELY (SCM_I_INUMP (y)))
2444 {
2445 scm_t_inum yy = SCM_I_INUM (y);
2446 if (SCM_UNLIKELY (yy == 0))
2447 scm_num_overflow (s_scm_truncate_remainder);
2448 else
2449 {
2450 scm_t_inum rr = (mpz_tdiv_ui (SCM_I_BIG_MPZ (x),
2451 (yy > 0) ? yy : -yy)
2452 * mpz_sgn (SCM_I_BIG_MPZ (x)));
2453 scm_remember_upto_here_1 (x);
2454 return SCM_I_MAKINUM (rr);
2455 }
2456 }
2457 else if (SCM_BIGP (y))
2458 {
2459 SCM r = scm_i_mkbig ();
2460 mpz_tdiv_r (SCM_I_BIG_MPZ (r),
2461 SCM_I_BIG_MPZ (x),
2462 SCM_I_BIG_MPZ (y));
2463 scm_remember_upto_here_2 (x, y);
2464 return scm_i_normbig (r);
2465 }
2466 else if (SCM_REALP (y))
2467 return scm_i_inexact_truncate_remainder
2468 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
2469 else if (SCM_FRACTIONP (y))
2470 return scm_i_exact_rational_truncate_remainder (x, y);
2471 else
2472 SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
2473 s_scm_truncate_remainder);
2474 }
2475 else if (SCM_REALP (x))
2476 {
2477 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
2478 SCM_BIGP (y) || SCM_FRACTIONP (y))
2479 return scm_i_inexact_truncate_remainder
2480 (SCM_REAL_VALUE (x), scm_to_double (y));
2481 else
2482 SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
2483 s_scm_truncate_remainder);
2484 }
2485 else if (SCM_FRACTIONP (x))
2486 {
2487 if (SCM_REALP (y))
2488 return scm_i_inexact_truncate_remainder
2489 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
2490 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
2491 return scm_i_exact_rational_truncate_remainder (x, y);
2492 else
2493 SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
2494 s_scm_truncate_remainder);
2495 }
2496 else
2497 SCM_WTA_DISPATCH_2 (g_scm_truncate_remainder, x, y, SCM_ARG1,
2498 s_scm_truncate_remainder);
2499}
2500#undef FUNC_NAME
2501
2502static SCM
2503scm_i_inexact_truncate_remainder (double x, double y)
2504{
2505 /* Although it would be more efficient to use fmod here, we can't
2506 because it would in some cases produce results inconsistent with
2507 scm_i_inexact_truncate_quotient, such that x != q * y + r (not even
2508 close). In particular, when x is very close to a multiple of y,
2509 then r might be either 0.0 or sgn(x)*|y|, but those two cases must
2510 correspond to different choices of q. If quotient chooses one and
2511 remainder chooses the other, it would be bad. */
2512 if (SCM_UNLIKELY (y == 0))
2513 scm_num_overflow (s_scm_truncate_remainder); /* or return a NaN? */
2514 else
c251ab63 2515 return scm_from_double (x - y * trunc (x / y));
8f9da340
MW
2516}
2517
2518static SCM
2519scm_i_exact_rational_truncate_remainder (SCM x, SCM y)
2520{
2521 SCM xd = scm_denominator (x);
2522 SCM yd = scm_denominator (y);
2523 SCM r1 = scm_truncate_remainder (scm_product (scm_numerator (x), yd),
2524 scm_product (scm_numerator (y), xd));
2525 return scm_divide (r1, scm_product (xd, yd));
2526}
2527
2528
2529static void scm_i_inexact_truncate_divide (double x, double y,
2530 SCM *qp, SCM *rp);
2531static void scm_i_exact_rational_truncate_divide (SCM x, SCM y,
2532 SCM *qp, SCM *rp);
2533
2534SCM_PRIMITIVE_GENERIC (scm_i_truncate_divide, "truncate/", 2, 0, 0,
2535 (SCM x, SCM y),
2536 "Return the integer @var{q} and the real number @var{r}\n"
2537 "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
2538 "and @math{@var{q} = truncate(@var{x} / @var{y})}.\n"
2539 "@lisp\n"
2540 "(truncate/ 123 10) @result{} 12 and 3\n"
2541 "(truncate/ 123 -10) @result{} -12 and 3\n"
2542 "(truncate/ -123 10) @result{} -12 and -3\n"
2543 "(truncate/ -123 -10) @result{} 12 and -3\n"
2544 "(truncate/ -123.2 -63.5) @result{} 1.0 and -59.7\n"
2545 "(truncate/ 16/3 -10/7) @result{} -3 and 22/21\n"
2546 "@end lisp")
2547#define FUNC_NAME s_scm_i_truncate_divide
2548{
2549 SCM q, r;
2550
2551 scm_truncate_divide(x, y, &q, &r);
2552 return scm_values (scm_list_2 (q, r));
2553}
2554#undef FUNC_NAME
2555
2556#define s_scm_truncate_divide s_scm_i_truncate_divide
2557#define g_scm_truncate_divide g_scm_i_truncate_divide
2558
2559void
2560scm_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp)
2561{
2562 if (SCM_LIKELY (SCM_I_INUMP (x)))
2563 {
2564 scm_t_inum xx = SCM_I_INUM (x);
2565 if (SCM_LIKELY (SCM_I_INUMP (y)))
2566 {
2567 scm_t_inum yy = SCM_I_INUM (y);
2568 if (SCM_UNLIKELY (yy == 0))
2569 scm_num_overflow (s_scm_truncate_divide);
2570 else
2571 {
2572 scm_t_inum qq = xx / yy;
2573 scm_t_inum rr = xx % yy;
2574 if (SCM_LIKELY (SCM_FIXABLE (qq)))
2575 *qp = SCM_I_MAKINUM (qq);
2576 else
2577 *qp = scm_i_inum2big (qq);
2578 *rp = SCM_I_MAKINUM (rr);
2579 }
2580 return;
2581 }
2582 else if (SCM_BIGP (y))
2583 {
2584 if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
2585 && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
2586 - SCM_MOST_NEGATIVE_FIXNUM) == 0))
2587 {
2588 /* Special case: x == fixnum-min && y == abs (fixnum-min) */
2589 scm_remember_upto_here_1 (y);
2590 *qp = SCM_I_MAKINUM (-1);
2591 *rp = SCM_INUM0;
2592 }
2593 else
2594 {
2595 *qp = SCM_INUM0;
2596 *rp = x;
2597 }
2598 return;
2599 }
2600 else if (SCM_REALP (y))
2601 return scm_i_inexact_truncate_divide (xx, SCM_REAL_VALUE (y), qp, rp);
2602 else if (SCM_FRACTIONP (y))
2603 return scm_i_exact_rational_truncate_divide (x, y, qp, rp);
2604 else
2605 return two_valued_wta_dispatch_2
2606 (g_scm_truncate_divide, x, y, SCM_ARG2,
2607 s_scm_truncate_divide, qp, rp);
2608 }
2609 else if (SCM_BIGP (x))
2610 {
2611 if (SCM_LIKELY (SCM_I_INUMP (y)))
2612 {
2613 scm_t_inum yy = SCM_I_INUM (y);
2614 if (SCM_UNLIKELY (yy == 0))
2615 scm_num_overflow (s_scm_truncate_divide);
2616 else
2617 {
2618 SCM q = scm_i_mkbig ();
2619 scm_t_inum rr;
2620 if (yy > 0)
2621 rr = mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q),
2622 SCM_I_BIG_MPZ (x), yy);
2623 else
2624 {
2625 rr = mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q),
2626 SCM_I_BIG_MPZ (x), -yy);
2627 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
2628 }
2629 rr *= mpz_sgn (SCM_I_BIG_MPZ (x));
2630 scm_remember_upto_here_1 (x);
2631 *qp = scm_i_normbig (q);
2632 *rp = SCM_I_MAKINUM (rr);
2633 }
2634 return;
2635 }
2636 else if (SCM_BIGP (y))
2637 {
2638 SCM q = scm_i_mkbig ();
2639 SCM r = scm_i_mkbig ();
2640 mpz_tdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
2641 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
2642 scm_remember_upto_here_2 (x, y);
2643 *qp = scm_i_normbig (q);
2644 *rp = scm_i_normbig (r);
2645 }
2646 else if (SCM_REALP (y))
2647 return scm_i_inexact_truncate_divide
2648 (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp);
2649 else if (SCM_FRACTIONP (y))
2650 return scm_i_exact_rational_truncate_divide (x, y, qp, rp);
2651 else
2652 return two_valued_wta_dispatch_2
2653 (g_scm_truncate_divide, x, y, SCM_ARG2,
2654 s_scm_truncate_divide, qp, rp);
2655 }
2656 else if (SCM_REALP (x))
2657 {
2658 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
2659 SCM_BIGP (y) || SCM_FRACTIONP (y))
2660 return scm_i_inexact_truncate_divide
2661 (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp);
2662 else
2663 return two_valued_wta_dispatch_2
2664 (g_scm_truncate_divide, x, y, SCM_ARG2,
2665 s_scm_truncate_divide, qp, rp);
2666 }
2667 else if (SCM_FRACTIONP (x))
2668 {
2669 if (SCM_REALP (y))
2670 return scm_i_inexact_truncate_divide
2671 (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
2672 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
2673 return scm_i_exact_rational_truncate_divide (x, y, qp, rp);
2674 else
2675 return two_valued_wta_dispatch_2
2676 (g_scm_truncate_divide, x, y, SCM_ARG2,
2677 s_scm_truncate_divide, qp, rp);
2678 }
2679 else
2680 return two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG1,
2681 s_scm_truncate_divide, qp, rp);
2682}
2683
2684static void
2685scm_i_inexact_truncate_divide (double x, double y, SCM *qp, SCM *rp)
2686{
2687 if (SCM_UNLIKELY (y == 0))
2688 scm_num_overflow (s_scm_truncate_divide); /* or return a NaN? */
2689 else
2690 {
c15fe499
MW
2691 double q = trunc (x / y);
2692 double r = x - q * y;
8f9da340
MW
2693 *qp = scm_from_double (q);
2694 *rp = scm_from_double (r);
2695 }
2696}
2697
2698static void
2699scm_i_exact_rational_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp)
2700{
2701 SCM r1;
2702 SCM xd = scm_denominator (x);
2703 SCM yd = scm_denominator (y);
2704
2705 scm_truncate_divide (scm_product (scm_numerator (x), yd),
2706 scm_product (scm_numerator (y), xd),
2707 qp, &r1);
2708 *rp = scm_divide (r1, scm_product (xd, yd));
2709}
2710
ff62c168
MW
2711static SCM scm_i_inexact_centered_quotient (double x, double y);
2712static SCM scm_i_bigint_centered_quotient (SCM x, SCM y);
03ddd15b 2713static SCM scm_i_exact_rational_centered_quotient (SCM x, SCM y);
ff62c168 2714
8f9da340
MW
2715SCM_PRIMITIVE_GENERIC (scm_centered_quotient, "centered-quotient", 2, 0, 0,
2716 (SCM x, SCM y),
2717 "Return the integer @var{q} such that\n"
2718 "@math{@var{x} = @var{q}*@var{y} + @var{r}} where\n"
2719 "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n"
2720 "@lisp\n"
2721 "(centered-quotient 123 10) @result{} 12\n"
2722 "(centered-quotient 123 -10) @result{} -12\n"
2723 "(centered-quotient -123 10) @result{} -12\n"
2724 "(centered-quotient -123 -10) @result{} 12\n"
2725 "(centered-quotient -123.2 -63.5) @result{} 2.0\n"
2726 "(centered-quotient 16/3 -10/7) @result{} -4\n"
2727 "@end lisp")
2728#define FUNC_NAME s_scm_centered_quotient
2729{
2730 if (SCM_LIKELY (SCM_I_INUMP (x)))
2731 {
2732 scm_t_inum xx = SCM_I_INUM (x);
2733 if (SCM_LIKELY (SCM_I_INUMP (y)))
2734 {
2735 scm_t_inum yy = SCM_I_INUM (y);
2736 if (SCM_UNLIKELY (yy == 0))
2737 scm_num_overflow (s_scm_centered_quotient);
2738 else
2739 {
2740 scm_t_inum qq = xx / yy;
2741 scm_t_inum rr = xx % yy;
2742 if (SCM_LIKELY (xx > 0))
2743 {
2744 if (SCM_LIKELY (yy > 0))
2745 {
2746 if (rr >= (yy + 1) / 2)
2747 qq++;
2748 }
2749 else
2750 {
2751 if (rr >= (1 - yy) / 2)
2752 qq--;
2753 }
2754 }
2755 else
2756 {
2757 if (SCM_LIKELY (yy > 0))
2758 {
2759 if (rr < -yy / 2)
2760 qq--;
2761 }
2762 else
2763 {
2764 if (rr < yy / 2)
2765 qq++;
2766 }
2767 }
2768 if (SCM_LIKELY (SCM_FIXABLE (qq)))
2769 return SCM_I_MAKINUM (qq);
2770 else
2771 return scm_i_inum2big (qq);
2772 }
2773 }
2774 else if (SCM_BIGP (y))
2775 {
2776 /* Pass a denormalized bignum version of x (even though it
2777 can fit in a fixnum) to scm_i_bigint_centered_quotient */
2778 return scm_i_bigint_centered_quotient (scm_i_long2big (xx), y);
2779 }
2780 else if (SCM_REALP (y))
2781 return scm_i_inexact_centered_quotient (xx, SCM_REAL_VALUE (y));
2782 else if (SCM_FRACTIONP (y))
2783 return scm_i_exact_rational_centered_quotient (x, y);
2784 else
2785 SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
2786 s_scm_centered_quotient);
2787 }
2788 else if (SCM_BIGP (x))
2789 {
2790 if (SCM_LIKELY (SCM_I_INUMP (y)))
2791 {
2792 scm_t_inum yy = SCM_I_INUM (y);
2793 if (SCM_UNLIKELY (yy == 0))
2794 scm_num_overflow (s_scm_centered_quotient);
2795 else if (SCM_UNLIKELY (yy == 1))
2796 return x;
2797 else
2798 {
2799 SCM q = scm_i_mkbig ();
2800 scm_t_inum rr;
2801 /* Arrange for rr to initially be non-positive,
2802 because that simplifies the test to see
2803 if it is within the needed bounds. */
2804 if (yy > 0)
2805 {
2806 rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
2807 SCM_I_BIG_MPZ (x), yy);
2808 scm_remember_upto_here_1 (x);
2809 if (rr < -yy / 2)
2810 mpz_sub_ui (SCM_I_BIG_MPZ (q),
2811 SCM_I_BIG_MPZ (q), 1);
2812 }
2813 else
2814 {
2815 rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
2816 SCM_I_BIG_MPZ (x), -yy);
2817 scm_remember_upto_here_1 (x);
2818 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
2819 if (rr < yy / 2)
2820 mpz_add_ui (SCM_I_BIG_MPZ (q),
2821 SCM_I_BIG_MPZ (q), 1);
2822 }
2823 return scm_i_normbig (q);
2824 }
2825 }
2826 else if (SCM_BIGP (y))
2827 return scm_i_bigint_centered_quotient (x, y);
2828 else if (SCM_REALP (y))
2829 return scm_i_inexact_centered_quotient
2830 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
2831 else if (SCM_FRACTIONP (y))
2832 return scm_i_exact_rational_centered_quotient (x, y);
2833 else
2834 SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
2835 s_scm_centered_quotient);
2836 }
2837 else if (SCM_REALP (x))
2838 {
2839 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
2840 SCM_BIGP (y) || SCM_FRACTIONP (y))
2841 return scm_i_inexact_centered_quotient
2842 (SCM_REAL_VALUE (x), scm_to_double (y));
2843 else
2844 SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
2845 s_scm_centered_quotient);
2846 }
2847 else if (SCM_FRACTIONP (x))
2848 {
2849 if (SCM_REALP (y))
2850 return scm_i_inexact_centered_quotient
2851 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
2852 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
2853 return scm_i_exact_rational_centered_quotient (x, y);
2854 else
2855 SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
2856 s_scm_centered_quotient);
2857 }
2858 else
2859 SCM_WTA_DISPATCH_2 (g_scm_centered_quotient, x, y, SCM_ARG1,
2860 s_scm_centered_quotient);
2861}
2862#undef FUNC_NAME
2863
2864static SCM
2865scm_i_inexact_centered_quotient (double x, double y)
2866{
2867 if (SCM_LIKELY (y > 0))
2868 return scm_from_double (floor (x/y + 0.5));
2869 else if (SCM_LIKELY (y < 0))
2870 return scm_from_double (ceil (x/y - 0.5));
2871 else if (y == 0)
2872 scm_num_overflow (s_scm_centered_quotient); /* or return a NaN? */
2873 else
2874 return scm_nan ();
2875}
2876
2877/* Assumes that both x and y are bigints, though
2878 x might be able to fit into a fixnum. */
2879static SCM
2880scm_i_bigint_centered_quotient (SCM x, SCM y)
2881{
2882 SCM q, r, min_r;
2883
2884 /* Note that x might be small enough to fit into a
2885 fixnum, so we must not let it escape into the wild */
2886 q = scm_i_mkbig ();
2887 r = scm_i_mkbig ();
2888
2889 /* min_r will eventually become -abs(y)/2 */
2890 min_r = scm_i_mkbig ();
2891 mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r),
2892 SCM_I_BIG_MPZ (y), 1);
2893
2894 /* Arrange for rr to initially be non-positive,
2895 because that simplifies the test to see
2896 if it is within the needed bounds. */
2897 if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0)
2898 {
2899 mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
2900 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
2901 scm_remember_upto_here_2 (x, y);
2902 mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r));
2903 if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
2904 mpz_sub_ui (SCM_I_BIG_MPZ (q),
2905 SCM_I_BIG_MPZ (q), 1);
2906 }
2907 else
2908 {
2909 mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
2910 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
2911 scm_remember_upto_here_2 (x, y);
2912 if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
2913 mpz_add_ui (SCM_I_BIG_MPZ (q),
2914 SCM_I_BIG_MPZ (q), 1);
2915 }
2916 scm_remember_upto_here_2 (r, min_r);
2917 return scm_i_normbig (q);
2918}
2919
2920static SCM
2921scm_i_exact_rational_centered_quotient (SCM x, SCM y)
2922{
2923 return scm_centered_quotient
2924 (scm_product (scm_numerator (x), scm_denominator (y)),
2925 scm_product (scm_numerator (y), scm_denominator (x)));
2926}
2927
2928static SCM scm_i_inexact_centered_remainder (double x, double y);
2929static SCM scm_i_bigint_centered_remainder (SCM x, SCM y);
2930static SCM scm_i_exact_rational_centered_remainder (SCM x, SCM y);
2931
2932SCM_PRIMITIVE_GENERIC (scm_centered_remainder, "centered-remainder", 2, 0, 0,
2933 (SCM x, SCM y),
2934 "Return the real number @var{r} such that\n"
2935 "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}\n"
2936 "and @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
2937 "for some integer @var{q}.\n"
2938 "@lisp\n"
2939 "(centered-remainder 123 10) @result{} 3\n"
2940 "(centered-remainder 123 -10) @result{} 3\n"
2941 "(centered-remainder -123 10) @result{} -3\n"
2942 "(centered-remainder -123 -10) @result{} -3\n"
2943 "(centered-remainder -123.2 -63.5) @result{} 3.8\n"
2944 "(centered-remainder 16/3 -10/7) @result{} -8/21\n"
2945 "@end lisp")
2946#define FUNC_NAME s_scm_centered_remainder
2947{
2948 if (SCM_LIKELY (SCM_I_INUMP (x)))
2949 {
2950 scm_t_inum xx = SCM_I_INUM (x);
2951 if (SCM_LIKELY (SCM_I_INUMP (y)))
2952 {
2953 scm_t_inum yy = SCM_I_INUM (y);
2954 if (SCM_UNLIKELY (yy == 0))
2955 scm_num_overflow (s_scm_centered_remainder);
2956 else
2957 {
2958 scm_t_inum rr = xx % yy;
2959 if (SCM_LIKELY (xx > 0))
2960 {
2961 if (SCM_LIKELY (yy > 0))
2962 {
2963 if (rr >= (yy + 1) / 2)
2964 rr -= yy;
2965 }
2966 else
2967 {
2968 if (rr >= (1 - yy) / 2)
2969 rr += yy;
2970 }
2971 }
2972 else
2973 {
2974 if (SCM_LIKELY (yy > 0))
2975 {
2976 if (rr < -yy / 2)
2977 rr += yy;
2978 }
2979 else
2980 {
2981 if (rr < yy / 2)
2982 rr -= yy;
2983 }
2984 }
2985 return SCM_I_MAKINUM (rr);
2986 }
2987 }
2988 else if (SCM_BIGP (y))
2989 {
2990 /* Pass a denormalized bignum version of x (even though it
2991 can fit in a fixnum) to scm_i_bigint_centered_remainder */
2992 return scm_i_bigint_centered_remainder (scm_i_long2big (xx), y);
2993 }
2994 else if (SCM_REALP (y))
2995 return scm_i_inexact_centered_remainder (xx, SCM_REAL_VALUE (y));
2996 else if (SCM_FRACTIONP (y))
2997 return scm_i_exact_rational_centered_remainder (x, y);
2998 else
2999 SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
3000 s_scm_centered_remainder);
3001 }
3002 else if (SCM_BIGP (x))
3003 {
3004 if (SCM_LIKELY (SCM_I_INUMP (y)))
3005 {
3006 scm_t_inum yy = SCM_I_INUM (y);
3007 if (SCM_UNLIKELY (yy == 0))
3008 scm_num_overflow (s_scm_centered_remainder);
3009 else
3010 {
3011 scm_t_inum rr;
3012 /* Arrange for rr to initially be non-positive,
3013 because that simplifies the test to see
3014 if it is within the needed bounds. */
3015 if (yy > 0)
3016 {
3017 rr = - mpz_cdiv_ui (SCM_I_BIG_MPZ (x), yy);
3018 scm_remember_upto_here_1 (x);
3019 if (rr < -yy / 2)
3020 rr += yy;
3021 }
3022 else
3023 {
3024 rr = - mpz_cdiv_ui (SCM_I_BIG_MPZ (x), -yy);
3025 scm_remember_upto_here_1 (x);
3026 if (rr < yy / 2)
3027 rr -= yy;
3028 }
3029 return SCM_I_MAKINUM (rr);
3030 }
3031 }
3032 else if (SCM_BIGP (y))
3033 return scm_i_bigint_centered_remainder (x, y);
3034 else if (SCM_REALP (y))
3035 return scm_i_inexact_centered_remainder
3036 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
3037 else if (SCM_FRACTIONP (y))
3038 return scm_i_exact_rational_centered_remainder (x, y);
3039 else
3040 SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
3041 s_scm_centered_remainder);
3042 }
3043 else if (SCM_REALP (x))
3044 {
3045 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
3046 SCM_BIGP (y) || SCM_FRACTIONP (y))
3047 return scm_i_inexact_centered_remainder
3048 (SCM_REAL_VALUE (x), scm_to_double (y));
3049 else
3050 SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
3051 s_scm_centered_remainder);
3052 }
3053 else if (SCM_FRACTIONP (x))
3054 {
3055 if (SCM_REALP (y))
3056 return scm_i_inexact_centered_remainder
3057 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
3058 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
3059 return scm_i_exact_rational_centered_remainder (x, y);
3060 else
3061 SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
3062 s_scm_centered_remainder);
3063 }
3064 else
3065 SCM_WTA_DISPATCH_2 (g_scm_centered_remainder, x, y, SCM_ARG1,
3066 s_scm_centered_remainder);
3067}
3068#undef FUNC_NAME
3069
3070static SCM
3071scm_i_inexact_centered_remainder (double x, double y)
3072{
3073 double q;
3074
3075 /* Although it would be more efficient to use fmod here, we can't
3076 because it would in some cases produce results inconsistent with
3077 scm_i_inexact_centered_quotient, such that x != r + q * y (not even
3078 close). In particular, when x-y/2 is very close to a multiple of
3079 y, then r might be either -abs(y/2) or abs(y/2)-epsilon, but those
3080 two cases must correspond to different choices of q. If quotient
3081 chooses one and remainder chooses the other, it would be bad. */
3082 if (SCM_LIKELY (y > 0))
3083 q = floor (x/y + 0.5);
3084 else if (SCM_LIKELY (y < 0))
3085 q = ceil (x/y - 0.5);
3086 else if (y == 0)
3087 scm_num_overflow (s_scm_centered_remainder); /* or return a NaN? */
3088 else
3089 return scm_nan ();
3090 return scm_from_double (x - q * y);
3091}
3092
3093/* Assumes that both x and y are bigints, though
3094 x might be able to fit into a fixnum. */
3095static SCM
3096scm_i_bigint_centered_remainder (SCM x, SCM y)
3097{
3098 SCM r, min_r;
3099
3100 /* Note that x might be small enough to fit into a
3101 fixnum, so we must not let it escape into the wild */
3102 r = scm_i_mkbig ();
3103
3104 /* min_r will eventually become -abs(y)/2 */
3105 min_r = scm_i_mkbig ();
3106 mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r),
3107 SCM_I_BIG_MPZ (y), 1);
3108
3109 /* Arrange for rr to initially be non-positive,
3110 because that simplifies the test to see
3111 if it is within the needed bounds. */
3112 if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0)
3113 {
3114 mpz_cdiv_r (SCM_I_BIG_MPZ (r),
3115 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3116 mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r));
3117 if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
3118 mpz_add (SCM_I_BIG_MPZ (r),
3119 SCM_I_BIG_MPZ (r),
3120 SCM_I_BIG_MPZ (y));
3121 }
3122 else
3123 {
3124 mpz_fdiv_r (SCM_I_BIG_MPZ (r),
3125 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3126 if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
3127 mpz_sub (SCM_I_BIG_MPZ (r),
3128 SCM_I_BIG_MPZ (r),
3129 SCM_I_BIG_MPZ (y));
3130 }
3131 scm_remember_upto_here_2 (x, y);
3132 return scm_i_normbig (r);
3133}
3134
3135static SCM
3136scm_i_exact_rational_centered_remainder (SCM x, SCM y)
3137{
3138 SCM xd = scm_denominator (x);
3139 SCM yd = scm_denominator (y);
3140 SCM r1 = scm_centered_remainder (scm_product (scm_numerator (x), yd),
3141 scm_product (scm_numerator (y), xd));
3142 return scm_divide (r1, scm_product (xd, yd));
3143}
3144
3145
3146static void scm_i_inexact_centered_divide (double x, double y,
3147 SCM *qp, SCM *rp);
3148static void scm_i_bigint_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp);
3149static void scm_i_exact_rational_centered_divide (SCM x, SCM y,
3150 SCM *qp, SCM *rp);
3151
3152SCM_PRIMITIVE_GENERIC (scm_i_centered_divide, "centered/", 2, 0, 0,
3153 (SCM x, SCM y),
3154 "Return the integer @var{q} and the real number @var{r}\n"
3155 "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
3156 "and @math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n"
3157 "@lisp\n"
3158 "(centered/ 123 10) @result{} 12 and 3\n"
3159 "(centered/ 123 -10) @result{} -12 and 3\n"
3160 "(centered/ -123 10) @result{} -12 and -3\n"
3161 "(centered/ -123 -10) @result{} 12 and -3\n"
3162 "(centered/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
3163 "(centered/ 16/3 -10/7) @result{} -4 and -8/21\n"
3164 "@end lisp")
3165#define FUNC_NAME s_scm_i_centered_divide
3166{
3167 SCM q, r;
3168
3169 scm_centered_divide(x, y, &q, &r);
3170 return scm_values (scm_list_2 (q, r));
3171}
3172#undef FUNC_NAME
3173
3174#define s_scm_centered_divide s_scm_i_centered_divide
3175#define g_scm_centered_divide g_scm_i_centered_divide
3176
3177void
3178scm_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
3179{
3180 if (SCM_LIKELY (SCM_I_INUMP (x)))
3181 {
3182 scm_t_inum xx = SCM_I_INUM (x);
3183 if (SCM_LIKELY (SCM_I_INUMP (y)))
3184 {
3185 scm_t_inum yy = SCM_I_INUM (y);
3186 if (SCM_UNLIKELY (yy == 0))
3187 scm_num_overflow (s_scm_centered_divide);
3188 else
3189 {
3190 scm_t_inum qq = xx / yy;
3191 scm_t_inum rr = xx % yy;
3192 if (SCM_LIKELY (xx > 0))
3193 {
3194 if (SCM_LIKELY (yy > 0))
3195 {
3196 if (rr >= (yy + 1) / 2)
3197 { qq++; rr -= yy; }
3198 }
3199 else
3200 {
3201 if (rr >= (1 - yy) / 2)
3202 { qq--; rr += yy; }
3203 }
3204 }
3205 else
3206 {
3207 if (SCM_LIKELY (yy > 0))
3208 {
3209 if (rr < -yy / 2)
3210 { qq--; rr += yy; }
3211 }
3212 else
3213 {
3214 if (rr < yy / 2)
3215 { qq++; rr -= yy; }
3216 }
3217 }
3218 if (SCM_LIKELY (SCM_FIXABLE (qq)))
3219 *qp = SCM_I_MAKINUM (qq);
3220 else
3221 *qp = scm_i_inum2big (qq);
3222 *rp = SCM_I_MAKINUM (rr);
3223 }
3224 return;
3225 }
3226 else if (SCM_BIGP (y))
3227 {
3228 /* Pass a denormalized bignum version of x (even though it
3229 can fit in a fixnum) to scm_i_bigint_centered_divide */
3230 return scm_i_bigint_centered_divide (scm_i_long2big (xx), y, qp, rp);
3231 }
3232 else if (SCM_REALP (y))
3233 return scm_i_inexact_centered_divide (xx, SCM_REAL_VALUE (y), qp, rp);
3234 else if (SCM_FRACTIONP (y))
3235 return scm_i_exact_rational_centered_divide (x, y, qp, rp);
3236 else
3237 return two_valued_wta_dispatch_2
3238 (g_scm_centered_divide, x, y, SCM_ARG2,
3239 s_scm_centered_divide, qp, rp);
3240 }
3241 else if (SCM_BIGP (x))
3242 {
3243 if (SCM_LIKELY (SCM_I_INUMP (y)))
3244 {
3245 scm_t_inum yy = SCM_I_INUM (y);
3246 if (SCM_UNLIKELY (yy == 0))
3247 scm_num_overflow (s_scm_centered_divide);
3248 else
3249 {
3250 SCM q = scm_i_mkbig ();
3251 scm_t_inum rr;
3252 /* Arrange for rr to initially be non-positive,
3253 because that simplifies the test to see
3254 if it is within the needed bounds. */
3255 if (yy > 0)
3256 {
3257 rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
3258 SCM_I_BIG_MPZ (x), yy);
3259 scm_remember_upto_here_1 (x);
3260 if (rr < -yy / 2)
3261 {
3262 mpz_sub_ui (SCM_I_BIG_MPZ (q),
3263 SCM_I_BIG_MPZ (q), 1);
3264 rr += yy;
3265 }
3266 }
3267 else
3268 {
3269 rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
3270 SCM_I_BIG_MPZ (x), -yy);
3271 scm_remember_upto_here_1 (x);
3272 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
3273 if (rr < yy / 2)
3274 {
3275 mpz_add_ui (SCM_I_BIG_MPZ (q),
3276 SCM_I_BIG_MPZ (q), 1);
3277 rr -= yy;
3278 }
3279 }
3280 *qp = scm_i_normbig (q);
3281 *rp = SCM_I_MAKINUM (rr);
3282 }
3283 return;
3284 }
3285 else if (SCM_BIGP (y))
3286 return scm_i_bigint_centered_divide (x, y, qp, rp);
3287 else if (SCM_REALP (y))
3288 return scm_i_inexact_centered_divide
3289 (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp);
3290 else if (SCM_FRACTIONP (y))
3291 return scm_i_exact_rational_centered_divide (x, y, qp, rp);
3292 else
3293 return two_valued_wta_dispatch_2
3294 (g_scm_centered_divide, x, y, SCM_ARG2,
3295 s_scm_centered_divide, qp, rp);
3296 }
3297 else if (SCM_REALP (x))
3298 {
3299 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
3300 SCM_BIGP (y) || SCM_FRACTIONP (y))
3301 return scm_i_inexact_centered_divide
3302 (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp);
3303 else
3304 return two_valued_wta_dispatch_2
3305 (g_scm_centered_divide, x, y, SCM_ARG2,
3306 s_scm_centered_divide, qp, rp);
3307 }
3308 else if (SCM_FRACTIONP (x))
3309 {
3310 if (SCM_REALP (y))
3311 return scm_i_inexact_centered_divide
3312 (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
3313 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
3314 return scm_i_exact_rational_centered_divide (x, y, qp, rp);
3315 else
3316 return two_valued_wta_dispatch_2
3317 (g_scm_centered_divide, x, y, SCM_ARG2,
3318 s_scm_centered_divide, qp, rp);
3319 }
3320 else
3321 return two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG1,
3322 s_scm_centered_divide, qp, rp);
3323}
3324
3325static void
3326scm_i_inexact_centered_divide (double x, double y, SCM *qp, SCM *rp)
3327{
3328 double q, r;
3329
3330 if (SCM_LIKELY (y > 0))
3331 q = floor (x/y + 0.5);
3332 else if (SCM_LIKELY (y < 0))
3333 q = ceil (x/y - 0.5);
3334 else if (y == 0)
3335 scm_num_overflow (s_scm_centered_divide); /* or return a NaN? */
3336 else
3337 q = guile_NaN;
3338 r = x - q * y;
3339 *qp = scm_from_double (q);
3340 *rp = scm_from_double (r);
3341}
3342
3343/* Assumes that both x and y are bigints, though
3344 x might be able to fit into a fixnum. */
3345static void
3346scm_i_bigint_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
3347{
3348 SCM q, r, min_r;
3349
3350 /* Note that x might be small enough to fit into a
3351 fixnum, so we must not let it escape into the wild */
3352 q = scm_i_mkbig ();
3353 r = scm_i_mkbig ();
3354
3355 /* min_r will eventually become -abs(y/2) */
3356 min_r = scm_i_mkbig ();
3357 mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r),
3358 SCM_I_BIG_MPZ (y), 1);
3359
3360 /* Arrange for rr to initially be non-positive,
3361 because that simplifies the test to see
3362 if it is within the needed bounds. */
3363 if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0)
3364 {
3365 mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
3366 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3367 mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r));
3368 if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
3369 {
3370 mpz_sub_ui (SCM_I_BIG_MPZ (q),
3371 SCM_I_BIG_MPZ (q), 1);
3372 mpz_add (SCM_I_BIG_MPZ (r),
3373 SCM_I_BIG_MPZ (r),
3374 SCM_I_BIG_MPZ (y));
3375 }
3376 }
3377 else
3378 {
3379 mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
3380 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3381 if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
3382 {
3383 mpz_add_ui (SCM_I_BIG_MPZ (q),
3384 SCM_I_BIG_MPZ (q), 1);
3385 mpz_sub (SCM_I_BIG_MPZ (r),
3386 SCM_I_BIG_MPZ (r),
3387 SCM_I_BIG_MPZ (y));
3388 }
3389 }
3390 scm_remember_upto_here_2 (x, y);
3391 *qp = scm_i_normbig (q);
3392 *rp = scm_i_normbig (r);
3393}
3394
3395static void
3396scm_i_exact_rational_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
3397{
3398 SCM r1;
3399 SCM xd = scm_denominator (x);
3400 SCM yd = scm_denominator (y);
3401
3402 scm_centered_divide (scm_product (scm_numerator (x), yd),
3403 scm_product (scm_numerator (y), xd),
3404 qp, &r1);
3405 *rp = scm_divide (r1, scm_product (xd, yd));
3406}
3407
3408static SCM scm_i_inexact_round_quotient (double x, double y);
3409static SCM scm_i_bigint_round_quotient (SCM x, SCM y);
3410static SCM scm_i_exact_rational_round_quotient (SCM x, SCM y);
3411
3412SCM_PRIMITIVE_GENERIC (scm_round_quotient, "round-quotient", 2, 0, 0,
ff62c168 3413 (SCM x, SCM y),
8f9da340
MW
3414 "Return @math{@var{x} / @var{y}} to the nearest integer,\n"
3415 "with ties going to the nearest even integer.\n"
ff62c168 3416 "@lisp\n"
8f9da340
MW
3417 "(round-quotient 123 10) @result{} 12\n"
3418 "(round-quotient 123 -10) @result{} -12\n"
3419 "(round-quotient -123 10) @result{} -12\n"
3420 "(round-quotient -123 -10) @result{} 12\n"
3421 "(round-quotient 125 10) @result{} 12\n"
3422 "(round-quotient 127 10) @result{} 13\n"
3423 "(round-quotient 135 10) @result{} 14\n"
3424 "(round-quotient -123.2 -63.5) @result{} 2.0\n"
3425 "(round-quotient 16/3 -10/7) @result{} -4\n"
ff62c168 3426 "@end lisp")
8f9da340 3427#define FUNC_NAME s_scm_round_quotient
ff62c168
MW
3428{
3429 if (SCM_LIKELY (SCM_I_INUMP (x)))
3430 {
4a46bc2a 3431 scm_t_inum xx = SCM_I_INUM (x);
ff62c168
MW
3432 if (SCM_LIKELY (SCM_I_INUMP (y)))
3433 {
3434 scm_t_inum yy = SCM_I_INUM (y);
3435 if (SCM_UNLIKELY (yy == 0))
8f9da340 3436 scm_num_overflow (s_scm_round_quotient);
ff62c168
MW
3437 else
3438 {
ff62c168 3439 scm_t_inum qq = xx / yy;
4a46bc2a 3440 scm_t_inum rr = xx % yy;
8f9da340
MW
3441 scm_t_inum ay = yy;
3442 scm_t_inum r2 = 2 * rr;
3443
3444 if (SCM_LIKELY (yy < 0))
ff62c168 3445 {
8f9da340
MW
3446 ay = -ay;
3447 r2 = -r2;
3448 }
3449
3450 if (qq & 1L)
3451 {
3452 if (r2 >= ay)
3453 qq++;
3454 else if (r2 <= -ay)
3455 qq--;
ff62c168
MW
3456 }
3457 else
3458 {
8f9da340
MW
3459 if (r2 > ay)
3460 qq++;
3461 else if (r2 < -ay)
3462 qq--;
ff62c168 3463 }
4a46bc2a
MW
3464 if (SCM_LIKELY (SCM_FIXABLE (qq)))
3465 return SCM_I_MAKINUM (qq);
3466 else
3467 return scm_i_inum2big (qq);
ff62c168
MW
3468 }
3469 }
3470 else if (SCM_BIGP (y))
3471 {
3472 /* Pass a denormalized bignum version of x (even though it
8f9da340
MW
3473 can fit in a fixnum) to scm_i_bigint_round_quotient */
3474 return scm_i_bigint_round_quotient (scm_i_long2big (xx), y);
ff62c168
MW
3475 }
3476 else if (SCM_REALP (y))
8f9da340 3477 return scm_i_inexact_round_quotient (xx, SCM_REAL_VALUE (y));
ff62c168 3478 else if (SCM_FRACTIONP (y))
8f9da340 3479 return scm_i_exact_rational_round_quotient (x, y);
ff62c168 3480 else
8f9da340
MW
3481 SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2,
3482 s_scm_round_quotient);
ff62c168
MW
3483 }
3484 else if (SCM_BIGP (x))
3485 {
3486 if (SCM_LIKELY (SCM_I_INUMP (y)))
3487 {
3488 scm_t_inum yy = SCM_I_INUM (y);
3489 if (SCM_UNLIKELY (yy == 0))
8f9da340 3490 scm_num_overflow (s_scm_round_quotient);
4a46bc2a
MW
3491 else if (SCM_UNLIKELY (yy == 1))
3492 return x;
ff62c168
MW
3493 else
3494 {
3495 SCM q = scm_i_mkbig ();
3496 scm_t_inum rr;
8f9da340
MW
3497 int needs_adjustment;
3498
ff62c168
MW
3499 if (yy > 0)
3500 {
8f9da340
MW
3501 rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q),
3502 SCM_I_BIG_MPZ (x), yy);
3503 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3504 needs_adjustment = (2*rr >= yy);
3505 else
3506 needs_adjustment = (2*rr > yy);
ff62c168
MW
3507 }
3508 else
3509 {
3510 rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
3511 SCM_I_BIG_MPZ (x), -yy);
ff62c168 3512 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
8f9da340
MW
3513 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3514 needs_adjustment = (2*rr <= yy);
3515 else
3516 needs_adjustment = (2*rr < yy);
ff62c168 3517 }
8f9da340
MW
3518 scm_remember_upto_here_1 (x);
3519 if (needs_adjustment)
3520 mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
ff62c168
MW
3521 return scm_i_normbig (q);
3522 }
3523 }
3524 else if (SCM_BIGP (y))
8f9da340 3525 return scm_i_bigint_round_quotient (x, y);
ff62c168 3526 else if (SCM_REALP (y))
8f9da340 3527 return scm_i_inexact_round_quotient
ff62c168
MW
3528 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
3529 else if (SCM_FRACTIONP (y))
8f9da340 3530 return scm_i_exact_rational_round_quotient (x, y);
ff62c168 3531 else
8f9da340
MW
3532 SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2,
3533 s_scm_round_quotient);
ff62c168
MW
3534 }
3535 else if (SCM_REALP (x))
3536 {
3537 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
3538 SCM_BIGP (y) || SCM_FRACTIONP (y))
8f9da340 3539 return scm_i_inexact_round_quotient
ff62c168
MW
3540 (SCM_REAL_VALUE (x), scm_to_double (y));
3541 else
8f9da340
MW
3542 SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2,
3543 s_scm_round_quotient);
ff62c168
MW
3544 }
3545 else if (SCM_FRACTIONP (x))
3546 {
3547 if (SCM_REALP (y))
8f9da340 3548 return scm_i_inexact_round_quotient
ff62c168 3549 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
03ddd15b 3550 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
8f9da340 3551 return scm_i_exact_rational_round_quotient (x, y);
ff62c168 3552 else
8f9da340
MW
3553 SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG2,
3554 s_scm_round_quotient);
ff62c168
MW
3555 }
3556 else
8f9da340
MW
3557 SCM_WTA_DISPATCH_2 (g_scm_round_quotient, x, y, SCM_ARG1,
3558 s_scm_round_quotient);
ff62c168
MW
3559}
3560#undef FUNC_NAME
3561
3562static SCM
8f9da340 3563scm_i_inexact_round_quotient (double x, double y)
ff62c168 3564{
8f9da340
MW
3565 if (SCM_UNLIKELY (y == 0))
3566 scm_num_overflow (s_scm_round_quotient); /* or return a NaN? */
ff62c168 3567 else
8f9da340 3568 return scm_from_double (scm_c_round (x / y));
ff62c168
MW
3569}
3570
3571/* Assumes that both x and y are bigints, though
3572 x might be able to fit into a fixnum. */
3573static SCM
8f9da340 3574scm_i_bigint_round_quotient (SCM x, SCM y)
ff62c168 3575{
8f9da340
MW
3576 SCM q, r, r2;
3577 int cmp, needs_adjustment;
ff62c168
MW
3578
3579 /* Note that x might be small enough to fit into a
3580 fixnum, so we must not let it escape into the wild */
3581 q = scm_i_mkbig ();
3582 r = scm_i_mkbig ();
8f9da340 3583 r2 = scm_i_mkbig ();
ff62c168 3584
8f9da340
MW
3585 mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
3586 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3587 mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */
3588 scm_remember_upto_here_2 (x, r);
ff62c168 3589
8f9da340
MW
3590 cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y));
3591 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3592 needs_adjustment = (cmp >= 0);
ff62c168 3593 else
8f9da340
MW
3594 needs_adjustment = (cmp > 0);
3595 scm_remember_upto_here_2 (r2, y);
3596
3597 if (needs_adjustment)
3598 mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
3599
ff62c168
MW
3600 return scm_i_normbig (q);
3601}
3602
ff62c168 3603static SCM
8f9da340 3604scm_i_exact_rational_round_quotient (SCM x, SCM y)
ff62c168 3605{
8f9da340 3606 return scm_round_quotient
03ddd15b
MW
3607 (scm_product (scm_numerator (x), scm_denominator (y)),
3608 scm_product (scm_numerator (y), scm_denominator (x)));
ff62c168
MW
3609}
3610
8f9da340
MW
3611static SCM scm_i_inexact_round_remainder (double x, double y);
3612static SCM scm_i_bigint_round_remainder (SCM x, SCM y);
3613static SCM scm_i_exact_rational_round_remainder (SCM x, SCM y);
ff62c168 3614
8f9da340 3615SCM_PRIMITIVE_GENERIC (scm_round_remainder, "round-remainder", 2, 0, 0,
ff62c168
MW
3616 (SCM x, SCM y),
3617 "Return the real number @var{r} such that\n"
8f9da340
MW
3618 "@math{@var{x} = @var{q}*@var{y} + @var{r}}, where\n"
3619 "@var{q} is @math{@var{x} / @var{y}} rounded to the\n"
3620 "nearest integer, with ties going to the nearest\n"
3621 "even integer.\n"
ff62c168 3622 "@lisp\n"
8f9da340
MW
3623 "(round-remainder 123 10) @result{} 3\n"
3624 "(round-remainder 123 -10) @result{} 3\n"
3625 "(round-remainder -123 10) @result{} -3\n"
3626 "(round-remainder -123 -10) @result{} -3\n"
3627 "(round-remainder 125 10) @result{} 5\n"
3628 "(round-remainder 127 10) @result{} -3\n"
3629 "(round-remainder 135 10) @result{} -5\n"
3630 "(round-remainder -123.2 -63.5) @result{} 3.8\n"
3631 "(round-remainder 16/3 -10/7) @result{} -8/21\n"
ff62c168 3632 "@end lisp")
8f9da340 3633#define FUNC_NAME s_scm_round_remainder
ff62c168
MW
3634{
3635 if (SCM_LIKELY (SCM_I_INUMP (x)))
3636 {
4a46bc2a 3637 scm_t_inum xx = SCM_I_INUM (x);
ff62c168
MW
3638 if (SCM_LIKELY (SCM_I_INUMP (y)))
3639 {
3640 scm_t_inum yy = SCM_I_INUM (y);
3641 if (SCM_UNLIKELY (yy == 0))
8f9da340 3642 scm_num_overflow (s_scm_round_remainder);
ff62c168
MW
3643 else
3644 {
8f9da340 3645 scm_t_inum qq = xx / yy;
ff62c168 3646 scm_t_inum rr = xx % yy;
8f9da340
MW
3647 scm_t_inum ay = yy;
3648 scm_t_inum r2 = 2 * rr;
3649
3650 if (SCM_LIKELY (yy < 0))
ff62c168 3651 {
8f9da340
MW
3652 ay = -ay;
3653 r2 = -r2;
3654 }
3655
3656 if (qq & 1L)
3657 {
3658 if (r2 >= ay)
3659 rr -= yy;
3660 else if (r2 <= -ay)
3661 rr += yy;
ff62c168
MW
3662 }
3663 else
3664 {
8f9da340
MW
3665 if (r2 > ay)
3666 rr -= yy;
3667 else if (r2 < -ay)
3668 rr += yy;
ff62c168
MW
3669 }
3670 return SCM_I_MAKINUM (rr);
3671 }
3672 }
3673 else if (SCM_BIGP (y))
3674 {
3675 /* Pass a denormalized bignum version of x (even though it
8f9da340
MW
3676 can fit in a fixnum) to scm_i_bigint_round_remainder */
3677 return scm_i_bigint_round_remainder
3678 (scm_i_long2big (xx), y);
ff62c168
MW
3679 }
3680 else if (SCM_REALP (y))
8f9da340 3681 return scm_i_inexact_round_remainder (xx, SCM_REAL_VALUE (y));
ff62c168 3682 else if (SCM_FRACTIONP (y))
8f9da340 3683 return scm_i_exact_rational_round_remainder (x, y);
ff62c168 3684 else
8f9da340
MW
3685 SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2,
3686 s_scm_round_remainder);
ff62c168
MW
3687 }
3688 else if (SCM_BIGP (x))
3689 {
3690 if (SCM_LIKELY (SCM_I_INUMP (y)))
3691 {
3692 scm_t_inum yy = SCM_I_INUM (y);
3693 if (SCM_UNLIKELY (yy == 0))
8f9da340 3694 scm_num_overflow (s_scm_round_remainder);
ff62c168
MW
3695 else
3696 {
8f9da340 3697 SCM q = scm_i_mkbig ();
ff62c168 3698 scm_t_inum rr;
8f9da340
MW
3699 int needs_adjustment;
3700
ff62c168
MW
3701 if (yy > 0)
3702 {
8f9da340
MW
3703 rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q),
3704 SCM_I_BIG_MPZ (x), yy);
3705 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3706 needs_adjustment = (2*rr >= yy);
3707 else
3708 needs_adjustment = (2*rr > yy);
ff62c168
MW
3709 }
3710 else
3711 {
8f9da340
MW
3712 rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
3713 SCM_I_BIG_MPZ (x), -yy);
3714 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3715 needs_adjustment = (2*rr <= yy);
3716 else
3717 needs_adjustment = (2*rr < yy);
ff62c168 3718 }
8f9da340
MW
3719 scm_remember_upto_here_2 (x, q);
3720 if (needs_adjustment)
3721 rr -= yy;
ff62c168
MW
3722 return SCM_I_MAKINUM (rr);
3723 }
3724 }
3725 else if (SCM_BIGP (y))
8f9da340 3726 return scm_i_bigint_round_remainder (x, y);
ff62c168 3727 else if (SCM_REALP (y))
8f9da340 3728 return scm_i_inexact_round_remainder
ff62c168
MW
3729 (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
3730 else if (SCM_FRACTIONP (y))
8f9da340 3731 return scm_i_exact_rational_round_remainder (x, y);
ff62c168 3732 else
8f9da340
MW
3733 SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2,
3734 s_scm_round_remainder);
ff62c168
MW
3735 }
3736 else if (SCM_REALP (x))
3737 {
3738 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
3739 SCM_BIGP (y) || SCM_FRACTIONP (y))
8f9da340 3740 return scm_i_inexact_round_remainder
ff62c168
MW
3741 (SCM_REAL_VALUE (x), scm_to_double (y));
3742 else
8f9da340
MW
3743 SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2,
3744 s_scm_round_remainder);
ff62c168
MW
3745 }
3746 else if (SCM_FRACTIONP (x))
3747 {
3748 if (SCM_REALP (y))
8f9da340 3749 return scm_i_inexact_round_remainder
ff62c168 3750 (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
03ddd15b 3751 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
8f9da340 3752 return scm_i_exact_rational_round_remainder (x, y);
ff62c168 3753 else
8f9da340
MW
3754 SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG2,
3755 s_scm_round_remainder);
ff62c168
MW
3756 }
3757 else
8f9da340
MW
3758 SCM_WTA_DISPATCH_2 (g_scm_round_remainder, x, y, SCM_ARG1,
3759 s_scm_round_remainder);
ff62c168
MW
3760}
3761#undef FUNC_NAME
3762
3763static SCM
8f9da340 3764scm_i_inexact_round_remainder (double x, double y)
ff62c168 3765{
ff62c168
MW
3766 /* Although it would be more efficient to use fmod here, we can't
3767 because it would in some cases produce results inconsistent with
8f9da340 3768 scm_i_inexact_round_quotient, such that x != r + q * y (not even
ff62c168 3769 close). In particular, when x-y/2 is very close to a multiple of
8f9da340
MW
3770 y, then r might be either -abs(y/2) or abs(y/2), but those two
3771 cases must correspond to different choices of q. If quotient
ff62c168 3772 chooses one and remainder chooses the other, it would be bad. */
8f9da340
MW
3773
3774 if (SCM_UNLIKELY (y == 0))
3775 scm_num_overflow (s_scm_round_remainder); /* or return a NaN? */
ff62c168 3776 else
8f9da340
MW
3777 {
3778 double q = scm_c_round (x / y);
3779 return scm_from_double (x - q * y);
3780 }
ff62c168
MW
3781}
3782
3783/* Assumes that both x and y are bigints, though
3784 x might be able to fit into a fixnum. */
3785static SCM
8f9da340 3786scm_i_bigint_round_remainder (SCM x, SCM y)
ff62c168 3787{
8f9da340
MW
3788 SCM q, r, r2;
3789 int cmp, needs_adjustment;
ff62c168
MW
3790
3791 /* Note that x might be small enough to fit into a
3792 fixnum, so we must not let it escape into the wild */
8f9da340 3793 q = scm_i_mkbig ();
ff62c168 3794 r = scm_i_mkbig ();
8f9da340 3795 r2 = scm_i_mkbig ();
ff62c168 3796
8f9da340
MW
3797 mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
3798 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3799 scm_remember_upto_here_1 (x);
3800 mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */
ff62c168 3801
8f9da340
MW
3802 cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y));
3803 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3804 needs_adjustment = (cmp >= 0);
ff62c168 3805 else
8f9da340
MW
3806 needs_adjustment = (cmp > 0);
3807 scm_remember_upto_here_2 (q, r2);
3808
3809 if (needs_adjustment)
3810 mpz_sub (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y));
3811
3812 scm_remember_upto_here_1 (y);
ff62c168
MW
3813 return scm_i_normbig (r);
3814}
3815
ff62c168 3816static SCM
8f9da340 3817scm_i_exact_rational_round_remainder (SCM x, SCM y)
ff62c168 3818{
03ddd15b
MW
3819 SCM xd = scm_denominator (x);
3820 SCM yd = scm_denominator (y);
8f9da340
MW
3821 SCM r1 = scm_round_remainder (scm_product (scm_numerator (x), yd),
3822 scm_product (scm_numerator (y), xd));
03ddd15b 3823 return scm_divide (r1, scm_product (xd, yd));
ff62c168
MW
3824}
3825
3826
8f9da340
MW
3827static void scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp);
3828static void scm_i_bigint_round_divide (SCM x, SCM y, SCM *qp, SCM *rp);
3829static void scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp);
ff62c168 3830
8f9da340 3831SCM_PRIMITIVE_GENERIC (scm_i_round_divide, "round/", 2, 0, 0,
ff62c168
MW
3832 (SCM x, SCM y),
3833 "Return the integer @var{q} and the real number @var{r}\n"
3834 "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
8f9da340
MW
3835 "and @var{q} is @math{@var{x} / @var{y}} rounded to the\n"
3836 "nearest integer, with ties going to the nearest even integer.\n"
ff62c168 3837 "@lisp\n"
8f9da340
MW
3838 "(round/ 123 10) @result{} 12 and 3\n"
3839 "(round/ 123 -10) @result{} -12 and 3\n"
3840 "(round/ -123 10) @result{} -12 and -3\n"
3841 "(round/ -123 -10) @result{} 12 and -3\n"
3842 "(round/ 125 10) @result{} 12 and 5\n"
3843 "(round/ 127 10) @result{} 13 and -3\n"
3844 "(round/ 135 10) @result{} 14 and -5\n"
3845 "(round/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
3846 "(round/ 16/3 -10/7) @result{} -4 and -8/21\n"
ff62c168 3847 "@end lisp")
8f9da340 3848#define FUNC_NAME s_scm_i_round_divide
5fbf680b
MW
3849{
3850 SCM q, r;
3851
8f9da340 3852 scm_round_divide(x, y, &q, &r);
5fbf680b
MW
3853 return scm_values (scm_list_2 (q, r));
3854}
3855#undef FUNC_NAME
3856
8f9da340
MW
3857#define s_scm_round_divide s_scm_i_round_divide
3858#define g_scm_round_divide g_scm_i_round_divide
5fbf680b
MW
3859
3860void
8f9da340 3861scm_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
ff62c168
MW
3862{
3863 if (SCM_LIKELY (SCM_I_INUMP (x)))
3864 {
4a46bc2a 3865 scm_t_inum xx = SCM_I_INUM (x);
ff62c168
MW
3866 if (SCM_LIKELY (SCM_I_INUMP (y)))
3867 {
3868 scm_t_inum yy = SCM_I_INUM (y);
3869 if (SCM_UNLIKELY (yy == 0))
8f9da340 3870 scm_num_overflow (s_scm_round_divide);
ff62c168
MW
3871 else
3872 {
ff62c168 3873 scm_t_inum qq = xx / yy;
4a46bc2a 3874 scm_t_inum rr = xx % yy;
8f9da340
MW
3875 scm_t_inum ay = yy;
3876 scm_t_inum r2 = 2 * rr;
3877
3878 if (SCM_LIKELY (yy < 0))
ff62c168 3879 {
8f9da340
MW
3880 ay = -ay;
3881 r2 = -r2;
3882 }
3883
3884 if (qq & 1L)
3885 {
3886 if (r2 >= ay)
3887 { qq++; rr -= yy; }
3888 else if (r2 <= -ay)
3889 { qq--; rr += yy; }
ff62c168
MW
3890 }
3891 else
3892 {
8f9da340
MW
3893 if (r2 > ay)
3894 { qq++; rr -= yy; }
3895 else if (r2 < -ay)
3896 { qq--; rr += yy; }
ff62c168 3897 }
4a46bc2a 3898 if (SCM_LIKELY (SCM_FIXABLE (qq)))
5fbf680b 3899 *qp = SCM_I_MAKINUM (qq);
4a46bc2a 3900 else
5fbf680b
MW
3901 *qp = scm_i_inum2big (qq);
3902 *rp = SCM_I_MAKINUM (rr);
ff62c168 3903 }
5fbf680b 3904 return;
ff62c168
MW
3905 }
3906 else if (SCM_BIGP (y))
3907 {
3908 /* Pass a denormalized bignum version of x (even though it
8f9da340
MW
3909 can fit in a fixnum) to scm_i_bigint_round_divide */
3910 return scm_i_bigint_round_divide
3911 (scm_i_long2big (SCM_I_INUM (x)), y, qp, rp);
ff62c168
MW
3912 }
3913 else if (SCM_REALP (y))
8f9da340 3914 return scm_i_inexact_round_divide (xx, SCM_REAL_VALUE (y), qp, rp);
ff62c168 3915 else if (SCM_FRACTIONP (y))
8f9da340 3916 return scm_i_exact_rational_round_divide (x, y, qp, rp);
ff62c168 3917 else
8f9da340
MW
3918 return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
3919 s_scm_round_divide, qp, rp);
ff62c168
MW
3920 }
3921 else if (SCM_BIGP (x))
3922 {
3923 if (SCM_LIKELY (SCM_I_INUMP (y)))
3924 {
3925 scm_t_inum yy = SCM_I_INUM (y);
3926 if (SCM_UNLIKELY (yy == 0))
8f9da340 3927 scm_num_overflow (s_scm_round_divide);
ff62c168
MW
3928 else
3929 {
3930 SCM q = scm_i_mkbig ();
3931 scm_t_inum rr;
8f9da340
MW
3932 int needs_adjustment;
3933
ff62c168
MW
3934 if (yy > 0)
3935 {
8f9da340
MW
3936 rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q),
3937 SCM_I_BIG_MPZ (x), yy);
3938 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3939 needs_adjustment = (2*rr >= yy);
3940 else
3941 needs_adjustment = (2*rr > yy);
ff62c168
MW
3942 }
3943 else
3944 {
3945 rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
3946 SCM_I_BIG_MPZ (x), -yy);
ff62c168 3947 mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
8f9da340
MW
3948 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
3949 needs_adjustment = (2*rr <= yy);
3950 else
3951 needs_adjustment = (2*rr < yy);
3952 }
3953 scm_remember_upto_here_1 (x);
3954 if (needs_adjustment)
3955 {
3956 mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
3957 rr -= yy;
ff62c168 3958 }
5fbf680b
MW
3959 *qp = scm_i_normbig (q);
3960 *rp = SCM_I_MAKINUM (rr);
ff62c168 3961 }
5fbf680b 3962 return;
ff62c168
MW
3963 }
3964 else if (SCM_BIGP (y))
8f9da340 3965 return scm_i_bigint_round_divide (x, y, qp, rp);
ff62c168 3966 else if (SCM_REALP (y))
8f9da340 3967 return scm_i_inexact_round_divide
5fbf680b 3968 (scm_i_big2dbl (x), SCM_REAL_VALUE (y), qp, rp);
ff62c168 3969 else if (SCM_FRACTIONP (y))
8f9da340 3970 return scm_i_exact_rational_round_divide (x, y, qp, rp);
ff62c168 3971 else
8f9da340
MW
3972 return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
3973 s_scm_round_divide, qp, rp);
ff62c168
MW
3974 }
3975 else if (SCM_REALP (x))
3976 {
3977 if (SCM_REALP (y) || SCM_I_INUMP (y) ||
3978 SCM_BIGP (y) || SCM_FRACTIONP (y))
8f9da340 3979 return scm_i_inexact_round_divide
5fbf680b 3980 (SCM_REAL_VALUE (x), scm_to_double (y), qp, rp);
03ddd15b 3981 else
8f9da340
MW
3982 return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
3983 s_scm_round_divide, qp, rp);
ff62c168
MW
3984 }
3985 else if (SCM_FRACTIONP (x))
3986 {
3987 if (SCM_REALP (y))
8f9da340 3988 return scm_i_inexact_round_divide
5fbf680b 3989 (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
03ddd15b 3990 else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
8f9da340 3991 return scm_i_exact_rational_round_divide (x, y, qp, rp);
ff62c168 3992 else
8f9da340
MW
3993 return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
3994 s_scm_round_divide, qp, rp);
ff62c168
MW
3995 }
3996 else
8f9da340
MW
3997 return two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG1,
3998 s_scm_round_divide, qp, rp);
ff62c168 3999}
ff62c168 4000
5fbf680b 4001static void
8f9da340 4002scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp)
ff62c168 4003{
8f9da340
MW
4004 if (SCM_UNLIKELY (y == 0))
4005 scm_num_overflow (s_scm_round_divide); /* or return a NaN? */
ff62c168 4006 else
8f9da340
MW
4007 {
4008 double q = scm_c_round (x / y);
4009 double r = x - q * y;
4010 *qp = scm_from_double (q);
4011 *rp = scm_from_double (r);
4012 }
ff62c168
MW
4013}
4014
4015/* Assumes that both x and y are bigints, though
4016 x might be able to fit into a fixnum. */
5fbf680b 4017static void
8f9da340 4018scm_i_bigint_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
ff62c168 4019{
8f9da340
MW
4020 SCM q, r, r2;
4021 int cmp, needs_adjustment;
ff62c168
MW
4022
4023 /* Note that x might be small enough to fit into a
4024 fixnum, so we must not let it escape into the wild */
4025 q = scm_i_mkbig ();
4026 r = scm_i_mkbig ();
8f9da340 4027 r2 = scm_i_mkbig ();
ff62c168 4028
8f9da340
MW
4029 mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
4030 SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
4031 scm_remember_upto_here_1 (x);
4032 mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */
ff62c168 4033
8f9da340
MW
4034 cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y));
4035 if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
4036 needs_adjustment = (cmp >= 0);
ff62c168 4037 else
8f9da340
MW
4038 needs_adjustment = (cmp > 0);
4039
4040 if (needs_adjustment)
ff62c168 4041 {
8f9da340
MW
4042 mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
4043 mpz_sub (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y));
ff62c168 4044 }
8f9da340
MW
4045
4046 scm_remember_upto_here_2 (r2, y);
5fbf680b
MW
4047 *qp = scm_i_normbig (q);
4048 *rp = scm_i_normbig (r);
ff62c168
MW
4049}
4050
5fbf680b 4051static void
8f9da340 4052scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
ff62c168 4053{
03ddd15b
MW
4054 SCM r1;
4055 SCM xd = scm_denominator (x);
4056 SCM yd = scm_denominator (y);
4057
8f9da340
MW
4058 scm_round_divide (scm_product (scm_numerator (x), yd),
4059 scm_product (scm_numerator (y), xd),
4060 qp, &r1);
03ddd15b 4061 *rp = scm_divide (r1, scm_product (xd, yd));
ff62c168
MW
4062}
4063
4064
78d3deb1
AW
4065SCM_PRIMITIVE_GENERIC (scm_i_gcd, "gcd", 0, 2, 1,
4066 (SCM x, SCM y, SCM rest),
4067 "Return the greatest common divisor of all parameter values.\n"
4068 "If called without arguments, 0 is returned.")
4069#define FUNC_NAME s_scm_i_gcd
4070{
4071 while (!scm_is_null (rest))
4072 { x = scm_gcd (x, y);
4073 y = scm_car (rest);
4074 rest = scm_cdr (rest);
4075 }
4076 return scm_gcd (x, y);
4077}
4078#undef FUNC_NAME
4079
4080#define s_gcd s_scm_i_gcd
4081#define g_gcd g_scm_i_gcd
4082
0f2d19dd 4083SCM
6e8d25a6 4084scm_gcd (SCM x, SCM y)
0f2d19dd 4085{
a2dead1b 4086 if (SCM_UNLIKELY (SCM_UNBNDP (y)))
1dd79792 4087 return SCM_UNBNDP (x) ? SCM_INUM0 : scm_abs (x);
ca46fb90 4088
a2dead1b 4089 if (SCM_LIKELY (SCM_I_INUMP (x)))
ca46fb90 4090 {
a2dead1b 4091 if (SCM_LIKELY (SCM_I_INUMP (y)))
ca46fb90 4092 {
e25f3727
AW
4093 scm_t_inum xx = SCM_I_INUM (x);
4094 scm_t_inum yy = SCM_I_INUM (y);
4095 scm_t_inum u = xx < 0 ? -xx : xx;
4096 scm_t_inum v = yy < 0 ? -yy : yy;
4097 scm_t_inum result;
a2dead1b 4098 if (SCM_UNLIKELY (xx == 0))
0aacf84e 4099 result = v;
a2dead1b 4100 else if (SCM_UNLIKELY (yy == 0))
0aacf84e
MD
4101 result = u;
4102 else
4103 {
a2dead1b 4104 int k = 0;
0aacf84e 4105 /* Determine a common factor 2^k */
a2dead1b 4106 while (((u | v) & 1) == 0)
0aacf84e 4107 {
a2dead1b 4108 k++;
0aacf84e
MD
4109 u >>= 1;
4110 v >>= 1;
4111 }
4112 /* Now, any factor 2^n can be eliminated */
a2dead1b
MW
4113 if ((u & 1) == 0)
4114 while ((u & 1) == 0)
4115 u >>= 1;
0aacf84e 4116 else
a2dead1b
MW
4117 while ((v & 1) == 0)
4118 v >>= 1;
4119 /* Both u and v are now odd. Subtract the smaller one
4120 from the larger one to produce an even number, remove
4121 more factors of two, and repeat. */
4122 while (u != v)
0aacf84e 4123 {
a2dead1b
MW
4124 if (u > v)
4125 {
4126 u -= v;
4127 while ((u & 1) == 0)
4128 u >>= 1;
4129 }
4130 else
4131 {
4132 v -= u;
4133 while ((v & 1) == 0)
4134 v >>= 1;
4135 }
0aacf84e 4136 }
a2dead1b 4137 result = u << k;
0aacf84e
MD
4138 }
4139 return (SCM_POSFIXABLE (result)
d956fa6f 4140 ? SCM_I_MAKINUM (result)
e25f3727 4141 : scm_i_inum2big (result));
ca46fb90
RB
4142 }
4143 else if (SCM_BIGP (y))
4144 {
0bff4dce
KR
4145 SCM_SWAP (x, y);
4146 goto big_inum;
ca46fb90
RB
4147 }
4148 else
4149 SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
f872b822 4150 }
ca46fb90
RB
4151 else if (SCM_BIGP (x))
4152 {
e11e83f3 4153 if (SCM_I_INUMP (y))
ca46fb90 4154 {
e25f3727
AW
4155 scm_t_bits result;
4156 scm_t_inum yy;
0bff4dce 4157 big_inum:
e11e83f3 4158 yy = SCM_I_INUM (y);
8c5b0afc
KR
4159 if (yy == 0)
4160 return scm_abs (x);
0aacf84e
MD
4161 if (yy < 0)
4162 yy = -yy;
ca46fb90
RB
4163 result = mpz_gcd_ui (NULL, SCM_I_BIG_MPZ (x), yy);
4164 scm_remember_upto_here_1 (x);
0aacf84e 4165 return (SCM_POSFIXABLE (result)
d956fa6f 4166 ? SCM_I_MAKINUM (result)
e25f3727 4167 : scm_from_unsigned_integer (result));
ca46fb90
RB
4168 }
4169 else if (SCM_BIGP (y))
4170 {
4171 SCM result = scm_i_mkbig ();
0aacf84e
MD
4172 mpz_gcd (SCM_I_BIG_MPZ (result),
4173 SCM_I_BIG_MPZ (x),
4174 SCM_I_BIG_MPZ (y));
4175 scm_remember_upto_here_2 (x, y);
ca46fb90
RB
4176 return scm_i_normbig (result);
4177 }
4178 else
4179 SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
09fb7599 4180 }
ca46fb90 4181 else
09fb7599 4182 SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG1, s_gcd);
0f2d19dd
JB
4183}
4184
78d3deb1
AW
4185SCM_PRIMITIVE_GENERIC (scm_i_lcm, "lcm", 0, 2, 1,
4186 (SCM x, SCM y, SCM rest),
4187 "Return the least common multiple of the arguments.\n"
4188 "If called without arguments, 1 is returned.")
4189#define FUNC_NAME s_scm_i_lcm
4190{
4191 while (!scm_is_null (rest))
4192 { x = scm_lcm (x, y);
4193 y = scm_car (rest);
4194 rest = scm_cdr (rest);
4195 }
4196 return scm_lcm (x, y);
4197}
4198#undef FUNC_NAME
4199
4200#define s_lcm s_scm_i_lcm
4201#define g_lcm g_scm_i_lcm
4202
0f2d19dd 4203SCM
6e8d25a6 4204scm_lcm (SCM n1, SCM n2)
0f2d19dd 4205{
ca46fb90
RB
4206 if (SCM_UNBNDP (n2))
4207 {
4208 if (SCM_UNBNDP (n1))
d956fa6f
MV
4209 return SCM_I_MAKINUM (1L);
4210 n2 = SCM_I_MAKINUM (1L);
09fb7599 4211 }
09fb7599 4212
e11e83f3 4213 SCM_GASSERT2 (SCM_I_INUMP (n1) || SCM_BIGP (n1),
ca46fb90 4214 g_lcm, n1, n2, SCM_ARG1, s_lcm);
e11e83f3 4215 SCM_GASSERT2 (SCM_I_INUMP (n2) || SCM_BIGP (n2),
ca46fb90 4216 g_lcm, n1, n2, SCM_ARGn, s_lcm);
09fb7599 4217
e11e83f3 4218 if (SCM_I_INUMP (n1))
ca46fb90 4219 {
e11e83f3 4220 if (SCM_I_INUMP (n2))
ca46fb90
RB
4221 {
4222 SCM d = scm_gcd (n1, n2);
bc36d050 4223 if (scm_is_eq (d, SCM_INUM0))
ca46fb90
RB
4224 return d;
4225 else
4226 return scm_abs (scm_product (n1, scm_quotient (n2, d)));
4227 }
4228 else
4229 {
4230 /* inum n1, big n2 */
4231 inumbig:
4232 {
4233 SCM result = scm_i_mkbig ();
e25f3727 4234 scm_t_inum nn1 = SCM_I_INUM (n1);
ca46fb90
RB
4235 if (nn1 == 0) return SCM_INUM0;
4236 if (nn1 < 0) nn1 = - nn1;
4237 mpz_lcm_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n2), nn1);
4238 scm_remember_upto_here_1 (n2);
4239 return result;
4240 }
4241 }
4242 }
4243 else
4244 {
4245 /* big n1 */
e11e83f3 4246 if (SCM_I_INUMP (n2))
ca46fb90
RB
4247 {
4248 SCM_SWAP (n1, n2);
4249 goto inumbig;
4250 }
4251 else
4252 {
4253 SCM result = scm_i_mkbig ();
4254 mpz_lcm(SCM_I_BIG_MPZ (result),
4255 SCM_I_BIG_MPZ (n1),
4256 SCM_I_BIG_MPZ (n2));
4257 scm_remember_upto_here_2(n1, n2);
4258 /* shouldn't need to normalize b/c lcm of 2 bigs should be big */
4259 return result;
4260 }
f872b822 4261 }
0f2d19dd
JB
4262}
4263
8a525303
GB
4264/* Emulating 2's complement bignums with sign magnitude arithmetic:
4265
4266 Logand:
4267 X Y Result Method:
4268 (len)
4269 + + + x (map digit:logand X Y)
4270 + - + x (map digit:logand X (lognot (+ -1 Y)))
4271 - + + y (map digit:logand (lognot (+ -1 X)) Y)
4272 - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y)))
4273
4274 Logior:
4275 X Y Result Method:
4276
4277 + + + (map digit:logior X Y)
4278 + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y)))
4279 - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y)))
4280 - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y)))
4281
4282 Logxor:
4283 X Y Result Method:
4284
4285 + + + (map digit:logxor X Y)
4286 + - - (+ 1 (map digit:logxor X (+ -1 Y)))
4287 - + - (+ 1 (map digit:logxor (+ -1 X) Y))
4288 - - + (map digit:logxor (+ -1 X) (+ -1 Y))
4289
4290 Logtest:
4291 X Y Result
4292
4293 + + (any digit:logand X Y)
4294 + - (any digit:logand X (lognot (+ -1 Y)))
4295 - + (any digit:logand (lognot (+ -1 X)) Y)
4296 - - #t
4297
4298*/
4299
78d3deb1
AW
4300SCM_DEFINE (scm_i_logand, "logand", 0, 2, 1,
4301 (SCM x, SCM y, SCM rest),
4302 "Return the bitwise AND of the integer arguments.\n\n"
4303 "@lisp\n"
4304 "(logand) @result{} -1\n"
4305 "(logand 7) @result{} 7\n"
4306 "(logand #b111 #b011 #b001) @result{} 1\n"
4307 "@end lisp")
4308#define FUNC_NAME s_scm_i_logand
4309{
4310 while (!scm_is_null (rest))
4311 { x = scm_logand (x, y);
4312 y = scm_car (rest);
4313 rest = scm_cdr (rest);
4314 }
4315 return scm_logand (x, y);
4316}
4317#undef FUNC_NAME
4318
4319#define s_scm_logand s_scm_i_logand
4320
4321SCM scm_logand (SCM n1, SCM n2)
1bbd0b84 4322#define FUNC_NAME s_scm_logand
0f2d19dd 4323{
e25f3727 4324 scm_t_inum nn1;
9a00c9fc 4325
0aacf84e
MD
4326 if (SCM_UNBNDP (n2))
4327 {
4328 if (SCM_UNBNDP (n1))
d956fa6f 4329 return SCM_I_MAKINUM (-1);
0aacf84e
MD
4330 else if (!SCM_NUMBERP (n1))
4331 SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
4332 else if (SCM_NUMBERP (n1))
4333 return n1;
4334 else
4335 SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
d28da049 4336 }
09fb7599 4337
e11e83f3 4338 if (SCM_I_INUMP (n1))
0aacf84e 4339 {
e11e83f3
MV
4340 nn1 = SCM_I_INUM (n1);
4341 if (SCM_I_INUMP (n2))
0aacf84e 4342 {
e25f3727 4343 scm_t_inum nn2 = SCM_I_INUM (n2);
d956fa6f 4344 return SCM_I_MAKINUM (nn1 & nn2);
0aacf84e
MD
4345 }
4346 else if SCM_BIGP (n2)
4347 {
4348 intbig:
2e16a342 4349 if (nn1 == 0)
0aacf84e
MD
4350 return SCM_INUM0;
4351 {
4352 SCM result_z = scm_i_mkbig ();
4353 mpz_t nn1_z;
4354 mpz_init_set_si (nn1_z, nn1);
4355 mpz_and (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
4356 scm_remember_upto_here_1 (n2);
4357 mpz_clear (nn1_z);
4358 return scm_i_normbig (result_z);
4359 }
4360 }
4361 else
4362 SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
4363 }
4364 else if (SCM_BIGP (n1))
4365 {
e11e83f3 4366 if (SCM_I_INUMP (n2))
0aacf84e
MD
4367 {
4368 SCM_SWAP (n1, n2);
e11e83f3 4369 nn1 = SCM_I_INUM (n1);
0aacf84e
MD
4370 goto intbig;
4371 }
4372 else if (SCM_BIGP (n2))
4373 {
4374 SCM result_z = scm_i_mkbig ();
4375 mpz_and (SCM_I_BIG_MPZ (result_z),
4376 SCM_I_BIG_MPZ (n1),
4377 SCM_I_BIG_MPZ (n2));
4378 scm_remember_upto_here_2 (n1, n2);
4379 return scm_i_normbig (result_z);
4380 }
4381 else
4382 SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
09fb7599 4383 }
0aacf84e 4384 else
09fb7599 4385 SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
0f2d19dd 4386}
1bbd0b84 4387#undef FUNC_NAME
0f2d19dd 4388
09fb7599 4389
78d3deb1
AW
4390SCM_DEFINE (scm_i_logior, "logior", 0, 2, 1,
4391 (SCM x, SCM y, SCM rest),
4392 "Return the bitwise OR of the integer arguments.\n\n"
4393 "@lisp\n"
4394 "(logior) @result{} 0\n"
4395 "(logior 7) @result{} 7\n"
4396 "(logior #b000 #b001 #b011) @result{} 3\n"
4397 "@end lisp")
4398#define FUNC_NAME s_scm_i_logior
4399{
4400 while (!scm_is_null (rest))
4401 { x = scm_logior (x, y);
4402 y = scm_car (rest);
4403 rest = scm_cdr (rest);
4404 }
4405 return scm_logior (x, y);
4406}
4407#undef FUNC_NAME
4408
4409#define s_scm_logior s_scm_i_logior
4410
4411SCM scm_logior (SCM n1, SCM n2)
1bbd0b84 4412#define FUNC_NAME s_scm_logior
0f2d19dd 4413{
e25f3727 4414 scm_t_inum nn1;
9a00c9fc 4415
0aacf84e
MD
4416 if (SCM_UNBNDP (n2))
4417 {
4418 if (SCM_UNBNDP (n1))
4419 return SCM_INUM0;
4420 else if (SCM_NUMBERP (n1))
4421 return n1;
4422 else
4423 SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
d28da049 4424 }
09fb7599 4425
e11e83f3 4426 if (SCM_I_INUMP (n1))
0aacf84e 4427 {
e11e83f3
MV
4428 nn1 = SCM_I_INUM (n1);
4429 if (SCM_I_INUMP (n2))
0aacf84e 4430 {
e11e83f3 4431 long nn2 = SCM_I_INUM (n2);
d956fa6f 4432 return SCM_I_MAKINUM (nn1 | nn2);
0aacf84e
MD
4433 }
4434 else if (SCM_BIGP (n2))
4435 {
4436 intbig:
4437 if (nn1 == 0)
4438 return n2;
4439 {
4440 SCM result_z = scm_i_mkbig ();
4441 mpz_t nn1_z;
4442 mpz_init_set_si (nn1_z, nn1);
4443 mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
4444 scm_remember_upto_here_1 (n2);
4445 mpz_clear (nn1_z);
9806de0d 4446 return scm_i_normbig (result_z);
0aacf84e
MD
4447 }
4448 }
4449 else
4450 SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
4451 }
4452 else if (SCM_BIGP (n1))
4453 {
e11e83f3 4454 if (SCM_I_INUMP (n2))
0aacf84e
MD
4455 {
4456 SCM_SWAP (n1, n2);
e11e83f3 4457 nn1 = SCM_I_INUM (n1);
0aacf84e
MD
4458 goto intbig;
4459 }
4460 else if (SCM_BIGP (n2))
4461 {
4462 SCM result_z = scm_i_mkbig ();
4463 mpz_ior (SCM_I_BIG_MPZ (result_z),
4464 SCM_I_BIG_MPZ (n1),
4465 SCM_I_BIG_MPZ (n2));
4466 scm_remember_upto_here_2 (n1, n2);
9806de0d 4467 return scm_i_normbig (result_z);
0aacf84e
MD
4468 }
4469 else
4470 SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
09fb7599 4471 }
0aacf84e 4472 else
09fb7599 4473 SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
0f2d19dd 4474}
1bbd0b84 4475#undef FUNC_NAME
0f2d19dd 4476
09fb7599 4477
78d3deb1
AW
4478SCM_DEFINE (scm_i_logxor, "logxor", 0, 2, 1,
4479 (SCM x, SCM y, SCM rest),
3c3db128
GH
4480 "Return the bitwise XOR of the integer arguments. A bit is\n"
4481 "set in the result if it is set in an odd number of arguments.\n"
4482 "@lisp\n"
4483 "(logxor) @result{} 0\n"
4484 "(logxor 7) @result{} 7\n"
4485 "(logxor #b000 #b001 #b011) @result{} 2\n"
4486 "(logxor #b000 #b001 #b011 #b011) @result{} 1\n"
1e6808ea 4487 "@end lisp")
78d3deb1
AW
4488#define FUNC_NAME s_scm_i_logxor
4489{
4490 while (!scm_is_null (rest))
4491 { x = scm_logxor (x, y);
4492 y = scm_car (rest);
4493 rest = scm_cdr (rest);
4494 }
4495 return scm_logxor (x, y);
4496}
4497#undef FUNC_NAME
4498
4499#define s_scm_logxor s_scm_i_logxor
4500
4501SCM scm_logxor (SCM n1, SCM n2)
1bbd0b84 4502#define FUNC_NAME s_scm_logxor
0f2d19dd 4503{
e25f3727 4504 scm_t_inum nn1;
9a00c9fc 4505
0aacf84e
MD
4506 if (SCM_UNBNDP (n2))
4507 {
4508 if (SCM_UNBNDP (n1))
4509 return SCM_INUM0;
4510 else if (SCM_NUMBERP (n1))
4511 return n1;
4512 else
4513 SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
d28da049 4514 }
09fb7599 4515
e11e83f3 4516 if (SCM_I_INUMP (n1))
0aacf84e 4517 {
e11e83f3
MV
4518 nn1 = SCM_I_INUM (n1);
4519 if (SCM_I_INUMP (n2))
0aacf84e 4520 {
e25f3727 4521 scm_t_inum nn2 = SCM_I_INUM (n2);
d956fa6f 4522 return SCM_I_MAKINUM (nn1 ^ nn2);
0aacf84e
MD
4523 }
4524 else if (SCM_BIGP (n2))
4525 {
4526 intbig:
4527 {
4528 SCM result_z = scm_i_mkbig ();
4529 mpz_t nn1_z;
4530 mpz_init_set_si (nn1_z, nn1);
4531 mpz_xor (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
4532 scm_remember_upto_here_1 (n2);
4533 mpz_clear (nn1_z);
4534 return scm_i_normbig (result_z);
4535 }
4536 }
4537 else
4538 SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
4539 }
4540 else if (SCM_BIGP (n1))
4541 {
e11e83f3 4542 if (SCM_I_INUMP (n2))
0aacf84e
MD
4543 {
4544 SCM_SWAP (n1, n2);
e11e83f3 4545 nn1 = SCM_I_INUM (n1);
0aacf84e
MD
4546 goto intbig;
4547 }
4548 else if (SCM_BIGP (n2))
4549 {
4550 SCM result_z = scm_i_mkbig ();
4551 mpz_xor (SCM_I_BIG_MPZ (result_z),
4552 SCM_I_BIG_MPZ (n1),
4553 SCM_I_BIG_MPZ (n2));
4554 scm_remember_upto_here_2 (n1, n2);
4555 return scm_i_normbig (result_z);
4556 }
4557 else
4558 SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
09fb7599 4559 }
0aacf84e 4560 else
09fb7599 4561 SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
0f2d19dd 4562}
1bbd0b84 4563#undef FUNC_NAME
0f2d19dd 4564
09fb7599 4565
a1ec6916 4566SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0,
1e6808ea 4567 (SCM j, SCM k),
ba6e7231
KR
4568 "Test whether @var{j} and @var{k} have any 1 bits in common.\n"
4569 "This is equivalent to @code{(not (zero? (logand j k)))}, but\n"
4570 "without actually calculating the @code{logand}, just testing\n"
4571 "for non-zero.\n"
4572 "\n"
1e6808ea 4573 "@lisp\n"
b380b885
MD
4574 "(logtest #b0100 #b1011) @result{} #f\n"
4575 "(logtest #b0100 #b0111) @result{} #t\n"
1e6808ea 4576 "@end lisp")
1bbd0b84 4577#define FUNC_NAME s_scm_logtest
0f2d19dd 4578{
e25f3727 4579 scm_t_inum nj;
9a00c9fc 4580
e11e83f3 4581 if (SCM_I_INUMP (j))
0aacf84e 4582 {
e11e83f3
MV
4583 nj = SCM_I_INUM (j);
4584 if (SCM_I_INUMP (k))
0aacf84e 4585 {
e25f3727 4586 scm_t_inum nk = SCM_I_INUM (k);
73e4de09 4587 return scm_from_bool (nj & nk);
0aacf84e
MD
4588 }
4589 else if (SCM_BIGP (k))
4590 {
4591 intbig:
4592 if (nj == 0)
4593 return SCM_BOOL_F;
4594 {
4595 SCM result;
4596 mpz_t nj_z;
4597 mpz_init_set_si (nj_z, nj);
4598 mpz_and (nj_z, nj_z, SCM_I_BIG_MPZ (k));
4599 scm_remember_upto_here_1 (k);
73e4de09 4600 result = scm_from_bool (mpz_sgn (nj_z) != 0);
0aacf84e
MD
4601 mpz_clear (nj_z);
4602 return result;
4603 }
4604 }
4605 else
4606 SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
4607 }
4608 else if (SCM_BIGP (j))
4609 {
e11e83f3 4610 if (SCM_I_INUMP (k))
0aacf84e
MD
4611 {
4612 SCM_SWAP (j, k);
e11e83f3 4613 nj = SCM_I_INUM (j);
0aacf84e
MD
4614 goto intbig;
4615 }
4616 else if (SCM_BIGP (k))
4617 {
4618 SCM result;
4619 mpz_t result_z;
4620 mpz_init (result_z);
4621 mpz_and (result_z,
4622 SCM_I_BIG_MPZ (j),
4623 SCM_I_BIG_MPZ (k));
4624 scm_remember_upto_here_2 (j, k);
73e4de09 4625 result = scm_from_bool (mpz_sgn (result_z) != 0);
0aacf84e
MD
4626 mpz_clear (result_z);
4627 return result;
4628 }
4629 else
4630 SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
4631 }
4632 else
4633 SCM_WRONG_TYPE_ARG (SCM_ARG1, j);
0f2d19dd 4634}
1bbd0b84 4635#undef FUNC_NAME
0f2d19dd 4636
c1bfcf60 4637
a1ec6916 4638SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
2cd04b42 4639 (SCM index, SCM j),
ba6e7231
KR
4640 "Test whether bit number @var{index} in @var{j} is set.\n"
4641 "@var{index} starts from 0 for the least significant bit.\n"
4642 "\n"
1e6808ea 4643 "@lisp\n"
b380b885
MD
4644 "(logbit? 0 #b1101) @result{} #t\n"
4645 "(logbit? 1 #b1101) @result{} #f\n"
4646 "(logbit? 2 #b1101) @result{} #t\n"
4647 "(logbit? 3 #b1101) @result{} #t\n"
4648 "(logbit? 4 #b1101) @result{} #f\n"
1e6808ea 4649 "@end lisp")
1bbd0b84 4650#define FUNC_NAME s_scm_logbit_p
0f2d19dd 4651{
78166ad5 4652 unsigned long int iindex;
5efd3c7d 4653 iindex = scm_to_ulong (index);
78166ad5 4654
e11e83f3 4655 if (SCM_I_INUMP (j))
0d75f6d8
KR
4656 {
4657 /* bits above what's in an inum follow the sign bit */
20fcc8ed 4658 iindex = min (iindex, SCM_LONG_BIT - 1);
e11e83f3 4659 return scm_from_bool ((1L << iindex) & SCM_I_INUM (j));
0d75f6d8 4660 }
0aacf84e
MD
4661 else if (SCM_BIGP (j))
4662 {
4663 int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex);
4664 scm_remember_upto_here_1 (j);
73e4de09 4665 return scm_from_bool (val);
0aacf84e
MD
4666 }
4667 else
78166ad5 4668 SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
0f2d19dd 4669}
1bbd0b84 4670#undef FUNC_NAME
0f2d19dd 4671
78166ad5 4672
a1ec6916 4673SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
1bbd0b84 4674 (SCM n),
4d814788 4675 "Return the integer which is the ones-complement of the integer\n"
1e6808ea
MG
4676 "argument.\n"
4677 "\n"
b380b885
MD
4678 "@lisp\n"
4679 "(number->string (lognot #b10000000) 2)\n"
4680 " @result{} \"-10000001\"\n"
4681 "(number->string (lognot #b0) 2)\n"
4682 " @result{} \"-1\"\n"
1e6808ea 4683 "@end lisp")
1bbd0b84 4684#define FUNC_NAME s_scm_lognot
0f2d19dd 4685{
e11e83f3 4686 if (SCM_I_INUMP (n)) {
f9811f9f
KR
4687 /* No overflow here, just need to toggle all the bits making up the inum.
4688 Enhancement: No need to strip the tag and add it back, could just xor
4689 a block of 1 bits, if that worked with the various debug versions of
4690 the SCM typedef. */
e11e83f3 4691 return SCM_I_MAKINUM (~ SCM_I_INUM (n));
f9811f9f
KR
4692
4693 } else if (SCM_BIGP (n)) {
4694 SCM result = scm_i_mkbig ();
4695 mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n));
4696 scm_remember_upto_here_1 (n);
4697 return result;
4698
4699 } else {
4700 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
4701 }
0f2d19dd 4702}
1bbd0b84 4703#undef FUNC_NAME
0f2d19dd 4704
518b7508
KR
4705/* returns 0 if IN is not an integer. OUT must already be
4706 initialized. */
4707static int
4708coerce_to_big (SCM in, mpz_t out)
4709{
4710 if (SCM_BIGP (in))
4711 mpz_set (out, SCM_I_BIG_MPZ (in));
e11e83f3
MV
4712 else if (SCM_I_INUMP (in))
4713 mpz_set_si (out, SCM_I_INUM (in));
518b7508
KR
4714 else
4715 return 0;
4716
4717 return 1;
4718}
4719
d885e204 4720SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0,
518b7508
KR
4721 (SCM n, SCM k, SCM m),
4722 "Return @var{n} raised to the integer exponent\n"
4723 "@var{k}, modulo @var{m}.\n"
4724 "\n"
4725 "@lisp\n"
4726 "(modulo-expt 2 3 5)\n"
4727 " @result{} 3\n"
4728 "@end lisp")
d885e204 4729#define FUNC_NAME s_scm_modulo_expt
518b7508
KR
4730{
4731 mpz_t n_tmp;
4732 mpz_t k_tmp;
4733 mpz_t m_tmp;
4734
4735 /* There are two classes of error we might encounter --
4736 1) Math errors, which we'll report by calling scm_num_overflow,
4737 and
4738 2) wrong-type errors, which of course we'll report by calling
4739 SCM_WRONG_TYPE_ARG.
4740 We don't report those errors immediately, however; instead we do
4741 some cleanup first. These variables tell us which error (if
4742 any) we should report after cleaning up.
4743 */
4744 int report_overflow = 0;
4745
4746 int position_of_wrong_type = 0;
4747 SCM value_of_wrong_type = SCM_INUM0;
4748
4749 SCM result = SCM_UNDEFINED;
4750
4751 mpz_init (n_tmp);
4752 mpz_init (k_tmp);
4753 mpz_init (m_tmp);
4754
bc36d050 4755 if (scm_is_eq (m, SCM_INUM0))
518b7508
KR
4756 {
4757 report_overflow = 1;
4758 goto cleanup;
4759 }
4760
4761 if (!coerce_to_big (n, n_tmp))
4762 {
4763 value_of_wrong_type = n;
4764 position_of_wrong_type = 1;
4765 goto cleanup;
4766 }
4767
4768 if (!coerce_to_big (k, k_tmp))
4769 {
4770 value_of_wrong_type = k;
4771 position_of_wrong_type = 2;
4772 goto cleanup;
4773 }
4774
4775 if (!coerce_to_big (m, m_tmp))
4776 {
4777 value_of_wrong_type = m;
4778 position_of_wrong_type = 3;
4779 goto cleanup;
4780 }
4781
4782 /* if the exponent K is negative, and we simply call mpz_powm, we
4783 will get a divide-by-zero exception when an inverse 1/n mod m
4784 doesn't exist (or is not unique). Since exceptions are hard to
4785 handle, we'll attempt the inversion "by hand" -- that way, we get
4786 a simple failure code, which is easy to handle. */
4787
4788 if (-1 == mpz_sgn (k_tmp))
4789 {
4790 if (!mpz_invert (n_tmp, n_tmp, m_tmp))
4791 {
4792 report_overflow = 1;
4793 goto cleanup;
4794 }
4795 mpz_neg (k_tmp, k_tmp);
4796 }
4797
4798 result = scm_i_mkbig ();
4799 mpz_powm (SCM_I_BIG_MPZ (result),
4800 n_tmp,
4801 k_tmp,
4802 m_tmp);
b7b8c575
KR
4803
4804 if (mpz_sgn (m_tmp) < 0 && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)
4805 mpz_add (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), m_tmp);
4806
518b7508
KR
4807 cleanup:
4808 mpz_clear (m_tmp);
4809 mpz_clear (k_tmp);
4810 mpz_clear (n_tmp);
4811
4812 if (report_overflow)
4813 scm_num_overflow (FUNC_NAME);
4814
4815 if (position_of_wrong_type)
4816 SCM_WRONG_TYPE_ARG (position_of_wrong_type,
4817 value_of_wrong_type);
4818
4819 return scm_i_normbig (result);
4820}
4821#undef FUNC_NAME
4822
a1ec6916 4823SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
2cd04b42 4824 (SCM n, SCM k),
ba6e7231
KR
4825 "Return @var{n} raised to the power @var{k}. @var{k} must be an\n"
4826 "exact integer, @var{n} can be any number.\n"
4827 "\n"
2519490c
MW
4828 "Negative @var{k} is supported, and results in\n"
4829 "@math{1/@var{n}^abs(@var{k})} in the usual way.\n"
4830 "@math{@var{n}^0} is 1, as usual, and that\n"
ba6e7231 4831 "includes @math{0^0} is 1.\n"
1e6808ea 4832 "\n"
b380b885 4833 "@lisp\n"
ba6e7231
KR
4834 "(integer-expt 2 5) @result{} 32\n"
4835 "(integer-expt -3 3) @result{} -27\n"
4836 "(integer-expt 5 -3) @result{} 1/125\n"
4837 "(integer-expt 0 0) @result{} 1\n"
b380b885 4838 "@end lisp")
1bbd0b84 4839#define FUNC_NAME s_scm_integer_expt
0f2d19dd 4840{
e25f3727 4841 scm_t_inum i2 = 0;
1c35cb19
RB
4842 SCM z_i2 = SCM_BOOL_F;
4843 int i2_is_big = 0;
d956fa6f 4844 SCM acc = SCM_I_MAKINUM (1L);
ca46fb90 4845
bfe1f03a
MW
4846 /* Specifically refrain from checking the type of the first argument.
4847 This allows us to exponentiate any object that can be multiplied.
4848 If we must raise to a negative power, we must also be able to
4849 take its reciprocal. */
4850 if (!SCM_LIKELY (SCM_I_INUMP (k)) && !SCM_LIKELY (SCM_BIGP (k)))
01c7284a 4851 SCM_WRONG_TYPE_ARG (2, k);
5a8fc758 4852
bfe1f03a
MW
4853 if (SCM_UNLIKELY (scm_is_eq (k, SCM_INUM0)))
4854 return SCM_INUM1; /* n^(exact0) is exact 1, regardless of n */
4855 else if (SCM_UNLIKELY (scm_is_eq (n, SCM_I_MAKINUM (-1L))))
4856 return scm_is_false (scm_even_p (k)) ? n : SCM_INUM1;
4857 /* The next check is necessary only because R6RS specifies different
4858 behavior for 0^(-k) than for (/ 0). If n is not a scheme number,
4859 we simply skip this case and move on. */
4860 else if (SCM_NUMBERP (n) && scm_is_true (scm_zero_p (n)))
4861 {
4862 /* k cannot be 0 at this point, because we
4863 have already checked for that case above */
4864 if (scm_is_true (scm_positive_p (k)))
01c7284a
MW
4865 return n;
4866 else /* return NaN for (0 ^ k) for negative k per R6RS */
4867 return scm_nan ();
4868 }
a285b18c
MW
4869 else if (SCM_FRACTIONP (n))
4870 {
4871 /* Optimize the fraction case by (a/b)^k ==> (a^k)/(b^k), to avoid
4872 needless reduction of intermediate products to lowest terms.
4873 If a and b have no common factors, then a^k and b^k have no
4874 common factors. Use 'scm_i_make_ratio_already_reduced' to
4875 construct the final result, so that no gcd computations are
4876 needed to exponentiate a fraction. */
4877 if (scm_is_true (scm_positive_p (k)))
4878 return scm_i_make_ratio_already_reduced
4879 (scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k),
4880 scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k));
4881 else
4882 {
4883 k = scm_difference (k, SCM_UNDEFINED);
4884 return scm_i_make_ratio_already_reduced
4885 (scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k),
4886 scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k));
4887 }
4888 }
ca46fb90 4889
e11e83f3
MV
4890 if (SCM_I_INUMP (k))
4891 i2 = SCM_I_INUM (k);
ca46fb90
RB
4892 else if (SCM_BIGP (k))
4893 {
4894 z_i2 = scm_i_clonebig (k, 1);
ca46fb90
RB
4895 scm_remember_upto_here_1 (k);
4896 i2_is_big = 1;
4897 }
2830fd91 4898 else
ca46fb90
RB
4899 SCM_WRONG_TYPE_ARG (2, k);
4900
4901 if (i2_is_big)
f872b822 4902 {
ca46fb90
RB
4903 if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == -1)
4904 {
4905 mpz_neg (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2));
4906 n = scm_divide (n, SCM_UNDEFINED);
4907 }
4908 while (1)
4909 {
4910 if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == 0)
4911 {
ca46fb90
RB
4912 return acc;
4913 }
4914 if (mpz_cmp_ui(SCM_I_BIG_MPZ (z_i2), 1) == 0)
4915 {
ca46fb90
RB
4916 return scm_product (acc, n);
4917 }
4918 if (mpz_tstbit(SCM_I_BIG_MPZ (z_i2), 0))
4919 acc = scm_product (acc, n);
4920 n = scm_product (n, n);
4921 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2), 1);
4922 }
f872b822 4923 }
ca46fb90 4924 else
f872b822 4925 {
ca46fb90
RB
4926 if (i2 < 0)
4927 {
4928 i2 = -i2;
4929 n = scm_divide (n, SCM_UNDEFINED);
4930 }
4931 while (1)
4932 {
4933 if (0 == i2)
4934 return acc;
4935 if (1 == i2)
4936 return scm_product (acc, n);
4937 if (i2 & 1)
4938 acc = scm_product (acc, n);
4939 n = scm_product (n, n);
4940 i2 >>= 1;
4941 }
f872b822 4942 }
0f2d19dd 4943}
1bbd0b84 4944#undef FUNC_NAME
0f2d19dd 4945
e08a12b5
MW
4946/* Efficiently compute (N * 2^COUNT),
4947 where N is an exact integer, and COUNT > 0. */
4948static SCM
4949left_shift_exact_integer (SCM n, long count)
4950{
4951 if (SCM_I_INUMP (n))
4952 {
4953 scm_t_inum nn = SCM_I_INUM (n);
4954
4955 /* Left shift of count >= SCM_I_FIXNUM_BIT-1 will always
4956 overflow a non-zero fixnum. For smaller shifts we check the
4957 bits going into positions above SCM_I_FIXNUM_BIT-1. If they're
4958 all 0s for nn>=0, or all 1s for nn<0 then there's no overflow.
4959 Those bits are "nn >> (SCM_I_FIXNUM_BIT-1 - count)". */
4960
4961 if (nn == 0)
4962 return n;
4963 else if (count < SCM_I_FIXNUM_BIT-1 &&
4964 ((scm_t_bits) (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - count)) + 1)
4965 <= 1))
4966 return SCM_I_MAKINUM (nn << count);
4967 else
4968 {
4969 SCM result = scm_i_inum2big (nn);
4970 mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
4971 count);
4972 return result;
4973 }
4974 }
4975 else if (SCM_BIGP (n))
4976 {
4977 SCM result = scm_i_mkbig ();
4978 mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n), count);
4979 scm_remember_upto_here_1 (n);
4980 return result;
4981 }
4982 else
4983 scm_syserror ("left_shift_exact_integer");
4984}
4985
4986/* Efficiently compute floor (N / 2^COUNT),
4987 where N is an exact integer and COUNT > 0. */
4988static SCM
4989floor_right_shift_exact_integer (SCM n, long count)
4990{
4991 if (SCM_I_INUMP (n))
4992 {
4993 scm_t_inum nn = SCM_I_INUM (n);
4994
4995 if (count >= SCM_I_FIXNUM_BIT)
4996 return (nn >= 0 ? SCM_INUM0 : SCM_I_MAKINUM (-1));
4997 else
4998 return SCM_I_MAKINUM (SCM_SRS (nn, count));
4999 }
5000 else if (SCM_BIGP (n))
5001 {
5002 SCM result = scm_i_mkbig ();
5003 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
5004 count);
5005 scm_remember_upto_here_1 (n);
5006 return scm_i_normbig (result);
5007 }
5008 else
5009 scm_syserror ("floor_right_shift_exact_integer");
5010}
5011
5012/* Efficiently compute round (N / 2^COUNT),
5013 where N is an exact integer and COUNT > 0. */
5014static SCM
5015round_right_shift_exact_integer (SCM n, long count)
5016{
5017 if (SCM_I_INUMP (n))
5018 {
5019 if (count >= SCM_I_FIXNUM_BIT)
5020 return SCM_INUM0;
5021 else
5022 {
5023 scm_t_inum nn = SCM_I_INUM (n);
5024 scm_t_inum qq = SCM_SRS (nn, count);
5025
5026 if (0 == (nn & (1L << (count-1))))
5027 return SCM_I_MAKINUM (qq); /* round down */
5028 else if (nn & ((1L << (count-1)) - 1))
5029 return SCM_I_MAKINUM (qq + 1); /* round up */
5030 else
5031 return SCM_I_MAKINUM ((~1L) & (qq + 1)); /* round to even */
5032 }
5033 }
5034 else if (SCM_BIGP (n))
5035 {
5036 SCM q = scm_i_mkbig ();
5037
5038 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), count);
5039 if (mpz_tstbit (SCM_I_BIG_MPZ (n), count-1)
5040 && (mpz_odd_p (SCM_I_BIG_MPZ (q))
5041 || (mpz_scan1 (SCM_I_BIG_MPZ (n), 0) < count-1)))
5042 mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
5043 scm_remember_upto_here_1 (n);
5044 return scm_i_normbig (q);
5045 }
5046 else
5047 scm_syserror ("round_right_shift_exact_integer");
5048}
5049
a1ec6916 5050SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
e08a12b5
MW
5051 (SCM n, SCM count),
5052 "Return @math{floor(@var{n} * 2^@var{count})}.\n"
5053 "@var{n} and @var{count} must be exact integers.\n"
1e6808ea 5054 "\n"
e08a12b5
MW
5055 "With @var{n} viewed as an infinite-precision twos-complement\n"
5056 "integer, @code{ash} means a left shift introducing zero bits\n"
5057 "when @var{count} is positive, or a right shift dropping bits\n"
5058 "when @var{count} is negative. This is an ``arithmetic'' shift.\n"
1e6808ea 5059 "\n"
b380b885 5060 "@lisp\n"
1e6808ea
MG
5061 "(number->string (ash #b1 3) 2) @result{} \"1000\"\n"
5062 "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n"
32f19569
KR
5063 "\n"
5064 ";; -23 is bits ...11101001, -6 is bits ...111010\n"
5065 "(ash -23 -2) @result{} -6\n"
a3c8b9fc 5066 "@end lisp")
1bbd0b84 5067#define FUNC_NAME s_scm_ash
0f2d19dd 5068{
e08a12b5 5069 if (SCM_I_INUMP (n) || SCM_BIGP (n))
788aca27 5070 {
e08a12b5 5071 long bits_to_shift = scm_to_long (count);
788aca27
KR
5072
5073 if (bits_to_shift > 0)
e08a12b5
MW
5074 return left_shift_exact_integer (n, bits_to_shift);
5075 else if (SCM_LIKELY (bits_to_shift < 0))
5076 return floor_right_shift_exact_integer (n, -bits_to_shift);
788aca27 5077 else
e08a12b5 5078 return n;
788aca27 5079 }
e08a12b5
MW
5080 else
5081 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
5082}
5083#undef FUNC_NAME
788aca27 5084
e08a12b5
MW
5085SCM_DEFINE (scm_round_ash, "round-ash", 2, 0, 0,
5086 (SCM n, SCM count),
5087 "Return @math{round(@var{n} * 2^@var{count})}.\n"
5088 "@var{n} and @var{count} must be exact integers.\n"
5089 "\n"
5090 "With @var{n} viewed as an infinite-precision twos-complement\n"
5091 "integer, @code{round-ash} means a left shift introducing zero\n"
5092 "bits when @var{count} is positive, or a right shift rounding\n"
5093 "to the nearest integer (with ties going to the nearest even\n"
5094 "integer) when @var{count} is negative. This is a rounded\n"
5095 "``arithmetic'' shift.\n"
5096 "\n"
5097 "@lisp\n"
5098 "(number->string (round-ash #b1 3) 2) @result{} \"1000\"\n"
5099 "(number->string (round-ash #b1010 -1) 2) @result{} \"101\"\n"
5100 "(number->string (round-ash #b1010 -2) 2) @result{} \"10\"\n"
5101 "(number->string (round-ash #b1011 -2) 2) @result{} \"11\"\n"
5102 "(number->string (round-ash #b1101 -2) 2) @result{} \"11\"\n"
5103 "(number->string (round-ash #b1110 -2) 2) @result{} \"100\"\n"
5104 "@end lisp")
5105#define FUNC_NAME s_scm_round_ash
5106{
5107 if (SCM_I_INUMP (n) || SCM_BIGP (n))
5108 {
5109 long bits_to_shift = scm_to_long (count);
788aca27 5110
e08a12b5
MW
5111 if (bits_to_shift > 0)
5112 return left_shift_exact_integer (n, bits_to_shift);
5113 else if (SCM_LIKELY (bits_to_shift < 0))
5114 return round_right_shift_exact_integer (n, -bits_to_shift);
ca46fb90 5115 else
e08a12b5 5116 return n;
ca46fb90
RB
5117 }
5118 else
e08a12b5 5119 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
0f2d19dd 5120}
1bbd0b84 5121#undef FUNC_NAME
0f2d19dd 5122
3c9f20f8 5123
a1ec6916 5124SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
1bbd0b84 5125 (SCM n, SCM start, SCM end),
1e6808ea
MG
5126 "Return the integer composed of the @var{start} (inclusive)\n"
5127 "through @var{end} (exclusive) bits of @var{n}. The\n"
5128 "@var{start}th bit becomes the 0-th bit in the result.\n"
5129 "\n"
b380b885
MD
5130 "@lisp\n"
5131 "(number->string (bit-extract #b1101101010 0 4) 2)\n"
5132 " @result{} \"1010\"\n"
5133 "(number->string (bit-extract #b1101101010 4 9) 2)\n"
5134 " @result{} \"10110\"\n"
5135 "@end lisp")
1bbd0b84 5136#define FUNC_NAME s_scm_bit_extract
0f2d19dd 5137{
7f848242 5138 unsigned long int istart, iend, bits;
5efd3c7d
MV
5139 istart = scm_to_ulong (start);
5140 iend = scm_to_ulong (end);
c1bfcf60 5141 SCM_ASSERT_RANGE (3, end, (iend >= istart));
78166ad5 5142
7f848242
KR
5143 /* how many bits to keep */
5144 bits = iend - istart;
5145
e11e83f3 5146 if (SCM_I_INUMP (n))
0aacf84e 5147 {
e25f3727 5148 scm_t_inum in = SCM_I_INUM (n);
7f848242
KR
5149
5150 /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
d77ad560 5151 SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */
857ae6af 5152 in = SCM_SRS (in, min (istart, SCM_I_FIXNUM_BIT-1));
ac0c002c 5153
0aacf84e
MD
5154 if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
5155 {
5156 /* Since we emulate two's complement encoded numbers, this
5157 * special case requires us to produce a result that has
7f848242 5158 * more bits than can be stored in a fixnum.
0aacf84e 5159 */
e25f3727 5160 SCM result = scm_i_inum2big (in);
7f848242
KR
5161 mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
5162 bits);
5163 return result;
0aacf84e 5164 }
ac0c002c 5165
7f848242 5166 /* mask down to requisite bits */
857ae6af 5167 bits = min (bits, SCM_I_FIXNUM_BIT);
d956fa6f 5168 return SCM_I_MAKINUM (in & ((1L << bits) - 1));
0aacf84e
MD
5169 }
5170 else if (SCM_BIGP (n))
ac0c002c 5171 {
7f848242
KR
5172 SCM result;
5173 if (bits == 1)
5174 {
d956fa6f 5175 result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart));
7f848242
KR
5176 }
5177 else
5178 {
5179 /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
5180 bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
5181 such bits into a ulong. */
5182 result = scm_i_mkbig ();
5183 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart);
5184 mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits);
5185 result = scm_i_normbig (result);
5186 }
5187 scm_remember_upto_here_1 (n);
5188 return result;
ac0c002c 5189 }
0aacf84e 5190 else
78166ad5 5191 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
0f2d19dd 5192}
1bbd0b84 5193#undef FUNC_NAME
0f2d19dd 5194
7f848242 5195
e4755e5c
JB
5196static const char scm_logtab[] = {
5197 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
5198};
1cc91f1b 5199
a1ec6916 5200SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
1bbd0b84 5201 (SCM n),
1e6808ea
MG
5202 "Return the number of bits in integer @var{n}. If integer is\n"
5203 "positive, the 1-bits in its binary representation are counted.\n"
5204 "If negative, the 0-bits in its two's-complement binary\n"
5205 "representation are counted. If 0, 0 is returned.\n"
5206 "\n"
b380b885
MD
5207 "@lisp\n"
5208 "(logcount #b10101010)\n"
ca46fb90
RB
5209 " @result{} 4\n"
5210 "(logcount 0)\n"
5211 " @result{} 0\n"
5212 "(logcount -2)\n"
5213 " @result{} 1\n"
5214 "@end lisp")
5215#define FUNC_NAME s_scm_logcount
5216{
e11e83f3 5217 if (SCM_I_INUMP (n))
f872b822 5218 {
e25f3727
AW
5219 unsigned long c = 0;
5220 scm_t_inum nn = SCM_I_INUM (n);
ca46fb90
RB
5221 if (nn < 0)
5222 nn = -1 - nn;
5223 while (nn)
5224 {
5225 c += scm_logtab[15 & nn];
5226 nn >>= 4;
5227 }
d956fa6f 5228 return SCM_I_MAKINUM (c);
f872b822 5229 }
ca46fb90 5230 else if (SCM_BIGP (n))
f872b822 5231 {
ca46fb90 5232 unsigned long count;
713a4259
KR
5233 if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0)
5234 count = mpz_popcount (SCM_I_BIG_MPZ (n));
ca46fb90 5235 else
713a4259
KR
5236 count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one);
5237 scm_remember_upto_here_1 (n);
d956fa6f 5238 return SCM_I_MAKINUM (count);
f872b822 5239 }
ca46fb90
RB
5240 else
5241 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
0f2d19dd 5242}
ca46fb90 5243#undef FUNC_NAME
0f2d19dd
JB
5244
5245
ca46fb90
RB
5246static const char scm_ilentab[] = {
5247 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
5248};
5249
0f2d19dd 5250
ca46fb90
RB
5251SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
5252 (SCM n),
5253 "Return the number of bits necessary to represent @var{n}.\n"
5254 "\n"
5255 "@lisp\n"
5256 "(integer-length #b10101010)\n"
5257 " @result{} 8\n"
5258 "(integer-length 0)\n"
5259 " @result{} 0\n"
5260 "(integer-length #b1111)\n"
5261 " @result{} 4\n"
5262 "@end lisp")
5263#define FUNC_NAME s_scm_integer_length
5264{
e11e83f3 5265 if (SCM_I_INUMP (n))
0aacf84e 5266 {
e25f3727 5267 unsigned long c = 0;
0aacf84e 5268 unsigned int l = 4;
e25f3727 5269 scm_t_inum nn = SCM_I_INUM (n);
0aacf84e
MD
5270 if (nn < 0)
5271 nn = -1 - nn;
5272 while (nn)
5273 {
5274 c += 4;
5275 l = scm_ilentab [15 & nn];
5276 nn >>= 4;
5277 }
d956fa6f 5278 return SCM_I_MAKINUM (c - 4 + l);
0aacf84e
MD
5279 }
5280 else if (SCM_BIGP (n))
5281 {
5282 /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
5283 want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
5284 1 too big, so check for that and adjust. */
5285 size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
5286 if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
5287 && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
5288 mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
5289 size--;
5290 scm_remember_upto_here_1 (n);
d956fa6f 5291 return SCM_I_MAKINUM (size);
0aacf84e
MD
5292 }
5293 else
ca46fb90 5294 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
ca46fb90
RB
5295}
5296#undef FUNC_NAME
0f2d19dd
JB
5297
5298/*** NUMBERS -> STRINGS ***/
0b799eea
MV
5299#define SCM_MAX_DBL_RADIX 36
5300
0b799eea 5301/* use this array as a way to generate a single digit */
9b5fcde6 5302static const char number_chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
0f2d19dd 5303
1ea37620
MW
5304static mpz_t dbl_minimum_normal_mantissa;
5305
1be6b49c 5306static size_t
1ea37620 5307idbl2str (double dbl, char *a, int radix)
0f2d19dd 5308{
1ea37620 5309 int ch = 0;
0b799eea 5310
1ea37620
MW
5311 if (radix < 2 || radix > SCM_MAX_DBL_RADIX)
5312 /* revert to existing behavior */
5313 radix = 10;
0f2d19dd 5314
1ea37620 5315 if (isinf (dbl))
abb7e44d 5316 {
1ea37620
MW
5317 strcpy (a, (dbl > 0.0) ? "+inf.0" : "-inf.0");
5318 return 6;
abb7e44d 5319 }
1ea37620
MW
5320 else if (dbl > 0.0)
5321 ;
5322 else if (dbl < 0.0)
7351e207 5323 {
1ea37620
MW
5324 dbl = -dbl;
5325 a[ch++] = '-';
7351e207 5326 }
1ea37620 5327 else if (dbl == 0.0)
7351e207 5328 {
1ea37620
MW
5329 if (!double_is_non_negative_zero (dbl))
5330 a[ch++] = '-';
5331 strcpy (a + ch, "0.0");
5332 return ch + 3;
7351e207 5333 }
1ea37620 5334 else if (isnan (dbl))
f872b822 5335 {
1ea37620
MW
5336 strcpy (a, "+nan.0");
5337 return 6;
f872b822 5338 }
7351e207 5339
1ea37620
MW
5340 /* Algorithm taken from "Printing Floating-Point Numbers Quickly and
5341 Accurately" by Robert G. Burger and R. Kent Dybvig */
5342 {
5343 int e, k;
5344 mpz_t f, r, s, mplus, mminus, hi, digit;
5345 int f_is_even, f_is_odd;
8150dfa1 5346 int expon;
1ea37620
MW
5347 int show_exp = 0;
5348
5349 mpz_inits (f, r, s, mplus, mminus, hi, digit, NULL);
5350 mpz_set_d (f, ldexp (frexp (dbl, &e), DBL_MANT_DIG));
5351 if (e < DBL_MIN_EXP)
5352 {
5353 mpz_tdiv_q_2exp (f, f, DBL_MIN_EXP - e);
5354 e = DBL_MIN_EXP;
5355 }
5356 e -= DBL_MANT_DIG;
0b799eea 5357
1ea37620
MW
5358 f_is_even = !mpz_odd_p (f);
5359 f_is_odd = !f_is_even;
0b799eea 5360
1ea37620
MW
5361 /* Initialize r, s, mplus, and mminus according
5362 to Table 1 from the paper. */
5363 if (e < 0)
5364 {
5365 mpz_set_ui (mminus, 1);
5366 if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0
5367 || e == DBL_MIN_EXP - DBL_MANT_DIG)
5368 {
5369 mpz_set_ui (mplus, 1);
5370 mpz_mul_2exp (r, f, 1);
5371 mpz_mul_2exp (s, mminus, 1 - e);
5372 }
5373 else
5374 {
5375 mpz_set_ui (mplus, 2);
5376 mpz_mul_2exp (r, f, 2);
5377 mpz_mul_2exp (s, mminus, 2 - e);
5378 }
5379 }
5380 else
5381 {
5382 mpz_set_ui (mminus, 1);
5383 mpz_mul_2exp (mminus, mminus, e);
5384 if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0)
5385 {
5386 mpz_set (mplus, mminus);
5387 mpz_mul_2exp (r, f, 1 + e);
5388 mpz_set_ui (s, 2);
5389 }
5390 else
5391 {
5392 mpz_mul_2exp (mplus, mminus, 1);
5393 mpz_mul_2exp (r, f, 2 + e);
5394 mpz_set_ui (s, 4);
5395 }
5396 }
0b799eea 5397
1ea37620
MW
5398 /* Find the smallest k such that:
5399 (r + mplus) / s < radix^k (if f is even)
5400 (r + mplus) / s <= radix^k (if f is odd) */
f872b822 5401 {
1ea37620
MW
5402 /* IMPROVE-ME: Make an initial guess to speed this up */
5403 mpz_add (hi, r, mplus);
5404 k = 0;
5405 while (mpz_cmp (hi, s) >= f_is_odd)
5406 {
5407 mpz_mul_ui (s, s, radix);
5408 k++;
5409 }
5410 if (k == 0)
5411 {
5412 mpz_mul_ui (hi, hi, radix);
5413 while (mpz_cmp (hi, s) < f_is_odd)
5414 {
5415 mpz_mul_ui (r, r, radix);
5416 mpz_mul_ui (mplus, mplus, radix);
5417 mpz_mul_ui (mminus, mminus, radix);
5418 mpz_mul_ui (hi, hi, radix);
5419 k--;
5420 }
5421 }
cda139a7 5422 }
f872b822 5423
8150dfa1
MW
5424 expon = k - 1;
5425 if (k <= 0)
1ea37620 5426 {
8150dfa1
MW
5427 if (k <= -3)
5428 {
5429 /* Use scientific notation */
5430 show_exp = 1;
5431 k = 1;
5432 }
5433 else
5434 {
5435 int i;
0f2d19dd 5436
8150dfa1
MW
5437 /* Print leading zeroes */
5438 a[ch++] = '0';
5439 a[ch++] = '.';
5440 for (i = 0; i > k; i--)
5441 a[ch++] = '0';
5442 }
1ea37620
MW
5443 }
5444
5445 for (;;)
5446 {
5447 int end_1_p, end_2_p;
5448 int d;
5449
5450 mpz_mul_ui (mplus, mplus, radix);
5451 mpz_mul_ui (mminus, mminus, radix);
5452 mpz_mul_ui (r, r, radix);
5453 mpz_fdiv_qr (digit, r, r, s);
5454 d = mpz_get_ui (digit);
5455
5456 mpz_add (hi, r, mplus);
5457 end_1_p = (mpz_cmp (r, mminus) < f_is_even);
5458 end_2_p = (mpz_cmp (s, hi) < f_is_even);
5459 if (end_1_p || end_2_p)
5460 {
5461 mpz_mul_2exp (r, r, 1);
5462 if (!end_2_p)
5463 ;
5464 else if (!end_1_p)
5465 d++;
5466 else if (mpz_cmp (r, s) >= !(d & 1))
5467 d++;
5468 a[ch++] = number_chars[d];
5469 if (--k == 0)
5470 a[ch++] = '.';
5471 break;
5472 }
5473 else
5474 {
5475 a[ch++] = number_chars[d];
5476 if (--k == 0)
5477 a[ch++] = '.';
5478 }
5479 }
5480
5481 if (k > 0)
5482 {
8150dfa1
MW
5483 if (expon >= 7 && k >= 4 && expon >= k)
5484 {
5485 /* Here we would have to print more than three zeroes
5486 followed by a decimal point and another zero. It
5487 makes more sense to use scientific notation. */
5488
5489 /* Adjust k to what it would have been if we had chosen
5490 scientific notation from the beginning. */
5491 k -= expon;
5492
5493 /* k will now be <= 0, with magnitude equal to the number of
5494 digits that we printed which should now be put after the
5495 decimal point. */
5496
5497 /* Insert a decimal point */
5498 memmove (a + ch + k + 1, a + ch + k, -k);
5499 a[ch + k] = '.';
5500 ch++;
5501
5502 show_exp = 1;
5503 }
5504 else
5505 {
5506 for (; k > 0; k--)
5507 a[ch++] = '0';
5508 a[ch++] = '.';
5509 }
1ea37620
MW
5510 }
5511
5512 if (k == 0)
5513 a[ch++] = '0';
5514
5515 if (show_exp)
5516 {
5517 a[ch++] = 'e';
8150dfa1 5518 ch += scm_iint2str (expon, radix, a + ch);
1ea37620
MW
5519 }
5520
5521 mpz_clears (f, r, s, mplus, mminus, hi, digit, NULL);
5522 }
0f2d19dd
JB
5523 return ch;
5524}
5525
7a1aba42
MV
5526
5527static size_t
5528icmplx2str (double real, double imag, char *str, int radix)
5529{
5530 size_t i;
c7218482 5531 double sgn;
7a1aba42
MV
5532
5533 i = idbl2str (real, str, radix);
c7218482
MW
5534#ifdef HAVE_COPYSIGN
5535 sgn = copysign (1.0, imag);
5536#else
5537 sgn = imag;
5538#endif
5539 /* Don't output a '+' for negative numbers or for Inf and
5540 NaN. They will provide their own sign. */
5541 if (sgn >= 0 && DOUBLE_IS_FINITE (imag))
5542 str[i++] = '+';
5543 i += idbl2str (imag, &str[i], radix);
5544 str[i++] = 'i';
7a1aba42
MV
5545 return i;
5546}
5547
1be6b49c 5548static size_t
0b799eea 5549iflo2str (SCM flt, char *str, int radix)
0f2d19dd 5550{
1be6b49c 5551 size_t i;
3c9a524f 5552 if (SCM_REALP (flt))
0b799eea 5553 i = idbl2str (SCM_REAL_VALUE (flt), str, radix);
0f2d19dd 5554 else
7a1aba42
MV
5555 i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt),
5556 str, radix);
0f2d19dd
JB
5557 return i;
5558}
0f2d19dd 5559
2881e77b 5560/* convert a scm_t_intmax to a string (unterminated). returns the number of
1bbd0b84
GB
5561 characters in the result.
5562 rad is output base
5563 p is destination: worst case (base 2) is SCM_INTBUFLEN */
1be6b49c 5564size_t
2881e77b
MV
5565scm_iint2str (scm_t_intmax num, int rad, char *p)
5566{
5567 if (num < 0)
5568 {
5569 *p++ = '-';
5570 return scm_iuint2str (-num, rad, p) + 1;
5571 }
5572 else
5573 return scm_iuint2str (num, rad, p);
5574}
5575
5576/* convert a scm_t_intmax to a string (unterminated). returns the number of
5577 characters in the result.
5578 rad is output base
5579 p is destination: worst case (base 2) is SCM_INTBUFLEN */
5580size_t
5581scm_iuint2str (scm_t_uintmax num, int rad, char *p)
0f2d19dd 5582{
1be6b49c
ML
5583 size_t j = 1;
5584 size_t i;
2881e77b 5585 scm_t_uintmax n = num;
5c11cc9d 5586
a6f3af16
AW
5587 if (rad < 2 || rad > 36)
5588 scm_out_of_range ("scm_iuint2str", scm_from_int (rad));
5589
f872b822 5590 for (n /= rad; n > 0; n /= rad)
5c11cc9d
GH
5591 j++;
5592
5593 i = j;
2881e77b 5594 n = num;
f872b822
MD
5595 while (i--)
5596 {
5c11cc9d
GH
5597 int d = n % rad;
5598
f872b822 5599 n /= rad;
a6f3af16 5600 p[i] = number_chars[d];
f872b822 5601 }
0f2d19dd
JB
5602 return j;
5603}
5604
a1ec6916 5605SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
bb628794
DH
5606 (SCM n, SCM radix),
5607 "Return a string holding the external representation of the\n"
942e5b91
MG
5608 "number @var{n} in the given @var{radix}. If @var{n} is\n"
5609 "inexact, a radix of 10 will be used.")
1bbd0b84 5610#define FUNC_NAME s_scm_number_to_string
0f2d19dd 5611{
1bbd0b84 5612 int base;
98cb6e75 5613
0aacf84e 5614 if (SCM_UNBNDP (radix))
98cb6e75 5615 base = 10;
0aacf84e 5616 else
5efd3c7d 5617 base = scm_to_signed_integer (radix, 2, 36);
98cb6e75 5618
e11e83f3 5619 if (SCM_I_INUMP (n))
0aacf84e
MD
5620 {
5621 char num_buf [SCM_INTBUFLEN];
e11e83f3 5622 size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf);
cc95e00a 5623 return scm_from_locale_stringn (num_buf, length);
0aacf84e
MD
5624 }
5625 else if (SCM_BIGP (n))
5626 {
5627 char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
d88f5323
AW
5628 size_t len = strlen (str);
5629 void (*freefunc) (void *, size_t);
5630 SCM ret;
5631 mp_get_memory_functions (NULL, NULL, &freefunc);
0aacf84e 5632 scm_remember_upto_here_1 (n);
d88f5323
AW
5633 ret = scm_from_latin1_stringn (str, len);
5634 freefunc (str, len + 1);
5635 return ret;
0aacf84e 5636 }
f92e85f7
MV
5637 else if (SCM_FRACTIONP (n))
5638 {
f92e85f7 5639 return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
cc95e00a 5640 scm_from_locale_string ("/"),
f92e85f7
MV
5641 scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
5642 }
0aacf84e
MD
5643 else if (SCM_INEXACTP (n))
5644 {
5645 char num_buf [FLOBUFLEN];
cc95e00a 5646 return scm_from_locale_stringn (num_buf, iflo2str (n, num_buf, base));
0aacf84e
MD
5647 }
5648 else
bb628794 5649 SCM_WRONG_TYPE_ARG (1, n);
0f2d19dd 5650}
1bbd0b84 5651#undef FUNC_NAME
0f2d19dd
JB
5652
5653
ca46fb90
RB
5654/* These print routines used to be stubbed here so that scm_repl.c
5655 wouldn't need SCM_BIGDIG conditionals (pre GMP) */
1cc91f1b 5656
0f2d19dd 5657int
e81d98ec 5658scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
0f2d19dd 5659{
56e55ac7 5660 char num_buf[FLOBUFLEN];
0b799eea 5661 scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
0f2d19dd
JB
5662 return !0;
5663}
5664
b479fe9a
MV
5665void
5666scm_i_print_double (double val, SCM port)
5667{
5668 char num_buf[FLOBUFLEN];
5669 scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port);
5670}
5671
f3ae5d60 5672int
e81d98ec 5673scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
f92e85f7 5674
f3ae5d60 5675{
56e55ac7 5676 char num_buf[FLOBUFLEN];
0b799eea 5677 scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
f3ae5d60
MD
5678 return !0;
5679}
1cc91f1b 5680
7a1aba42
MV
5681void
5682scm_i_print_complex (double real, double imag, SCM port)
5683{
5684 char num_buf[FLOBUFLEN];
5685 scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port);
5686}
5687
f92e85f7
MV
5688int
5689scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
5690{
5691 SCM str;
f92e85f7 5692 str = scm_number_to_string (sexp, SCM_UNDEFINED);
a9178715 5693 scm_display (str, port);
f92e85f7
MV
5694 scm_remember_upto_here_1 (str);
5695 return !0;
5696}
5697
0f2d19dd 5698int
e81d98ec 5699scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
0f2d19dd 5700{
ca46fb90 5701 char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp));
b57bf272
AW
5702 size_t len = strlen (str);
5703 void (*freefunc) (void *, size_t);
5704 mp_get_memory_functions (NULL, NULL, &freefunc);
ca46fb90 5705 scm_remember_upto_here_1 (exp);
b57bf272
AW
5706 scm_lfwrite (str, len, port);
5707 freefunc (str, len + 1);
0f2d19dd
JB
5708 return !0;
5709}
5710/*** END nums->strs ***/
5711
3c9a524f 5712
0f2d19dd 5713/*** STRINGS -> NUMBERS ***/
2a8fecee 5714
3c9a524f
DH
5715/* The following functions implement the conversion from strings to numbers.
5716 * The implementation somehow follows the grammar for numbers as it is given
5717 * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
5718 * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
5719 * points should be noted about the implementation:
bc3d34f5 5720 *
3c9a524f
DH
5721 * * Each function keeps a local index variable 'idx' that points at the
5722 * current position within the parsed string. The global index is only
5723 * updated if the function could parse the corresponding syntactic unit
5724 * successfully.
bc3d34f5 5725 *
3c9a524f 5726 * * Similarly, the functions keep track of indicators of inexactness ('#',
bc3d34f5
MW
5727 * '.' or exponents) using local variables ('hash_seen', 'x').
5728 *
3c9a524f
DH
5729 * * Sequences of digits are parsed into temporary variables holding fixnums.
5730 * Only if these fixnums would overflow, the result variables are updated
5731 * using the standard functions scm_add, scm_product, scm_divide etc. Then,
5732 * the temporary variables holding the fixnums are cleared, and the process
5733 * starts over again. If for example fixnums were able to store five decimal
5734 * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
5735 * and the result was computed as 12345 * 100000 + 67890. In other words,
5736 * only every five digits two bignum operations were performed.
bc3d34f5
MW
5737 *
5738 * Notes on the handling of exactness specifiers:
5739 *
5740 * When parsing non-real complex numbers, we apply exactness specifiers on
5741 * per-component basis, as is done in PLT Scheme. For complex numbers
5742 * written in rectangular form, exactness specifiers are applied to the
5743 * real and imaginary parts before calling scm_make_rectangular. For
5744 * complex numbers written in polar form, exactness specifiers are applied
5745 * to the magnitude and angle before calling scm_make_polar.
5746 *
5747 * There are two kinds of exactness specifiers: forced and implicit. A
5748 * forced exactness specifier is a "#e" or "#i" prefix at the beginning of
5749 * the entire number, and applies to both components of a complex number.
5750 * "#e" causes each component to be made exact, and "#i" causes each
5751 * component to be made inexact. If no forced exactness specifier is
5752 * present, then the exactness of each component is determined
5753 * independently by the presence or absence of a decimal point or hash mark
5754 * within that component. If a decimal point or hash mark is present, the
5755 * component is made inexact, otherwise it is made exact.
5756 *
5757 * After the exactness specifiers have been applied to each component, they
5758 * are passed to either scm_make_rectangular or scm_make_polar to produce
5759 * the final result. Note that this will result in a real number if the
5760 * imaginary part, magnitude, or angle is an exact 0.
5761 *
5762 * For example, (string->number "#i5.0+0i") does the equivalent of:
5763 *
5764 * (make-rectangular (exact->inexact 5) (exact->inexact 0))
3c9a524f
DH
5765 */
5766
5767enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
5768
5769/* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
5770
a6f3af16
AW
5771/* Caller is responsible for checking that the return value is in range
5772 for the given radix, which should be <= 36. */
5773static unsigned int
5774char_decimal_value (scm_t_uint32 c)
5775{
5776 /* uc_decimal_value returns -1 on error. When cast to an unsigned int,
5777 that's certainly above any valid decimal, so we take advantage of
5778 that to elide some tests. */
5779 unsigned int d = (unsigned int) uc_decimal_value (c);
5780
5781 /* If that failed, try extended hexadecimals, then. Only accept ascii
5782 hexadecimals. */
5783 if (d >= 10U)
5784 {
5785 c = uc_tolower (c);
5786 if (c >= (scm_t_uint32) 'a')
5787 d = c - (scm_t_uint32)'a' + 10U;
5788 }
5789 return d;
5790}
3c9a524f 5791
91db4a37
LC
5792/* Parse the substring of MEM starting at *P_IDX for an unsigned integer
5793 in base RADIX. Upon success, return the unsigned integer and update
5794 *P_IDX and *P_EXACTNESS accordingly. Return #f on failure. */
2a8fecee 5795static SCM
3f47e526 5796mem2uinteger (SCM mem, unsigned int *p_idx,
3c9a524f 5797 unsigned int radix, enum t_exactness *p_exactness)
2a8fecee 5798{
3c9a524f
DH
5799 unsigned int idx = *p_idx;
5800 unsigned int hash_seen = 0;
5801 scm_t_bits shift = 1;
5802 scm_t_bits add = 0;
5803 unsigned int digit_value;
5804 SCM result;
5805 char c;
3f47e526 5806 size_t len = scm_i_string_length (mem);
3c9a524f
DH
5807
5808 if (idx == len)
5809 return SCM_BOOL_F;
2a8fecee 5810
3f47e526 5811 c = scm_i_string_ref (mem, idx);
a6f3af16 5812 digit_value = char_decimal_value (c);
3c9a524f
DH
5813 if (digit_value >= radix)
5814 return SCM_BOOL_F;
5815
5816 idx++;
d956fa6f 5817 result = SCM_I_MAKINUM (digit_value);
3c9a524f 5818 while (idx != len)
f872b822 5819 {
3f47e526 5820 scm_t_wchar c = scm_i_string_ref (mem, idx);
a6f3af16 5821 if (c == '#')
3c9a524f
DH
5822 {
5823 hash_seen = 1;
5824 digit_value = 0;
5825 }
a6f3af16
AW
5826 else if (hash_seen)
5827 break;
3c9a524f 5828 else
a6f3af16
AW
5829 {
5830 digit_value = char_decimal_value (c);
5831 /* This check catches non-decimals in addition to out-of-range
5832 decimals. */
5833 if (digit_value >= radix)
5834 break;
5835 }
3c9a524f
DH
5836
5837 idx++;
5838 if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
5839 {
d956fa6f 5840 result = scm_product (result, SCM_I_MAKINUM (shift));
3c9a524f 5841 if (add > 0)
d956fa6f 5842 result = scm_sum (result, SCM_I_MAKINUM (add));
3c9a524f
DH
5843
5844 shift = radix;
5845 add = digit_value;
5846 }
5847 else
5848 {
5849 shift = shift * radix;
5850 add = add * radix + digit_value;
5851 }
5852 };
5853
5854 if (shift > 1)
d956fa6f 5855 result = scm_product (result, SCM_I_MAKINUM (shift));
3c9a524f 5856 if (add > 0)
d956fa6f 5857 result = scm_sum (result, SCM_I_MAKINUM (add));
3c9a524f
DH
5858
5859 *p_idx = idx;
5860 if (hash_seen)
5861 *p_exactness = INEXACT;
5862
5863 return result;
2a8fecee
JB
5864}
5865
5866
3c9a524f
DH
5867/* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
5868 * covers the parts of the rules that start at a potential point. The value
5869 * of the digits up to the point have been parsed by the caller and are given
79d34f68
DH
5870 * in variable result. The content of *p_exactness indicates, whether a hash
5871 * has already been seen in the digits before the point.
3c9a524f 5872 */
1cc91f1b 5873
3f47e526 5874#define DIGIT2UINT(d) (uc_numeric_value(d).numerator)
3c9a524f
DH
5875
5876static SCM
3f47e526 5877mem2decimal_from_point (SCM result, SCM mem,
3c9a524f 5878 unsigned int *p_idx, enum t_exactness *p_exactness)
0f2d19dd 5879{
3c9a524f
DH
5880 unsigned int idx = *p_idx;
5881 enum t_exactness x = *p_exactness;
3f47e526 5882 size_t len = scm_i_string_length (mem);
3c9a524f
DH
5883
5884 if (idx == len)
79d34f68 5885 return result;
3c9a524f 5886
3f47e526 5887 if (scm_i_string_ref (mem, idx) == '.')
3c9a524f
DH
5888 {
5889 scm_t_bits shift = 1;
5890 scm_t_bits add = 0;
5891 unsigned int digit_value;
cff5fa33 5892 SCM big_shift = SCM_INUM1;
3c9a524f
DH
5893
5894 idx++;
5895 while (idx != len)
5896 {
3f47e526
MG
5897 scm_t_wchar c = scm_i_string_ref (mem, idx);
5898 if (uc_is_property_decimal_digit ((scm_t_uint32) c))
3c9a524f
DH
5899 {
5900 if (x == INEXACT)
5901 return SCM_BOOL_F;
5902 else
5903 digit_value = DIGIT2UINT (c);
5904 }
5905 else if (c == '#')
5906 {
5907 x = INEXACT;
5908 digit_value = 0;
5909 }
5910 else
5911 break;
5912
5913 idx++;
5914 if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
5915 {
d956fa6f
MV
5916 big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
5917 result = scm_product (result, SCM_I_MAKINUM (shift));
3c9a524f 5918 if (add > 0)
d956fa6f 5919 result = scm_sum (result, SCM_I_MAKINUM (add));
3c9a524f
DH
5920
5921 shift = 10;
5922 add = digit_value;
5923 }
5924 else
5925 {
5926 shift = shift * 10;
5927 add = add * 10 + digit_value;
5928 }
5929 };
5930
5931 if (add > 0)
5932 {
d956fa6f
MV
5933 big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
5934 result = scm_product (result, SCM_I_MAKINUM (shift));
5935 result = scm_sum (result, SCM_I_MAKINUM (add));
3c9a524f
DH
5936 }
5937
d8592269 5938 result = scm_divide (result, big_shift);
79d34f68 5939
3c9a524f
DH
5940 /* We've seen a decimal point, thus the value is implicitly inexact. */
5941 x = INEXACT;
f872b822 5942 }
3c9a524f 5943
3c9a524f 5944 if (idx != len)
f872b822 5945 {
3c9a524f
DH
5946 int sign = 1;
5947 unsigned int start;
3f47e526 5948 scm_t_wchar c;
3c9a524f
DH
5949 int exponent;
5950 SCM e;
5951
5952 /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
5953
3f47e526 5954 switch (scm_i_string_ref (mem, idx))
f872b822 5955 {
3c9a524f
DH
5956 case 'd': case 'D':
5957 case 'e': case 'E':
5958 case 'f': case 'F':
5959 case 'l': case 'L':
5960 case 's': case 'S':
5961 idx++;
ee0ddd21
AW
5962 if (idx == len)
5963 return SCM_BOOL_F;
5964
3c9a524f 5965 start = idx;
3f47e526 5966 c = scm_i_string_ref (mem, idx);
3c9a524f
DH
5967 if (c == '-')
5968 {
5969 idx++;
ee0ddd21
AW
5970 if (idx == len)
5971 return SCM_BOOL_F;
5972
3c9a524f 5973 sign = -1;
3f47e526 5974 c = scm_i_string_ref (mem, idx);
3c9a524f
DH
5975 }
5976 else if (c == '+')
5977 {
5978 idx++;
ee0ddd21
AW
5979 if (idx == len)
5980 return SCM_BOOL_F;
5981
3c9a524f 5982 sign = 1;
3f47e526 5983 c = scm_i_string_ref (mem, idx);
3c9a524f
DH
5984 }
5985 else
5986 sign = 1;
5987
3f47e526 5988 if (!uc_is_property_decimal_digit ((scm_t_uint32) c))
3c9a524f
DH
5989 return SCM_BOOL_F;
5990
5991 idx++;
5992 exponent = DIGIT2UINT (c);
5993 while (idx != len)
f872b822 5994 {
3f47e526
MG
5995 scm_t_wchar c = scm_i_string_ref (mem, idx);
5996 if (uc_is_property_decimal_digit ((scm_t_uint32) c))
3c9a524f
DH
5997 {
5998 idx++;
5999 if (exponent <= SCM_MAXEXP)
6000 exponent = exponent * 10 + DIGIT2UINT (c);
6001 }
6002 else
6003 break;
f872b822 6004 }
3c9a524f 6005
1ea37620 6006 if (exponent > ((sign == 1) ? SCM_MAXEXP : SCM_MAXEXP + DBL_DIG + 1))
f872b822 6007 {
3c9a524f 6008 size_t exp_len = idx - start;
3f47e526 6009 SCM exp_string = scm_i_substring_copy (mem, start, start + exp_len);
3c9a524f
DH
6010 SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
6011 scm_out_of_range ("string->number", exp_num);
f872b822 6012 }
3c9a524f 6013
d956fa6f 6014 e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent));
3c9a524f
DH
6015 if (sign == 1)
6016 result = scm_product (result, e);
6017 else
6ebecdeb 6018 result = scm_divide (result, e);
3c9a524f
DH
6019
6020 /* We've seen an exponent, thus the value is implicitly inexact. */
6021 x = INEXACT;
6022
f872b822 6023 break;
3c9a524f 6024
f872b822 6025 default:
3c9a524f 6026 break;
f872b822 6027 }
0f2d19dd 6028 }
3c9a524f
DH
6029
6030 *p_idx = idx;
6031 if (x == INEXACT)
6032 *p_exactness = x;
6033
6034 return result;
0f2d19dd 6035}
0f2d19dd 6036
3c9a524f
DH
6037
6038/* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
6039
6040static SCM
3f47e526 6041mem2ureal (SCM mem, unsigned int *p_idx,
929d11b2
MW
6042 unsigned int radix, enum t_exactness forced_x,
6043 int allow_inf_or_nan)
0f2d19dd 6044{
3c9a524f 6045 unsigned int idx = *p_idx;
164d2481 6046 SCM result;
3f47e526 6047 size_t len = scm_i_string_length (mem);
3c9a524f 6048
40f89215
NJ
6049 /* Start off believing that the number will be exact. This changes
6050 to INEXACT if we see a decimal point or a hash. */
9d427b2c 6051 enum t_exactness implicit_x = EXACT;
40f89215 6052
3c9a524f
DH
6053 if (idx == len)
6054 return SCM_BOOL_F;
6055
929d11b2
MW
6056 if (allow_inf_or_nan && forced_x != EXACT && idx+5 <= len)
6057 switch (scm_i_string_ref (mem, idx))
6058 {
6059 case 'i': case 'I':
6060 switch (scm_i_string_ref (mem, idx + 1))
6061 {
6062 case 'n': case 'N':
6063 switch (scm_i_string_ref (mem, idx + 2))
6064 {
6065 case 'f': case 'F':
6066 if (scm_i_string_ref (mem, idx + 3) == '.'
6067 && scm_i_string_ref (mem, idx + 4) == '0')
6068 {
6069 *p_idx = idx+5;
6070 return scm_inf ();
6071 }
6072 }
6073 }
6074 case 'n': case 'N':
6075 switch (scm_i_string_ref (mem, idx + 1))
6076 {
6077 case 'a': case 'A':
6078 switch (scm_i_string_ref (mem, idx + 2))
6079 {
6080 case 'n': case 'N':
6081 if (scm_i_string_ref (mem, idx + 3) == '.')
6082 {
6083 /* Cobble up the fractional part. We might want to
6084 set the NaN's mantissa from it. */
6085 idx += 4;
6086 if (!scm_is_eq (mem2uinteger (mem, &idx, 10, &implicit_x),
6087 SCM_INUM0))
6088 {
5f237d6e 6089#if SCM_ENABLE_DEPRECATED == 1
929d11b2
MW
6090 scm_c_issue_deprecation_warning
6091 ("Non-zero suffixes to `+nan.' are deprecated. Use `+nan.0'.");
5f237d6e 6092#else
929d11b2 6093 return SCM_BOOL_F;
5f237d6e 6094#endif
929d11b2 6095 }
5f237d6e 6096
929d11b2
MW
6097 *p_idx = idx;
6098 return scm_nan ();
6099 }
6100 }
6101 }
6102 }
7351e207 6103
3f47e526 6104 if (scm_i_string_ref (mem, idx) == '.')
3c9a524f
DH
6105 {
6106 if (radix != 10)
6107 return SCM_BOOL_F;
6108 else if (idx + 1 == len)
6109 return SCM_BOOL_F;
3f47e526 6110 else if (!uc_is_property_decimal_digit ((scm_t_uint32) scm_i_string_ref (mem, idx+1)))
3c9a524f
DH
6111 return SCM_BOOL_F;
6112 else
cff5fa33 6113 result = mem2decimal_from_point (SCM_INUM0, mem,
9d427b2c 6114 p_idx, &implicit_x);
f872b822 6115 }
3c9a524f
DH
6116 else
6117 {
3c9a524f 6118 SCM uinteger;
3c9a524f 6119
9d427b2c 6120 uinteger = mem2uinteger (mem, &idx, radix, &implicit_x);
73e4de09 6121 if (scm_is_false (uinteger))
3c9a524f
DH
6122 return SCM_BOOL_F;
6123
6124 if (idx == len)
6125 result = uinteger;
3f47e526 6126 else if (scm_i_string_ref (mem, idx) == '/')
f872b822 6127 {
3c9a524f
DH
6128 SCM divisor;
6129
6130 idx++;
ee0ddd21
AW
6131 if (idx == len)
6132 return SCM_BOOL_F;
3c9a524f 6133
9d427b2c 6134 divisor = mem2uinteger (mem, &idx, radix, &implicit_x);
929d11b2 6135 if (scm_is_false (divisor) || scm_is_eq (divisor, SCM_INUM0))
3c9a524f
DH
6136 return SCM_BOOL_F;
6137
f92e85f7 6138 /* both are int/big here, I assume */
cba42c93 6139 result = scm_i_make_ratio (uinteger, divisor);
f872b822 6140 }
3c9a524f
DH
6141 else if (radix == 10)
6142 {
9d427b2c 6143 result = mem2decimal_from_point (uinteger, mem, &idx, &implicit_x);
73e4de09 6144 if (scm_is_false (result))
3c9a524f
DH
6145 return SCM_BOOL_F;
6146 }
6147 else
6148 result = uinteger;
6149
6150 *p_idx = idx;
f872b822 6151 }
164d2481 6152
9d427b2c
MW
6153 switch (forced_x)
6154 {
6155 case EXACT:
6156 if (SCM_INEXACTP (result))
6157 return scm_inexact_to_exact (result);
6158 else
6159 return result;
6160 case INEXACT:
6161 if (SCM_INEXACTP (result))
6162 return result;
6163 else
6164 return scm_exact_to_inexact (result);
6165 case NO_EXACTNESS:
6166 if (implicit_x == INEXACT)
6167 {
6168 if (SCM_INEXACTP (result))
6169 return result;
6170 else
6171 return scm_exact_to_inexact (result);
6172 }
6173 else
6174 return result;
6175 }
164d2481 6176
9d427b2c
MW
6177 /* We should never get here */
6178 scm_syserror ("mem2ureal");
3c9a524f 6179}
0f2d19dd 6180
0f2d19dd 6181
3c9a524f 6182/* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
0f2d19dd 6183
3c9a524f 6184static SCM
3f47e526 6185mem2complex (SCM mem, unsigned int idx,
9d427b2c 6186 unsigned int radix, enum t_exactness forced_x)
3c9a524f 6187{
3f47e526 6188 scm_t_wchar c;
3c9a524f
DH
6189 int sign = 0;
6190 SCM ureal;
3f47e526 6191 size_t len = scm_i_string_length (mem);
3c9a524f
DH
6192
6193 if (idx == len)
6194 return SCM_BOOL_F;
6195
3f47e526 6196 c = scm_i_string_ref (mem, idx);
3c9a524f
DH
6197 if (c == '+')
6198 {
6199 idx++;
6200 sign = 1;
6201 }
6202 else if (c == '-')
6203 {
6204 idx++;
6205 sign = -1;
0f2d19dd 6206 }
0f2d19dd 6207
3c9a524f
DH
6208 if (idx == len)
6209 return SCM_BOOL_F;
6210
929d11b2 6211 ureal = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
73e4de09 6212 if (scm_is_false (ureal))
f872b822 6213 {
3c9a524f
DH
6214 /* input must be either +i or -i */
6215
6216 if (sign == 0)
6217 return SCM_BOOL_F;
6218
3f47e526
MG
6219 if (scm_i_string_ref (mem, idx) == 'i'
6220 || scm_i_string_ref (mem, idx) == 'I')
f872b822 6221 {
3c9a524f
DH
6222 idx++;
6223 if (idx != len)
6224 return SCM_BOOL_F;
6225
cff5fa33 6226 return scm_make_rectangular (SCM_INUM0, SCM_I_MAKINUM (sign));
f872b822 6227 }
3c9a524f
DH
6228 else
6229 return SCM_BOOL_F;
0f2d19dd 6230 }
3c9a524f
DH
6231 else
6232 {
73e4de09 6233 if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
3c9a524f 6234 ureal = scm_difference (ureal, SCM_UNDEFINED);
f872b822 6235
3c9a524f
DH
6236 if (idx == len)
6237 return ureal;
6238
3f47e526 6239 c = scm_i_string_ref (mem, idx);
3c9a524f 6240 switch (c)
f872b822 6241 {
3c9a524f
DH
6242 case 'i': case 'I':
6243 /* either +<ureal>i or -<ureal>i */
6244
6245 idx++;
6246 if (sign == 0)
6247 return SCM_BOOL_F;
6248 if (idx != len)
6249 return SCM_BOOL_F;
cff5fa33 6250 return scm_make_rectangular (SCM_INUM0, ureal);
3c9a524f
DH
6251
6252 case '@':
6253 /* polar input: <real>@<real>. */
6254
6255 idx++;
6256 if (idx == len)
6257 return SCM_BOOL_F;
6258 else
f872b822 6259 {
3c9a524f
DH
6260 int sign;
6261 SCM angle;
6262 SCM result;
6263
3f47e526 6264 c = scm_i_string_ref (mem, idx);
3c9a524f
DH
6265 if (c == '+')
6266 {
6267 idx++;
ee0ddd21
AW
6268 if (idx == len)
6269 return SCM_BOOL_F;
3c9a524f
DH
6270 sign = 1;
6271 }
6272 else if (c == '-')
6273 {
6274 idx++;
ee0ddd21
AW
6275 if (idx == len)
6276 return SCM_BOOL_F;
3c9a524f
DH
6277 sign = -1;
6278 }
6279 else
929d11b2 6280 sign = 0;
3c9a524f 6281
929d11b2 6282 angle = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
73e4de09 6283 if (scm_is_false (angle))
3c9a524f
DH
6284 return SCM_BOOL_F;
6285 if (idx != len)
6286 return SCM_BOOL_F;
6287
73e4de09 6288 if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
3c9a524f
DH
6289 angle = scm_difference (angle, SCM_UNDEFINED);
6290
6291 result = scm_make_polar (ureal, angle);
6292 return result;
f872b822 6293 }
3c9a524f
DH
6294 case '+':
6295 case '-':
6296 /* expecting input matching <real>[+-]<ureal>?i */
0f2d19dd 6297
3c9a524f
DH
6298 idx++;
6299 if (idx == len)
6300 return SCM_BOOL_F;
6301 else
6302 {
6303 int sign = (c == '+') ? 1 : -1;
929d11b2 6304 SCM imag = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
0f2d19dd 6305
73e4de09 6306 if (scm_is_false (imag))
d956fa6f 6307 imag = SCM_I_MAKINUM (sign);
23295dc3 6308 else if (sign == -1 && scm_is_false (scm_nan_p (imag)))
1fe5e088 6309 imag = scm_difference (imag, SCM_UNDEFINED);
0f2d19dd 6310
3c9a524f
DH
6311 if (idx == len)
6312 return SCM_BOOL_F;
3f47e526
MG
6313 if (scm_i_string_ref (mem, idx) != 'i'
6314 && scm_i_string_ref (mem, idx) != 'I')
3c9a524f 6315 return SCM_BOOL_F;
0f2d19dd 6316
3c9a524f
DH
6317 idx++;
6318 if (idx != len)
6319 return SCM_BOOL_F;
0f2d19dd 6320
1fe5e088 6321 return scm_make_rectangular (ureal, imag);
3c9a524f
DH
6322 }
6323 default:
6324 return SCM_BOOL_F;
6325 }
6326 }
0f2d19dd 6327}
0f2d19dd
JB
6328
6329
3c9a524f
DH
6330/* R5RS, section 7.1.1, lexical structure of numbers: <number> */
6331
6332enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
1cc91f1b 6333
0f2d19dd 6334SCM
3f47e526 6335scm_i_string_to_number (SCM mem, unsigned int default_radix)
0f2d19dd 6336{
3c9a524f
DH
6337 unsigned int idx = 0;
6338 unsigned int radix = NO_RADIX;
6339 enum t_exactness forced_x = NO_EXACTNESS;
3f47e526 6340 size_t len = scm_i_string_length (mem);
3c9a524f
DH
6341
6342 /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
3f47e526 6343 while (idx + 2 < len && scm_i_string_ref (mem, idx) == '#')
3c9a524f 6344 {
3f47e526 6345 switch (scm_i_string_ref (mem, idx + 1))
3c9a524f
DH
6346 {
6347 case 'b': case 'B':
6348 if (radix != NO_RADIX)
6349 return SCM_BOOL_F;
6350 radix = DUAL;
6351 break;
6352 case 'd': case 'D':
6353 if (radix != NO_RADIX)
6354 return SCM_BOOL_F;
6355 radix = DEC;
6356 break;
6357 case 'i': case 'I':
6358 if (forced_x != NO_EXACTNESS)
6359 return SCM_BOOL_F;
6360 forced_x = INEXACT;
6361 break;
6362 case 'e': case 'E':
6363 if (forced_x != NO_EXACTNESS)
6364 return SCM_BOOL_F;
6365 forced_x = EXACT;
6366 break;
6367 case 'o': case 'O':
6368 if (radix != NO_RADIX)
6369 return SCM_BOOL_F;
6370 radix = OCT;
6371 break;
6372 case 'x': case 'X':
6373 if (radix != NO_RADIX)
6374 return SCM_BOOL_F;
6375 radix = HEX;
6376 break;
6377 default:
f872b822 6378 return SCM_BOOL_F;
3c9a524f
DH
6379 }
6380 idx += 2;
6381 }
6382
6383 /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
6384 if (radix == NO_RADIX)
9d427b2c 6385 radix = default_radix;
f872b822 6386
9d427b2c 6387 return mem2complex (mem, idx, radix, forced_x);
0f2d19dd
JB
6388}
6389
3f47e526
MG
6390SCM
6391scm_c_locale_stringn_to_number (const char* mem, size_t len,
6392 unsigned int default_radix)
6393{
6394 SCM str = scm_from_locale_stringn (mem, len);
6395
6396 return scm_i_string_to_number (str, default_radix);
6397}
6398
0f2d19dd 6399
a1ec6916 6400SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
bb628794 6401 (SCM string, SCM radix),
1e6808ea 6402 "Return a number of the maximally precise representation\n"
942e5b91 6403 "expressed by the given @var{string}. @var{radix} must be an\n"
5352393c
MG
6404 "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
6405 "is a default radix that may be overridden by an explicit radix\n"
6406 "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
6407 "supplied, then the default radix is 10. If string is not a\n"
6408 "syntactically valid notation for a number, then\n"
6409 "@code{string->number} returns @code{#f}.")
1bbd0b84 6410#define FUNC_NAME s_scm_string_to_number
0f2d19dd
JB
6411{
6412 SCM answer;
5efd3c7d 6413 unsigned int base;
a6d9e5ab 6414 SCM_VALIDATE_STRING (1, string);
5efd3c7d
MV
6415
6416 if (SCM_UNBNDP (radix))
6417 base = 10;
6418 else
6419 base = scm_to_unsigned_integer (radix, 2, INT_MAX);
6420
3f47e526 6421 answer = scm_i_string_to_number (string, base);
8824ac88
MV
6422 scm_remember_upto_here_1 (string);
6423 return answer;
0f2d19dd 6424}
1bbd0b84 6425#undef FUNC_NAME
3c9a524f
DH
6426
6427
0f2d19dd
JB
6428/*** END strs->nums ***/
6429
5986c47d 6430
8507ec80
MV
6431SCM_DEFINE (scm_number_p, "number?", 1, 0, 0,
6432 (SCM x),
6433 "Return @code{#t} if @var{x} is a number, @code{#f}\n"
6434 "otherwise.")
6435#define FUNC_NAME s_scm_number_p
6436{
6437 return scm_from_bool (SCM_NUMBERP (x));
6438}
6439#undef FUNC_NAME
6440
6441SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0,
1bbd0b84 6442 (SCM x),
942e5b91 6443 "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
bb2c02f2 6444 "otherwise. Note that the sets of real, rational and integer\n"
942e5b91
MG
6445 "values form subsets of the set of complex numbers, i. e. the\n"
6446 "predicate will also be fulfilled if @var{x} is a real,\n"
6447 "rational or integer number.")
8507ec80 6448#define FUNC_NAME s_scm_complex_p
0f2d19dd 6449{
8507ec80
MV
6450 /* all numbers are complex. */
6451 return scm_number_p (x);
0f2d19dd 6452}
1bbd0b84 6453#undef FUNC_NAME
0f2d19dd 6454
f92e85f7
MV
6455SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
6456 (SCM x),
6457 "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
6458 "otherwise. Note that the set of integer values forms a subset of\n"
6459 "the set of real numbers, i. e. the predicate will also be\n"
6460 "fulfilled if @var{x} is an integer number.")
6461#define FUNC_NAME s_scm_real_p
6462{
c960e556
MW
6463 return scm_from_bool
6464 (SCM_I_INUMP (x) || SCM_REALP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x));
f92e85f7
MV
6465}
6466#undef FUNC_NAME
6467
6468SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
1bbd0b84 6469 (SCM x),
942e5b91 6470 "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
bb2c02f2 6471 "otherwise. Note that the set of integer values forms a subset of\n"
942e5b91 6472 "the set of rational numbers, i. e. the predicate will also be\n"
f92e85f7
MV
6473 "fulfilled if @var{x} is an integer number.")
6474#define FUNC_NAME s_scm_rational_p
0f2d19dd 6475{
c960e556 6476 if (SCM_I_INUMP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x))
f92e85f7
MV
6477 return SCM_BOOL_T;
6478 else if (SCM_REALP (x))
c960e556
MW
6479 /* due to their limited precision, finite floating point numbers are
6480 rational as well. (finite means neither infinity nor a NaN) */
6481 return scm_from_bool (DOUBLE_IS_FINITE (SCM_REAL_VALUE (x)));
0aacf84e 6482 else
bb628794 6483 return SCM_BOOL_F;
0f2d19dd 6484}
1bbd0b84 6485#undef FUNC_NAME
0f2d19dd 6486
a1ec6916 6487SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
1bbd0b84 6488 (SCM x),
942e5b91
MG
6489 "Return @code{#t} if @var{x} is an integer number, @code{#f}\n"
6490 "else.")
1bbd0b84 6491#define FUNC_NAME s_scm_integer_p
0f2d19dd 6492{
c960e556 6493 if (SCM_I_INUMP (x) || SCM_BIGP (x))
f872b822 6494 return SCM_BOOL_T;
c960e556
MW
6495 else if (SCM_REALP (x))
6496 {
6497 double val = SCM_REAL_VALUE (x);
6498 return scm_from_bool (!isinf (val) && (val == floor (val)));
6499 }
6500 else
8e43ed5d 6501 return SCM_BOOL_F;
0f2d19dd 6502}
1bbd0b84 6503#undef FUNC_NAME
0f2d19dd
JB
6504
6505
8a1f4f98
AW
6506SCM scm_i_num_eq_p (SCM, SCM, SCM);
6507SCM_PRIMITIVE_GENERIC (scm_i_num_eq_p, "=", 0, 2, 1,
6508 (SCM x, SCM y, SCM rest),
6509 "Return @code{#t} if all parameters are numerically equal.")
6510#define FUNC_NAME s_scm_i_num_eq_p
6511{
6512 if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
6513 return SCM_BOOL_T;
6514 while (!scm_is_null (rest))
6515 {
6516 if (scm_is_false (scm_num_eq_p (x, y)))
6517 return SCM_BOOL_F;
6518 x = y;
6519 y = scm_car (rest);
6520 rest = scm_cdr (rest);
6521 }
6522 return scm_num_eq_p (x, y);
6523}
6524#undef FUNC_NAME
0f2d19dd 6525SCM
6e8d25a6 6526scm_num_eq_p (SCM x, SCM y)
0f2d19dd 6527{
d8b95e27 6528 again:
e11e83f3 6529 if (SCM_I_INUMP (x))
0aacf84e 6530 {
e25f3727 6531 scm_t_signed_bits xx = SCM_I_INUM (x);
e11e83f3 6532 if (SCM_I_INUMP (y))
0aacf84e 6533 {
e25f3727 6534 scm_t_signed_bits yy = SCM_I_INUM (y);
73e4de09 6535 return scm_from_bool (xx == yy);
0aacf84e
MD
6536 }
6537 else if (SCM_BIGP (y))
6538 return SCM_BOOL_F;
6539 else if (SCM_REALP (y))
e8c5b1f2
KR
6540 {
6541 /* On a 32-bit system an inum fits a double, we can cast the inum
6542 to a double and compare.
6543
6544 But on a 64-bit system an inum is bigger than a double and
01329288
MW
6545 casting it to a double (call that dxx) will round.
6546 Although dxx will not in general be equal to xx, dxx will
6547 always be an integer and within a factor of 2 of xx, so if
6548 dxx==yy, we know that yy is an integer and fits in
6549 scm_t_signed_bits. So we cast yy to scm_t_signed_bits and
e8c5b1f2
KR
6550 compare with plain xx.
6551
6552 An alternative (for any size system actually) would be to check
6553 yy is an integer (with floor) and is in range of an inum
6554 (compare against appropriate powers of 2) then test
e25f3727
AW
6555 xx==(scm_t_signed_bits)yy. It's just a matter of which
6556 casts/comparisons might be fastest or easiest for the cpu. */
e8c5b1f2
KR
6557
6558 double yy = SCM_REAL_VALUE (y);
3a1b45fd
MV
6559 return scm_from_bool ((double) xx == yy
6560 && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
e25f3727 6561 || xx == (scm_t_signed_bits) yy));
e8c5b1f2 6562 }
0aacf84e 6563 else if (SCM_COMPLEXP (y))
01329288
MW
6564 {
6565 /* see comments with inum/real above */
6566 double ry = SCM_COMPLEX_REAL (y);
6567 return scm_from_bool ((double) xx == ry
6568 && 0.0 == SCM_COMPLEX_IMAG (y)
6569 && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
6570 || xx == (scm_t_signed_bits) ry));
6571 }
f92e85f7
MV
6572 else if (SCM_FRACTIONP (y))
6573 return SCM_BOOL_F;
0aacf84e 6574 else
8a1f4f98 6575 SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p);
f872b822 6576 }
0aacf84e
MD
6577 else if (SCM_BIGP (x))
6578 {
e11e83f3 6579 if (SCM_I_INUMP (y))
0aacf84e
MD
6580 return SCM_BOOL_F;
6581 else if (SCM_BIGP (y))
6582 {
6583 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
6584 scm_remember_upto_here_2 (x, y);
73e4de09 6585 return scm_from_bool (0 == cmp);
0aacf84e
MD
6586 }
6587 else if (SCM_REALP (y))
6588 {
6589 int cmp;
2e65b52f 6590 if (isnan (SCM_REAL_VALUE (y)))
0aacf84e
MD
6591 return SCM_BOOL_F;
6592 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
6593 scm_remember_upto_here_1 (x);
73e4de09 6594 return scm_from_bool (0 == cmp);
0aacf84e
MD
6595 }
6596 else if (SCM_COMPLEXP (y))
6597 {
6598 int cmp;
6599 if (0.0 != SCM_COMPLEX_IMAG (y))
6600 return SCM_BOOL_F;
2e65b52f 6601 if (isnan (SCM_COMPLEX_REAL (y)))
0aacf84e
MD
6602 return SCM_BOOL_F;
6603 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y));
6604 scm_remember_upto_here_1 (x);
73e4de09 6605 return scm_from_bool (0 == cmp);
0aacf84e 6606 }
f92e85f7
MV
6607 else if (SCM_FRACTIONP (y))
6608 return SCM_BOOL_F;
0aacf84e 6609 else
8a1f4f98 6610 SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p);
f4c627b3 6611 }
0aacf84e
MD
6612 else if (SCM_REALP (x))
6613 {
e8c5b1f2 6614 double xx = SCM_REAL_VALUE (x);
e11e83f3 6615 if (SCM_I_INUMP (y))
e8c5b1f2
KR
6616 {
6617 /* see comments with inum/real above */
e25f3727 6618 scm_t_signed_bits yy = SCM_I_INUM (y);
3a1b45fd
MV
6619 return scm_from_bool (xx == (double) yy
6620 && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
e25f3727 6621 || (scm_t_signed_bits) xx == yy));
e8c5b1f2 6622 }
0aacf84e
MD
6623 else if (SCM_BIGP (y))
6624 {
6625 int cmp;
01329288 6626 if (isnan (xx))
0aacf84e 6627 return SCM_BOOL_F;
01329288 6628 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), xx);
0aacf84e 6629 scm_remember_upto_here_1 (y);
73e4de09 6630 return scm_from_bool (0 == cmp);
0aacf84e
MD
6631 }
6632 else if (SCM_REALP (y))
01329288 6633 return scm_from_bool (xx == SCM_REAL_VALUE (y));
0aacf84e 6634 else if (SCM_COMPLEXP (y))
01329288
MW
6635 return scm_from_bool ((xx == SCM_COMPLEX_REAL (y))
6636 && (0.0 == SCM_COMPLEX_IMAG (y)));
f92e85f7 6637 else if (SCM_FRACTIONP (y))
d8b95e27 6638 {
01329288 6639 if (isnan (xx) || isinf (xx))
d8b95e27 6640 return SCM_BOOL_F;
d8b95e27
KR
6641 x = scm_inexact_to_exact (x); /* with x as frac or int */
6642 goto again;
6643 }
0aacf84e 6644 else
8a1f4f98 6645 SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p);
f872b822 6646 }
0aacf84e
MD
6647 else if (SCM_COMPLEXP (x))
6648 {
e11e83f3 6649 if (SCM_I_INUMP (y))
01329288
MW
6650 {
6651 /* see comments with inum/real above */
6652 double rx = SCM_COMPLEX_REAL (x);
6653 scm_t_signed_bits yy = SCM_I_INUM (y);
6654 return scm_from_bool (rx == (double) yy
6655 && 0.0 == SCM_COMPLEX_IMAG (x)
6656 && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
6657 || (scm_t_signed_bits) rx == yy));
6658 }
0aacf84e
MD
6659 else if (SCM_BIGP (y))
6660 {
6661 int cmp;
6662 if (0.0 != SCM_COMPLEX_IMAG (x))
6663 return SCM_BOOL_F;
2e65b52f 6664 if (isnan (SCM_COMPLEX_REAL (x)))
0aacf84e
MD
6665 return SCM_BOOL_F;
6666 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x));
6667 scm_remember_upto_here_1 (y);
73e4de09 6668 return scm_from_bool (0 == cmp);
0aacf84e
MD
6669 }
6670 else if (SCM_REALP (y))
73e4de09 6671 return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
01329288 6672 && (SCM_COMPLEX_IMAG (x) == 0.0));
0aacf84e 6673 else if (SCM_COMPLEXP (y))
73e4de09 6674 return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
01329288 6675 && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
f92e85f7 6676 else if (SCM_FRACTIONP (y))
d8b95e27
KR
6677 {
6678 double xx;
6679 if (SCM_COMPLEX_IMAG (x) != 0.0)
6680 return SCM_BOOL_F;
6681 xx = SCM_COMPLEX_REAL (x);
01329288 6682 if (isnan (xx) || isinf (xx))
d8b95e27 6683 return SCM_BOOL_F;
d8b95e27
KR
6684 x = scm_inexact_to_exact (x); /* with x as frac or int */
6685 goto again;
6686 }
f92e85f7 6687 else
8a1f4f98 6688 SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p);
f92e85f7
MV
6689 }
6690 else if (SCM_FRACTIONP (x))
6691 {
e11e83f3 6692 if (SCM_I_INUMP (y))
f92e85f7
MV
6693 return SCM_BOOL_F;
6694 else if (SCM_BIGP (y))
6695 return SCM_BOOL_F;
6696 else if (SCM_REALP (y))
d8b95e27
KR
6697 {
6698 double yy = SCM_REAL_VALUE (y);
01329288 6699 if (isnan (yy) || isinf (yy))
d8b95e27 6700 return SCM_BOOL_F;
d8b95e27
KR
6701 y = scm_inexact_to_exact (y); /* with y as frac or int */
6702 goto again;
6703 }
f92e85f7 6704 else if (SCM_COMPLEXP (y))
d8b95e27
KR
6705 {
6706 double yy;
6707 if (SCM_COMPLEX_IMAG (y) != 0.0)
6708 return SCM_BOOL_F;
6709 yy = SCM_COMPLEX_REAL (y);
01329288 6710 if (isnan (yy) || isinf(yy))
d8b95e27 6711 return SCM_BOOL_F;
d8b95e27
KR
6712 y = scm_inexact_to_exact (y); /* with y as frac or int */
6713 goto again;
6714 }
f92e85f7
MV
6715 else if (SCM_FRACTIONP (y))
6716 return scm_i_fraction_equalp (x, y);
0aacf84e 6717 else
8a1f4f98 6718 SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn, s_scm_i_num_eq_p);
f4c627b3 6719 }
0aacf84e 6720 else
8a1f4f98 6721 SCM_WTA_DISPATCH_2 (g_scm_i_num_eq_p, x, y, SCM_ARG1, s_scm_i_num_eq_p);
0f2d19dd
JB
6722}
6723
6724
a5f0b599
KR
6725/* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
6726 done are good for inums, but for bignums an answer can almost always be
6727 had by just examining a few high bits of the operands, as done by GMP in
6728 mpq_cmp. flonum/frac compares likewise, but with the slight complication
6729 of the float exponent to take into account. */
6730
8c93b597 6731SCM_INTERNAL SCM scm_i_num_less_p (SCM, SCM, SCM);
8a1f4f98
AW
6732SCM_PRIMITIVE_GENERIC (scm_i_num_less_p, "<", 0, 2, 1,
6733 (SCM x, SCM y, SCM rest),
6734 "Return @code{#t} if the list of parameters is monotonically\n"
6735 "increasing.")
6736#define FUNC_NAME s_scm_i_num_less_p
6737{
6738 if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
6739 return SCM_BOOL_T;
6740 while (!scm_is_null (rest))
6741 {
6742 if (scm_is_false (scm_less_p (x, y)))
6743 return SCM_BOOL_F;
6744 x = y;
6745 y = scm_car (rest);
6746 rest = scm_cdr (rest);
6747 }
6748 return scm_less_p (x, y);
6749}
6750#undef FUNC_NAME
0f2d19dd 6751SCM
6e8d25a6 6752scm_less_p (SCM x, SCM y)
0f2d19dd 6753{
a5f0b599 6754 again:
e11e83f3 6755 if (SCM_I_INUMP (x))
0aacf84e 6756 {
e25f3727 6757 scm_t_inum xx = SCM_I_INUM (x);
e11e83f3 6758 if (SCM_I_INUMP (y))
0aacf84e 6759 {
e25f3727 6760 scm_t_inum yy = SCM_I_INUM (y);
73e4de09 6761 return scm_from_bool (xx < yy);
0aacf84e
MD
6762 }
6763 else if (SCM_BIGP (y))
6764 {
6765 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
6766 scm_remember_upto_here_1 (y);
73e4de09 6767 return scm_from_bool (sgn > 0);
0aacf84e
MD
6768 }
6769 else if (SCM_REALP (y))
95ed2217
MW
6770 {
6771 /* We can safely take the ceiling of y without changing the
6772 result of x<y, given that x is an integer. */
6773 double yy = ceil (SCM_REAL_VALUE (y));
6774
6775 /* In the following comparisons, it's important that the right
6776 hand side always be a power of 2, so that it can be
6777 losslessly converted to a double even on 64-bit
6778 machines. */
6779 if (yy >= (double) (SCM_MOST_POSITIVE_FIXNUM+1))
6780 return SCM_BOOL_T;
6781 else if (!(yy > (double) SCM_MOST_NEGATIVE_FIXNUM))
6782 /* The condition above is carefully written to include the
6783 case where yy==NaN. */
6784 return SCM_BOOL_F;
6785 else
6786 /* yy is a finite integer that fits in an inum. */
6787 return scm_from_bool (xx < (scm_t_inum) yy);
6788 }
f92e85f7 6789 else if (SCM_FRACTIONP (y))
a5f0b599
KR
6790 {
6791 /* "x < a/b" becomes "x*b < a" */
6792 int_frac:
6793 x = scm_product (x, SCM_FRACTION_DENOMINATOR (y));
6794 y = SCM_FRACTION_NUMERATOR (y);
6795 goto again;
6796 }
0aacf84e 6797 else
8a1f4f98 6798 SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p);
f872b822 6799 }
0aacf84e
MD
6800 else if (SCM_BIGP (x))
6801 {
e11e83f3 6802 if (SCM_I_INUMP (y))
0aacf84e
MD
6803 {
6804 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
6805 scm_remember_upto_here_1 (x);
73e4de09 6806 return scm_from_bool (sgn < 0);
0aacf84e
MD
6807 }
6808 else if (SCM_BIGP (y))
6809 {
6810 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
6811 scm_remember_upto_here_2 (x, y);
73e4de09 6812 return scm_from_bool (cmp < 0);
0aacf84e
MD
6813 }
6814 else if (SCM_REALP (y))
6815 {
6816 int cmp;
2e65b52f 6817 if (isnan (SCM_REAL_VALUE (y)))
0aacf84e
MD
6818 return SCM_BOOL_F;
6819 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
6820 scm_remember_upto_here_1 (x);
73e4de09 6821 return scm_from_bool (cmp < 0);
0aacf84e 6822 }
f92e85f7 6823 else if (SCM_FRACTIONP (y))
a5f0b599 6824 goto int_frac;
0aacf84e 6825 else
8a1f4f98 6826 SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p);
f4c627b3 6827 }
0aacf84e
MD
6828 else if (SCM_REALP (x))
6829 {
e11e83f3 6830 if (SCM_I_INUMP (y))
95ed2217
MW
6831 {
6832 /* We can safely take the floor of x without changing the
6833 result of x<y, given that y is an integer. */
6834 double xx = floor (SCM_REAL_VALUE (x));
6835
6836 /* In the following comparisons, it's important that the right
6837 hand side always be a power of 2, so that it can be
6838 losslessly converted to a double even on 64-bit
6839 machines. */
6840 if (xx < (double) SCM_MOST_NEGATIVE_FIXNUM)
6841 return SCM_BOOL_T;
6842 else if (!(xx < (double) (SCM_MOST_POSITIVE_FIXNUM+1)))
6843 /* The condition above is carefully written to include the
6844 case where xx==NaN. */
6845 return SCM_BOOL_F;
6846 else
6847 /* xx is a finite integer that fits in an inum. */
6848 return scm_from_bool ((scm_t_inum) xx < SCM_I_INUM (y));
6849 }
0aacf84e
MD
6850 else if (SCM_BIGP (y))
6851 {
6852 int cmp;
2e65b52f 6853 if (isnan (SCM_REAL_VALUE (x)))
0aacf84e
MD
6854 return SCM_BOOL_F;
6855 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
6856 scm_remember_upto_here_1 (y);
73e4de09 6857 return scm_from_bool (cmp > 0);
0aacf84e
MD
6858 }
6859 else if (SCM_REALP (y))
73e4de09 6860 return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
f92e85f7 6861 else if (SCM_FRACTIONP (y))
a5f0b599
KR
6862 {
6863 double xx = SCM_REAL_VALUE (x);
2e65b52f 6864 if (isnan (xx))
a5f0b599 6865 return SCM_BOOL_F;
2e65b52f 6866 if (isinf (xx))
73e4de09 6867 return scm_from_bool (xx < 0.0);
a5f0b599
KR
6868 x = scm_inexact_to_exact (x); /* with x as frac or int */
6869 goto again;
6870 }
f92e85f7 6871 else
8a1f4f98 6872 SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p);
f92e85f7
MV
6873 }
6874 else if (SCM_FRACTIONP (x))
6875 {
e11e83f3 6876 if (SCM_I_INUMP (y) || SCM_BIGP (y))
a5f0b599
KR
6877 {
6878 /* "a/b < y" becomes "a < y*b" */
6879 y = scm_product (y, SCM_FRACTION_DENOMINATOR (x));
6880 x = SCM_FRACTION_NUMERATOR (x);
6881 goto again;
6882 }
f92e85f7 6883 else if (SCM_REALP (y))
a5f0b599
KR
6884 {
6885 double yy = SCM_REAL_VALUE (y);
2e65b52f 6886 if (isnan (yy))
a5f0b599 6887 return SCM_BOOL_F;
2e65b52f 6888 if (isinf (yy))
73e4de09 6889 return scm_from_bool (0.0 < yy);
a5f0b599
KR
6890 y = scm_inexact_to_exact (y); /* with y as frac or int */
6891 goto again;
6892 }
f92e85f7 6893 else if (SCM_FRACTIONP (y))
a5f0b599
KR
6894 {
6895 /* "a/b < c/d" becomes "a*d < c*b" */
6896 SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x),
6897 SCM_FRACTION_DENOMINATOR (y));
6898 SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y),
6899 SCM_FRACTION_DENOMINATOR (x));
6900 x = new_x;
6901 y = new_y;
6902 goto again;
6903 }
0aacf84e 6904 else
8a1f4f98 6905 SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARGn, s_scm_i_num_less_p);
f872b822 6906 }
0aacf84e 6907 else
8a1f4f98 6908 SCM_WTA_DISPATCH_2 (g_scm_i_num_less_p, x, y, SCM_ARG1, s_scm_i_num_less_p);
0f2d19dd
JB
6909}
6910
6911
8a1f4f98
AW
6912SCM scm_i_num_gr_p (SCM, SCM, SCM);
6913SCM_PRIMITIVE_GENERIC (scm_i_num_gr_p, ">", 0, 2, 1,
6914 (SCM x, SCM y, SCM rest),
6915 "Return @code{#t} if the list of parameters is monotonically\n"
6916 "decreasing.")
6917#define FUNC_NAME s_scm_i_num_gr_p
6918{
6919 if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
6920 return SCM_BOOL_T;
6921 while (!scm_is_null (rest))
6922 {
6923 if (scm_is_false (scm_gr_p (x, y)))
6924 return SCM_BOOL_F;
6925 x = y;
6926 y = scm_car (rest);
6927 rest = scm_cdr (rest);
6928 }
6929 return scm_gr_p (x, y);
6930}
6931#undef FUNC_NAME
6932#define FUNC_NAME s_scm_i_num_gr_p
c76b1eaf
MD
6933SCM
6934scm_gr_p (SCM x, SCM y)
0f2d19dd 6935{
c76b1eaf 6936 if (!SCM_NUMBERP (x))
8a1f4f98 6937 SCM_WTA_DISPATCH_2 (g_scm_i_num_gr_p, x, y, SCM_ARG1, FUNC_NAME);
c76b1eaf 6938 else if (!SCM_NUMBERP (y))
8a1f4f98 6939 SCM_WTA_DISPATCH_2 (g_scm_i_num_gr_p, x, y, SCM_ARG2, FUNC_NAME);
c76b1eaf
MD
6940 else
6941 return scm_less_p (y, x);
0f2d19dd 6942}
1bbd0b84 6943#undef FUNC_NAME
0f2d19dd
JB
6944
6945
8a1f4f98
AW
6946SCM scm_i_num_leq_p (SCM, SCM, SCM);
6947SCM_PRIMITIVE_GENERIC (scm_i_num_leq_p, "<=", 0, 2, 1,
6948 (SCM x, SCM y, SCM rest),
6949 "Return @code{#t} if the list of parameters is monotonically\n"
6950 "non-decreasing.")
6951#define FUNC_NAME s_scm_i_num_leq_p
6952{
6953 if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
6954 return SCM_BOOL_T;
6955 while (!scm_is_null (rest))
6956 {
6957 if (scm_is_false (scm_leq_p (x, y)))
6958 return SCM_BOOL_F;
6959 x = y;
6960 y = scm_car (rest);
6961 rest = scm_cdr (rest);
6962 }
6963 return scm_leq_p (x, y);
6964}
6965#undef FUNC_NAME
6966#define FUNC_NAME s_scm_i_num_leq_p
c76b1eaf
MD
6967SCM
6968scm_leq_p (SCM x, SCM y)
0f2d19dd 6969{
c76b1eaf 6970 if (!SCM_NUMBERP (x))
8a1f4f98 6971 SCM_WTA_DISPATCH_2 (g_scm_i_num_leq_p, x, y, SCM_ARG1, FUNC_NAME);
c76b1eaf 6972 else if (!SCM_NUMBERP (y))
8a1f4f98 6973 SCM_WTA_DISPATCH_2 (g_scm_i_num_leq_p, x, y, SCM_ARG2, FUNC_NAME);
73e4de09 6974 else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
fc194577 6975 return SCM_BOOL_F;
c76b1eaf 6976 else
73e4de09 6977 return scm_not (scm_less_p (y, x));
0f2d19dd 6978}
1bbd0b84 6979#undef FUNC_NAME
0f2d19dd
JB
6980
6981
8a1f4f98
AW
6982SCM scm_i_num_geq_p (SCM, SCM, SCM);
6983SCM_PRIMITIVE_GENERIC (scm_i_num_geq_p, ">=", 0, 2, 1,
6984 (SCM x, SCM y, SCM rest),
6985 "Return @code{#t} if the list of parameters is monotonically\n"
6986 "non-increasing.")
6987#define FUNC_NAME s_scm_i_num_geq_p
6988{
6989 if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
6990 return SCM_BOOL_T;
6991 while (!scm_is_null (rest))
6992 {
6993 if (scm_is_false (scm_geq_p (x, y)))
6994 return SCM_BOOL_F;
6995 x = y;
6996 y = scm_car (rest);
6997 rest = scm_cdr (rest);
6998 }
6999 return scm_geq_p (x, y);
7000}
7001#undef FUNC_NAME
7002#define FUNC_NAME s_scm_i_num_geq_p
c76b1eaf
MD
7003SCM
7004scm_geq_p (SCM x, SCM y)
0f2d19dd 7005{
c76b1eaf 7006 if (!SCM_NUMBERP (x))
8a1f4f98 7007 SCM_WTA_DISPATCH_2 (g_scm_i_num_geq_p, x, y, SCM_ARG1, FUNC_NAME);
c76b1eaf 7008 else if (!SCM_NUMBERP (y))
8a1f4f98 7009 SCM_WTA_DISPATCH_2 (g_scm_i_num_geq_p, x, y, SCM_ARG2, FUNC_NAME);
73e4de09 7010 else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
fc194577 7011 return SCM_BOOL_F;
c76b1eaf 7012 else
73e4de09 7013 return scm_not (scm_less_p (x, y));
0f2d19dd 7014}
1bbd0b84 7015#undef FUNC_NAME
0f2d19dd
JB
7016
7017
2519490c
MW
7018SCM_PRIMITIVE_GENERIC (scm_zero_p, "zero?", 1, 0, 0,
7019 (SCM z),
7020 "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
7021 "zero.")
7022#define FUNC_NAME s_scm_zero_p
0f2d19dd 7023{
e11e83f3 7024 if (SCM_I_INUMP (z))
bc36d050 7025 return scm_from_bool (scm_is_eq (z, SCM_INUM0));
0aacf84e 7026 else if (SCM_BIGP (z))
c2ff8ab0 7027 return SCM_BOOL_F;
0aacf84e 7028 else if (SCM_REALP (z))
73e4de09 7029 return scm_from_bool (SCM_REAL_VALUE (z) == 0.0);
0aacf84e 7030 else if (SCM_COMPLEXP (z))
73e4de09 7031 return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0
c2ff8ab0 7032 && SCM_COMPLEX_IMAG (z) == 0.0);
f92e85f7
MV
7033 else if (SCM_FRACTIONP (z))
7034 return SCM_BOOL_F;
0aacf84e 7035 else
2519490c 7036 SCM_WTA_DISPATCH_1 (g_scm_zero_p, z, SCM_ARG1, s_scm_zero_p);
0f2d19dd 7037}
2519490c 7038#undef FUNC_NAME
0f2d19dd
JB
7039
7040
2519490c
MW
7041SCM_PRIMITIVE_GENERIC (scm_positive_p, "positive?", 1, 0, 0,
7042 (SCM x),
7043 "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
7044 "zero.")
7045#define FUNC_NAME s_scm_positive_p
0f2d19dd 7046{
e11e83f3
MV
7047 if (SCM_I_INUMP (x))
7048 return scm_from_bool (SCM_I_INUM (x) > 0);
0aacf84e
MD
7049 else if (SCM_BIGP (x))
7050 {
7051 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
7052 scm_remember_upto_here_1 (x);
73e4de09 7053 return scm_from_bool (sgn > 0);
0aacf84e
MD
7054 }
7055 else if (SCM_REALP (x))
73e4de09 7056 return scm_from_bool(SCM_REAL_VALUE (x) > 0.0);
f92e85f7
MV
7057 else if (SCM_FRACTIONP (x))
7058 return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
0aacf84e 7059 else
2519490c 7060 SCM_WTA_DISPATCH_1 (g_scm_positive_p, x, SCM_ARG1, s_scm_positive_p);
0f2d19dd 7061}
2519490c 7062#undef FUNC_NAME
0f2d19dd
JB
7063
7064
2519490c
MW
7065SCM_PRIMITIVE_GENERIC (scm_negative_p, "negative?", 1, 0, 0,
7066 (SCM x),
7067 "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
7068 "zero.")
7069#define FUNC_NAME s_scm_negative_p
0f2d19dd 7070{
e11e83f3
MV
7071 if (SCM_I_INUMP (x))
7072 return scm_from_bool (SCM_I_INUM (x) < 0);
0aacf84e
MD
7073 else if (SCM_BIGP (x))
7074 {
7075 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
7076 scm_remember_upto_here_1 (x);
73e4de09 7077 return scm_from_bool (sgn < 0);
0aacf84e
MD
7078 }
7079 else if (SCM_REALP (x))
73e4de09 7080 return scm_from_bool(SCM_REAL_VALUE (x) < 0.0);
f92e85f7
MV
7081 else if (SCM_FRACTIONP (x))
7082 return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
0aacf84e 7083 else
2519490c 7084 SCM_WTA_DISPATCH_1 (g_scm_negative_p, x, SCM_ARG1, s_scm_negative_p);
0f2d19dd 7085}
2519490c 7086#undef FUNC_NAME
0f2d19dd
JB
7087
7088
2a06f791
KR
7089/* scm_min and scm_max return an inexact when either argument is inexact, as
7090 required by r5rs. On that basis, for exact/inexact combinations the
7091 exact is converted to inexact to compare and possibly return. This is
7092 unlike scm_less_p above which takes some trouble to preserve all bits in
7093 its test, such trouble is not required for min and max. */
7094
78d3deb1
AW
7095SCM_PRIMITIVE_GENERIC (scm_i_max, "max", 0, 2, 1,
7096 (SCM x, SCM y, SCM rest),
7097 "Return the maximum of all parameter values.")
7098#define FUNC_NAME s_scm_i_max
7099{
7100 while (!scm_is_null (rest))
7101 { x = scm_max (x, y);
7102 y = scm_car (rest);
7103 rest = scm_cdr (rest);
7104 }
7105 return scm_max (x, y);
7106}
7107#undef FUNC_NAME
7108
7109#define s_max s_scm_i_max
7110#define g_max g_scm_i_max
7111
0f2d19dd 7112SCM
6e8d25a6 7113scm_max (SCM x, SCM y)
0f2d19dd 7114{
0aacf84e
MD
7115 if (SCM_UNBNDP (y))
7116 {
7117 if (SCM_UNBNDP (x))
7118 SCM_WTA_DISPATCH_0 (g_max, s_max);
e11e83f3 7119 else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
0aacf84e
MD
7120 return x;
7121 else
7122 SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max);
f872b822 7123 }
f4c627b3 7124
e11e83f3 7125 if (SCM_I_INUMP (x))
0aacf84e 7126 {
e25f3727 7127 scm_t_inum xx = SCM_I_INUM (x);
e11e83f3 7128 if (SCM_I_INUMP (y))
0aacf84e 7129 {
e25f3727 7130 scm_t_inum yy = SCM_I_INUM (y);
0aacf84e
MD
7131 return (xx < yy) ? y : x;
7132 }
7133 else if (SCM_BIGP (y))
7134 {
7135 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
7136 scm_remember_upto_here_1 (y);
7137 return (sgn < 0) ? x : y;
7138 }
7139 else if (SCM_REALP (y))
7140 {
2e274311
MW
7141 double xxd = xx;
7142 double yyd = SCM_REAL_VALUE (y);
7143
7144 if (xxd > yyd)
7145 return scm_from_double (xxd);
7146 /* If y is a NaN, then "==" is false and we return the NaN */
7147 else if (SCM_LIKELY (!(xxd == yyd)))
7148 return y;
7149 /* Handle signed zeroes properly */
7150 else if (xx == 0)
7151 return flo0;
7152 else
7153 return y;
0aacf84e 7154 }
f92e85f7
MV
7155 else if (SCM_FRACTIONP (y))
7156 {
e4bc5d6c 7157 use_less:
73e4de09 7158 return (scm_is_false (scm_less_p (x, y)) ? x : y);
f92e85f7 7159 }
0aacf84e
MD
7160 else
7161 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
f872b822 7162 }
0aacf84e
MD
7163 else if (SCM_BIGP (x))
7164 {
e11e83f3 7165 if (SCM_I_INUMP (y))
0aacf84e
MD
7166 {
7167 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
7168 scm_remember_upto_here_1 (x);
7169 return (sgn < 0) ? y : x;
7170 }
7171 else if (SCM_BIGP (y))
7172 {
7173 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
7174 scm_remember_upto_here_2 (x, y);
7175 return (cmp > 0) ? x : y;
7176 }
7177 else if (SCM_REALP (y))
7178 {
2a06f791
KR
7179 /* if y==NaN then xx>yy is false, so we return the NaN y */
7180 double xx, yy;
7181 big_real:
7182 xx = scm_i_big2dbl (x);
7183 yy = SCM_REAL_VALUE (y);
55f26379 7184 return (xx > yy ? scm_from_double (xx) : y);
0aacf84e 7185 }
f92e85f7
MV
7186 else if (SCM_FRACTIONP (y))
7187 {
e4bc5d6c 7188 goto use_less;
f92e85f7 7189 }
0aacf84e
MD
7190 else
7191 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
f4c627b3 7192 }
0aacf84e
MD
7193 else if (SCM_REALP (x))
7194 {
e11e83f3 7195 if (SCM_I_INUMP (y))
0aacf84e 7196 {
2e274311
MW
7197 scm_t_inum yy = SCM_I_INUM (y);
7198 double xxd = SCM_REAL_VALUE (x);
7199 double yyd = yy;
7200
7201 if (yyd > xxd)
7202 return scm_from_double (yyd);
7203 /* If x is a NaN, then "==" is false and we return the NaN */
7204 else if (SCM_LIKELY (!(xxd == yyd)))
7205 return x;
7206 /* Handle signed zeroes properly */
7207 else if (yy == 0)
7208 return flo0;
7209 else
7210 return x;
0aacf84e
MD
7211 }
7212 else if (SCM_BIGP (y))
7213 {
b6f8f763 7214 SCM_SWAP (x, y);
2a06f791 7215 goto big_real;
0aacf84e
MD
7216 }
7217 else if (SCM_REALP (y))
7218 {
0aacf84e 7219 double xx = SCM_REAL_VALUE (x);
2e274311
MW
7220 double yy = SCM_REAL_VALUE (y);
7221
b4c55c9c
MW
7222 /* For purposes of max: nan > +inf.0 > everything else,
7223 per the R6RS errata */
2e274311
MW
7224 if (xx > yy)
7225 return x;
7226 else if (SCM_LIKELY (xx < yy))
7227 return y;
7228 /* If neither (xx > yy) nor (xx < yy), then
7229 either they're equal or one is a NaN */
b4c55c9c
MW
7230 else if (SCM_UNLIKELY (xx != yy))
7231 return (xx != xx) ? x : y; /* Return the NaN */
2e274311
MW
7232 /* xx == yy, but handle signed zeroes properly */
7233 else if (double_is_non_negative_zero (yy))
7234 return y;
7235 else
7236 return x;
0aacf84e 7237 }
f92e85f7
MV
7238 else if (SCM_FRACTIONP (y))
7239 {
7240 double yy = scm_i_fraction2double (y);
7241 double xx = SCM_REAL_VALUE (x);
55f26379 7242 return (xx < yy) ? scm_from_double (yy) : x;
f92e85f7
MV
7243 }
7244 else
7245 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
7246 }
7247 else if (SCM_FRACTIONP (x))
7248 {
e11e83f3 7249 if (SCM_I_INUMP (y))
f92e85f7 7250 {
e4bc5d6c 7251 goto use_less;
f92e85f7
MV
7252 }
7253 else if (SCM_BIGP (y))
7254 {
e4bc5d6c 7255 goto use_less;
f92e85f7
MV
7256 }
7257 else if (SCM_REALP (y))
7258 {
7259 double xx = scm_i_fraction2double (x);
2e274311
MW
7260 /* if y==NaN then ">" is false, so we return the NaN y */
7261 return (xx > SCM_REAL_VALUE (y)) ? scm_from_double (xx) : y;
f92e85f7
MV
7262 }
7263 else if (SCM_FRACTIONP (y))
7264 {
e4bc5d6c 7265 goto use_less;
f92e85f7 7266 }
0aacf84e
MD
7267 else
7268 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
f872b822 7269 }
0aacf84e 7270 else
f4c627b3 7271 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max);
0f2d19dd
JB
7272}
7273
7274
78d3deb1
AW
7275SCM_PRIMITIVE_GENERIC (scm_i_min, "min", 0, 2, 1,
7276 (SCM x, SCM y, SCM rest),
7277 "Return the minimum of all parameter values.")
7278#define FUNC_NAME s_scm_i_min
7279{
7280 while (!scm_is_null (rest))
7281 { x = scm_min (x, y);
7282 y = scm_car (rest);
7283 rest = scm_cdr (rest);
7284 }
7285 return scm_min (x, y);
7286}
7287#undef FUNC_NAME
7288
7289#define s_min s_scm_i_min
7290#define g_min g_scm_i_min
7291
0f2d19dd 7292SCM
6e8d25a6 7293scm_min (SCM x, SCM y)
0f2d19dd 7294{
0aacf84e
MD
7295 if (SCM_UNBNDP (y))
7296 {
7297 if (SCM_UNBNDP (x))
7298 SCM_WTA_DISPATCH_0 (g_min, s_min);
e11e83f3 7299 else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
0aacf84e
MD
7300 return x;
7301 else
7302 SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min);
f872b822 7303 }
f4c627b3 7304
e11e83f3 7305 if (SCM_I_INUMP (x))
0aacf84e 7306 {
e25f3727 7307 scm_t_inum xx = SCM_I_INUM (x);
e11e83f3 7308 if (SCM_I_INUMP (y))
0aacf84e 7309 {
e25f3727 7310 scm_t_inum yy = SCM_I_INUM (y);
0aacf84e
MD
7311 return (xx < yy) ? x : y;
7312 }
7313 else if (SCM_BIGP (y))
7314 {
7315 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
7316 scm_remember_upto_here_1 (y);
7317 return (sgn < 0) ? y : x;
7318 }
7319 else if (SCM_REALP (y))
7320 {
7321 double z = xx;
7322 /* if y==NaN then "<" is false and we return NaN */
55f26379 7323 return (z < SCM_REAL_VALUE (y)) ? scm_from_double (z) : y;
0aacf84e 7324 }
f92e85f7
MV
7325 else if (SCM_FRACTIONP (y))
7326 {
e4bc5d6c 7327 use_less:
73e4de09 7328 return (scm_is_false (scm_less_p (x, y)) ? y : x);
f92e85f7 7329 }
0aacf84e
MD
7330 else
7331 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
f872b822 7332 }
0aacf84e
MD
7333 else if (SCM_BIGP (x))
7334 {
e11e83f3 7335 if (SCM_I_INUMP (y))
0aacf84e
MD
7336 {
7337 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
7338 scm_remember_upto_here_1 (x);
7339 return (sgn < 0) ? x : y;
7340 }
7341 else if (SCM_BIGP (y))
7342 {
7343 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
7344 scm_remember_upto_here_2 (x, y);
7345 return (cmp > 0) ? y : x;
7346 }
7347 else if (SCM_REALP (y))
7348 {
2a06f791
KR
7349 /* if y==NaN then xx<yy is false, so we return the NaN y */
7350 double xx, yy;
7351 big_real:
7352 xx = scm_i_big2dbl (x);
7353 yy = SCM_REAL_VALUE (y);
55f26379 7354 return (xx < yy ? scm_from_double (xx) : y);
0aacf84e 7355 }
f92e85f7
MV
7356 else if (SCM_FRACTIONP (y))
7357 {
e4bc5d6c 7358 goto use_less;
f92e85f7 7359 }
0aacf84e
MD
7360 else
7361 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
f4c627b3 7362 }
0aacf84e
MD
7363 else if (SCM_REALP (x))
7364 {
e11e83f3 7365 if (SCM_I_INUMP (y))
0aacf84e 7366 {
e11e83f3 7367 double z = SCM_I_INUM (y);
0aacf84e 7368 /* if x==NaN then "<" is false and we return NaN */
55f26379 7369 return (z < SCM_REAL_VALUE (x)) ? scm_from_double (z) : x;
0aacf84e
MD
7370 }
7371 else if (SCM_BIGP (y))
7372 {
b6f8f763 7373 SCM_SWAP (x, y);
2a06f791 7374 goto big_real;
0aacf84e
MD
7375 }
7376 else if (SCM_REALP (y))
7377 {
0aacf84e 7378 double xx = SCM_REAL_VALUE (x);
2e274311
MW
7379 double yy = SCM_REAL_VALUE (y);
7380
b4c55c9c
MW
7381 /* For purposes of min: nan < -inf.0 < everything else,
7382 per the R6RS errata */
2e274311
MW
7383 if (xx < yy)
7384 return x;
7385 else if (SCM_LIKELY (xx > yy))
7386 return y;
7387 /* If neither (xx < yy) nor (xx > yy), then
7388 either they're equal or one is a NaN */
b4c55c9c
MW
7389 else if (SCM_UNLIKELY (xx != yy))
7390 return (xx != xx) ? x : y; /* Return the NaN */
2e274311
MW
7391 /* xx == yy, but handle signed zeroes properly */
7392 else if (double_is_non_negative_zero (xx))
7393 return y;
7394 else
7395 return x;
0aacf84e 7396 }
f92e85f7
MV
7397 else if (SCM_FRACTIONP (y))
7398 {
7399 double yy = scm_i_fraction2double (y);
7400 double xx = SCM_REAL_VALUE (x);
55f26379 7401 return (yy < xx) ? scm_from_double (yy) : x;
f92e85f7 7402 }
0aacf84e
MD
7403 else
7404 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
f872b822 7405 }
f92e85f7
MV
7406 else if (SCM_FRACTIONP (x))
7407 {
e11e83f3 7408 if (SCM_I_INUMP (y))
f92e85f7 7409 {
e4bc5d6c 7410 goto use_less;
f92e85f7
MV
7411 }
7412 else if (SCM_BIGP (y))
7413 {
e4bc5d6c 7414 goto use_less;
f92e85f7
MV
7415 }
7416 else if (SCM_REALP (y))
7417 {
7418 double xx = scm_i_fraction2double (x);
2e274311
MW
7419 /* if y==NaN then "<" is false, so we return the NaN y */
7420 return (xx < SCM_REAL_VALUE (y)) ? scm_from_double (xx) : y;
f92e85f7
MV
7421 }
7422 else if (SCM_FRACTIONP (y))
7423 {
e4bc5d6c 7424 goto use_less;
f92e85f7
MV
7425 }
7426 else
78d3deb1 7427 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
f92e85f7 7428 }
0aacf84e 7429 else
f4c627b3 7430 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min);
0f2d19dd
JB
7431}
7432
7433
8ccd24f7
AW
7434SCM_PRIMITIVE_GENERIC (scm_i_sum, "+", 0, 2, 1,
7435 (SCM x, SCM y, SCM rest),
7436 "Return the sum of all parameter values. Return 0 if called without\n"
7437 "any parameters." )
7438#define FUNC_NAME s_scm_i_sum
7439{
7440 while (!scm_is_null (rest))
7441 { x = scm_sum (x, y);
7442 y = scm_car (rest);
7443 rest = scm_cdr (rest);
7444 }
7445 return scm_sum (x, y);
7446}
7447#undef FUNC_NAME
7448
7449#define s_sum s_scm_i_sum
7450#define g_sum g_scm_i_sum
7451
0f2d19dd 7452SCM
6e8d25a6 7453scm_sum (SCM x, SCM y)
0f2d19dd 7454{
9cc37597 7455 if (SCM_UNLIKELY (SCM_UNBNDP (y)))
ca46fb90
RB
7456 {
7457 if (SCM_NUMBERP (x)) return x;
7458 if (SCM_UNBNDP (x)) return SCM_INUM0;
98cb6e75 7459 SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum);
f872b822 7460 }
c209c88e 7461
9cc37597 7462 if (SCM_LIKELY (SCM_I_INUMP (x)))
ca46fb90 7463 {
9cc37597 7464 if (SCM_LIKELY (SCM_I_INUMP (y)))
ca46fb90 7465 {
e25f3727
AW
7466 scm_t_inum xx = SCM_I_INUM (x);
7467 scm_t_inum yy = SCM_I_INUM (y);
7468 scm_t_inum z = xx + yy;
7469 return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_inum2big (z);
ca46fb90
RB
7470 }
7471 else if (SCM_BIGP (y))
7472 {
7473 SCM_SWAP (x, y);
7474 goto add_big_inum;
7475 }
7476 else if (SCM_REALP (y))
7477 {
e25f3727 7478 scm_t_inum xx = SCM_I_INUM (x);
55f26379 7479 return scm_from_double (xx + SCM_REAL_VALUE (y));
ca46fb90
RB
7480 }
7481 else if (SCM_COMPLEXP (y))
7482 {
e25f3727 7483 scm_t_inum xx = SCM_I_INUM (x);
8507ec80 7484 return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y),
ca46fb90
RB
7485 SCM_COMPLEX_IMAG (y));
7486 }
f92e85f7 7487 else if (SCM_FRACTIONP (y))
cba42c93 7488 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
f92e85f7
MV
7489 scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
7490 SCM_FRACTION_DENOMINATOR (y));
ca46fb90
RB
7491 else
7492 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
0aacf84e
MD
7493 } else if (SCM_BIGP (x))
7494 {
e11e83f3 7495 if (SCM_I_INUMP (y))
0aacf84e 7496 {
e25f3727 7497 scm_t_inum inum;
0aacf84e
MD
7498 int bigsgn;
7499 add_big_inum:
e11e83f3 7500 inum = SCM_I_INUM (y);
0aacf84e
MD
7501 if (inum == 0)
7502 return x;
7503 bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x));
7504 if (inum < 0)
7505 {
7506 SCM result = scm_i_mkbig ();
7507 mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum);
7508 scm_remember_upto_here_1 (x);
7509 /* we know the result will have to be a bignum */
7510 if (bigsgn == -1)
7511 return result;
7512 return scm_i_normbig (result);
7513 }
7514 else
7515 {
7516 SCM result = scm_i_mkbig ();
7517 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum);
7518 scm_remember_upto_here_1 (x);
7519 /* we know the result will have to be a bignum */
7520 if (bigsgn == 1)
7521 return result;
7522 return scm_i_normbig (result);
7523 }
7524 }
7525 else if (SCM_BIGP (y))
7526 {
7527 SCM result = scm_i_mkbig ();
7528 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
7529 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
7530 mpz_add (SCM_I_BIG_MPZ (result),
7531 SCM_I_BIG_MPZ (x),
7532 SCM_I_BIG_MPZ (y));
7533 scm_remember_upto_here_2 (x, y);
7534 /* we know the result will have to be a bignum */
7535 if (sgn_x == sgn_y)
7536 return result;
7537 return scm_i_normbig (result);
7538 }
7539 else if (SCM_REALP (y))
7540 {
7541 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y);
7542 scm_remember_upto_here_1 (x);
55f26379 7543 return scm_from_double (result);
0aacf84e
MD
7544 }
7545 else if (SCM_COMPLEXP (y))
7546 {
7547 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
7548 + SCM_COMPLEX_REAL (y));
7549 scm_remember_upto_here_1 (x);
8507ec80 7550 return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
0aacf84e 7551 }
f92e85f7 7552 else if (SCM_FRACTIONP (y))
cba42c93 7553 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
f92e85f7
MV
7554 scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
7555 SCM_FRACTION_DENOMINATOR (y));
0aacf84e
MD
7556 else
7557 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
0f2d19dd 7558 }
0aacf84e
MD
7559 else if (SCM_REALP (x))
7560 {
e11e83f3 7561 if (SCM_I_INUMP (y))
55f26379 7562 return scm_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y));
0aacf84e
MD
7563 else if (SCM_BIGP (y))
7564 {
7565 double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x);
7566 scm_remember_upto_here_1 (y);
55f26379 7567 return scm_from_double (result);
0aacf84e
MD
7568 }
7569 else if (SCM_REALP (y))
55f26379 7570 return scm_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
0aacf84e 7571 else if (SCM_COMPLEXP (y))
8507ec80 7572 return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
0aacf84e 7573 SCM_COMPLEX_IMAG (y));
f92e85f7 7574 else if (SCM_FRACTIONP (y))
55f26379 7575 return scm_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
0aacf84e
MD
7576 else
7577 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
f872b822 7578 }
0aacf84e
MD
7579 else if (SCM_COMPLEXP (x))
7580 {
e11e83f3 7581 if (SCM_I_INUMP (y))
8507ec80 7582 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y),
0aacf84e
MD
7583 SCM_COMPLEX_IMAG (x));
7584 else if (SCM_BIGP (y))
7585 {
7586 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y))
7587 + SCM_COMPLEX_REAL (x));
7588 scm_remember_upto_here_1 (y);
8507ec80 7589 return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x));
0aacf84e
MD
7590 }
7591 else if (SCM_REALP (y))
8507ec80 7592 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
0aacf84e
MD
7593 SCM_COMPLEX_IMAG (x));
7594 else if (SCM_COMPLEXP (y))
8507ec80 7595 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
0aacf84e 7596 SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
f92e85f7 7597 else if (SCM_FRACTIONP (y))
8507ec80 7598 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
f92e85f7
MV
7599 SCM_COMPLEX_IMAG (x));
7600 else
7601 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
7602 }
7603 else if (SCM_FRACTIONP (x))
7604 {
e11e83f3 7605 if (SCM_I_INUMP (y))
cba42c93 7606 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
f92e85f7
MV
7607 scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
7608 SCM_FRACTION_DENOMINATOR (x));
7609 else if (SCM_BIGP (y))
cba42c93 7610 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
f92e85f7
MV
7611 scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
7612 SCM_FRACTION_DENOMINATOR (x));
7613 else if (SCM_REALP (y))
55f26379 7614 return scm_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x));
f92e85f7 7615 else if (SCM_COMPLEXP (y))
8507ec80 7616 return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x),
f92e85f7
MV
7617 SCM_COMPLEX_IMAG (y));
7618 else if (SCM_FRACTIONP (y))
7619 /* a/b + c/d = (ad + bc) / bd */
cba42c93 7620 return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
f92e85f7
MV
7621 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
7622 scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
0aacf84e
MD
7623 else
7624 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
98cb6e75 7625 }
0aacf84e 7626 else
98cb6e75 7627 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum);
0f2d19dd
JB
7628}
7629
7630
40882e3d
KR
7631SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0,
7632 (SCM x),
7633 "Return @math{@var{x}+1}.")
7634#define FUNC_NAME s_scm_oneplus
7635{
cff5fa33 7636 return scm_sum (x, SCM_INUM1);
40882e3d
KR
7637}
7638#undef FUNC_NAME
7639
7640
78d3deb1
AW
7641SCM_PRIMITIVE_GENERIC (scm_i_difference, "-", 0, 2, 1,
7642 (SCM x, SCM y, SCM rest),
7643 "If called with one argument @var{z1}, -@var{z1} returned. Otherwise\n"
7644 "the sum of all but the first argument are subtracted from the first\n"
7645 "argument.")
7646#define FUNC_NAME s_scm_i_difference
7647{
7648 while (!scm_is_null (rest))
7649 { x = scm_difference (x, y);
7650 y = scm_car (rest);
7651 rest = scm_cdr (rest);
7652 }
7653 return scm_difference (x, y);
7654}
7655#undef FUNC_NAME
7656
7657#define s_difference s_scm_i_difference
7658#define g_difference g_scm_i_difference
7659
0f2d19dd 7660SCM
6e8d25a6 7661scm_difference (SCM x, SCM y)
78d3deb1 7662#define FUNC_NAME s_difference
0f2d19dd 7663{
9cc37597 7664 if (SCM_UNLIKELY (SCM_UNBNDP (y)))
ca46fb90
RB
7665 {
7666 if (SCM_UNBNDP (x))
7667 SCM_WTA_DISPATCH_0 (g_difference, s_difference);
7668 else
e11e83f3 7669 if (SCM_I_INUMP (x))
ca46fb90 7670 {
e25f3727 7671 scm_t_inum xx = -SCM_I_INUM (x);
ca46fb90 7672 if (SCM_FIXABLE (xx))
d956fa6f 7673 return SCM_I_MAKINUM (xx);
ca46fb90 7674 else
e25f3727 7675 return scm_i_inum2big (xx);
ca46fb90
RB
7676 }
7677 else if (SCM_BIGP (x))
a9ad4847
KR
7678 /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
7679 bignum, but negating that gives a fixnum. */
ca46fb90
RB
7680 return scm_i_normbig (scm_i_clonebig (x, 0));
7681 else if (SCM_REALP (x))
55f26379 7682 return scm_from_double (-SCM_REAL_VALUE (x));
ca46fb90 7683 else if (SCM_COMPLEXP (x))
8507ec80 7684 return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x),
ca46fb90 7685 -SCM_COMPLEX_IMAG (x));
f92e85f7 7686 else if (SCM_FRACTIONP (x))
a285b18c
MW
7687 return scm_i_make_ratio_already_reduced
7688 (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
7689 SCM_FRACTION_DENOMINATOR (x));
ca46fb90
RB
7690 else
7691 SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference);
f872b822 7692 }
ca46fb90 7693
9cc37597 7694 if (SCM_LIKELY (SCM_I_INUMP (x)))
0aacf84e 7695 {
9cc37597 7696 if (SCM_LIKELY (SCM_I_INUMP (y)))
0aacf84e 7697 {
e25f3727
AW
7698 scm_t_inum xx = SCM_I_INUM (x);
7699 scm_t_inum yy = SCM_I_INUM (y);
7700 scm_t_inum z = xx - yy;
0aacf84e 7701 if (SCM_FIXABLE (z))
d956fa6f 7702 return SCM_I_MAKINUM (z);
0aacf84e 7703 else
e25f3727 7704 return scm_i_inum2big (z);
0aacf84e
MD
7705 }
7706 else if (SCM_BIGP (y))
7707 {
7708 /* inum-x - big-y */
e25f3727 7709 scm_t_inum xx = SCM_I_INUM (x);
ca46fb90 7710
0aacf84e 7711 if (xx == 0)
b5c40589
MW
7712 {
7713 /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
7714 bignum, but negating that gives a fixnum. */
7715 return scm_i_normbig (scm_i_clonebig (y, 0));
7716 }
0aacf84e
MD
7717 else
7718 {
7719 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
7720 SCM result = scm_i_mkbig ();
ca46fb90 7721
0aacf84e
MD
7722 if (xx >= 0)
7723 mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y));
7724 else
7725 {
7726 /* x - y == -(y + -x) */
7727 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx);
7728 mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
7729 }
7730 scm_remember_upto_here_1 (y);
ca46fb90 7731
0aacf84e
MD
7732 if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0))
7733 /* we know the result will have to be a bignum */
7734 return result;
7735 else
7736 return scm_i_normbig (result);
7737 }
7738 }
7739 else if (SCM_REALP (y))
7740 {
e25f3727 7741 scm_t_inum xx = SCM_I_INUM (x);
9b9ef10c
MW
7742
7743 /*
7744 * We need to handle x == exact 0
7745 * specially because R6RS states that:
7746 * (- 0.0) ==> -0.0 and
7747 * (- 0.0 0.0) ==> 0.0
7748 * and the scheme compiler changes
7749 * (- 0.0) into (- 0 0.0)
7750 * So we need to treat (- 0 0.0) like (- 0.0).
7751 * At the C level, (-x) is different than (0.0 - x).
7752 * (0.0 - 0.0) ==> 0.0, but (- 0.0) ==> -0.0.
7753 */
7754 if (xx == 0)
7755 return scm_from_double (- SCM_REAL_VALUE (y));
7756 else
7757 return scm_from_double (xx - SCM_REAL_VALUE (y));
0aacf84e
MD
7758 }
7759 else if (SCM_COMPLEXP (y))
7760 {
e25f3727 7761 scm_t_inum xx = SCM_I_INUM (x);
9b9ef10c
MW
7762
7763 /* We need to handle x == exact 0 specially.
7764 See the comment above (for SCM_REALP (y)) */
7765 if (xx == 0)
7766 return scm_c_make_rectangular (- SCM_COMPLEX_REAL (y),
7767 - SCM_COMPLEX_IMAG (y));
7768 else
7769 return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y),
7770 - SCM_COMPLEX_IMAG (y));
0aacf84e 7771 }
f92e85f7
MV
7772 else if (SCM_FRACTIONP (y))
7773 /* a - b/c = (ac - b) / c */
cba42c93 7774 return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
f92e85f7
MV
7775 SCM_FRACTION_NUMERATOR (y)),
7776 SCM_FRACTION_DENOMINATOR (y));
0aacf84e
MD
7777 else
7778 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
f872b822 7779 }
0aacf84e
MD
7780 else if (SCM_BIGP (x))
7781 {
e11e83f3 7782 if (SCM_I_INUMP (y))
0aacf84e
MD
7783 {
7784 /* big-x - inum-y */
e25f3727 7785 scm_t_inum yy = SCM_I_INUM (y);
0aacf84e 7786 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
ca46fb90 7787
0aacf84e
MD
7788 scm_remember_upto_here_1 (x);
7789 if (sgn_x == 0)
c71b0706 7790 return (SCM_FIXABLE (-yy) ?
e25f3727 7791 SCM_I_MAKINUM (-yy) : scm_from_inum (-yy));
0aacf84e
MD
7792 else
7793 {
7794 SCM result = scm_i_mkbig ();
ca46fb90 7795
708f22c6
KR
7796 if (yy >= 0)
7797 mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
7798 else
7799 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy);
0aacf84e 7800 scm_remember_upto_here_1 (x);
ca46fb90 7801
0aacf84e
MD
7802 if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0))
7803 /* we know the result will have to be a bignum */
7804 return result;
7805 else
7806 return scm_i_normbig (result);
7807 }
7808 }
7809 else if (SCM_BIGP (y))
7810 {
7811 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
7812 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
7813 SCM result = scm_i_mkbig ();
7814 mpz_sub (SCM_I_BIG_MPZ (result),
7815 SCM_I_BIG_MPZ (x),
7816 SCM_I_BIG_MPZ (y));
7817 scm_remember_upto_here_2 (x, y);
7818 /* we know the result will have to be a bignum */
7819 if ((sgn_x == 1) && (sgn_y == -1))
7820 return result;
7821 if ((sgn_x == -1) && (sgn_y == 1))
7822 return result;
7823 return scm_i_normbig (result);
7824 }
7825 else if (SCM_REALP (y))
7826 {
7827 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y);
7828 scm_remember_upto_here_1 (x);
55f26379 7829 return scm_from_double (result);
0aacf84e
MD
7830 }
7831 else if (SCM_COMPLEXP (y))
7832 {
7833 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
7834 - SCM_COMPLEX_REAL (y));
7835 scm_remember_upto_here_1 (x);
8507ec80 7836 return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y));
0aacf84e 7837 }
f92e85f7 7838 else if (SCM_FRACTIONP (y))
cba42c93 7839 return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
f92e85f7
MV
7840 SCM_FRACTION_NUMERATOR (y)),
7841 SCM_FRACTION_DENOMINATOR (y));
0aacf84e 7842 else SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
ca46fb90 7843 }
0aacf84e
MD
7844 else if (SCM_REALP (x))
7845 {
e11e83f3 7846 if (SCM_I_INUMP (y))
55f26379 7847 return scm_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y));
0aacf84e
MD
7848 else if (SCM_BIGP (y))
7849 {
7850 double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y));
7851 scm_remember_upto_here_1 (x);
55f26379 7852 return scm_from_double (result);
0aacf84e
MD
7853 }
7854 else if (SCM_REALP (y))
55f26379 7855 return scm_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
0aacf84e 7856 else if (SCM_COMPLEXP (y))
8507ec80 7857 return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
0aacf84e 7858 -SCM_COMPLEX_IMAG (y));
f92e85f7 7859 else if (SCM_FRACTIONP (y))
55f26379 7860 return scm_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y));
0aacf84e
MD
7861 else
7862 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
98cb6e75 7863 }
0aacf84e
MD
7864 else if (SCM_COMPLEXP (x))
7865 {
e11e83f3 7866 if (SCM_I_INUMP (y))
8507ec80 7867 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y),
0aacf84e
MD
7868 SCM_COMPLEX_IMAG (x));
7869 else if (SCM_BIGP (y))
7870 {
7871 double real_part = (SCM_COMPLEX_REAL (x)
7872 - mpz_get_d (SCM_I_BIG_MPZ (y)));
7873 scm_remember_upto_here_1 (x);
8507ec80 7874 return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
0aacf84e
MD
7875 }
7876 else if (SCM_REALP (y))
8507ec80 7877 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
0aacf84e
MD
7878 SCM_COMPLEX_IMAG (x));
7879 else if (SCM_COMPLEXP (y))
8507ec80 7880 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
0aacf84e 7881 SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
f92e85f7 7882 else if (SCM_FRACTIONP (y))
8507ec80 7883 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y),
f92e85f7
MV
7884 SCM_COMPLEX_IMAG (x));
7885 else
7886 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
7887 }
7888 else if (SCM_FRACTIONP (x))
7889 {
e11e83f3 7890 if (SCM_I_INUMP (y))
f92e85f7 7891 /* a/b - c = (a - cb) / b */
cba42c93 7892 return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
f92e85f7
MV
7893 scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
7894 SCM_FRACTION_DENOMINATOR (x));
7895 else if (SCM_BIGP (y))
cba42c93 7896 return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
f92e85f7
MV
7897 scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
7898 SCM_FRACTION_DENOMINATOR (x));
7899 else if (SCM_REALP (y))
55f26379 7900 return scm_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y));
f92e85f7 7901 else if (SCM_COMPLEXP (y))
8507ec80 7902 return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y),
f92e85f7
MV
7903 -SCM_COMPLEX_IMAG (y));
7904 else if (SCM_FRACTIONP (y))
7905 /* a/b - c/d = (ad - bc) / bd */
cba42c93 7906 return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
f92e85f7
MV
7907 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
7908 scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
0aacf84e
MD
7909 else
7910 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
98cb6e75 7911 }
0aacf84e 7912 else
98cb6e75 7913 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference);
0f2d19dd 7914}
c05e97b7 7915#undef FUNC_NAME
0f2d19dd 7916
ca46fb90 7917
40882e3d
KR
7918SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0,
7919 (SCM x),
7920 "Return @math{@var{x}-1}.")
7921#define FUNC_NAME s_scm_oneminus
7922{
cff5fa33 7923 return scm_difference (x, SCM_INUM1);
40882e3d
KR
7924}
7925#undef FUNC_NAME
7926
7927
78d3deb1
AW
7928SCM_PRIMITIVE_GENERIC (scm_i_product, "*", 0, 2, 1,
7929 (SCM x, SCM y, SCM rest),
7930 "Return the product of all arguments. If called without arguments,\n"
7931 "1 is returned.")
7932#define FUNC_NAME s_scm_i_product
7933{
7934 while (!scm_is_null (rest))
7935 { x = scm_product (x, y);
7936 y = scm_car (rest);
7937 rest = scm_cdr (rest);
7938 }
7939 return scm_product (x, y);
7940}
7941#undef FUNC_NAME
7942
7943#define s_product s_scm_i_product
7944#define g_product g_scm_i_product
7945
0f2d19dd 7946SCM
6e8d25a6 7947scm_product (SCM x, SCM y)
0f2d19dd 7948{
9cc37597 7949 if (SCM_UNLIKELY (SCM_UNBNDP (y)))
0aacf84e
MD
7950 {
7951 if (SCM_UNBNDP (x))
d956fa6f 7952 return SCM_I_MAKINUM (1L);
0aacf84e
MD
7953 else if (SCM_NUMBERP (x))
7954 return x;
7955 else
7956 SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product);
f872b822 7957 }
ca46fb90 7958
9cc37597 7959 if (SCM_LIKELY (SCM_I_INUMP (x)))
0aacf84e 7960 {
e25f3727 7961 scm_t_inum xx;
f4c627b3 7962
5e791807 7963 xinum:
e11e83f3 7964 xx = SCM_I_INUM (x);
f4c627b3 7965
0aacf84e
MD
7966 switch (xx)
7967 {
5e791807
MW
7968 case 1:
7969 /* exact1 is the universal multiplicative identity */
7970 return y;
7971 break;
7972 case 0:
7973 /* exact0 times a fixnum is exact0: optimize this case */
7974 if (SCM_LIKELY (SCM_I_INUMP (y)))
7975 return SCM_INUM0;
7976 /* if the other argument is inexact, the result is inexact,
7977 and we must do the multiplication in order to handle
7978 infinities and NaNs properly. */
7979 else if (SCM_REALP (y))
7980 return scm_from_double (0.0 * SCM_REAL_VALUE (y));
7981 else if (SCM_COMPLEXP (y))
7982 return scm_c_make_rectangular (0.0 * SCM_COMPLEX_REAL (y),
7983 0.0 * SCM_COMPLEX_IMAG (y));
7984 /* we've already handled inexact numbers,
7985 so y must be exact, and we return exact0 */
7986 else if (SCM_NUMP (y))
7987 return SCM_INUM0;
7988 else
7989 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
7990 break;
7991 case -1:
b5c40589 7992 /*
5e791807
MW
7993 * This case is important for more than just optimization.
7994 * It handles the case of negating
b5c40589
MW
7995 * (+ 1 most-positive-fixnum) aka (- most-negative-fixnum),
7996 * which is a bignum that must be changed back into a fixnum.
7997 * Failure to do so will cause the following to return #f:
7998 * (= most-negative-fixnum (* -1 (- most-negative-fixnum)))
7999 */
b5c40589
MW
8000 return scm_difference(y, SCM_UNDEFINED);
8001 break;
0aacf84e 8002 }
f4c627b3 8003
9cc37597 8004 if (SCM_LIKELY (SCM_I_INUMP (y)))
0aacf84e 8005 {
e25f3727 8006 scm_t_inum yy = SCM_I_INUM (y);
2355f017
MW
8007#if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64
8008 scm_t_int64 kk = xx * (scm_t_int64) yy;
8009 if (SCM_FIXABLE (kk))
8010 return SCM_I_MAKINUM (kk);
8011#else
8012 scm_t_inum axx = (xx > 0) ? xx : -xx;
8013 scm_t_inum ayy = (yy > 0) ? yy : -yy;
8014 if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy)
8015 return SCM_I_MAKINUM (xx * yy);
8016#endif
0aacf84e
MD
8017 else
8018 {
e25f3727 8019 SCM result = scm_i_inum2big (xx);
0aacf84e
MD
8020 mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy);
8021 return scm_i_normbig (result);
8022 }
8023 }
8024 else if (SCM_BIGP (y))
8025 {
8026 SCM result = scm_i_mkbig ();
8027 mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx);
8028 scm_remember_upto_here_1 (y);
8029 return result;
8030 }
8031 else if (SCM_REALP (y))
55f26379 8032 return scm_from_double (xx * SCM_REAL_VALUE (y));
0aacf84e 8033 else if (SCM_COMPLEXP (y))
8507ec80 8034 return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
0aacf84e 8035 xx * SCM_COMPLEX_IMAG (y));
f92e85f7 8036 else if (SCM_FRACTIONP (y))
cba42c93 8037 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
f92e85f7 8038 SCM_FRACTION_DENOMINATOR (y));
0aacf84e
MD
8039 else
8040 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
f4c627b3 8041 }
0aacf84e
MD
8042 else if (SCM_BIGP (x))
8043 {
e11e83f3 8044 if (SCM_I_INUMP (y))
0aacf84e
MD
8045 {
8046 SCM_SWAP (x, y);
5e791807 8047 goto xinum;
0aacf84e
MD
8048 }
8049 else if (SCM_BIGP (y))
8050 {
8051 SCM result = scm_i_mkbig ();
8052 mpz_mul (SCM_I_BIG_MPZ (result),
8053 SCM_I_BIG_MPZ (x),
8054 SCM_I_BIG_MPZ (y));
8055 scm_remember_upto_here_2 (x, y);
8056 return result;
8057 }
8058 else if (SCM_REALP (y))
8059 {
8060 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y);
8061 scm_remember_upto_here_1 (x);
55f26379 8062 return scm_from_double (result);
0aacf84e
MD
8063 }
8064 else if (SCM_COMPLEXP (y))
8065 {
8066 double z = mpz_get_d (SCM_I_BIG_MPZ (x));
8067 scm_remember_upto_here_1 (x);
8507ec80 8068 return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y),
0aacf84e
MD
8069 z * SCM_COMPLEX_IMAG (y));
8070 }
f92e85f7 8071 else if (SCM_FRACTIONP (y))
cba42c93 8072 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
f92e85f7 8073 SCM_FRACTION_DENOMINATOR (y));
0aacf84e
MD
8074 else
8075 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
f4c627b3 8076 }
0aacf84e
MD
8077 else if (SCM_REALP (x))
8078 {
e11e83f3 8079 if (SCM_I_INUMP (y))
5e791807
MW
8080 {
8081 SCM_SWAP (x, y);
8082 goto xinum;
8083 }
0aacf84e
MD
8084 else if (SCM_BIGP (y))
8085 {
8086 double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x);
8087 scm_remember_upto_here_1 (y);
55f26379 8088 return scm_from_double (result);
0aacf84e
MD
8089 }
8090 else if (SCM_REALP (y))
55f26379 8091 return scm_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
0aacf84e 8092 else if (SCM_COMPLEXP (y))
8507ec80 8093 return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
0aacf84e 8094 SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
f92e85f7 8095 else if (SCM_FRACTIONP (y))
55f26379 8096 return scm_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
0aacf84e
MD
8097 else
8098 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
f4c627b3 8099 }
0aacf84e
MD
8100 else if (SCM_COMPLEXP (x))
8101 {
e11e83f3 8102 if (SCM_I_INUMP (y))
5e791807
MW
8103 {
8104 SCM_SWAP (x, y);
8105 goto xinum;
8106 }
0aacf84e
MD
8107 else if (SCM_BIGP (y))
8108 {
8109 double z = mpz_get_d (SCM_I_BIG_MPZ (y));
8110 scm_remember_upto_here_1 (y);
8507ec80 8111 return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x),
76506335 8112 z * SCM_COMPLEX_IMAG (x));
0aacf84e
MD
8113 }
8114 else if (SCM_REALP (y))
8507ec80 8115 return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
0aacf84e
MD
8116 SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
8117 else if (SCM_COMPLEXP (y))
8118 {
8507ec80 8119 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
0aacf84e
MD
8120 - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
8121 SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
8122 + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
8123 }
f92e85f7
MV
8124 else if (SCM_FRACTIONP (y))
8125 {
8126 double yy = scm_i_fraction2double (y);
8507ec80 8127 return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x),
f92e85f7
MV
8128 yy * SCM_COMPLEX_IMAG (x));
8129 }
8130 else
8131 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
8132 }
8133 else if (SCM_FRACTIONP (x))
8134 {
e11e83f3 8135 if (SCM_I_INUMP (y))
cba42c93 8136 return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
f92e85f7
MV
8137 SCM_FRACTION_DENOMINATOR (x));
8138 else if (SCM_BIGP (y))
cba42c93 8139 return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
f92e85f7
MV
8140 SCM_FRACTION_DENOMINATOR (x));
8141 else if (SCM_REALP (y))
55f26379 8142 return scm_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y));
f92e85f7
MV
8143 else if (SCM_COMPLEXP (y))
8144 {
8145 double xx = scm_i_fraction2double (x);
8507ec80 8146 return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
f92e85f7
MV
8147 xx * SCM_COMPLEX_IMAG (y));
8148 }
8149 else if (SCM_FRACTIONP (y))
8150 /* a/b * c/d = ac / bd */
cba42c93 8151 return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
f92e85f7
MV
8152 SCM_FRACTION_NUMERATOR (y)),
8153 scm_product (SCM_FRACTION_DENOMINATOR (x),
8154 SCM_FRACTION_DENOMINATOR (y)));
0aacf84e
MD
8155 else
8156 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
f4c627b3 8157 }
0aacf84e 8158 else
f4c627b3 8159 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product);
0f2d19dd
JB
8160}
8161
7351e207
MV
8162#if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \
8163 || (defined (HAVE_FINITE) && defined (HAVE_ISNAN)))
8164#define ALLOW_DIVIDE_BY_ZERO
8165/* #define ALLOW_DIVIDE_BY_EXACT_ZERO */
8166#endif
0f2d19dd 8167
ba74ef4e
MV
8168/* The code below for complex division is adapted from the GNU
8169 libstdc++, which adapted it from f2c's libF77, and is subject to
8170 this copyright: */
8171
8172/****************************************************************
8173Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
8174
8175Permission to use, copy, modify, and distribute this software
8176and its documentation for any purpose and without fee is hereby
8177granted, provided that the above copyright notice appear in all
8178copies and that both that the copyright notice and this
8179permission notice and warranty disclaimer appear in supporting
8180documentation, and that the names of AT&T Bell Laboratories or
8181Bellcore or any of their entities not be used in advertising or
8182publicity pertaining to distribution of the software without
8183specific, written prior permission.
8184
8185AT&T and Bellcore disclaim all warranties with regard to this
8186software, including all implied warranties of merchantability
8187and fitness. In no event shall AT&T or Bellcore be liable for
8188any special, indirect or consequential damages or any damages
8189whatsoever resulting from loss of use, data or profits, whether
8190in an action of contract, negligence or other tortious action,
8191arising out of or in connection with the use or performance of
8192this software.
8193****************************************************************/
8194
78d3deb1
AW
8195SCM_PRIMITIVE_GENERIC (scm_i_divide, "/", 0, 2, 1,
8196 (SCM x, SCM y, SCM rest),
8197 "Divide the first argument by the product of the remaining\n"
8198 "arguments. If called with one argument @var{z1}, 1/@var{z1} is\n"
8199 "returned.")
8200#define FUNC_NAME s_scm_i_divide
8201{
8202 while (!scm_is_null (rest))
8203 { x = scm_divide (x, y);
8204 y = scm_car (rest);
8205 rest = scm_cdr (rest);
8206 }
8207 return scm_divide (x, y);
8208}
8209#undef FUNC_NAME
8210
8211#define s_divide s_scm_i_divide
8212#define g_divide g_scm_i_divide
8213
98237784
MW
8214SCM
8215scm_divide (SCM x, SCM y)
78d3deb1 8216#define FUNC_NAME s_divide
0f2d19dd 8217{
f8de44c1
DH
8218 double a;
8219
9cc37597 8220 if (SCM_UNLIKELY (SCM_UNBNDP (y)))
0aacf84e
MD
8221 {
8222 if (SCM_UNBNDP (x))
8223 SCM_WTA_DISPATCH_0 (g_divide, s_divide);
e11e83f3 8224 else if (SCM_I_INUMP (x))
0aacf84e 8225 {
e25f3727 8226 scm_t_inum xx = SCM_I_INUM (x);
0aacf84e
MD
8227 if (xx == 1 || xx == -1)
8228 return x;
7351e207 8229#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
0aacf84e
MD
8230 else if (xx == 0)
8231 scm_num_overflow (s_divide);
7351e207 8232#endif
0aacf84e 8233 else
98237784 8234 return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
0aacf84e
MD
8235 }
8236 else if (SCM_BIGP (x))
98237784 8237 return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
0aacf84e
MD
8238 else if (SCM_REALP (x))
8239 {
8240 double xx = SCM_REAL_VALUE (x);
7351e207 8241#ifndef ALLOW_DIVIDE_BY_ZERO
0aacf84e
MD
8242 if (xx == 0.0)
8243 scm_num_overflow (s_divide);
8244 else
7351e207 8245#endif
55f26379 8246 return scm_from_double (1.0 / xx);
0aacf84e
MD
8247 }
8248 else if (SCM_COMPLEXP (x))
8249 {
8250 double r = SCM_COMPLEX_REAL (x);
8251 double i = SCM_COMPLEX_IMAG (x);
4c6e36a6 8252 if (fabs(r) <= fabs(i))
0aacf84e
MD
8253 {
8254 double t = r / i;
8255 double d = i * (1.0 + t * t);
8507ec80 8256 return scm_c_make_rectangular (t / d, -1.0 / d);
0aacf84e
MD
8257 }
8258 else
8259 {
8260 double t = i / r;
8261 double d = r * (1.0 + t * t);
8507ec80 8262 return scm_c_make_rectangular (1.0 / d, -t / d);
0aacf84e
MD
8263 }
8264 }
f92e85f7 8265 else if (SCM_FRACTIONP (x))
a285b18c
MW
8266 return scm_i_make_ratio_already_reduced (SCM_FRACTION_DENOMINATOR (x),
8267 SCM_FRACTION_NUMERATOR (x));
0aacf84e
MD
8268 else
8269 SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide);
f8de44c1 8270 }
f8de44c1 8271
9cc37597 8272 if (SCM_LIKELY (SCM_I_INUMP (x)))
0aacf84e 8273 {
e25f3727 8274 scm_t_inum xx = SCM_I_INUM (x);
9cc37597 8275 if (SCM_LIKELY (SCM_I_INUMP (y)))
0aacf84e 8276 {
e25f3727 8277 scm_t_inum yy = SCM_I_INUM (y);
0aacf84e
MD
8278 if (yy == 0)
8279 {
7351e207 8280#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
0aacf84e 8281 scm_num_overflow (s_divide);
7351e207 8282#else
55f26379 8283 return scm_from_double ((double) xx / (double) yy);
7351e207 8284#endif
0aacf84e
MD
8285 }
8286 else if (xx % yy != 0)
98237784 8287 return scm_i_make_ratio (x, y);
0aacf84e
MD
8288 else
8289 {
e25f3727 8290 scm_t_inum z = xx / yy;
0aacf84e 8291 if (SCM_FIXABLE (z))
d956fa6f 8292 return SCM_I_MAKINUM (z);
0aacf84e 8293 else
e25f3727 8294 return scm_i_inum2big (z);
0aacf84e 8295 }
f872b822 8296 }
0aacf84e 8297 else if (SCM_BIGP (y))
98237784 8298 return scm_i_make_ratio (x, y);
0aacf84e
MD
8299 else if (SCM_REALP (y))
8300 {
8301 double yy = SCM_REAL_VALUE (y);
7351e207 8302#ifndef ALLOW_DIVIDE_BY_ZERO
0aacf84e
MD
8303 if (yy == 0.0)
8304 scm_num_overflow (s_divide);
8305 else
7351e207 8306#endif
98237784
MW
8307 /* FIXME: Precision may be lost here due to:
8308 (1) The cast from 'scm_t_inum' to 'double'
8309 (2) Double rounding */
55f26379 8310 return scm_from_double ((double) xx / yy);
ba74ef4e 8311 }
0aacf84e
MD
8312 else if (SCM_COMPLEXP (y))
8313 {
8314 a = xx;
8315 complex_div: /* y _must_ be a complex number */
8316 {
8317 double r = SCM_COMPLEX_REAL (y);
8318 double i = SCM_COMPLEX_IMAG (y);
4c6e36a6 8319 if (fabs(r) <= fabs(i))
0aacf84e
MD
8320 {
8321 double t = r / i;
8322 double d = i * (1.0 + t * t);
8507ec80 8323 return scm_c_make_rectangular ((a * t) / d, -a / d);
0aacf84e
MD
8324 }
8325 else
8326 {
8327 double t = i / r;
8328 double d = r * (1.0 + t * t);
8507ec80 8329 return scm_c_make_rectangular (a / d, -(a * t) / d);
0aacf84e
MD
8330 }
8331 }
8332 }
f92e85f7
MV
8333 else if (SCM_FRACTIONP (y))
8334 /* a / b/c = ac / b */
cba42c93 8335 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
98237784 8336 SCM_FRACTION_NUMERATOR (y));
0aacf84e
MD
8337 else
8338 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
f8de44c1 8339 }
0aacf84e
MD
8340 else if (SCM_BIGP (x))
8341 {
e11e83f3 8342 if (SCM_I_INUMP (y))
0aacf84e 8343 {
e25f3727 8344 scm_t_inum yy = SCM_I_INUM (y);
0aacf84e
MD
8345 if (yy == 0)
8346 {
7351e207 8347#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
0aacf84e 8348 scm_num_overflow (s_divide);
7351e207 8349#else
0aacf84e
MD
8350 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
8351 scm_remember_upto_here_1 (x);
8352 return (sgn == 0) ? scm_nan () : scm_inf ();
7351e207 8353#endif
0aacf84e
MD
8354 }
8355 else if (yy == 1)
8356 return x;
8357 else
8358 {
8359 /* FIXME: HMM, what are the relative performance issues here?
8360 We need to test. Is it faster on average to test
8361 divisible_p, then perform whichever operation, or is it
8362 faster to perform the integer div opportunistically and
8363 switch to real if there's a remainder? For now we take the
8364 middle ground: test, then if divisible, use the faster div
8365 func. */
8366
e25f3727 8367 scm_t_inum abs_yy = yy < 0 ? -yy : yy;
0aacf84e
MD
8368 int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy);
8369
8370 if (divisible_p)
8371 {
8372 SCM result = scm_i_mkbig ();
8373 mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy);
8374 scm_remember_upto_here_1 (x);
8375 if (yy < 0)
8376 mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
8377 return scm_i_normbig (result);
8378 }
8379 else
98237784 8380 return scm_i_make_ratio (x, y);
0aacf84e
MD
8381 }
8382 }
8383 else if (SCM_BIGP (y))
8384 {
98237784
MW
8385 int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x),
8386 SCM_I_BIG_MPZ (y));
8387 if (divisible_p)
8388 {
8389 SCM result = scm_i_mkbig ();
8390 mpz_divexact (SCM_I_BIG_MPZ (result),
8391 SCM_I_BIG_MPZ (x),
8392 SCM_I_BIG_MPZ (y));
8393 scm_remember_upto_here_2 (x, y);
8394 return scm_i_normbig (result);
8395 }
8396 else
8397 return scm_i_make_ratio (x, y);
0aacf84e
MD
8398 }
8399 else if (SCM_REALP (y))
8400 {
8401 double yy = SCM_REAL_VALUE (y);
7351e207 8402#ifndef ALLOW_DIVIDE_BY_ZERO
0aacf84e
MD
8403 if (yy == 0.0)
8404 scm_num_overflow (s_divide);
8405 else
7351e207 8406#endif
98237784
MW
8407 /* FIXME: Precision may be lost here due to:
8408 (1) scm_i_big2dbl (2) Double rounding */
55f26379 8409 return scm_from_double (scm_i_big2dbl (x) / yy);
0aacf84e
MD
8410 }
8411 else if (SCM_COMPLEXP (y))
8412 {
8413 a = scm_i_big2dbl (x);
8414 goto complex_div;
8415 }
f92e85f7 8416 else if (SCM_FRACTIONP (y))
cba42c93 8417 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
98237784 8418 SCM_FRACTION_NUMERATOR (y));
0aacf84e
MD
8419 else
8420 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
f872b822 8421 }
0aacf84e
MD
8422 else if (SCM_REALP (x))
8423 {
8424 double rx = SCM_REAL_VALUE (x);
e11e83f3 8425 if (SCM_I_INUMP (y))
0aacf84e 8426 {
e25f3727 8427 scm_t_inum yy = SCM_I_INUM (y);
7351e207 8428#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
0aacf84e
MD
8429 if (yy == 0)
8430 scm_num_overflow (s_divide);
8431 else
7351e207 8432#endif
98237784
MW
8433 /* FIXME: Precision may be lost here due to:
8434 (1) The cast from 'scm_t_inum' to 'double'
8435 (2) Double rounding */
55f26379 8436 return scm_from_double (rx / (double) yy);
0aacf84e
MD
8437 }
8438 else if (SCM_BIGP (y))
8439 {
98237784
MW
8440 /* FIXME: Precision may be lost here due to:
8441 (1) The conversion from bignum to double
8442 (2) Double rounding */
0aacf84e
MD
8443 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
8444 scm_remember_upto_here_1 (y);
55f26379 8445 return scm_from_double (rx / dby);
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
55f26379 8455 return scm_from_double (rx / yy);
0aacf84e
MD
8456 }
8457 else if (SCM_COMPLEXP (y))
8458 {
8459 a = rx;
8460 goto complex_div;
8461 }
f92e85f7 8462 else if (SCM_FRACTIONP (y))
55f26379 8463 return scm_from_double (rx / scm_i_fraction2double (y));
0aacf84e
MD
8464 else
8465 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
f872b822 8466 }
0aacf84e
MD
8467 else if (SCM_COMPLEXP (x))
8468 {
8469 double rx = SCM_COMPLEX_REAL (x);
8470 double ix = SCM_COMPLEX_IMAG (x);
e11e83f3 8471 if (SCM_I_INUMP (y))
0aacf84e 8472 {
e25f3727 8473 scm_t_inum yy = SCM_I_INUM (y);
7351e207 8474#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
0aacf84e
MD
8475 if (yy == 0)
8476 scm_num_overflow (s_divide);
8477 else
7351e207 8478#endif
0aacf84e 8479 {
98237784
MW
8480 /* FIXME: Precision may be lost here due to:
8481 (1) The conversion from 'scm_t_inum' to double
8482 (2) Double rounding */
0aacf84e 8483 double d = yy;
8507ec80 8484 return scm_c_make_rectangular (rx / d, ix / d);
0aacf84e
MD
8485 }
8486 }
8487 else if (SCM_BIGP (y))
8488 {
98237784
MW
8489 /* FIXME: Precision may be lost here due to:
8490 (1) The conversion from bignum to double
8491 (2) Double rounding */
0aacf84e
MD
8492 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
8493 scm_remember_upto_here_1 (y);
8507ec80 8494 return scm_c_make_rectangular (rx / dby, ix / dby);
0aacf84e
MD
8495 }
8496 else if (SCM_REALP (y))
8497 {
8498 double yy = SCM_REAL_VALUE (y);
7351e207 8499#ifndef ALLOW_DIVIDE_BY_ZERO
0aacf84e
MD
8500 if (yy == 0.0)
8501 scm_num_overflow (s_divide);
8502 else
7351e207 8503#endif
8507ec80 8504 return scm_c_make_rectangular (rx / yy, ix / yy);
0aacf84e
MD
8505 }
8506 else if (SCM_COMPLEXP (y))
8507 {
8508 double ry = SCM_COMPLEX_REAL (y);
8509 double iy = SCM_COMPLEX_IMAG (y);
4c6e36a6 8510 if (fabs(ry) <= fabs(iy))
0aacf84e
MD
8511 {
8512 double t = ry / iy;
8513 double d = iy * (1.0 + t * t);
8507ec80 8514 return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d);
0aacf84e
MD
8515 }
8516 else
8517 {
8518 double t = iy / ry;
8519 double d = ry * (1.0 + t * t);
8507ec80 8520 return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d);
0aacf84e
MD
8521 }
8522 }
f92e85f7
MV
8523 else if (SCM_FRACTIONP (y))
8524 {
98237784
MW
8525 /* FIXME: Precision may be lost here due to:
8526 (1) The conversion from fraction to double
8527 (2) Double rounding */
f92e85f7 8528 double yy = scm_i_fraction2double (y);
8507ec80 8529 return scm_c_make_rectangular (rx / yy, ix / yy);
f92e85f7 8530 }
0aacf84e
MD
8531 else
8532 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
f8de44c1 8533 }
f92e85f7
MV
8534 else if (SCM_FRACTIONP (x))
8535 {
e11e83f3 8536 if (SCM_I_INUMP (y))
f92e85f7 8537 {
e25f3727 8538 scm_t_inum yy = SCM_I_INUM (y);
f92e85f7
MV
8539#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
8540 if (yy == 0)
8541 scm_num_overflow (s_divide);
8542 else
8543#endif
cba42c93 8544 return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
98237784 8545 scm_product (SCM_FRACTION_DENOMINATOR (x), y));
f92e85f7
MV
8546 }
8547 else if (SCM_BIGP (y))
8548 {
cba42c93 8549 return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
98237784 8550 scm_product (SCM_FRACTION_DENOMINATOR (x), y));
f92e85f7
MV
8551 }
8552 else if (SCM_REALP (y))
8553 {
8554 double yy = SCM_REAL_VALUE (y);
8555#ifndef ALLOW_DIVIDE_BY_ZERO
8556 if (yy == 0.0)
8557 scm_num_overflow (s_divide);
8558 else
8559#endif
98237784
MW
8560 /* FIXME: Precision may be lost here due to:
8561 (1) The conversion from fraction to double
8562 (2) Double rounding */
55f26379 8563 return scm_from_double (scm_i_fraction2double (x) / yy);
f92e85f7
MV
8564 }
8565 else if (SCM_COMPLEXP (y))
8566 {
98237784
MW
8567 /* FIXME: Precision may be lost here due to:
8568 (1) The conversion from fraction to double
8569 (2) Double rounding */
f92e85f7
MV
8570 a = scm_i_fraction2double (x);
8571 goto complex_div;
8572 }
8573 else if (SCM_FRACTIONP (y))
cba42c93 8574 return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
98237784 8575 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x)));
f92e85f7
MV
8576 else
8577 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
8578 }
0aacf84e 8579 else
f8de44c1 8580 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide);
0f2d19dd 8581}
c05e97b7 8582#undef FUNC_NAME
0f2d19dd 8583
fa605590 8584
0f2d19dd 8585double
3101f40f 8586scm_c_truncate (double x)
0f2d19dd 8587{
fa605590 8588 return trunc (x);
0f2d19dd 8589}
0f2d19dd 8590
3101f40f
MV
8591/* scm_c_round is done using floor(x+0.5) to round to nearest and with
8592 half-way case (ie. when x is an integer plus 0.5) going upwards.
8593 Then half-way cases are identified and adjusted down if the
8594 round-upwards didn't give the desired even integer.
6187f48b
KR
8595
8596 "plus_half == result" identifies a half-way case. If plus_half, which is
8597 x + 0.5, is an integer then x must be an integer plus 0.5.
8598
8599 An odd "result" value is identified with result/2 != floor(result/2).
8600 This is done with plus_half, since that value is ready for use sooner in
8601 a pipelined cpu, and we're already requiring plus_half == result.
8602
8603 Note however that we need to be careful when x is big and already an
8604 integer. In that case "x+0.5" may round to an adjacent integer, causing
8605 us to return such a value, incorrectly. For instance if the hardware is
8606 in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
8607 (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
8608 returned. Or if the hardware is in round-upwards mode, then other bigger
8609 values like say x == 2^128 will see x+0.5 rounding up to the next higher
8610 representable value, 2^128+2^76 (or whatever), again incorrect.
8611
8612 These bad roundings of x+0.5 are avoided by testing at the start whether
8613 x is already an integer. If it is then clearly that's the desired result
8614 already. And if it's not then the exponent must be small enough to allow
8615 an 0.5 to be represented, and hence added without a bad rounding. */
8616
0f2d19dd 8617double
3101f40f 8618scm_c_round (double x)
0f2d19dd 8619{
6187f48b
KR
8620 double plus_half, result;
8621
8622 if (x == floor (x))
8623 return x;
8624
8625 plus_half = x + 0.5;
8626 result = floor (plus_half);
3101f40f 8627 /* Adjust so that the rounding is towards even. */
0aacf84e
MD
8628 return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
8629 ? result - 1
8630 : result);
0f2d19dd
JB
8631}
8632
8b56bcec
MW
8633SCM_PRIMITIVE_GENERIC (scm_truncate_number, "truncate", 1, 0, 0,
8634 (SCM x),
8635 "Round the number @var{x} towards zero.")
f92e85f7
MV
8636#define FUNC_NAME s_scm_truncate_number
8637{
8b56bcec
MW
8638 if (SCM_I_INUMP (x) || SCM_BIGP (x))
8639 return x;
8640 else if (SCM_REALP (x))
c251ab63 8641 return scm_from_double (trunc (SCM_REAL_VALUE (x)));
8b56bcec
MW
8642 else if (SCM_FRACTIONP (x))
8643 return scm_truncate_quotient (SCM_FRACTION_NUMERATOR (x),
8644 SCM_FRACTION_DENOMINATOR (x));
f92e85f7 8645 else
8b56bcec
MW
8646 SCM_WTA_DISPATCH_1 (g_scm_truncate_number, x, SCM_ARG1,
8647 s_scm_truncate_number);
f92e85f7
MV
8648}
8649#undef FUNC_NAME
8650
8b56bcec
MW
8651SCM_PRIMITIVE_GENERIC (scm_round_number, "round", 1, 0, 0,
8652 (SCM x),
8653 "Round the number @var{x} towards the nearest integer. "
8654 "When it is exactly halfway between two integers, "
8655 "round towards the even one.")
f92e85f7
MV
8656#define FUNC_NAME s_scm_round_number
8657{
e11e83f3 8658 if (SCM_I_INUMP (x) || SCM_BIGP (x))
bae30667
KR
8659 return x;
8660 else if (SCM_REALP (x))
3101f40f 8661 return scm_from_double (scm_c_round (SCM_REAL_VALUE (x)));
8b56bcec
MW
8662 else if (SCM_FRACTIONP (x))
8663 return scm_round_quotient (SCM_FRACTION_NUMERATOR (x),
8664 SCM_FRACTION_DENOMINATOR (x));
f92e85f7 8665 else
8b56bcec
MW
8666 SCM_WTA_DISPATCH_1 (g_scm_round_number, x, SCM_ARG1,
8667 s_scm_round_number);
f92e85f7
MV
8668}
8669#undef FUNC_NAME
8670
8671SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
8672 (SCM x),
8673 "Round the number @var{x} towards minus infinity.")
8674#define FUNC_NAME s_scm_floor
8675{
e11e83f3 8676 if (SCM_I_INUMP (x) || SCM_BIGP (x))
f92e85f7
MV
8677 return x;
8678 else if (SCM_REALP (x))
55f26379 8679 return scm_from_double (floor (SCM_REAL_VALUE (x)));
f92e85f7 8680 else if (SCM_FRACTIONP (x))
8b56bcec
MW
8681 return scm_floor_quotient (SCM_FRACTION_NUMERATOR (x),
8682 SCM_FRACTION_DENOMINATOR (x));
f92e85f7
MV
8683 else
8684 SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor);
8685}
8686#undef FUNC_NAME
8687
8688SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
8689 (SCM x),
8690 "Round the number @var{x} towards infinity.")
8691#define FUNC_NAME s_scm_ceiling
8692{
e11e83f3 8693 if (SCM_I_INUMP (x) || SCM_BIGP (x))
f92e85f7
MV
8694 return x;
8695 else if (SCM_REALP (x))
55f26379 8696 return scm_from_double (ceil (SCM_REAL_VALUE (x)));
f92e85f7 8697 else if (SCM_FRACTIONP (x))
8b56bcec
MW
8698 return scm_ceiling_quotient (SCM_FRACTION_NUMERATOR (x),
8699 SCM_FRACTION_DENOMINATOR (x));
f92e85f7
MV
8700 else
8701 SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
8702}
8703#undef FUNC_NAME
0f2d19dd 8704
2519490c
MW
8705SCM_PRIMITIVE_GENERIC (scm_expt, "expt", 2, 0, 0,
8706 (SCM x, SCM y),
8707 "Return @var{x} raised to the power of @var{y}.")
6fc4d012 8708#define FUNC_NAME s_scm_expt
0f2d19dd 8709{
01c7284a
MW
8710 if (scm_is_integer (y))
8711 {
8712 if (scm_is_true (scm_exact_p (y)))
8713 return scm_integer_expt (x, y);
8714 else
8715 {
8716 /* Here we handle the case where the exponent is an inexact
8717 integer. We make the exponent exact in order to use
8718 scm_integer_expt, and thus avoid the spurious imaginary
8719 parts that may result from round-off errors in the general
8720 e^(y log x) method below (for example when squaring a large
8721 negative number). In this case, we must return an inexact
8722 result for correctness. We also make the base inexact so
8723 that scm_integer_expt will use fast inexact arithmetic
8724 internally. Note that making the base inexact is not
8725 sufficient to guarantee an inexact result, because
8726 scm_integer_expt will return an exact 1 when the exponent
8727 is 0, even if the base is inexact. */
8728 return scm_exact_to_inexact
8729 (scm_integer_expt (scm_exact_to_inexact (x),
8730 scm_inexact_to_exact (y)));
8731 }
8732 }
6fc4d012
AW
8733 else if (scm_is_real (x) && scm_is_real (y) && scm_to_double (x) >= 0.0)
8734 {
8735 return scm_from_double (pow (scm_to_double (x), scm_to_double (y)));
8736 }
2519490c 8737 else if (scm_is_complex (x) && scm_is_complex (y))
6fc4d012 8738 return scm_exp (scm_product (scm_log (x), y));
2519490c
MW
8739 else if (scm_is_complex (x))
8740 SCM_WTA_DISPATCH_2 (g_scm_expt, x, y, SCM_ARG2, s_scm_expt);
8741 else
8742 SCM_WTA_DISPATCH_2 (g_scm_expt, x, y, SCM_ARG1, s_scm_expt);
0f2d19dd 8743}
1bbd0b84 8744#undef FUNC_NAME
0f2d19dd 8745
7f41099e
MW
8746/* sin/cos/tan/asin/acos/atan
8747 sinh/cosh/tanh/asinh/acosh/atanh
8748 Derived from "Transcen.scm", Complex trancendental functions for SCM.
8749 Written by Jerry D. Hedden, (C) FSF.
8750 See the file `COPYING' for terms applying to this program. */
8751
ad79736c
AW
8752SCM_PRIMITIVE_GENERIC (scm_sin, "sin", 1, 0, 0,
8753 (SCM z),
8754 "Compute the sine of @var{z}.")
8755#define FUNC_NAME s_scm_sin
8756{
8deddc94
MW
8757 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8758 return z; /* sin(exact0) = exact0 */
8759 else if (scm_is_real (z))
ad79736c
AW
8760 return scm_from_double (sin (scm_to_double (z)));
8761 else if (SCM_COMPLEXP (z))
8762 { double x, y;
8763 x = SCM_COMPLEX_REAL (z);
8764 y = SCM_COMPLEX_IMAG (z);
8765 return scm_c_make_rectangular (sin (x) * cosh (y),
8766 cos (x) * sinh (y));
8767 }
8768 else
8769 SCM_WTA_DISPATCH_1 (g_scm_sin, z, 1, s_scm_sin);
8770}
8771#undef FUNC_NAME
0f2d19dd 8772
ad79736c
AW
8773SCM_PRIMITIVE_GENERIC (scm_cos, "cos", 1, 0, 0,
8774 (SCM z),
8775 "Compute the cosine of @var{z}.")
8776#define FUNC_NAME s_scm_cos
8777{
8deddc94
MW
8778 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8779 return SCM_INUM1; /* cos(exact0) = exact1 */
8780 else if (scm_is_real (z))
ad79736c
AW
8781 return scm_from_double (cos (scm_to_double (z)));
8782 else if (SCM_COMPLEXP (z))
8783 { double x, y;
8784 x = SCM_COMPLEX_REAL (z);
8785 y = SCM_COMPLEX_IMAG (z);
8786 return scm_c_make_rectangular (cos (x) * cosh (y),
8787 -sin (x) * sinh (y));
8788 }
8789 else
8790 SCM_WTA_DISPATCH_1 (g_scm_cos, z, 1, s_scm_cos);
8791}
8792#undef FUNC_NAME
8793
8794SCM_PRIMITIVE_GENERIC (scm_tan, "tan", 1, 0, 0,
8795 (SCM z),
8796 "Compute the tangent of @var{z}.")
8797#define FUNC_NAME s_scm_tan
0f2d19dd 8798{
8deddc94
MW
8799 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8800 return z; /* tan(exact0) = exact0 */
8801 else if (scm_is_real (z))
ad79736c
AW
8802 return scm_from_double (tan (scm_to_double (z)));
8803 else if (SCM_COMPLEXP (z))
8804 { double x, y, w;
8805 x = 2.0 * SCM_COMPLEX_REAL (z);
8806 y = 2.0 * SCM_COMPLEX_IMAG (z);
8807 w = cos (x) + cosh (y);
8808#ifndef ALLOW_DIVIDE_BY_ZERO
8809 if (w == 0.0)
8810 scm_num_overflow (s_scm_tan);
8811#endif
8812 return scm_c_make_rectangular (sin (x) / w, sinh (y) / w);
8813 }
8814 else
8815 SCM_WTA_DISPATCH_1 (g_scm_tan, z, 1, s_scm_tan);
8816}
8817#undef FUNC_NAME
8818
8819SCM_PRIMITIVE_GENERIC (scm_sinh, "sinh", 1, 0, 0,
8820 (SCM z),
8821 "Compute the hyperbolic sine of @var{z}.")
8822#define FUNC_NAME s_scm_sinh
8823{
8deddc94
MW
8824 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8825 return z; /* sinh(exact0) = exact0 */
8826 else if (scm_is_real (z))
ad79736c
AW
8827 return scm_from_double (sinh (scm_to_double (z)));
8828 else if (SCM_COMPLEXP (z))
8829 { double x, y;
8830 x = SCM_COMPLEX_REAL (z);
8831 y = SCM_COMPLEX_IMAG (z);
8832 return scm_c_make_rectangular (sinh (x) * cos (y),
8833 cosh (x) * sin (y));
8834 }
8835 else
8836 SCM_WTA_DISPATCH_1 (g_scm_sinh, z, 1, s_scm_sinh);
8837}
8838#undef FUNC_NAME
8839
8840SCM_PRIMITIVE_GENERIC (scm_cosh, "cosh", 1, 0, 0,
8841 (SCM z),
8842 "Compute the hyperbolic cosine of @var{z}.")
8843#define FUNC_NAME s_scm_cosh
8844{
8deddc94
MW
8845 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8846 return SCM_INUM1; /* cosh(exact0) = exact1 */
8847 else if (scm_is_real (z))
ad79736c
AW
8848 return scm_from_double (cosh (scm_to_double (z)));
8849 else if (SCM_COMPLEXP (z))
8850 { double x, y;
8851 x = SCM_COMPLEX_REAL (z);
8852 y = SCM_COMPLEX_IMAG (z);
8853 return scm_c_make_rectangular (cosh (x) * cos (y),
8854 sinh (x) * sin (y));
8855 }
8856 else
8857 SCM_WTA_DISPATCH_1 (g_scm_cosh, z, 1, s_scm_cosh);
8858}
8859#undef FUNC_NAME
8860
8861SCM_PRIMITIVE_GENERIC (scm_tanh, "tanh", 1, 0, 0,
8862 (SCM z),
8863 "Compute the hyperbolic tangent of @var{z}.")
8864#define FUNC_NAME s_scm_tanh
8865{
8deddc94
MW
8866 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8867 return z; /* tanh(exact0) = exact0 */
8868 else if (scm_is_real (z))
ad79736c
AW
8869 return scm_from_double (tanh (scm_to_double (z)));
8870 else if (SCM_COMPLEXP (z))
8871 { double x, y, w;
8872 x = 2.0 * SCM_COMPLEX_REAL (z);
8873 y = 2.0 * SCM_COMPLEX_IMAG (z);
8874 w = cosh (x) + cos (y);
8875#ifndef ALLOW_DIVIDE_BY_ZERO
8876 if (w == 0.0)
8877 scm_num_overflow (s_scm_tanh);
8878#endif
8879 return scm_c_make_rectangular (sinh (x) / w, sin (y) / w);
8880 }
8881 else
8882 SCM_WTA_DISPATCH_1 (g_scm_tanh, z, 1, s_scm_tanh);
8883}
8884#undef FUNC_NAME
8885
8886SCM_PRIMITIVE_GENERIC (scm_asin, "asin", 1, 0, 0,
8887 (SCM z),
8888 "Compute the arc sine of @var{z}.")
8889#define FUNC_NAME s_scm_asin
8890{
8deddc94
MW
8891 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8892 return z; /* asin(exact0) = exact0 */
8893 else if (scm_is_real (z))
ad79736c
AW
8894 {
8895 double w = scm_to_double (z);
8896 if (w >= -1.0 && w <= 1.0)
8897 return scm_from_double (asin (w));
8898 else
8899 return scm_product (scm_c_make_rectangular (0, -1),
8900 scm_sys_asinh (scm_c_make_rectangular (0, w)));
8901 }
8902 else if (SCM_COMPLEXP (z))
8903 { double x, y;
8904 x = SCM_COMPLEX_REAL (z);
8905 y = SCM_COMPLEX_IMAG (z);
8906 return scm_product (scm_c_make_rectangular (0, -1),
8907 scm_sys_asinh (scm_c_make_rectangular (-y, x)));
8908 }
8909 else
8910 SCM_WTA_DISPATCH_1 (g_scm_asin, z, 1, s_scm_asin);
8911}
8912#undef FUNC_NAME
8913
8914SCM_PRIMITIVE_GENERIC (scm_acos, "acos", 1, 0, 0,
8915 (SCM z),
8916 "Compute the arc cosine of @var{z}.")
8917#define FUNC_NAME s_scm_acos
8918{
8deddc94
MW
8919 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
8920 return SCM_INUM0; /* acos(exact1) = exact0 */
8921 else if (scm_is_real (z))
ad79736c
AW
8922 {
8923 double w = scm_to_double (z);
8924 if (w >= -1.0 && w <= 1.0)
8925 return scm_from_double (acos (w));
8926 else
8927 return scm_sum (scm_from_double (acos (0.0)),
8928 scm_product (scm_c_make_rectangular (0, 1),
8929 scm_sys_asinh (scm_c_make_rectangular (0, w))));
8930 }
8931 else if (SCM_COMPLEXP (z))
8932 { double x, y;
8933 x = SCM_COMPLEX_REAL (z);
8934 y = SCM_COMPLEX_IMAG (z);
8935 return scm_sum (scm_from_double (acos (0.0)),
8936 scm_product (scm_c_make_rectangular (0, 1),
8937 scm_sys_asinh (scm_c_make_rectangular (-y, x))));
8938 }
8939 else
8940 SCM_WTA_DISPATCH_1 (g_scm_acos, z, 1, s_scm_acos);
8941}
8942#undef FUNC_NAME
8943
8944SCM_PRIMITIVE_GENERIC (scm_atan, "atan", 1, 1, 0,
8945 (SCM z, SCM y),
8946 "With one argument, compute the arc tangent of @var{z}.\n"
8947 "If @var{y} is present, compute the arc tangent of @var{z}/@var{y},\n"
8948 "using the sign of @var{z} and @var{y} to determine the quadrant.")
8949#define FUNC_NAME s_scm_atan
8950{
8951 if (SCM_UNBNDP (y))
8952 {
8deddc94
MW
8953 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8954 return z; /* atan(exact0) = exact0 */
8955 else if (scm_is_real (z))
ad79736c
AW
8956 return scm_from_double (atan (scm_to_double (z)));
8957 else if (SCM_COMPLEXP (z))
8958 {
8959 double v, w;
8960 v = SCM_COMPLEX_REAL (z);
8961 w = SCM_COMPLEX_IMAG (z);
8962 return scm_divide (scm_log (scm_divide (scm_c_make_rectangular (v, w - 1.0),
8963 scm_c_make_rectangular (v, w + 1.0))),
8964 scm_c_make_rectangular (0, 2));
8965 }
8966 else
18104cac 8967 SCM_WTA_DISPATCH_1 (g_scm_atan, z, SCM_ARG1, s_scm_atan);
ad79736c
AW
8968 }
8969 else if (scm_is_real (z))
8970 {
8971 if (scm_is_real (y))
8972 return scm_from_double (atan2 (scm_to_double (z), scm_to_double (y)));
8973 else
8974 SCM_WTA_DISPATCH_2 (g_scm_atan, z, y, SCM_ARG2, s_scm_atan);
8975 }
8976 else
8977 SCM_WTA_DISPATCH_2 (g_scm_atan, z, y, SCM_ARG1, s_scm_atan);
8978}
8979#undef FUNC_NAME
8980
8981SCM_PRIMITIVE_GENERIC (scm_sys_asinh, "asinh", 1, 0, 0,
8982 (SCM z),
8983 "Compute the inverse hyperbolic sine of @var{z}.")
8984#define FUNC_NAME s_scm_sys_asinh
8985{
8deddc94
MW
8986 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
8987 return z; /* asinh(exact0) = exact0 */
8988 else if (scm_is_real (z))
ad79736c
AW
8989 return scm_from_double (asinh (scm_to_double (z)));
8990 else if (scm_is_number (z))
8991 return scm_log (scm_sum (z,
8992 scm_sqrt (scm_sum (scm_product (z, z),
cff5fa33 8993 SCM_INUM1))));
ad79736c
AW
8994 else
8995 SCM_WTA_DISPATCH_1 (g_scm_sys_asinh, z, 1, s_scm_sys_asinh);
8996}
8997#undef FUNC_NAME
8998
8999SCM_PRIMITIVE_GENERIC (scm_sys_acosh, "acosh", 1, 0, 0,
9000 (SCM z),
9001 "Compute the inverse hyperbolic cosine of @var{z}.")
9002#define FUNC_NAME s_scm_sys_acosh
9003{
8deddc94
MW
9004 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
9005 return SCM_INUM0; /* acosh(exact1) = exact0 */
9006 else if (scm_is_real (z) && scm_to_double (z) >= 1.0)
ad79736c
AW
9007 return scm_from_double (acosh (scm_to_double (z)));
9008 else if (scm_is_number (z))
9009 return scm_log (scm_sum (z,
9010 scm_sqrt (scm_difference (scm_product (z, z),
cff5fa33 9011 SCM_INUM1))));
ad79736c
AW
9012 else
9013 SCM_WTA_DISPATCH_1 (g_scm_sys_acosh, z, 1, s_scm_sys_acosh);
9014}
9015#undef FUNC_NAME
9016
9017SCM_PRIMITIVE_GENERIC (scm_sys_atanh, "atanh", 1, 0, 0,
9018 (SCM z),
9019 "Compute the inverse hyperbolic tangent of @var{z}.")
9020#define FUNC_NAME s_scm_sys_atanh
9021{
8deddc94
MW
9022 if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
9023 return z; /* atanh(exact0) = exact0 */
9024 else if (scm_is_real (z) && scm_to_double (z) >= -1.0 && scm_to_double (z) <= 1.0)
ad79736c
AW
9025 return scm_from_double (atanh (scm_to_double (z)));
9026 else if (scm_is_number (z))
cff5fa33
MW
9027 return scm_divide (scm_log (scm_divide (scm_sum (SCM_INUM1, z),
9028 scm_difference (SCM_INUM1, z))),
ad79736c
AW
9029 SCM_I_MAKINUM (2));
9030 else
9031 SCM_WTA_DISPATCH_1 (g_scm_sys_atanh, z, 1, s_scm_sys_atanh);
0f2d19dd 9032}
1bbd0b84 9033#undef FUNC_NAME
0f2d19dd 9034
8507ec80
MV
9035SCM
9036scm_c_make_rectangular (double re, double im)
9037{
c7218482 9038 SCM z;
03604fcf 9039
c7218482
MW
9040 z = PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_complex),
9041 "complex"));
9042 SCM_SET_CELL_TYPE (z, scm_tc16_complex);
9043 SCM_COMPLEX_REAL (z) = re;
9044 SCM_COMPLEX_IMAG (z) = im;
9045 return z;
8507ec80 9046}
0f2d19dd 9047
a1ec6916 9048SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
a2c25234 9049 (SCM real_part, SCM imaginary_part),
b7e64f8b
BT
9050 "Return a complex number constructed of the given @var{real_part} "
9051 "and @var{imaginary_part} parts.")
1bbd0b84 9052#define FUNC_NAME s_scm_make_rectangular
0f2d19dd 9053{
ad79736c
AW
9054 SCM_ASSERT_TYPE (scm_is_real (real_part), real_part,
9055 SCM_ARG1, FUNC_NAME, "real");
9056 SCM_ASSERT_TYPE (scm_is_real (imaginary_part), imaginary_part,
9057 SCM_ARG2, FUNC_NAME, "real");
c7218482
MW
9058
9059 /* Return a real if and only if the imaginary_part is an _exact_ 0 */
9060 if (scm_is_eq (imaginary_part, SCM_INUM0))
9061 return real_part;
9062 else
9063 return scm_c_make_rectangular (scm_to_double (real_part),
9064 scm_to_double (imaginary_part));
0f2d19dd 9065}
1bbd0b84 9066#undef FUNC_NAME
0f2d19dd 9067
8507ec80
MV
9068SCM
9069scm_c_make_polar (double mag, double ang)
9070{
9071 double s, c;
5e647d08
LC
9072
9073 /* The sincos(3) function is undocumented an broken on Tru64. Thus we only
9074 use it on Glibc-based systems that have it (it's a GNU extension). See
9075 http://lists.gnu.org/archive/html/guile-user/2009-04/msg00033.html for
9076 details. */
9077#if (defined HAVE_SINCOS) && (defined __GLIBC__) && (defined _GNU_SOURCE)
8507ec80
MV
9078 sincos (ang, &s, &c);
9079#else
9080 s = sin (ang);
9081 c = cos (ang);
9082#endif
9d427b2c
MW
9083
9084 /* If s and c are NaNs, this indicates that the angle is a NaN,
9085 infinite, or perhaps simply too large to determine its value
9086 mod 2*pi. However, we know something that the floating-point
9087 implementation doesn't know: We know that s and c are finite.
9088 Therefore, if the magnitude is zero, return a complex zero.
9089
9090 The reason we check for the NaNs instead of using this case
9091 whenever mag == 0.0 is because when the angle is known, we'd
9092 like to return the correct kind of non-real complex zero:
9093 +0.0+0.0i, -0.0+0.0i, -0.0-0.0i, or +0.0-0.0i, depending
9094 on which quadrant the angle is in.
9095 */
9096 if (SCM_UNLIKELY (isnan(s)) && isnan(c) && (mag == 0.0))
9097 return scm_c_make_rectangular (0.0, 0.0);
9098 else
9099 return scm_c_make_rectangular (mag * c, mag * s);
8507ec80 9100}
0f2d19dd 9101
a1ec6916 9102SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
c7218482
MW
9103 (SCM mag, SCM ang),
9104 "Return the complex number @var{mag} * e^(i * @var{ang}).")
1bbd0b84 9105#define FUNC_NAME s_scm_make_polar
0f2d19dd 9106{
c7218482
MW
9107 SCM_ASSERT_TYPE (scm_is_real (mag), mag, SCM_ARG1, FUNC_NAME, "real");
9108 SCM_ASSERT_TYPE (scm_is_real (ang), ang, SCM_ARG2, FUNC_NAME, "real");
9109
9110 /* If mag is exact0, return exact0 */
9111 if (scm_is_eq (mag, SCM_INUM0))
9112 return SCM_INUM0;
9113 /* Return a real if ang is exact0 */
9114 else if (scm_is_eq (ang, SCM_INUM0))
9115 return mag;
9116 else
9117 return scm_c_make_polar (scm_to_double (mag), scm_to_double (ang));
0f2d19dd 9118}
1bbd0b84 9119#undef FUNC_NAME
0f2d19dd
JB
9120
9121
2519490c
MW
9122SCM_PRIMITIVE_GENERIC (scm_real_part, "real-part", 1, 0, 0,
9123 (SCM z),
9124 "Return the real part of the number @var{z}.")
9125#define FUNC_NAME s_scm_real_part
0f2d19dd 9126{
2519490c 9127 if (SCM_COMPLEXP (z))
55f26379 9128 return scm_from_double (SCM_COMPLEX_REAL (z));
2519490c 9129 else if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_REALP (z) || SCM_FRACTIONP (z))
2fa2d879 9130 return z;
0aacf84e 9131 else
2519490c 9132 SCM_WTA_DISPATCH_1 (g_scm_real_part, z, SCM_ARG1, s_scm_real_part);
0f2d19dd 9133}
2519490c 9134#undef FUNC_NAME
0f2d19dd
JB
9135
9136
2519490c
MW
9137SCM_PRIMITIVE_GENERIC (scm_imag_part, "imag-part", 1, 0, 0,
9138 (SCM z),
9139 "Return the imaginary part of the number @var{z}.")
9140#define FUNC_NAME s_scm_imag_part
0f2d19dd 9141{
2519490c
MW
9142 if (SCM_COMPLEXP (z))
9143 return scm_from_double (SCM_COMPLEX_IMAG (z));
c7218482 9144 else if (SCM_I_INUMP (z) || SCM_REALP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
f92e85f7 9145 return SCM_INUM0;
0aacf84e 9146 else
2519490c 9147 SCM_WTA_DISPATCH_1 (g_scm_imag_part, z, SCM_ARG1, s_scm_imag_part);
0f2d19dd 9148}
2519490c 9149#undef FUNC_NAME
0f2d19dd 9150
2519490c
MW
9151SCM_PRIMITIVE_GENERIC (scm_numerator, "numerator", 1, 0, 0,
9152 (SCM z),
9153 "Return the numerator of the number @var{z}.")
9154#define FUNC_NAME s_scm_numerator
f92e85f7 9155{
2519490c 9156 if (SCM_I_INUMP (z) || SCM_BIGP (z))
f92e85f7
MV
9157 return z;
9158 else if (SCM_FRACTIONP (z))
e2bf3b19 9159 return SCM_FRACTION_NUMERATOR (z);
f92e85f7
MV
9160 else if (SCM_REALP (z))
9161 return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
9162 else
2519490c 9163 SCM_WTA_DISPATCH_1 (g_scm_numerator, z, SCM_ARG1, s_scm_numerator);
f92e85f7 9164}
2519490c 9165#undef FUNC_NAME
f92e85f7
MV
9166
9167
2519490c
MW
9168SCM_PRIMITIVE_GENERIC (scm_denominator, "denominator", 1, 0, 0,
9169 (SCM z),
9170 "Return the denominator of the number @var{z}.")
9171#define FUNC_NAME s_scm_denominator
f92e85f7 9172{
2519490c 9173 if (SCM_I_INUMP (z) || SCM_BIGP (z))
cff5fa33 9174 return SCM_INUM1;
f92e85f7 9175 else if (SCM_FRACTIONP (z))
e2bf3b19 9176 return SCM_FRACTION_DENOMINATOR (z);
f92e85f7
MV
9177 else if (SCM_REALP (z))
9178 return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
9179 else
2519490c 9180 SCM_WTA_DISPATCH_1 (g_scm_denominator, z, SCM_ARG1, s_scm_denominator);
f92e85f7 9181}
2519490c 9182#undef FUNC_NAME
0f2d19dd 9183
2519490c
MW
9184
9185SCM_PRIMITIVE_GENERIC (scm_magnitude, "magnitude", 1, 0, 0,
9186 (SCM z),
9187 "Return the magnitude of the number @var{z}. This is the same as\n"
9188 "@code{abs} for real arguments, but also allows complex numbers.")
9189#define FUNC_NAME s_scm_magnitude
0f2d19dd 9190{
e11e83f3 9191 if (SCM_I_INUMP (z))
0aacf84e 9192 {
e25f3727 9193 scm_t_inum zz = SCM_I_INUM (z);
0aacf84e
MD
9194 if (zz >= 0)
9195 return z;
9196 else if (SCM_POSFIXABLE (-zz))
d956fa6f 9197 return SCM_I_MAKINUM (-zz);
0aacf84e 9198 else
e25f3727 9199 return scm_i_inum2big (-zz);
5986c47d 9200 }
0aacf84e
MD
9201 else if (SCM_BIGP (z))
9202 {
9203 int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
9204 scm_remember_upto_here_1 (z);
9205 if (sgn < 0)
9206 return scm_i_clonebig (z, 0);
9207 else
9208 return z;
5986c47d 9209 }
0aacf84e 9210 else if (SCM_REALP (z))
55f26379 9211 return scm_from_double (fabs (SCM_REAL_VALUE (z)));
0aacf84e 9212 else if (SCM_COMPLEXP (z))
55f26379 9213 return scm_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
f92e85f7
MV
9214 else if (SCM_FRACTIONP (z))
9215 {
73e4de09 9216 if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
f92e85f7 9217 return z;
a285b18c
MW
9218 return scm_i_make_ratio_already_reduced
9219 (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
9220 SCM_FRACTION_DENOMINATOR (z));
f92e85f7 9221 }
0aacf84e 9222 else
2519490c 9223 SCM_WTA_DISPATCH_1 (g_scm_magnitude, z, SCM_ARG1, s_scm_magnitude);
0f2d19dd 9224}
2519490c 9225#undef FUNC_NAME
0f2d19dd
JB
9226
9227
2519490c
MW
9228SCM_PRIMITIVE_GENERIC (scm_angle, "angle", 1, 0, 0,
9229 (SCM z),
9230 "Return the angle of the complex number @var{z}.")
9231#define FUNC_NAME s_scm_angle
0f2d19dd 9232{
c8ae173e 9233 /* atan(0,-1) is pi and it'd be possible to have that as a constant like
e7efe8e7 9234 flo0 to save allocating a new flonum with scm_from_double each time.
c8ae173e
KR
9235 But if atan2 follows the floating point rounding mode, then the value
9236 is not a constant. Maybe it'd be close enough though. */
e11e83f3 9237 if (SCM_I_INUMP (z))
0aacf84e 9238 {
e11e83f3 9239 if (SCM_I_INUM (z) >= 0)
e7efe8e7 9240 return flo0;
0aacf84e 9241 else
55f26379 9242 return scm_from_double (atan2 (0.0, -1.0));
f872b822 9243 }
0aacf84e
MD
9244 else if (SCM_BIGP (z))
9245 {
9246 int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
9247 scm_remember_upto_here_1 (z);
9248 if (sgn < 0)
55f26379 9249 return scm_from_double (atan2 (0.0, -1.0));
0aacf84e 9250 else
e7efe8e7 9251 return flo0;
0f2d19dd 9252 }
0aacf84e 9253 else if (SCM_REALP (z))
c8ae173e 9254 {
10a97755
MW
9255 double x = SCM_REAL_VALUE (z);
9256 if (x > 0.0 || double_is_non_negative_zero (x))
e7efe8e7 9257 return flo0;
c8ae173e 9258 else
55f26379 9259 return scm_from_double (atan2 (0.0, -1.0));
c8ae173e 9260 }
0aacf84e 9261 else if (SCM_COMPLEXP (z))
55f26379 9262 return scm_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
f92e85f7
MV
9263 else if (SCM_FRACTIONP (z))
9264 {
73e4de09 9265 if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
e7efe8e7 9266 return flo0;
55f26379 9267 else return scm_from_double (atan2 (0.0, -1.0));
f92e85f7 9268 }
0aacf84e 9269 else
2519490c 9270 SCM_WTA_DISPATCH_1 (g_scm_angle, z, SCM_ARG1, s_scm_angle);
0f2d19dd 9271}
2519490c 9272#undef FUNC_NAME
0f2d19dd
JB
9273
9274
2519490c
MW
9275SCM_PRIMITIVE_GENERIC (scm_exact_to_inexact, "exact->inexact", 1, 0, 0,
9276 (SCM z),
9277 "Convert the number @var{z} to its inexact representation.\n")
9278#define FUNC_NAME s_scm_exact_to_inexact
3c9a524f 9279{
e11e83f3 9280 if (SCM_I_INUMP (z))
55f26379 9281 return scm_from_double ((double) SCM_I_INUM (z));
3c9a524f 9282 else if (SCM_BIGP (z))
55f26379 9283 return scm_from_double (scm_i_big2dbl (z));
f92e85f7 9284 else if (SCM_FRACTIONP (z))
55f26379 9285 return scm_from_double (scm_i_fraction2double (z));
3c9a524f
DH
9286 else if (SCM_INEXACTP (z))
9287 return z;
9288 else
2519490c 9289 SCM_WTA_DISPATCH_1 (g_scm_exact_to_inexact, z, 1, s_scm_exact_to_inexact);
3c9a524f 9290}
2519490c 9291#undef FUNC_NAME
3c9a524f
DH
9292
9293
2519490c
MW
9294SCM_PRIMITIVE_GENERIC (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
9295 (SCM z),
9296 "Return an exact number that is numerically closest to @var{z}.")
1bbd0b84 9297#define FUNC_NAME s_scm_inexact_to_exact
0f2d19dd 9298{
c7218482 9299 if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
f872b822 9300 return z;
c7218482 9301 else
0aacf84e 9302 {
c7218482
MW
9303 double val;
9304
9305 if (SCM_REALP (z))
9306 val = SCM_REAL_VALUE (z);
9307 else if (SCM_COMPLEXP (z) && SCM_COMPLEX_IMAG (z) == 0.0)
9308 val = SCM_COMPLEX_REAL (z);
9309 else
9310 SCM_WTA_DISPATCH_1 (g_scm_inexact_to_exact, z, 1, s_scm_inexact_to_exact);
9311
9312 if (!SCM_LIKELY (DOUBLE_IS_FINITE (val)))
f92e85f7 9313 SCM_OUT_OF_RANGE (1, z);
24475b86
MW
9314 else if (val == 0.0)
9315 return SCM_INUM0;
2be24db4 9316 else
f92e85f7 9317 {
24475b86
MW
9318 int expon;
9319 SCM numerator;
9320
9321 numerator = scm_i_dbl2big (ldexp (frexp (val, &expon),
9322 DBL_MANT_DIG));
9323 expon -= DBL_MANT_DIG;
9324 if (expon < 0)
9325 {
9326 int shift = mpz_scan1 (SCM_I_BIG_MPZ (numerator), 0);
9327
9328 if (shift > -expon)
9329 shift = -expon;
9330 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (numerator),
9331 SCM_I_BIG_MPZ (numerator),
9332 shift);
9333 expon += shift;
9334 }
9335 numerator = scm_i_normbig (numerator);
9336 if (expon < 0)
9337 return scm_i_make_ratio_already_reduced
9338 (numerator, left_shift_exact_integer (SCM_INUM1, -expon));
9339 else if (expon > 0)
9340 return left_shift_exact_integer (numerator, expon);
9341 else
9342 return numerator;
f92e85f7 9343 }
c2ff8ab0 9344 }
0f2d19dd 9345}
1bbd0b84 9346#undef FUNC_NAME
0f2d19dd 9347
f92e85f7 9348SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
76dae881
NJ
9349 (SCM x, SCM eps),
9350 "Returns the @emph{simplest} rational number differing\n"
9351 "from @var{x} by no more than @var{eps}.\n"
9352 "\n"
9353 "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
9354 "exact result when both its arguments are exact. Thus, you might need\n"
9355 "to use @code{inexact->exact} on the arguments.\n"
9356 "\n"
9357 "@lisp\n"
9358 "(rationalize (inexact->exact 1.2) 1/100)\n"
9359 "@result{} 6/5\n"
9360 "@end lisp")
f92e85f7
MV
9361#define FUNC_NAME s_scm_rationalize
9362{
605f6980
MW
9363 SCM_ASSERT_TYPE (scm_is_real (x), x, SCM_ARG1, FUNC_NAME, "real");
9364 SCM_ASSERT_TYPE (scm_is_real (eps), eps, SCM_ARG2, FUNC_NAME, "real");
9365 eps = scm_abs (eps);
9366 if (scm_is_false (scm_positive_p (eps)))
9367 {
9368 /* eps is either zero or a NaN */
9369 if (scm_is_true (scm_nan_p (eps)))
9370 return scm_nan ();
9371 else if (SCM_INEXACTP (eps))
9372 return scm_exact_to_inexact (x);
9373 else
9374 return x;
9375 }
9376 else if (scm_is_false (scm_finite_p (eps)))
9377 {
9378 if (scm_is_true (scm_finite_p (x)))
9379 return flo0;
9380 else
9381 return scm_nan ();
9382 }
9383 else if (scm_is_false (scm_finite_p (x))) /* checks for both inf and nan */
f92e85f7 9384 return x;
605f6980
MW
9385 else if (scm_is_false (scm_less_p (scm_floor (scm_sum (x, eps)),
9386 scm_ceiling (scm_difference (x, eps)))))
9387 {
9388 /* There's an integer within range; we want the one closest to zero */
9389 if (scm_is_false (scm_less_p (eps, scm_abs (x))))
9390 {
9391 /* zero is within range */
9392 if (SCM_INEXACTP (x) || SCM_INEXACTP (eps))
9393 return flo0;
9394 else
9395 return SCM_INUM0;
9396 }
9397 else if (scm_is_true (scm_positive_p (x)))
9398 return scm_ceiling (scm_difference (x, eps));
9399 else
9400 return scm_floor (scm_sum (x, eps));
9401 }
9402 else
f92e85f7
MV
9403 {
9404 /* Use continued fractions to find closest ratio. All
9405 arithmetic is done with exact numbers.
9406 */
9407
9408 SCM ex = scm_inexact_to_exact (x);
9409 SCM int_part = scm_floor (ex);
cff5fa33
MW
9410 SCM tt = SCM_INUM1;
9411 SCM a1 = SCM_INUM0, a2 = SCM_INUM1, a = SCM_INUM0;
9412 SCM b1 = SCM_INUM1, b2 = SCM_INUM0, b = SCM_INUM0;
f92e85f7
MV
9413 SCM rx;
9414 int i = 0;
9415
f92e85f7
MV
9416 ex = scm_difference (ex, int_part); /* x = x-int_part */
9417 rx = scm_divide (ex, SCM_UNDEFINED); /* rx = 1/x */
9418
9419 /* We stop after a million iterations just to be absolutely sure
9420 that we don't go into an infinite loop. The process normally
9421 converges after less than a dozen iterations.
9422 */
9423
f92e85f7
MV
9424 while (++i < 1000000)
9425 {
9426 a = scm_sum (scm_product (a1, tt), a2); /* a = a1*tt + a2 */
9427 b = scm_sum (scm_product (b1, tt), b2); /* b = b1*tt + b2 */
73e4de09
MV
9428 if (scm_is_false (scm_zero_p (b)) && /* b != 0 */
9429 scm_is_false
f92e85f7 9430 (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))),
76dae881 9431 eps))) /* abs(x-a/b) <= eps */
02164269
MV
9432 {
9433 SCM res = scm_sum (int_part, scm_divide (a, b));
605f6980 9434 if (SCM_INEXACTP (x) || SCM_INEXACTP (eps))
02164269
MV
9435 return scm_exact_to_inexact (res);
9436 else
9437 return res;
9438 }
f92e85f7
MV
9439 rx = scm_divide (scm_difference (rx, tt), /* rx = 1/(rx - tt) */
9440 SCM_UNDEFINED);
9441 tt = scm_floor (rx); /* tt = floor (rx) */
9442 a2 = a1;
9443 b2 = b1;
9444 a1 = a;
9445 b1 = b;
9446 }
9447 scm_num_overflow (s_scm_rationalize);
9448 }
f92e85f7
MV
9449}
9450#undef FUNC_NAME
9451
73e4de09
MV
9452/* conversion functions */
9453
9454int
9455scm_is_integer (SCM val)
9456{
9457 return scm_is_true (scm_integer_p (val));
9458}
9459
9460int
9461scm_is_signed_integer (SCM val, scm_t_intmax min, scm_t_intmax max)
9462{
e11e83f3 9463 if (SCM_I_INUMP (val))
73e4de09 9464 {
e11e83f3 9465 scm_t_signed_bits n = SCM_I_INUM (val);
73e4de09
MV
9466 return n >= min && n <= max;
9467 }
9468 else if (SCM_BIGP (val))
9469 {
9470 if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM)
9471 return 0;
9472 else if (min >= LONG_MIN && max <= LONG_MAX)
d956fa6f
MV
9473 {
9474 if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val)))
9475 {
9476 long n = mpz_get_si (SCM_I_BIG_MPZ (val));
9477 return n >= min && n <= max;
9478 }
9479 else
9480 return 0;
9481 }
73e4de09
MV
9482 else
9483 {
d956fa6f
MV
9484 scm_t_intmax n;
9485 size_t count;
73e4de09 9486
d956fa6f
MV
9487 if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
9488 > CHAR_BIT*sizeof (scm_t_uintmax))
9489 return 0;
9490
9491 mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
9492 SCM_I_BIG_MPZ (val));
73e4de09 9493
d956fa6f 9494 if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
73e4de09 9495 {
d956fa6f
MV
9496 if (n < 0)
9497 return 0;
73e4de09 9498 }
73e4de09
MV
9499 else
9500 {
d956fa6f
MV
9501 n = -n;
9502 if (n >= 0)
9503 return 0;
73e4de09 9504 }
d956fa6f
MV
9505
9506 return n >= min && n <= max;
73e4de09
MV
9507 }
9508 }
73e4de09
MV
9509 else
9510 return 0;
9511}
9512
9513int
9514scm_is_unsigned_integer (SCM val, scm_t_uintmax min, scm_t_uintmax max)
9515{
e11e83f3 9516 if (SCM_I_INUMP (val))
73e4de09 9517 {
e11e83f3 9518 scm_t_signed_bits n = SCM_I_INUM (val);
73e4de09
MV
9519 return n >= 0 && ((scm_t_uintmax)n) >= min && ((scm_t_uintmax)n) <= max;
9520 }
9521 else if (SCM_BIGP (val))
9522 {
9523 if (max <= SCM_MOST_POSITIVE_FIXNUM)
9524 return 0;
9525 else if (max <= ULONG_MAX)
d956fa6f
MV
9526 {
9527 if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
9528 {
9529 unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
9530 return n >= min && n <= max;
9531 }
9532 else
9533 return 0;
9534 }
73e4de09
MV
9535 else
9536 {
d956fa6f
MV
9537 scm_t_uintmax n;
9538 size_t count;
73e4de09 9539
d956fa6f
MV
9540 if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
9541 return 0;
73e4de09 9542
d956fa6f
MV
9543 if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
9544 > CHAR_BIT*sizeof (scm_t_uintmax))
73e4de09 9545 return 0;
d956fa6f
MV
9546
9547 mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
9548 SCM_I_BIG_MPZ (val));
73e4de09 9549
d956fa6f 9550 return n >= min && n <= max;
73e4de09
MV
9551 }
9552 }
73e4de09
MV
9553 else
9554 return 0;
9555}
9556
1713d319
MV
9557static void
9558scm_i_range_error (SCM bad_val, SCM min, SCM max)
9559{
9560 scm_error (scm_out_of_range_key,
9561 NULL,
9562 "Value out of range ~S to ~S: ~S",
9563 scm_list_3 (min, max, bad_val),
9564 scm_list_1 (bad_val));
9565}
9566
bfd7932e
MV
9567#define TYPE scm_t_intmax
9568#define TYPE_MIN min
9569#define TYPE_MAX max
9570#define SIZEOF_TYPE 0
9571#define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, scm_t_intmax min, scm_t_intmax max)
9572#define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg)
9573#include "libguile/conv-integer.i.c"
9574
9575#define TYPE scm_t_uintmax
9576#define TYPE_MIN min
9577#define TYPE_MAX max
9578#define SIZEOF_TYPE 0
9579#define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, scm_t_uintmax min, scm_t_uintmax max)
9580#define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg)
9581#include "libguile/conv-uinteger.i.c"
9582
9583#define TYPE scm_t_int8
9584#define TYPE_MIN SCM_T_INT8_MIN
9585#define TYPE_MAX SCM_T_INT8_MAX
9586#define SIZEOF_TYPE 1
9587#define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg)
9588#define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg)
9589#include "libguile/conv-integer.i.c"
9590
9591#define TYPE scm_t_uint8
9592#define TYPE_MIN 0
9593#define TYPE_MAX SCM_T_UINT8_MAX
9594#define SIZEOF_TYPE 1
9595#define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg)
9596#define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg)
9597#include "libguile/conv-uinteger.i.c"
9598
9599#define TYPE scm_t_int16
9600#define TYPE_MIN SCM_T_INT16_MIN
9601#define TYPE_MAX SCM_T_INT16_MAX
9602#define SIZEOF_TYPE 2
9603#define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg)
9604#define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg)
9605#include "libguile/conv-integer.i.c"
9606
9607#define TYPE scm_t_uint16
9608#define TYPE_MIN 0
9609#define TYPE_MAX SCM_T_UINT16_MAX
9610#define SIZEOF_TYPE 2
9611#define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg)
9612#define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg)
9613#include "libguile/conv-uinteger.i.c"
9614
9615#define TYPE scm_t_int32
9616#define TYPE_MIN SCM_T_INT32_MIN
9617#define TYPE_MAX SCM_T_INT32_MAX
9618#define SIZEOF_TYPE 4
9619#define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg)
9620#define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg)
9621#include "libguile/conv-integer.i.c"
9622
9623#define TYPE scm_t_uint32
9624#define TYPE_MIN 0
9625#define TYPE_MAX SCM_T_UINT32_MAX
9626#define SIZEOF_TYPE 4
9627#define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg)
9628#define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg)
9629#include "libguile/conv-uinteger.i.c"
9630
904a78f1
MG
9631#define TYPE scm_t_wchar
9632#define TYPE_MIN (scm_t_int32)-1
9633#define TYPE_MAX (scm_t_int32)0x10ffff
9634#define SIZEOF_TYPE 4
9635#define SCM_TO_TYPE_PROTO(arg) scm_to_wchar (arg)
9636#define SCM_FROM_TYPE_PROTO(arg) scm_from_wchar (arg)
9637#include "libguile/conv-integer.i.c"
9638
bfd7932e
MV
9639#define TYPE scm_t_int64
9640#define TYPE_MIN SCM_T_INT64_MIN
9641#define TYPE_MAX SCM_T_INT64_MAX
9642#define SIZEOF_TYPE 8
9643#define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg)
9644#define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg)
9645#include "libguile/conv-integer.i.c"
9646
9647#define TYPE scm_t_uint64
9648#define TYPE_MIN 0
9649#define TYPE_MAX SCM_T_UINT64_MAX
9650#define SIZEOF_TYPE 8
9651#define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg)
9652#define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg)
9653#include "libguile/conv-uinteger.i.c"
73e4de09 9654
cd036260
MV
9655void
9656scm_to_mpz (SCM val, mpz_t rop)
9657{
9658 if (SCM_I_INUMP (val))
9659 mpz_set_si (rop, SCM_I_INUM (val));
9660 else if (SCM_BIGP (val))
9661 mpz_set (rop, SCM_I_BIG_MPZ (val));
9662 else
9663 scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
9664}
9665
9666SCM
9667scm_from_mpz (mpz_t val)
9668{
9669 return scm_i_mpz2num (val);
9670}
9671
73e4de09
MV
9672int
9673scm_is_real (SCM val)
9674{
9675 return scm_is_true (scm_real_p (val));
9676}
9677
55f26379
MV
9678int
9679scm_is_rational (SCM val)
9680{
9681 return scm_is_true (scm_rational_p (val));
9682}
9683
73e4de09
MV
9684double
9685scm_to_double (SCM val)
9686{
55f26379
MV
9687 if (SCM_I_INUMP (val))
9688 return SCM_I_INUM (val);
9689 else if (SCM_BIGP (val))
9690 return scm_i_big2dbl (val);
9691 else if (SCM_FRACTIONP (val))
9692 return scm_i_fraction2double (val);
9693 else if (SCM_REALP (val))
9694 return SCM_REAL_VALUE (val);
9695 else
7a1aba42 9696 scm_wrong_type_arg_msg (NULL, 0, val, "real number");
73e4de09
MV
9697}
9698
9699SCM
9700scm_from_double (double val)
9701{
978c52d1
LC
9702 SCM z;
9703
9704 z = PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_double), "real"));
9705
9706 SCM_SET_CELL_TYPE (z, scm_tc16_real);
55f26379 9707 SCM_REAL_VALUE (z) = val;
978c52d1 9708
55f26379 9709 return z;
73e4de09
MV
9710}
9711
220058a8 9712#if SCM_ENABLE_DEPRECATED == 1
55f26379
MV
9713
9714float
e25f3727 9715scm_num2float (SCM num, unsigned long pos, const char *s_caller)
55f26379 9716{
220058a8
AW
9717 scm_c_issue_deprecation_warning
9718 ("`scm_num2float' is deprecated. Use scm_to_double instead.");
9719
55f26379
MV
9720 if (SCM_BIGP (num))
9721 {
9722 float res = mpz_get_d (SCM_I_BIG_MPZ (num));
2e65b52f 9723 if (!isinf (res))
55f26379
MV
9724 return res;
9725 else
9726 scm_out_of_range (NULL, num);
9727 }
9728 else
9729 return scm_to_double (num);
9730}
9731
9732double
e25f3727 9733scm_num2double (SCM num, unsigned long pos, const char *s_caller)
55f26379 9734{
220058a8
AW
9735 scm_c_issue_deprecation_warning
9736 ("`scm_num2double' is deprecated. Use scm_to_double instead.");
9737
55f26379
MV
9738 if (SCM_BIGP (num))
9739 {
9740 double res = mpz_get_d (SCM_I_BIG_MPZ (num));
2e65b52f 9741 if (!isinf (res))
55f26379
MV
9742 return res;
9743 else
9744 scm_out_of_range (NULL, num);
9745 }
9746 else
9747 return scm_to_double (num);
9748}
9749
9750#endif
9751
8507ec80
MV
9752int
9753scm_is_complex (SCM val)
9754{
9755 return scm_is_true (scm_complex_p (val));
9756}
9757
9758double
9759scm_c_real_part (SCM z)
9760{
9761 if (SCM_COMPLEXP (z))
9762 return SCM_COMPLEX_REAL (z);
9763 else
9764 {
9765 /* Use the scm_real_part to get proper error checking and
9766 dispatching.
9767 */
9768 return scm_to_double (scm_real_part (z));
9769 }
9770}
9771
9772double
9773scm_c_imag_part (SCM z)
9774{
9775 if (SCM_COMPLEXP (z))
9776 return SCM_COMPLEX_IMAG (z);
9777 else
9778 {
9779 /* Use the scm_imag_part to get proper error checking and
9780 dispatching. The result will almost always be 0.0, but not
9781 always.
9782 */
9783 return scm_to_double (scm_imag_part (z));
9784 }
9785}
9786
9787double
9788scm_c_magnitude (SCM z)
9789{
9790 return scm_to_double (scm_magnitude (z));
9791}
9792
9793double
9794scm_c_angle (SCM z)
9795{
9796 return scm_to_double (scm_angle (z));
9797}
9798
9799int
9800scm_is_number (SCM z)
9801{
9802 return scm_is_true (scm_number_p (z));
9803}
9804
8ab3d8a0 9805
a5f6b751
MW
9806/* Returns log(x * 2^shift) */
9807static SCM
9808log_of_shifted_double (double x, long shift)
9809{
9810 double ans = log (fabs (x)) + shift * M_LN2;
9811
9812 if (x > 0.0 || double_is_non_negative_zero (x))
9813 return scm_from_double (ans);
9814 else
9815 return scm_c_make_rectangular (ans, M_PI);
9816}
9817
85bdb6ac 9818/* Returns log(n), for exact integer n */
a5f6b751
MW
9819static SCM
9820log_of_exact_integer (SCM n)
9821{
7f34acd8
MW
9822 if (SCM_I_INUMP (n))
9823 return log_of_shifted_double (SCM_I_INUM (n), 0);
9824 else if (SCM_BIGP (n))
9825 {
9826 long expon;
9827 double signif = scm_i_big2dbl_2exp (n, &expon);
9828 return log_of_shifted_double (signif, expon);
9829 }
9830 else
9831 scm_wrong_type_arg ("log_of_exact_integer", SCM_ARG1, n);
a5f6b751
MW
9832}
9833
9834/* Returns log(n/d), for exact non-zero integers n and d */
9835static SCM
9836log_of_fraction (SCM n, SCM d)
9837{
9838 long n_size = scm_to_long (scm_integer_length (n));
9839 long d_size = scm_to_long (scm_integer_length (d));
9840
9841 if (abs (n_size - d_size) > 1)
7f34acd8
MW
9842 return (scm_difference (log_of_exact_integer (n),
9843 log_of_exact_integer (d)));
a5f6b751
MW
9844 else if (scm_is_false (scm_negative_p (n)))
9845 return scm_from_double
98237784 9846 (log1p (scm_i_divide2double (scm_difference (n, d), d)));
a5f6b751
MW
9847 else
9848 return scm_c_make_rectangular
98237784
MW
9849 (log1p (scm_i_divide2double (scm_difference (scm_abs (n), d),
9850 d)),
a5f6b751
MW
9851 M_PI);
9852}
9853
9854
8ab3d8a0
KR
9855/* In the following functions we dispatch to the real-arg funcs like log()
9856 when we know the arg is real, instead of just handing everything to
9857 clog() for instance. This is in case clog() doesn't optimize for a
9858 real-only case, and because we have to test SCM_COMPLEXP anyway so may as
9859 well use it to go straight to the applicable C func. */
9860
2519490c
MW
9861SCM_PRIMITIVE_GENERIC (scm_log, "log", 1, 0, 0,
9862 (SCM z),
9863 "Return the natural logarithm of @var{z}.")
8ab3d8a0
KR
9864#define FUNC_NAME s_scm_log
9865{
9866 if (SCM_COMPLEXP (z))
9867 {
03976fee
AW
9868#if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG \
9869 && defined (SCM_COMPLEX_VALUE)
8ab3d8a0
KR
9870 return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z)));
9871#else
9872 double re = SCM_COMPLEX_REAL (z);
9873 double im = SCM_COMPLEX_IMAG (z);
9874 return scm_c_make_rectangular (log (hypot (re, im)),
9875 atan2 (im, re));
9876#endif
9877 }
a5f6b751
MW
9878 else if (SCM_REALP (z))
9879 return log_of_shifted_double (SCM_REAL_VALUE (z), 0);
9880 else if (SCM_I_INUMP (z))
8ab3d8a0 9881 {
a5f6b751
MW
9882#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
9883 if (scm_is_eq (z, SCM_INUM0))
9884 scm_num_overflow (s_scm_log);
9885#endif
9886 return log_of_shifted_double (SCM_I_INUM (z), 0);
8ab3d8a0 9887 }
a5f6b751
MW
9888 else if (SCM_BIGP (z))
9889 return log_of_exact_integer (z);
9890 else if (SCM_FRACTIONP (z))
9891 return log_of_fraction (SCM_FRACTION_NUMERATOR (z),
9892 SCM_FRACTION_DENOMINATOR (z));
2519490c
MW
9893 else
9894 SCM_WTA_DISPATCH_1 (g_scm_log, z, 1, s_scm_log);
8ab3d8a0
KR
9895}
9896#undef FUNC_NAME
9897
9898
2519490c
MW
9899SCM_PRIMITIVE_GENERIC (scm_log10, "log10", 1, 0, 0,
9900 (SCM z),
9901 "Return the base 10 logarithm of @var{z}.")
8ab3d8a0
KR
9902#define FUNC_NAME s_scm_log10
9903{
9904 if (SCM_COMPLEXP (z))
9905 {
9906 /* Mingw has clog() but not clog10(). (Maybe it'd be worth using
9907 clog() and a multiply by M_LOG10E, rather than the fallback
9908 log10+hypot+atan2.) */
f328f862
LC
9909#if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG10 \
9910 && defined SCM_COMPLEX_VALUE
8ab3d8a0
KR
9911 return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z)));
9912#else
9913 double re = SCM_COMPLEX_REAL (z);
9914 double im = SCM_COMPLEX_IMAG (z);
9915 return scm_c_make_rectangular (log10 (hypot (re, im)),
9916 M_LOG10E * atan2 (im, re));
9917#endif
9918 }
a5f6b751 9919 else if (SCM_REALP (z) || SCM_I_INUMP (z))
8ab3d8a0 9920 {
a5f6b751
MW
9921#ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
9922 if (scm_is_eq (z, SCM_INUM0))
9923 scm_num_overflow (s_scm_log10);
9924#endif
9925 {
9926 double re = scm_to_double (z);
9927 double l = log10 (fabs (re));
9928 if (re > 0.0 || double_is_non_negative_zero (re))
9929 return scm_from_double (l);
9930 else
9931 return scm_c_make_rectangular (l, M_LOG10E * M_PI);
9932 }
8ab3d8a0 9933 }
a5f6b751
MW
9934 else if (SCM_BIGP (z))
9935 return scm_product (flo_log10e, log_of_exact_integer (z));
9936 else if (SCM_FRACTIONP (z))
9937 return scm_product (flo_log10e,
9938 log_of_fraction (SCM_FRACTION_NUMERATOR (z),
9939 SCM_FRACTION_DENOMINATOR (z)));
2519490c
MW
9940 else
9941 SCM_WTA_DISPATCH_1 (g_scm_log10, z, 1, s_scm_log10);
8ab3d8a0
KR
9942}
9943#undef FUNC_NAME
9944
9945
2519490c
MW
9946SCM_PRIMITIVE_GENERIC (scm_exp, "exp", 1, 0, 0,
9947 (SCM z),
9948 "Return @math{e} to the power of @var{z}, where @math{e} is the\n"
9949 "base of natural logarithms (2.71828@dots{}).")
8ab3d8a0
KR
9950#define FUNC_NAME s_scm_exp
9951{
9952 if (SCM_COMPLEXP (z))
9953 {
93723f3d
MW
9954#if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CEXP \
9955 && defined (SCM_COMPLEX_VALUE)
9956 return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z)));
9957#else
8ab3d8a0
KR
9958 return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)),
9959 SCM_COMPLEX_IMAG (z));
93723f3d 9960#endif
8ab3d8a0 9961 }
2519490c 9962 else if (SCM_NUMBERP (z))
8ab3d8a0
KR
9963 {
9964 /* When z is a negative bignum the conversion to double overflows,
9965 giving -infinity, but that's ok, the exp is still 0.0. */
9966 return scm_from_double (exp (scm_to_double (z)));
9967 }
2519490c
MW
9968 else
9969 SCM_WTA_DISPATCH_1 (g_scm_exp, z, 1, s_scm_exp);
8ab3d8a0
KR
9970}
9971#undef FUNC_NAME
9972
9973
882c8963
MW
9974SCM_DEFINE (scm_i_exact_integer_sqrt, "exact-integer-sqrt", 1, 0, 0,
9975 (SCM k),
9976 "Return two exact non-negative integers @var{s} and @var{r}\n"
9977 "such that @math{@var{k} = @var{s}^2 + @var{r}} and\n"
9978 "@math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.\n"
9979 "An error is raised if @var{k} is not an exact non-negative integer.\n"
9980 "\n"
9981 "@lisp\n"
9982 "(exact-integer-sqrt 10) @result{} 3 and 1\n"
9983 "@end lisp")
9984#define FUNC_NAME s_scm_i_exact_integer_sqrt
9985{
9986 SCM s, r;
9987
9988 scm_exact_integer_sqrt (k, &s, &r);
9989 return scm_values (scm_list_2 (s, r));
9990}
9991#undef FUNC_NAME
9992
9993void
9994scm_exact_integer_sqrt (SCM k, SCM *sp, SCM *rp)
9995{
9996 if (SCM_LIKELY (SCM_I_INUMP (k)))
9997 {
687a87bf 9998 mpz_t kk, ss, rr;
882c8963 9999
687a87bf 10000 if (SCM_I_INUM (k) < 0)
882c8963
MW
10001 scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
10002 "exact non-negative integer");
687a87bf
MW
10003 mpz_init_set_ui (kk, SCM_I_INUM (k));
10004 mpz_inits (ss, rr, NULL);
10005 mpz_sqrtrem (ss, rr, kk);
10006 *sp = SCM_I_MAKINUM (mpz_get_ui (ss));
10007 *rp = SCM_I_MAKINUM (mpz_get_ui (rr));
10008 mpz_clears (kk, ss, rr, NULL);
882c8963
MW
10009 }
10010 else if (SCM_LIKELY (SCM_BIGP (k)))
10011 {
10012 SCM s, r;
10013
10014 if (mpz_sgn (SCM_I_BIG_MPZ (k)) < 0)
10015 scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
10016 "exact non-negative integer");
10017 s = scm_i_mkbig ();
10018 r = scm_i_mkbig ();
10019 mpz_sqrtrem (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (k));
10020 scm_remember_upto_here_1 (k);
10021 *sp = scm_i_normbig (s);
10022 *rp = scm_i_normbig (r);
10023 }
10024 else
10025 scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
10026 "exact non-negative integer");
10027}
10028
ddb71742
MW
10029/* Return true iff K is a perfect square.
10030 K must be an exact integer. */
10031static int
10032exact_integer_is_perfect_square (SCM k)
10033{
10034 int result;
10035
10036 if (SCM_LIKELY (SCM_I_INUMP (k)))
10037 {
10038 mpz_t kk;
10039
10040 mpz_init_set_si (kk, SCM_I_INUM (k));
10041 result = mpz_perfect_square_p (kk);
10042 mpz_clear (kk);
10043 }
10044 else
10045 {
10046 result = mpz_perfect_square_p (SCM_I_BIG_MPZ (k));
10047 scm_remember_upto_here_1 (k);
10048 }
10049 return result;
10050}
10051
10052/* Return the floor of the square root of K.
10053 K must be an exact integer. */
10054static SCM
10055exact_integer_floor_square_root (SCM k)
10056{
10057 if (SCM_LIKELY (SCM_I_INUMP (k)))
10058 {
10059 mpz_t kk;
10060 scm_t_inum ss;
10061
10062 mpz_init_set_ui (kk, SCM_I_INUM (k));
10063 mpz_sqrt (kk, kk);
10064 ss = mpz_get_ui (kk);
10065 mpz_clear (kk);
10066 return SCM_I_MAKINUM (ss);
10067 }
10068 else
10069 {
10070 SCM s;
10071
10072 s = scm_i_mkbig ();
10073 mpz_sqrt (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (k));
10074 scm_remember_upto_here_1 (k);
10075 return scm_i_normbig (s);
10076 }
10077}
10078
882c8963 10079
2519490c
MW
10080SCM_PRIMITIVE_GENERIC (scm_sqrt, "sqrt", 1, 0, 0,
10081 (SCM z),
10082 "Return the square root of @var{z}. Of the two possible roots\n"
ffb62a43 10083 "(positive and negative), the one with positive real part\n"
2519490c
MW
10084 "is returned, or if that's zero then a positive imaginary part.\n"
10085 "Thus,\n"
10086 "\n"
10087 "@example\n"
10088 "(sqrt 9.0) @result{} 3.0\n"
10089 "(sqrt -9.0) @result{} 0.0+3.0i\n"
10090 "(sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i\n"
10091 "(sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i\n"
10092 "@end example")
8ab3d8a0
KR
10093#define FUNC_NAME s_scm_sqrt
10094{
2519490c 10095 if (SCM_COMPLEXP (z))
8ab3d8a0 10096 {
f328f862
LC
10097#if defined HAVE_COMPLEX_DOUBLE && defined HAVE_USABLE_CSQRT \
10098 && defined SCM_COMPLEX_VALUE
2519490c 10099 return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (z)));
8ab3d8a0 10100#else
2519490c
MW
10101 double re = SCM_COMPLEX_REAL (z);
10102 double im = SCM_COMPLEX_IMAG (z);
8ab3d8a0
KR
10103 return scm_c_make_polar (sqrt (hypot (re, im)),
10104 0.5 * atan2 (im, re));
10105#endif
10106 }
2519490c 10107 else if (SCM_NUMBERP (z))
8ab3d8a0 10108 {
44002664
MW
10109 if (SCM_I_INUMP (z))
10110 {
ddb71742
MW
10111 scm_t_inum x = SCM_I_INUM (z);
10112
10113 if (SCM_LIKELY (x >= 0))
44002664 10114 {
ddb71742
MW
10115 if (SCM_LIKELY (SCM_I_FIXNUM_BIT < DBL_MANT_DIG
10116 || x < (1L << (DBL_MANT_DIG - 1))))
44002664 10117 {
ddb71742 10118 double root = sqrt (x);
44002664
MW
10119
10120 /* If 0 <= x < 2^(DBL_MANT_DIG-1) and sqrt(x) is an
10121 integer, then the result is exact. */
10122 if (root == floor (root))
10123 return SCM_I_MAKINUM ((scm_t_inum) root);
10124 else
10125 return scm_from_double (root);
10126 }
10127 else
10128 {
ddb71742 10129 mpz_t xx;
44002664
MW
10130 scm_t_inum root;
10131
ddb71742
MW
10132 mpz_init_set_ui (xx, x);
10133 if (mpz_perfect_square_p (xx))
44002664 10134 {
ddb71742
MW
10135 mpz_sqrt (xx, xx);
10136 root = mpz_get_ui (xx);
10137 mpz_clear (xx);
44002664
MW
10138 return SCM_I_MAKINUM (root);
10139 }
10140 else
ddb71742 10141 mpz_clear (xx);
44002664
MW
10142 }
10143 }
10144 }
10145 else if (SCM_BIGP (z))
10146 {
ddb71742 10147 if (mpz_perfect_square_p (SCM_I_BIG_MPZ (z)))
44002664
MW
10148 {
10149 SCM root = scm_i_mkbig ();
10150
10151 mpz_sqrt (SCM_I_BIG_MPZ (root), SCM_I_BIG_MPZ (z));
10152 scm_remember_upto_here_1 (z);
10153 return scm_i_normbig (root);
10154 }
ddb71742
MW
10155 else
10156 {
10157 long expon;
10158 double signif = scm_i_big2dbl_2exp (z, &expon);
10159
10160 if (expon & 1)
10161 {
10162 signif *= 2;
10163 expon--;
10164 }
10165 if (signif < 0)
10166 return scm_c_make_rectangular
10167 (0.0, ldexp (sqrt (-signif), expon / 2));
10168 else
10169 return scm_from_double (ldexp (sqrt (signif), expon / 2));
10170 }
44002664
MW
10171 }
10172 else if (SCM_FRACTIONP (z))
ddb71742
MW
10173 {
10174 SCM n = SCM_FRACTION_NUMERATOR (z);
10175 SCM d = SCM_FRACTION_DENOMINATOR (z);
10176
10177 if (exact_integer_is_perfect_square (n)
10178 && exact_integer_is_perfect_square (d))
10179 return scm_i_make_ratio_already_reduced
10180 (exact_integer_floor_square_root (n),
10181 exact_integer_floor_square_root (d));
10182 else
10183 {
10184 double xx = scm_i_divide2double (n, d);
10185 double abs_xx = fabs (xx);
10186 long shift = 0;
10187
10188 if (SCM_UNLIKELY (abs_xx > DBL_MAX || abs_xx < DBL_MIN))
10189 {
10190 shift = (scm_to_long (scm_integer_length (n))
10191 - scm_to_long (scm_integer_length (d))) / 2;
10192 if (shift > 0)
10193 d = left_shift_exact_integer (d, 2 * shift);
10194 else
10195 n = left_shift_exact_integer (n, -2 * shift);
10196 xx = scm_i_divide2double (n, d);
10197 }
10198
10199 if (xx < 0)
10200 return scm_c_make_rectangular (0.0, ldexp (sqrt (-xx), shift));
10201 else
10202 return scm_from_double (ldexp (sqrt (xx), shift));
10203 }
10204 }
44002664
MW
10205
10206 /* Fallback method, when the cases above do not apply. */
10207 {
10208 double xx = scm_to_double (z);
10209 if (xx < 0)
10210 return scm_c_make_rectangular (0.0, sqrt (-xx));
10211 else
10212 return scm_from_double (sqrt (xx));
10213 }
8ab3d8a0 10214 }
2519490c
MW
10215 else
10216 SCM_WTA_DISPATCH_1 (g_scm_sqrt, z, 1, s_scm_sqrt);
8ab3d8a0
KR
10217}
10218#undef FUNC_NAME
10219
10220
10221
0f2d19dd
JB
10222void
10223scm_init_numbers ()
0f2d19dd 10224{
b57bf272
AW
10225 if (scm_install_gmp_memory_functions)
10226 mp_set_memory_functions (custom_gmp_malloc,
10227 custom_gmp_realloc,
10228 custom_gmp_free);
10229
713a4259
KR
10230 mpz_init_set_si (z_negative_one, -1);
10231
a261c0e9
DH
10232 /* It may be possible to tune the performance of some algorithms by using
10233 * the following constants to avoid the creation of bignums. Please, before
10234 * using these values, remember the two rules of program optimization:
10235 * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
86d31dfe 10236 scm_c_define ("most-positive-fixnum",
d956fa6f 10237 SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
86d31dfe 10238 scm_c_define ("most-negative-fixnum",
d956fa6f 10239 SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
a261c0e9 10240
f3ae5d60
MD
10241 scm_add_feature ("complex");
10242 scm_add_feature ("inexact");
e7efe8e7 10243 flo0 = scm_from_double (0.0);
a5f6b751 10244 flo_log10e = scm_from_double (M_LOG10E);
0b799eea 10245
cff5fa33 10246 exactly_one_half = scm_divide (SCM_INUM1, SCM_I_MAKINUM (2));
98237784
MW
10247
10248 {
10249 /* Set scm_i_divide2double_lo2b to (2 b^p - 1) */
10250 mpz_init_set_ui (scm_i_divide2double_lo2b, 1);
10251 mpz_mul_2exp (scm_i_divide2double_lo2b,
10252 scm_i_divide2double_lo2b,
10253 DBL_MANT_DIG + 1); /* 2 b^p */
10254 mpz_sub_ui (scm_i_divide2double_lo2b, scm_i_divide2double_lo2b, 1);
10255 }
10256
1ea37620
MW
10257 {
10258 /* Set dbl_minimum_normal_mantissa to b^{p-1} */
10259 mpz_init_set_ui (dbl_minimum_normal_mantissa, 1);
10260 mpz_mul_2exp (dbl_minimum_normal_mantissa,
10261 dbl_minimum_normal_mantissa,
10262 DBL_MANT_DIG - 1);
10263 }
10264
a0599745 10265#include "libguile/numbers.x"
0f2d19dd 10266}
89e00824
ML
10267
10268/*
10269 Local Variables:
10270 c-file-style: "gnu"
10271 End:
10272*/