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