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