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