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