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