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