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