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