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