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