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