(scm_bit_extract): Use min instead of MIN.
[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_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 (10+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_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_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_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_INUMP (denominator))
333 {
334 if (SCM_EQ_P (denominator, SCM_INUM0))
335 scm_num_overflow ("make-ratio");
336 if (SCM_EQ_P (denominator, SCM_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_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_NFALSEP (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_INUMP (numerator))
359 {
360 long x = SCM_INUM (numerator);
361 if (SCM_EQ_P (numerator, SCM_INUM0))
362 return SCM_INUM0;
363 if (SCM_INUMP (denominator))
364 {
365 long y;
366 y = SCM_INUM (denominator);
367 if (x == y)
368 return SCM_MAKINUM(1);
369 if ((x % y) == 0)
370 return SCM_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_MAKINUM(-1);
382 }
383 }
384 else if (SCM_BIGP (numerator))
385 {
386 if (SCM_INUMP (denominator))
387 {
388 long yy = SCM_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_EQ_P (numerator, denominator))
395 return SCM_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_EQ_P (divisor, SCM_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_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_INUMP (n))
460 {
461 long val = SCM_INUM (n);
462 return SCM_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_BOOL (odd_p);
469 }
470 else if (!SCM_FALSEP (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_INUMP (n))
495 {
496 long val = SCM_INUM (n);
497 return SCM_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_BOOL (even_p);
504 }
505 else if (!SCM_FALSEP (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_BOOL (xisinf (SCM_REAL_VALUE (n)));
530 else if (SCM_COMPLEXP (n))
531 return SCM_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_BOOL (xisnan (SCM_REAL_VALUE (n)));
546 else if (SCM_COMPLEXP (n))
547 return SCM_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_INUMP (x))
646 {
647 long int xx = SCM_INUM (x);
648 if (xx >= 0)
649 return x;
650 else if (SCM_POSFIXABLE (-xx))
651 return SCM_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_FALSEP (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_INUMP (x))
692 {
693 long xx = SCM_INUM (x);
694 if (SCM_INUMP (y))
695 {
696 long yy = SCM_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_MAKINUM (z);
704 else
705 return scm_i_long2big (z);
706 }
707 }
708 else if (SCM_BIGP (y))
709 {
710 if ((SCM_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_MAKINUM (-1);
717 }
718 else
719 return SCM_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_INUMP (y))
727 {
728 long yy = SCM_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_INUMP (x))
776 {
777 if (SCM_INUMP (y))
778 {
779 long yy = SCM_INUM (y);
780 if (yy == 0)
781 scm_num_overflow (s_remainder);
782 else
783 {
784 long z = SCM_INUM (x) % yy;
785 return SCM_MAKINUM (z);
786 }
787 }
788 else if (SCM_BIGP (y))
789 {
790 if ((SCM_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_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_INUMP (y))
807 {
808 long yy = SCM_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_INUMP (x))
849 {
850 long xx = SCM_INUM (x);
851 if (SCM_INUMP (y))
852 {
853 long yy = SCM_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_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_INUMP (y))
925 {
926 long yy = SCM_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_INUMP (x))
980 {
981 if (SCM_INUMP (y))
982 {
983 long xx = SCM_INUM (x);
984 long yy = SCM_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_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_INUMP (y))
1038 {
1039 unsigned long result;
1040 long yy;
1041 big_inum:
1042 yy = SCM_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_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_MAKINUM (1L);
1080 n2 = SCM_MAKINUM (1L);
1081 }
1082
1083 SCM_GASSERT2 (SCM_INUMP (n1) || SCM_BIGP (n1),
1084 g_lcm, n1, n2, SCM_ARG1, s_lcm);
1085 SCM_GASSERT2 (SCM_INUMP (n2) || SCM_BIGP (n2),
1086 g_lcm, n1, n2, SCM_ARGn, s_lcm);
1087
1088 if (SCM_INUMP (n1))
1089 {
1090 if (SCM_INUMP (n2))
1091 {
1092 SCM d = scm_gcd (n1, n2);
1093 if (SCM_EQ_P (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_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_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_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_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_INUMP (n1))
1201 {
1202 nn1 = SCM_INUM (n1);
1203 if (SCM_INUMP (n2))
1204 {
1205 long nn2 = SCM_INUM (n2);
1206 return SCM_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_INUMP (n2))
1229 {
1230 SCM_SWAP (n1, n2);
1231 nn1 = SCM_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_INUMP (n1))
1275 {
1276 nn1 = SCM_INUM (n1);
1277 if (SCM_INUMP (n2))
1278 {
1279 long nn2 = SCM_INUM (n2);
1280 return SCM_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_INUMP (n2))
1303 {
1304 SCM_SWAP (n1, n2);
1305 nn1 = SCM_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_INUMP (n1))
1351 {
1352 nn1 = SCM_INUM (n1);
1353 if (SCM_INUMP (n2))
1354 {
1355 long nn2 = SCM_INUM (n2);
1356 return SCM_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_INUMP (n2))
1377 {
1378 SCM_SWAP (n1, n2);
1379 nn1 = SCM_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_INUMP (j))
1412 {
1413 nj = SCM_INUM (j);
1414 if (SCM_INUMP (k))
1415 {
1416 long nk = SCM_INUM (k);
1417 return SCM_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_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_INUMP (k))
1441 {
1442 SCM_SWAP (j, k);
1443 nj = SCM_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_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
1482 SCM_VALIDATE_INUM_MIN (SCM_ARG1, index, 0);
1483 iindex = (unsigned long int) SCM_INUM (index);
1484
1485 if (SCM_INUMP (j))
1486 return SCM_BOOL ((1L << iindex) & SCM_INUM (j));
1487 else if (SCM_BIGP (j))
1488 {
1489 int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex);
1490 scm_remember_upto_here_1 (j);
1491 return SCM_BOOL (val);
1492 }
1493 else
1494 SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
1495 }
1496 #undef FUNC_NAME
1497
1498
1499 SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
1500 (SCM n),
1501 "Return the integer which is the ones-complement of the integer\n"
1502 "argument.\n"
1503 "\n"
1504 "@lisp\n"
1505 "(number->string (lognot #b10000000) 2)\n"
1506 " @result{} \"-10000001\"\n"
1507 "(number->string (lognot #b0) 2)\n"
1508 " @result{} \"-1\"\n"
1509 "@end lisp")
1510 #define FUNC_NAME s_scm_lognot
1511 {
1512 if (SCM_INUMP (n)) {
1513 /* No overflow here, just need to toggle all the bits making up the inum.
1514 Enhancement: No need to strip the tag and add it back, could just xor
1515 a block of 1 bits, if that worked with the various debug versions of
1516 the SCM typedef. */
1517 return SCM_MAKINUM (~ SCM_INUM (n));
1518
1519 } else if (SCM_BIGP (n)) {
1520 SCM result = scm_i_mkbig ();
1521 mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n));
1522 scm_remember_upto_here_1 (n);
1523 return result;
1524
1525 } else {
1526 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
1527 }
1528 }
1529 #undef FUNC_NAME
1530
1531 /* returns 0 if IN is not an integer. OUT must already be
1532 initialized. */
1533 static int
1534 coerce_to_big (SCM in, mpz_t out)
1535 {
1536 if (SCM_BIGP (in))
1537 mpz_set (out, SCM_I_BIG_MPZ (in));
1538 else if (SCM_INUMP (in))
1539 mpz_set_si (out, SCM_INUM (in));
1540 else
1541 return 0;
1542
1543 return 1;
1544 }
1545
1546 SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0,
1547 (SCM n, SCM k, SCM m),
1548 "Return @var{n} raised to the integer exponent\n"
1549 "@var{k}, modulo @var{m}.\n"
1550 "\n"
1551 "@lisp\n"
1552 "(modulo-expt 2 3 5)\n"
1553 " @result{} 3\n"
1554 "@end lisp")
1555 #define FUNC_NAME s_scm_modulo_expt
1556 {
1557 mpz_t n_tmp;
1558 mpz_t k_tmp;
1559 mpz_t m_tmp;
1560
1561 /* There are two classes of error we might encounter --
1562 1) Math errors, which we'll report by calling scm_num_overflow,
1563 and
1564 2) wrong-type errors, which of course we'll report by calling
1565 SCM_WRONG_TYPE_ARG.
1566 We don't report those errors immediately, however; instead we do
1567 some cleanup first. These variables tell us which error (if
1568 any) we should report after cleaning up.
1569 */
1570 int report_overflow = 0;
1571
1572 int position_of_wrong_type = 0;
1573 SCM value_of_wrong_type = SCM_INUM0;
1574
1575 SCM result = SCM_UNDEFINED;
1576
1577 mpz_init (n_tmp);
1578 mpz_init (k_tmp);
1579 mpz_init (m_tmp);
1580
1581 if (SCM_EQ_P (m, SCM_INUM0))
1582 {
1583 report_overflow = 1;
1584 goto cleanup;
1585 }
1586
1587 if (!coerce_to_big (n, n_tmp))
1588 {
1589 value_of_wrong_type = n;
1590 position_of_wrong_type = 1;
1591 goto cleanup;
1592 }
1593
1594 if (!coerce_to_big (k, k_tmp))
1595 {
1596 value_of_wrong_type = k;
1597 position_of_wrong_type = 2;
1598 goto cleanup;
1599 }
1600
1601 if (!coerce_to_big (m, m_tmp))
1602 {
1603 value_of_wrong_type = m;
1604 position_of_wrong_type = 3;
1605 goto cleanup;
1606 }
1607
1608 /* if the exponent K is negative, and we simply call mpz_powm, we
1609 will get a divide-by-zero exception when an inverse 1/n mod m
1610 doesn't exist (or is not unique). Since exceptions are hard to
1611 handle, we'll attempt the inversion "by hand" -- that way, we get
1612 a simple failure code, which is easy to handle. */
1613
1614 if (-1 == mpz_sgn (k_tmp))
1615 {
1616 if (!mpz_invert (n_tmp, n_tmp, m_tmp))
1617 {
1618 report_overflow = 1;
1619 goto cleanup;
1620 }
1621 mpz_neg (k_tmp, k_tmp);
1622 }
1623
1624 result = scm_i_mkbig ();
1625 mpz_powm (SCM_I_BIG_MPZ (result),
1626 n_tmp,
1627 k_tmp,
1628 m_tmp);
1629
1630 if (mpz_sgn (m_tmp) < 0 && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)
1631 mpz_add (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), m_tmp);
1632
1633 cleanup:
1634 mpz_clear (m_tmp);
1635 mpz_clear (k_tmp);
1636 mpz_clear (n_tmp);
1637
1638 if (report_overflow)
1639 scm_num_overflow (FUNC_NAME);
1640
1641 if (position_of_wrong_type)
1642 SCM_WRONG_TYPE_ARG (position_of_wrong_type,
1643 value_of_wrong_type);
1644
1645 return scm_i_normbig (result);
1646 }
1647 #undef FUNC_NAME
1648
1649 SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
1650 (SCM n, SCM k),
1651 "Return @var{n} raised to the non-negative integer exponent\n"
1652 "@var{k}.\n"
1653 "\n"
1654 "@lisp\n"
1655 "(integer-expt 2 5)\n"
1656 " @result{} 32\n"
1657 "(integer-expt -3 3)\n"
1658 " @result{} -27\n"
1659 "@end lisp")
1660 #define FUNC_NAME s_scm_integer_expt
1661 {
1662 long i2 = 0;
1663 SCM z_i2 = SCM_BOOL_F;
1664 int i2_is_big = 0;
1665 SCM acc = SCM_MAKINUM (1L);
1666
1667 /* 0^0 == 1 according to R5RS */
1668 if (SCM_EQ_P (n, SCM_INUM0) || SCM_EQ_P (n, acc))
1669 return SCM_FALSEP (scm_zero_p(k)) ? n : acc;
1670 else if (SCM_EQ_P (n, SCM_MAKINUM (-1L)))
1671 return SCM_FALSEP (scm_even_p (k)) ? n : acc;
1672
1673 if (SCM_INUMP (k))
1674 i2 = SCM_INUM (k);
1675 else if (SCM_BIGP (k))
1676 {
1677 z_i2 = scm_i_clonebig (k, 1);
1678 scm_remember_upto_here_1 (k);
1679 i2_is_big = 1;
1680 }
1681 else if (SCM_REALP (k))
1682 {
1683 double r = SCM_REAL_VALUE (k);
1684 if (floor (r) != r)
1685 SCM_WRONG_TYPE_ARG (2, k);
1686 if ((r > SCM_MOST_POSITIVE_FIXNUM) || (r < SCM_MOST_NEGATIVE_FIXNUM))
1687 {
1688 z_i2 = scm_i_mkbig ();
1689 mpz_set_d (SCM_I_BIG_MPZ (z_i2), r);
1690 i2_is_big = 1;
1691 }
1692 else
1693 {
1694 i2 = r;
1695 }
1696 }
1697 else
1698 SCM_WRONG_TYPE_ARG (2, k);
1699
1700 if (i2_is_big)
1701 {
1702 if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == -1)
1703 {
1704 mpz_neg (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2));
1705 n = scm_divide (n, SCM_UNDEFINED);
1706 }
1707 while (1)
1708 {
1709 if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == 0)
1710 {
1711 return acc;
1712 }
1713 if (mpz_cmp_ui(SCM_I_BIG_MPZ (z_i2), 1) == 0)
1714 {
1715 return scm_product (acc, n);
1716 }
1717 if (mpz_tstbit(SCM_I_BIG_MPZ (z_i2), 0))
1718 acc = scm_product (acc, n);
1719 n = scm_product (n, n);
1720 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2), 1);
1721 }
1722 }
1723 else
1724 {
1725 if (i2 < 0)
1726 {
1727 i2 = -i2;
1728 n = scm_divide (n, SCM_UNDEFINED);
1729 }
1730 while (1)
1731 {
1732 if (0 == i2)
1733 return acc;
1734 if (1 == i2)
1735 return scm_product (acc, n);
1736 if (i2 & 1)
1737 acc = scm_product (acc, n);
1738 n = scm_product (n, n);
1739 i2 >>= 1;
1740 }
1741 }
1742 }
1743 #undef FUNC_NAME
1744
1745 SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
1746 (SCM n, SCM cnt),
1747 "Return @var{n} shifted left by @var{cnt} bits, or shifted right\n"
1748 "if @var{cnt} is negative. This is an ``arithmetic'' shift.\n"
1749 "\n"
1750 "This is effectively a multiplication by 2^@var{cnt}, and when\n"
1751 "@var{cnt} is negative it's a division, rounded towards negative\n"
1752 "infinity. (Note that this is not the same rounding as\n"
1753 "@code{quotient} does.)\n"
1754 "\n"
1755 "With @var{n} viewed as an infinite precision twos complement,\n"
1756 "@code{ash} means a left shift introducing zero bits, or a right\n"
1757 "shift dropping bits.\n"
1758 "\n"
1759 "@lisp\n"
1760 "(number->string (ash #b1 3) 2) @result{} \"1000\"\n"
1761 "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n"
1762 "\n"
1763 ";; -23 is bits ...11101001, -6 is bits ...111010\n"
1764 "(ash -23 -2) @result{} -6\n"
1765 "@end lisp")
1766 #define FUNC_NAME s_scm_ash
1767 {
1768 long bits_to_shift;
1769
1770 SCM_VALIDATE_INUM (2, cnt);
1771
1772 bits_to_shift = SCM_INUM (cnt);
1773
1774 if (bits_to_shift < 0)
1775 {
1776 /* Shift right by abs(cnt) bits. This is realized as a division
1777 by div:=2^abs(cnt). However, to guarantee the floor
1778 rounding, negative values require some special treatment.
1779 */
1780 SCM div = scm_integer_expt (SCM_MAKINUM (2),
1781 SCM_MAKINUM (-bits_to_shift));
1782
1783 /* scm_quotient assumes its arguments are integers, but it's legal to (ash 1/2 -1) */
1784 if (SCM_FALSEP (scm_negative_p (n)))
1785 return scm_quotient (n, div);
1786 else
1787 return scm_sum (SCM_MAKINUM (-1L),
1788 scm_quotient (scm_sum (SCM_MAKINUM (1L), n), div));
1789 }
1790 else
1791 /* Shift left is done by multiplication with 2^CNT */
1792 return scm_product (n, scm_integer_expt (SCM_MAKINUM (2), cnt));
1793 }
1794 #undef FUNC_NAME
1795
1796
1797 SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
1798 (SCM n, SCM start, SCM end),
1799 "Return the integer composed of the @var{start} (inclusive)\n"
1800 "through @var{end} (exclusive) bits of @var{n}. The\n"
1801 "@var{start}th bit becomes the 0-th bit in the result.\n"
1802 "\n"
1803 "@lisp\n"
1804 "(number->string (bit-extract #b1101101010 0 4) 2)\n"
1805 " @result{} \"1010\"\n"
1806 "(number->string (bit-extract #b1101101010 4 9) 2)\n"
1807 " @result{} \"10110\"\n"
1808 "@end lisp")
1809 #define FUNC_NAME s_scm_bit_extract
1810 {
1811 unsigned long int istart, iend, bits;
1812 SCM_VALIDATE_INUM_MIN_COPY (2, start,0, istart);
1813 SCM_VALIDATE_INUM_MIN_COPY (3, end, 0, iend);
1814 SCM_ASSERT_RANGE (3, end, (iend >= istart));
1815
1816 /* how many bits to keep */
1817 bits = iend - istart;
1818
1819 if (SCM_INUMP (n))
1820 {
1821 long int in = SCM_INUM (n);
1822
1823 /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
1824 SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */
1825 in = SCM_SRS (in, min (istart, SCM_I_FIXNUM_BIT-1));
1826
1827 if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
1828 {
1829 /* Since we emulate two's complement encoded numbers, this
1830 * special case requires us to produce a result that has
1831 * more bits than can be stored in a fixnum.
1832 */
1833 SCM result = scm_i_long2big (in);
1834 mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
1835 bits);
1836 return result;
1837 }
1838
1839 /* mask down to requisite bits */
1840 bits = min (bits, SCM_I_FIXNUM_BIT);
1841 return SCM_MAKINUM (in & ((1L << bits) - 1));
1842 }
1843 else if (SCM_BIGP (n))
1844 {
1845 SCM result;
1846 if (bits == 1)
1847 {
1848 result = SCM_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart));
1849 }
1850 else
1851 {
1852 /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
1853 bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
1854 such bits into a ulong. */
1855 result = scm_i_mkbig ();
1856 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart);
1857 mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits);
1858 result = scm_i_normbig (result);
1859 }
1860 scm_remember_upto_here_1 (n);
1861 return result;
1862 }
1863 else
1864 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
1865 }
1866 #undef FUNC_NAME
1867
1868
1869 static const char scm_logtab[] = {
1870 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1871 };
1872
1873 SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
1874 (SCM n),
1875 "Return the number of bits in integer @var{n}. If integer is\n"
1876 "positive, the 1-bits in its binary representation are counted.\n"
1877 "If negative, the 0-bits in its two's-complement binary\n"
1878 "representation are counted. If 0, 0 is returned.\n"
1879 "\n"
1880 "@lisp\n"
1881 "(logcount #b10101010)\n"
1882 " @result{} 4\n"
1883 "(logcount 0)\n"
1884 " @result{} 0\n"
1885 "(logcount -2)\n"
1886 " @result{} 1\n"
1887 "@end lisp")
1888 #define FUNC_NAME s_scm_logcount
1889 {
1890 if (SCM_INUMP (n))
1891 {
1892 unsigned long int c = 0;
1893 long int nn = SCM_INUM (n);
1894 if (nn < 0)
1895 nn = -1 - nn;
1896 while (nn)
1897 {
1898 c += scm_logtab[15 & nn];
1899 nn >>= 4;
1900 }
1901 return SCM_MAKINUM (c);
1902 }
1903 else if (SCM_BIGP (n))
1904 {
1905 unsigned long count;
1906 if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0)
1907 count = mpz_popcount (SCM_I_BIG_MPZ (n));
1908 else
1909 count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one);
1910 scm_remember_upto_here_1 (n);
1911 return SCM_MAKINUM (count);
1912 }
1913 else
1914 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
1915 }
1916 #undef FUNC_NAME
1917
1918
1919 static const char scm_ilentab[] = {
1920 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
1921 };
1922
1923
1924 SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
1925 (SCM n),
1926 "Return the number of bits necessary to represent @var{n}.\n"
1927 "\n"
1928 "@lisp\n"
1929 "(integer-length #b10101010)\n"
1930 " @result{} 8\n"
1931 "(integer-length 0)\n"
1932 " @result{} 0\n"
1933 "(integer-length #b1111)\n"
1934 " @result{} 4\n"
1935 "@end lisp")
1936 #define FUNC_NAME s_scm_integer_length
1937 {
1938 if (SCM_INUMP (n))
1939 {
1940 unsigned long int c = 0;
1941 unsigned int l = 4;
1942 long int nn = SCM_INUM (n);
1943 if (nn < 0)
1944 nn = -1 - nn;
1945 while (nn)
1946 {
1947 c += 4;
1948 l = scm_ilentab [15 & nn];
1949 nn >>= 4;
1950 }
1951 return SCM_MAKINUM (c - 4 + l);
1952 }
1953 else if (SCM_BIGP (n))
1954 {
1955 /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
1956 want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
1957 1 too big, so check for that and adjust. */
1958 size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
1959 if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
1960 && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
1961 mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
1962 size--;
1963 scm_remember_upto_here_1 (n);
1964 return SCM_MAKINUM (size);
1965 }
1966 else
1967 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
1968 }
1969 #undef FUNC_NAME
1970
1971 /*** NUMBERS -> STRINGS ***/
1972 int scm_dblprec;
1973 static const double fx[] =
1974 { 0.0, 5e-1, 5e-2, 5e-3, 5e-4, 5e-5,
1975 5e-6, 5e-7, 5e-8, 5e-9, 5e-10,
1976 5e-11, 5e-12, 5e-13, 5e-14, 5e-15,
1977 5e-16, 5e-17, 5e-18, 5e-19, 5e-20};
1978
1979 static size_t
1980 idbl2str (double f, char *a)
1981 {
1982 int efmt, dpt, d, i, wp = scm_dblprec;
1983 size_t ch = 0;
1984 int exp = 0;
1985
1986 if (f == 0.0)
1987 {
1988 #ifdef HAVE_COPYSIGN
1989 double sgn = copysign (1.0, f);
1990
1991 if (sgn < 0.0)
1992 a[ch++] = '-';
1993 #endif
1994
1995 goto zero; /*{a[0]='0'; a[1]='.'; a[2]='0'; return 3;} */
1996 }
1997
1998 if (xisinf (f))
1999 {
2000 if (f < 0)
2001 strcpy (a, "-inf.0");
2002 else
2003 strcpy (a, "+inf.0");
2004 return ch+6;
2005 }
2006 else if (xisnan (f))
2007 {
2008 strcpy (a, "+nan.0");
2009 return ch+6;
2010 }
2011
2012 if (f < 0.0)
2013 {
2014 f = -f;
2015 a[ch++] = '-';
2016 }
2017
2018 #ifdef DBL_MIN_10_EXP /* Prevent unnormalized values, as from
2019 make-uniform-vector, from causing infinite loops. */
2020 while (f < 1.0)
2021 {
2022 f *= 10.0;
2023 if (exp-- < DBL_MIN_10_EXP)
2024 {
2025 a[ch++] = '#';
2026 a[ch++] = '.';
2027 a[ch++] = '#';
2028 return ch;
2029 }
2030 }
2031 while (f > 10.0)
2032 {
2033 f *= 0.10;
2034 if (exp++ > DBL_MAX_10_EXP)
2035 {
2036 a[ch++] = '#';
2037 a[ch++] = '.';
2038 a[ch++] = '#';
2039 return ch;
2040 }
2041 }
2042 #else
2043 while (f < 1.0)
2044 {
2045 f *= 10.0;
2046 exp--;
2047 }
2048 while (f > 10.0)
2049 {
2050 f /= 10.0;
2051 exp++;
2052 }
2053 #endif
2054 if (f + fx[wp] >= 10.0)
2055 {
2056 f = 1.0;
2057 exp++;
2058 }
2059 zero:
2060 #ifdef ENGNOT
2061 dpt = (exp + 9999) % 3;
2062 exp -= dpt++;
2063 efmt = 1;
2064 #else
2065 efmt = (exp < -3) || (exp > wp + 2);
2066 if (!efmt)
2067 {
2068 if (exp < 0)
2069 {
2070 a[ch++] = '0';
2071 a[ch++] = '.';
2072 dpt = exp;
2073 while (++dpt)
2074 a[ch++] = '0';
2075 }
2076 else
2077 dpt = exp + 1;
2078 }
2079 else
2080 dpt = 1;
2081 #endif
2082
2083 do
2084 {
2085 d = f;
2086 f -= d;
2087 a[ch++] = d + '0';
2088 if (f < fx[wp])
2089 break;
2090 if (f + fx[wp] >= 1.0)
2091 {
2092 a[ch - 1]++;
2093 break;
2094 }
2095 f *= 10.0;
2096 if (!(--dpt))
2097 a[ch++] = '.';
2098 }
2099 while (wp--);
2100
2101 if (dpt > 0)
2102 {
2103 #ifndef ENGNOT
2104 if ((dpt > 4) && (exp > 6))
2105 {
2106 d = (a[0] == '-' ? 2 : 1);
2107 for (i = ch++; i > d; i--)
2108 a[i] = a[i - 1];
2109 a[d] = '.';
2110 efmt = 1;
2111 }
2112 else
2113 #endif
2114 {
2115 while (--dpt)
2116 a[ch++] = '0';
2117 a[ch++] = '.';
2118 }
2119 }
2120 if (a[ch - 1] == '.')
2121 a[ch++] = '0'; /* trailing zero */
2122 if (efmt && exp)
2123 {
2124 a[ch++] = 'e';
2125 if (exp < 0)
2126 {
2127 exp = -exp;
2128 a[ch++] = '-';
2129 }
2130 for (i = 10; i <= exp; i *= 10);
2131 for (i /= 10; i; i /= 10)
2132 {
2133 a[ch++] = exp / i + '0';
2134 exp %= i;
2135 }
2136 }
2137 return ch;
2138 }
2139
2140
2141 static size_t
2142 iflo2str (SCM flt, char *str)
2143 {
2144 size_t i;
2145 if (SCM_REALP (flt))
2146 i = idbl2str (SCM_REAL_VALUE (flt), str);
2147 else
2148 {
2149 i = idbl2str (SCM_COMPLEX_REAL (flt), str);
2150 if (SCM_COMPLEX_IMAG (flt) != 0.0)
2151 {
2152 double imag = SCM_COMPLEX_IMAG (flt);
2153 /* Don't output a '+' for negative numbers or for Inf and
2154 NaN. They will provide their own sign. */
2155 if (0 <= imag && !xisinf (imag) && !xisnan (imag))
2156 str[i++] = '+';
2157 i += idbl2str (imag, &str[i]);
2158 str[i++] = 'i';
2159 }
2160 }
2161 return i;
2162 }
2163
2164 /* convert a long to a string (unterminated). returns the number of
2165 characters in the result.
2166 rad is output base
2167 p is destination: worst case (base 2) is SCM_INTBUFLEN */
2168 size_t
2169 scm_iint2str (long num, int rad, char *p)
2170 {
2171 size_t j = 1;
2172 size_t i;
2173 unsigned long n = (num < 0) ? -num : num;
2174
2175 for (n /= rad; n > 0; n /= rad)
2176 j++;
2177
2178 i = j;
2179 if (num < 0)
2180 {
2181 *p++ = '-';
2182 j++;
2183 n = -num;
2184 }
2185 else
2186 n = num;
2187 while (i--)
2188 {
2189 int d = n % rad;
2190
2191 n /= rad;
2192 p[i] = d + ((d < 10) ? '0' : 'a' - 10);
2193 }
2194 return j;
2195 }
2196
2197 SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
2198 (SCM n, SCM radix),
2199 "Return a string holding the external representation of the\n"
2200 "number @var{n} in the given @var{radix}. If @var{n} is\n"
2201 "inexact, a radix of 10 will be used.")
2202 #define FUNC_NAME s_scm_number_to_string
2203 {
2204 int base;
2205
2206 if (SCM_UNBNDP (radix))
2207 base = 10;
2208 else
2209 {
2210 SCM_VALIDATE_INUM (2, radix);
2211 base = SCM_INUM (radix);
2212 /* FIXME: ask if range limit was OK, and if so, document */
2213 SCM_ASSERT_RANGE (2, radix, (base >= 2) && (base <= 36));
2214 }
2215
2216 if (SCM_INUMP (n))
2217 {
2218 char num_buf [SCM_INTBUFLEN];
2219 size_t length = scm_iint2str (SCM_INUM (n), base, num_buf);
2220 return scm_mem2string (num_buf, length);
2221 }
2222 else if (SCM_BIGP (n))
2223 {
2224 char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
2225 scm_remember_upto_here_1 (n);
2226 return scm_take0str (str);
2227 }
2228 else if (SCM_FRACTIONP (n))
2229 {
2230 scm_i_fraction_reduce (n);
2231 return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
2232 scm_mem2string ("/", 1),
2233 scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
2234 }
2235 else if (SCM_INEXACTP (n))
2236 {
2237 char num_buf [FLOBUFLEN];
2238 return scm_mem2string (num_buf, iflo2str (n, num_buf));
2239 }
2240 else
2241 SCM_WRONG_TYPE_ARG (1, n);
2242 }
2243 #undef FUNC_NAME
2244
2245
2246 /* These print routines used to be stubbed here so that scm_repl.c
2247 wouldn't need SCM_BIGDIG conditionals (pre GMP) */
2248
2249 int
2250 scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
2251 {
2252 char num_buf[FLOBUFLEN];
2253 scm_lfwrite (num_buf, iflo2str (sexp, num_buf), port);
2254 return !0;
2255 }
2256
2257 int
2258 scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
2259
2260 {
2261 char num_buf[FLOBUFLEN];
2262 scm_lfwrite (num_buf, iflo2str (sexp, num_buf), port);
2263 return !0;
2264 }
2265
2266 int
2267 scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
2268 {
2269 SCM str;
2270 scm_i_fraction_reduce (sexp);
2271 str = scm_number_to_string (sexp, SCM_UNDEFINED);
2272 scm_lfwrite (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port);
2273 scm_remember_upto_here_1 (str);
2274 return !0;
2275 }
2276
2277 int
2278 scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
2279 {
2280 char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp));
2281 scm_remember_upto_here_1 (exp);
2282 scm_lfwrite (str, (size_t) strlen (str), port);
2283 free (str);
2284 return !0;
2285 }
2286 /*** END nums->strs ***/
2287
2288
2289 /*** STRINGS -> NUMBERS ***/
2290
2291 /* The following functions implement the conversion from strings to numbers.
2292 * The implementation somehow follows the grammar for numbers as it is given
2293 * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
2294 * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
2295 * points should be noted about the implementation:
2296 * * Each function keeps a local index variable 'idx' that points at the
2297 * current position within the parsed string. The global index is only
2298 * updated if the function could parse the corresponding syntactic unit
2299 * successfully.
2300 * * Similarly, the functions keep track of indicators of inexactness ('#',
2301 * '.' or exponents) using local variables ('hash_seen', 'x'). Again, the
2302 * global exactness information is only updated after each part has been
2303 * successfully parsed.
2304 * * Sequences of digits are parsed into temporary variables holding fixnums.
2305 * Only if these fixnums would overflow, the result variables are updated
2306 * using the standard functions scm_add, scm_product, scm_divide etc. Then,
2307 * the temporary variables holding the fixnums are cleared, and the process
2308 * starts over again. If for example fixnums were able to store five decimal
2309 * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
2310 * and the result was computed as 12345 * 100000 + 67890. In other words,
2311 * only every five digits two bignum operations were performed.
2312 */
2313
2314 enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
2315
2316 /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
2317
2318 /* In non ASCII-style encodings the following macro might not work. */
2319 #define XDIGIT2UINT(d) (isdigit (d) ? (d) - '0' : tolower (d) - 'a' + 10)
2320
2321 static SCM
2322 mem2uinteger (const char* mem, size_t len, unsigned int *p_idx,
2323 unsigned int radix, enum t_exactness *p_exactness)
2324 {
2325 unsigned int idx = *p_idx;
2326 unsigned int hash_seen = 0;
2327 scm_t_bits shift = 1;
2328 scm_t_bits add = 0;
2329 unsigned int digit_value;
2330 SCM result;
2331 char c;
2332
2333 if (idx == len)
2334 return SCM_BOOL_F;
2335
2336 c = mem[idx];
2337 if (!isxdigit (c))
2338 return SCM_BOOL_F;
2339 digit_value = XDIGIT2UINT (c);
2340 if (digit_value >= radix)
2341 return SCM_BOOL_F;
2342
2343 idx++;
2344 result = SCM_MAKINUM (digit_value);
2345 while (idx != len)
2346 {
2347 char c = mem[idx];
2348 if (isxdigit (c))
2349 {
2350 if (hash_seen)
2351 break;
2352 digit_value = XDIGIT2UINT (c);
2353 if (digit_value >= radix)
2354 break;
2355 }
2356 else if (c == '#')
2357 {
2358 hash_seen = 1;
2359 digit_value = 0;
2360 }
2361 else
2362 break;
2363
2364 idx++;
2365 if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
2366 {
2367 result = scm_product (result, SCM_MAKINUM (shift));
2368 if (add > 0)
2369 result = scm_sum (result, SCM_MAKINUM (add));
2370
2371 shift = radix;
2372 add = digit_value;
2373 }
2374 else
2375 {
2376 shift = shift * radix;
2377 add = add * radix + digit_value;
2378 }
2379 };
2380
2381 if (shift > 1)
2382 result = scm_product (result, SCM_MAKINUM (shift));
2383 if (add > 0)
2384 result = scm_sum (result, SCM_MAKINUM (add));
2385
2386 *p_idx = idx;
2387 if (hash_seen)
2388 *p_exactness = INEXACT;
2389
2390 return result;
2391 }
2392
2393
2394 /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
2395 * covers the parts of the rules that start at a potential point. The value
2396 * of the digits up to the point have been parsed by the caller and are given
2397 * in variable result. The content of *p_exactness indicates, whether a hash
2398 * has already been seen in the digits before the point.
2399 */
2400
2401 /* In non ASCII-style encodings the following macro might not work. */
2402 #define DIGIT2UINT(d) ((d) - '0')
2403
2404 static SCM
2405 mem2decimal_from_point (SCM result, const char* mem, size_t len,
2406 unsigned int *p_idx, enum t_exactness *p_exactness)
2407 {
2408 unsigned int idx = *p_idx;
2409 enum t_exactness x = *p_exactness;
2410
2411 if (idx == len)
2412 return result;
2413
2414 if (mem[idx] == '.')
2415 {
2416 scm_t_bits shift = 1;
2417 scm_t_bits add = 0;
2418 unsigned int digit_value;
2419 SCM big_shift = SCM_MAKINUM (1);
2420
2421 idx++;
2422 while (idx != len)
2423 {
2424 char c = mem[idx];
2425 if (isdigit (c))
2426 {
2427 if (x == INEXACT)
2428 return SCM_BOOL_F;
2429 else
2430 digit_value = DIGIT2UINT (c);
2431 }
2432 else if (c == '#')
2433 {
2434 x = INEXACT;
2435 digit_value = 0;
2436 }
2437 else
2438 break;
2439
2440 idx++;
2441 if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
2442 {
2443 big_shift = scm_product (big_shift, SCM_MAKINUM (shift));
2444 result = scm_product (result, SCM_MAKINUM (shift));
2445 if (add > 0)
2446 result = scm_sum (result, SCM_MAKINUM (add));
2447
2448 shift = 10;
2449 add = digit_value;
2450 }
2451 else
2452 {
2453 shift = shift * 10;
2454 add = add * 10 + digit_value;
2455 }
2456 };
2457
2458 if (add > 0)
2459 {
2460 big_shift = scm_product (big_shift, SCM_MAKINUM (shift));
2461 result = scm_product (result, SCM_MAKINUM (shift));
2462 result = scm_sum (result, SCM_MAKINUM (add));
2463 }
2464
2465 result = scm_divide (result, big_shift);
2466
2467 /* We've seen a decimal point, thus the value is implicitly inexact. */
2468 x = INEXACT;
2469 }
2470
2471 if (idx != len)
2472 {
2473 int sign = 1;
2474 unsigned int start;
2475 char c;
2476 int exponent;
2477 SCM e;
2478
2479 /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
2480
2481 switch (mem[idx])
2482 {
2483 case 'd': case 'D':
2484 case 'e': case 'E':
2485 case 'f': case 'F':
2486 case 'l': case 'L':
2487 case 's': case 'S':
2488 idx++;
2489 start = idx;
2490 c = mem[idx];
2491 if (c == '-')
2492 {
2493 idx++;
2494 sign = -1;
2495 c = mem[idx];
2496 }
2497 else if (c == '+')
2498 {
2499 idx++;
2500 sign = 1;
2501 c = mem[idx];
2502 }
2503 else
2504 sign = 1;
2505
2506 if (!isdigit (c))
2507 return SCM_BOOL_F;
2508
2509 idx++;
2510 exponent = DIGIT2UINT (c);
2511 while (idx != len)
2512 {
2513 char c = mem[idx];
2514 if (isdigit (c))
2515 {
2516 idx++;
2517 if (exponent <= SCM_MAXEXP)
2518 exponent = exponent * 10 + DIGIT2UINT (c);
2519 }
2520 else
2521 break;
2522 }
2523
2524 if (exponent > SCM_MAXEXP)
2525 {
2526 size_t exp_len = idx - start;
2527 SCM exp_string = scm_mem2string (&mem[start], exp_len);
2528 SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
2529 scm_out_of_range ("string->number", exp_num);
2530 }
2531
2532 e = scm_integer_expt (SCM_MAKINUM (10), SCM_MAKINUM (exponent));
2533 if (sign == 1)
2534 result = scm_product (result, e);
2535 else
2536 result = scm_divide2real (result, e);
2537
2538 /* We've seen an exponent, thus the value is implicitly inexact. */
2539 x = INEXACT;
2540
2541 break;
2542
2543 default:
2544 break;
2545 }
2546 }
2547
2548 *p_idx = idx;
2549 if (x == INEXACT)
2550 *p_exactness = x;
2551
2552 return result;
2553 }
2554
2555
2556 /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
2557
2558 static SCM
2559 mem2ureal (const char* mem, size_t len, unsigned int *p_idx,
2560 unsigned int radix, enum t_exactness *p_exactness)
2561 {
2562 unsigned int idx = *p_idx;
2563 SCM result;
2564
2565 if (idx == len)
2566 return SCM_BOOL_F;
2567
2568 if (idx+5 <= len && !strncmp (mem+idx, "inf.0", 5))
2569 {
2570 *p_idx = idx+5;
2571 return scm_inf ();
2572 }
2573
2574 if (idx+4 < len && !strncmp (mem+idx, "nan.", 4))
2575 {
2576 enum t_exactness x = EXACT;
2577
2578 /* Cobble up the fractional part. We might want to set the
2579 NaN's mantissa from it. */
2580 idx += 4;
2581 mem2uinteger (mem, len, &idx, 10, &x);
2582 *p_idx = idx;
2583 return scm_nan ();
2584 }
2585
2586 if (mem[idx] == '.')
2587 {
2588 if (radix != 10)
2589 return SCM_BOOL_F;
2590 else if (idx + 1 == len)
2591 return SCM_BOOL_F;
2592 else if (!isdigit (mem[idx + 1]))
2593 return SCM_BOOL_F;
2594 else
2595 result = mem2decimal_from_point (SCM_MAKINUM (0), mem, len,
2596 p_idx, p_exactness);
2597 }
2598 else
2599 {
2600 enum t_exactness x = EXACT;
2601 SCM uinteger;
2602
2603 uinteger = mem2uinteger (mem, len, &idx, radix, &x);
2604 if (SCM_FALSEP (uinteger))
2605 return SCM_BOOL_F;
2606
2607 if (idx == len)
2608 result = uinteger;
2609 else if (mem[idx] == '/')
2610 {
2611 SCM divisor;
2612
2613 idx++;
2614
2615 divisor = mem2uinteger (mem, len, &idx, radix, &x);
2616 if (SCM_FALSEP (divisor))
2617 return SCM_BOOL_F;
2618
2619 /* both are int/big here, I assume */
2620 result = scm_make_ratio (uinteger, divisor);
2621 }
2622 else if (radix == 10)
2623 {
2624 result = mem2decimal_from_point (uinteger, mem, len, &idx, &x);
2625 if (SCM_FALSEP (result))
2626 return SCM_BOOL_F;
2627 }
2628 else
2629 result = uinteger;
2630
2631 *p_idx = idx;
2632 if (x == INEXACT)
2633 *p_exactness = x;
2634 }
2635
2636 /* When returning an inexact zero, make sure it is represented as a
2637 floating point value so that we can change its sign.
2638 */
2639 if (SCM_EQ_P (result, SCM_MAKINUM(0)) && *p_exactness == INEXACT)
2640 result = scm_make_real (0.0);
2641
2642 return result;
2643 }
2644
2645
2646 /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
2647
2648 static SCM
2649 mem2complex (const char* mem, size_t len, unsigned int idx,
2650 unsigned int radix, enum t_exactness *p_exactness)
2651 {
2652 char c;
2653 int sign = 0;
2654 SCM ureal;
2655
2656 if (idx == len)
2657 return SCM_BOOL_F;
2658
2659 c = mem[idx];
2660 if (c == '+')
2661 {
2662 idx++;
2663 sign = 1;
2664 }
2665 else if (c == '-')
2666 {
2667 idx++;
2668 sign = -1;
2669 }
2670
2671 if (idx == len)
2672 return SCM_BOOL_F;
2673
2674 ureal = mem2ureal (mem, len, &idx, radix, p_exactness);
2675 if (SCM_FALSEP (ureal))
2676 {
2677 /* input must be either +i or -i */
2678
2679 if (sign == 0)
2680 return SCM_BOOL_F;
2681
2682 if (mem[idx] == 'i' || mem[idx] == 'I')
2683 {
2684 idx++;
2685 if (idx != len)
2686 return SCM_BOOL_F;
2687
2688 return scm_make_rectangular (SCM_MAKINUM (0), SCM_MAKINUM (sign));
2689 }
2690 else
2691 return SCM_BOOL_F;
2692 }
2693 else
2694 {
2695 if (sign == -1 && SCM_FALSEP (scm_nan_p (ureal)))
2696 ureal = scm_difference (ureal, SCM_UNDEFINED);
2697
2698 if (idx == len)
2699 return ureal;
2700
2701 c = mem[idx];
2702 switch (c)
2703 {
2704 case 'i': case 'I':
2705 /* either +<ureal>i or -<ureal>i */
2706
2707 idx++;
2708 if (sign == 0)
2709 return SCM_BOOL_F;
2710 if (idx != len)
2711 return SCM_BOOL_F;
2712 return scm_make_rectangular (SCM_MAKINUM (0), ureal);
2713
2714 case '@':
2715 /* polar input: <real>@<real>. */
2716
2717 idx++;
2718 if (idx == len)
2719 return SCM_BOOL_F;
2720 else
2721 {
2722 int sign;
2723 SCM angle;
2724 SCM result;
2725
2726 c = mem[idx];
2727 if (c == '+')
2728 {
2729 idx++;
2730 sign = 1;
2731 }
2732 else if (c == '-')
2733 {
2734 idx++;
2735 sign = -1;
2736 }
2737 else
2738 sign = 1;
2739
2740 angle = mem2ureal (mem, len, &idx, radix, p_exactness);
2741 if (SCM_FALSEP (angle))
2742 return SCM_BOOL_F;
2743 if (idx != len)
2744 return SCM_BOOL_F;
2745
2746 if (sign == -1 && SCM_FALSEP (scm_nan_p (ureal)))
2747 angle = scm_difference (angle, SCM_UNDEFINED);
2748
2749 result = scm_make_polar (ureal, angle);
2750 return result;
2751 }
2752 case '+':
2753 case '-':
2754 /* expecting input matching <real>[+-]<ureal>?i */
2755
2756 idx++;
2757 if (idx == len)
2758 return SCM_BOOL_F;
2759 else
2760 {
2761 int sign = (c == '+') ? 1 : -1;
2762 SCM imag = mem2ureal (mem, len, &idx, radix, p_exactness);
2763
2764 if (SCM_FALSEP (imag))
2765 imag = SCM_MAKINUM (sign);
2766 else if (sign == -1 && SCM_FALSEP (scm_nan_p (ureal)))
2767 imag = scm_difference (imag, SCM_UNDEFINED);
2768
2769 if (idx == len)
2770 return SCM_BOOL_F;
2771 if (mem[idx] != 'i' && mem[idx] != 'I')
2772 return SCM_BOOL_F;
2773
2774 idx++;
2775 if (idx != len)
2776 return SCM_BOOL_F;
2777
2778 return scm_make_rectangular (ureal, imag);
2779 }
2780 default:
2781 return SCM_BOOL_F;
2782 }
2783 }
2784 }
2785
2786
2787 /* R5RS, section 7.1.1, lexical structure of numbers: <number> */
2788
2789 enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
2790
2791 SCM
2792 scm_i_mem2number (const char* mem, size_t len, unsigned int default_radix)
2793 {
2794 unsigned int idx = 0;
2795 unsigned int radix = NO_RADIX;
2796 enum t_exactness forced_x = NO_EXACTNESS;
2797 enum t_exactness implicit_x = EXACT;
2798 SCM result;
2799
2800 /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
2801 while (idx + 2 < len && mem[idx] == '#')
2802 {
2803 switch (mem[idx + 1])
2804 {
2805 case 'b': case 'B':
2806 if (radix != NO_RADIX)
2807 return SCM_BOOL_F;
2808 radix = DUAL;
2809 break;
2810 case 'd': case 'D':
2811 if (radix != NO_RADIX)
2812 return SCM_BOOL_F;
2813 radix = DEC;
2814 break;
2815 case 'i': case 'I':
2816 if (forced_x != NO_EXACTNESS)
2817 return SCM_BOOL_F;
2818 forced_x = INEXACT;
2819 break;
2820 case 'e': case 'E':
2821 if (forced_x != NO_EXACTNESS)
2822 return SCM_BOOL_F;
2823 forced_x = EXACT;
2824 break;
2825 case 'o': case 'O':
2826 if (radix != NO_RADIX)
2827 return SCM_BOOL_F;
2828 radix = OCT;
2829 break;
2830 case 'x': case 'X':
2831 if (radix != NO_RADIX)
2832 return SCM_BOOL_F;
2833 radix = HEX;
2834 break;
2835 default:
2836 return SCM_BOOL_F;
2837 }
2838 idx += 2;
2839 }
2840
2841 /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
2842 if (radix == NO_RADIX)
2843 result = mem2complex (mem, len, idx, default_radix, &implicit_x);
2844 else
2845 result = mem2complex (mem, len, idx, (unsigned int) radix, &implicit_x);
2846
2847 if (SCM_FALSEP (result))
2848 return SCM_BOOL_F;
2849
2850 switch (forced_x)
2851 {
2852 case EXACT:
2853 if (SCM_INEXACTP (result))
2854 return scm_inexact_to_exact (result);
2855 else
2856 return result;
2857 case INEXACT:
2858 if (SCM_INEXACTP (result))
2859 return result;
2860 else
2861 return scm_exact_to_inexact (result);
2862 case NO_EXACTNESS:
2863 default:
2864 if (implicit_x == INEXACT)
2865 {
2866 if (SCM_INEXACTP (result))
2867 return result;
2868 else
2869 return scm_exact_to_inexact (result);
2870 }
2871 else
2872 return result;
2873 }
2874 }
2875
2876
2877 SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
2878 (SCM string, SCM radix),
2879 "Return a number of the maximally precise representation\n"
2880 "expressed by the given @var{string}. @var{radix} must be an\n"
2881 "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
2882 "is a default radix that may be overridden by an explicit radix\n"
2883 "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
2884 "supplied, then the default radix is 10. If string is not a\n"
2885 "syntactically valid notation for a number, then\n"
2886 "@code{string->number} returns @code{#f}.")
2887 #define FUNC_NAME s_scm_string_to_number
2888 {
2889 SCM answer;
2890 int base;
2891 SCM_VALIDATE_STRING (1, string);
2892 SCM_VALIDATE_INUM_MIN_DEF_COPY (2, radix,2,10, base);
2893 answer = scm_i_mem2number (SCM_STRING_CHARS (string),
2894 SCM_STRING_LENGTH (string),
2895 base);
2896 return scm_return_first (answer, string);
2897 }
2898 #undef FUNC_NAME
2899
2900
2901 /*** END strs->nums ***/
2902
2903
2904 SCM
2905 scm_make_real (double x)
2906 {
2907 SCM z = scm_double_cell (scm_tc16_real, 0, 0, 0);
2908
2909 SCM_REAL_VALUE (z) = x;
2910 return z;
2911 }
2912
2913
2914 SCM
2915 scm_make_complex (double x, double y)
2916 {
2917 if (y == 0.0)
2918 return scm_make_real (x);
2919 else
2920 {
2921 SCM z;
2922 SCM_NEWSMOB (z, scm_tc16_complex, scm_gc_malloc (sizeof (scm_t_complex),
2923 "complex"));
2924 SCM_COMPLEX_REAL (z) = x;
2925 SCM_COMPLEX_IMAG (z) = y;
2926 return z;
2927 }
2928 }
2929
2930
2931 SCM
2932 scm_bigequal (SCM x, SCM y)
2933 {
2934 int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
2935 scm_remember_upto_here_2 (x, y);
2936 return SCM_BOOL (0 == result);
2937 }
2938
2939 SCM
2940 scm_real_equalp (SCM x, SCM y)
2941 {
2942 return SCM_BOOL (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
2943 }
2944
2945 SCM
2946 scm_complex_equalp (SCM x, SCM y)
2947 {
2948 return SCM_BOOL (SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)
2949 && SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y));
2950 }
2951
2952 SCM
2953 scm_i_fraction_equalp (SCM x, SCM y)
2954 {
2955 scm_i_fraction_reduce (x);
2956 scm_i_fraction_reduce (y);
2957 if (SCM_FALSEP (scm_equal_p (SCM_FRACTION_NUMERATOR (x),
2958 SCM_FRACTION_NUMERATOR (y)))
2959 || SCM_FALSEP (scm_equal_p (SCM_FRACTION_DENOMINATOR (x),
2960 SCM_FRACTION_DENOMINATOR (y))))
2961 return SCM_BOOL_F;
2962 else
2963 return SCM_BOOL_T;
2964 }
2965
2966
2967 SCM_REGISTER_PROC (s_number_p, "number?", 1, 0, 0, scm_number_p);
2968 /* "Return @code{#t} if @var{x} is a number, @code{#f}\n"
2969 * "else. Note that the sets of complex, real, rational and\n"
2970 * "integer values form subsets of the set of numbers, i. e. the\n"
2971 * "predicate will be fulfilled for any number."
2972 */
2973 SCM_DEFINE (scm_number_p, "complex?", 1, 0, 0,
2974 (SCM x),
2975 "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
2976 "otherwise. Note that the sets of real, rational and integer\n"
2977 "values form subsets of the set of complex numbers, i. e. the\n"
2978 "predicate will also be fulfilled if @var{x} is a real,\n"
2979 "rational or integer number.")
2980 #define FUNC_NAME s_scm_number_p
2981 {
2982 return SCM_BOOL (SCM_NUMBERP (x));
2983 }
2984 #undef FUNC_NAME
2985
2986
2987 SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
2988 (SCM x),
2989 "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
2990 "otherwise. Note that the set of integer values forms a subset of\n"
2991 "the set of real numbers, i. e. the predicate will also be\n"
2992 "fulfilled if @var{x} is an integer number.")
2993 #define FUNC_NAME s_scm_real_p
2994 {
2995 /* we can't represent irrational numbers. */
2996 return scm_rational_p (x);
2997 }
2998 #undef FUNC_NAME
2999
3000 SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
3001 (SCM x),
3002 "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
3003 "otherwise. Note that the set of integer values forms a subset of\n"
3004 "the set of rational numbers, i. e. the predicate will also be\n"
3005 "fulfilled if @var{x} is an integer number.")
3006 #define FUNC_NAME s_scm_rational_p
3007 {
3008 if (SCM_INUMP (x))
3009 return SCM_BOOL_T;
3010 else if (SCM_IMP (x))
3011 return SCM_BOOL_F;
3012 else if (SCM_BIGP (x))
3013 return SCM_BOOL_T;
3014 else if (SCM_FRACTIONP (x))
3015 return SCM_BOOL_T;
3016 else if (SCM_REALP (x))
3017 /* due to their limited precision, all floating point numbers are
3018 rational as well. */
3019 return SCM_BOOL_T;
3020 else
3021 return SCM_BOOL_F;
3022 }
3023 #undef FUNC_NAME
3024
3025
3026 SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
3027 (SCM x),
3028 "Return @code{#t} if @var{x} is an integer number, @code{#f}\n"
3029 "else.")
3030 #define FUNC_NAME s_scm_integer_p
3031 {
3032 double r;
3033 if (SCM_INUMP (x))
3034 return SCM_BOOL_T;
3035 if (SCM_IMP (x))
3036 return SCM_BOOL_F;
3037 if (SCM_BIGP (x))
3038 return SCM_BOOL_T;
3039 if (!SCM_INEXACTP (x))
3040 return SCM_BOOL_F;
3041 if (SCM_COMPLEXP (x))
3042 return SCM_BOOL_F;
3043 r = SCM_REAL_VALUE (x);
3044 if (r == floor (r))
3045 return SCM_BOOL_T;
3046 return SCM_BOOL_F;
3047 }
3048 #undef FUNC_NAME
3049
3050
3051 SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0,
3052 (SCM x),
3053 "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
3054 "else.")
3055 #define FUNC_NAME s_scm_inexact_p
3056 {
3057 if (SCM_INEXACTP (x))
3058 return SCM_BOOL_T;
3059 if (SCM_NUMBERP (x))
3060 return SCM_BOOL_F;
3061 SCM_WRONG_TYPE_ARG (1, x);
3062 }
3063 #undef FUNC_NAME
3064
3065
3066 SCM_GPROC1 (s_eq_p, "=", scm_tc7_rpsubr, scm_num_eq_p, g_eq_p);
3067 /* "Return @code{#t} if all parameters are numerically equal." */
3068 SCM
3069 scm_num_eq_p (SCM x, SCM y)
3070 {
3071 again:
3072 if (SCM_INUMP (x))
3073 {
3074 long xx = SCM_INUM (x);
3075 if (SCM_INUMP (y))
3076 {
3077 long yy = SCM_INUM (y);
3078 return SCM_BOOL (xx == yy);
3079 }
3080 else if (SCM_BIGP (y))
3081 return SCM_BOOL_F;
3082 else if (SCM_REALP (y))
3083 return SCM_BOOL ((double) xx == SCM_REAL_VALUE (y));
3084 else if (SCM_COMPLEXP (y))
3085 return SCM_BOOL (((double) xx == SCM_COMPLEX_REAL (y))
3086 && (0.0 == SCM_COMPLEX_IMAG (y)));
3087 else if (SCM_FRACTIONP (y))
3088 return SCM_BOOL_F;
3089 else
3090 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3091 }
3092 else if (SCM_BIGP (x))
3093 {
3094 if (SCM_INUMP (y))
3095 return SCM_BOOL_F;
3096 else if (SCM_BIGP (y))
3097 {
3098 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3099 scm_remember_upto_here_2 (x, y);
3100 return SCM_BOOL (0 == cmp);
3101 }
3102 else if (SCM_REALP (y))
3103 {
3104 int cmp;
3105 if (xisnan (SCM_REAL_VALUE (y)))
3106 return SCM_BOOL_F;
3107 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
3108 scm_remember_upto_here_1 (x);
3109 return SCM_BOOL (0 == cmp);
3110 }
3111 else if (SCM_COMPLEXP (y))
3112 {
3113 int cmp;
3114 if (0.0 != SCM_COMPLEX_IMAG (y))
3115 return SCM_BOOL_F;
3116 if (xisnan (SCM_COMPLEX_REAL (y)))
3117 return SCM_BOOL_F;
3118 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y));
3119 scm_remember_upto_here_1 (x);
3120 return SCM_BOOL (0 == cmp);
3121 }
3122 else if (SCM_FRACTIONP (y))
3123 return SCM_BOOL_F;
3124 else
3125 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3126 }
3127 else if (SCM_REALP (x))
3128 {
3129 if (SCM_INUMP (y))
3130 return SCM_BOOL (SCM_REAL_VALUE (x) == (double) SCM_INUM (y));
3131 else if (SCM_BIGP (y))
3132 {
3133 int cmp;
3134 if (xisnan (SCM_REAL_VALUE (x)))
3135 return SCM_BOOL_F;
3136 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
3137 scm_remember_upto_here_1 (y);
3138 return SCM_BOOL (0 == cmp);
3139 }
3140 else if (SCM_REALP (y))
3141 return SCM_BOOL (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
3142 else if (SCM_COMPLEXP (y))
3143 return SCM_BOOL ((SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y))
3144 && (0.0 == SCM_COMPLEX_IMAG (y)));
3145 else if (SCM_FRACTIONP (y))
3146 {
3147 double xx = SCM_REAL_VALUE (x);
3148 if (xisnan (xx))
3149 return SCM_BOOL_F;
3150 if (xisinf (xx))
3151 return SCM_BOOL (xx < 0.0);
3152 x = scm_inexact_to_exact (x); /* with x as frac or int */
3153 goto again;
3154 }
3155 else
3156 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3157 }
3158 else if (SCM_COMPLEXP (x))
3159 {
3160 if (SCM_INUMP (y))
3161 return SCM_BOOL ((SCM_COMPLEX_REAL (x) == (double) SCM_INUM (y))
3162 && (SCM_COMPLEX_IMAG (x) == 0.0));
3163 else if (SCM_BIGP (y))
3164 {
3165 int cmp;
3166 if (0.0 != SCM_COMPLEX_IMAG (x))
3167 return SCM_BOOL_F;
3168 if (xisnan (SCM_COMPLEX_REAL (x)))
3169 return SCM_BOOL_F;
3170 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x));
3171 scm_remember_upto_here_1 (y);
3172 return SCM_BOOL (0 == cmp);
3173 }
3174 else if (SCM_REALP (y))
3175 return SCM_BOOL ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
3176 && (SCM_COMPLEX_IMAG (x) == 0.0));
3177 else if (SCM_COMPLEXP (y))
3178 return SCM_BOOL ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
3179 && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
3180 else if (SCM_FRACTIONP (y))
3181 {
3182 double xx;
3183 if (SCM_COMPLEX_IMAG (x) != 0.0)
3184 return SCM_BOOL_F;
3185 xx = SCM_COMPLEX_REAL (x);
3186 if (xisnan (xx))
3187 return SCM_BOOL_F;
3188 if (xisinf (xx))
3189 return SCM_BOOL (xx < 0.0);
3190 x = scm_inexact_to_exact (x); /* with x as frac or int */
3191 goto again;
3192 }
3193 else
3194 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3195 }
3196 else if (SCM_FRACTIONP (x))
3197 {
3198 if (SCM_INUMP (y))
3199 return SCM_BOOL_F;
3200 else if (SCM_BIGP (y))
3201 return SCM_BOOL_F;
3202 else if (SCM_REALP (y))
3203 {
3204 double yy = SCM_REAL_VALUE (y);
3205 if (xisnan (yy))
3206 return SCM_BOOL_F;
3207 if (xisinf (yy))
3208 return SCM_BOOL (0.0 < yy);
3209 y = scm_inexact_to_exact (y); /* with y as frac or int */
3210 goto again;
3211 }
3212 else if (SCM_COMPLEXP (y))
3213 {
3214 double yy;
3215 if (SCM_COMPLEX_IMAG (y) != 0.0)
3216 return SCM_BOOL_F;
3217 yy = SCM_COMPLEX_REAL (y);
3218 if (xisnan (yy))
3219 return SCM_BOOL_F;
3220 if (xisinf (yy))
3221 return SCM_BOOL (0.0 < yy);
3222 y = scm_inexact_to_exact (y); /* with y as frac or int */
3223 goto again;
3224 }
3225 else if (SCM_FRACTIONP (y))
3226 return scm_i_fraction_equalp (x, y);
3227 else
3228 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3229 }
3230 else
3231 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARG1, s_eq_p);
3232 }
3233
3234
3235 /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
3236 done are good for inums, but for bignums an answer can almost always be
3237 had by just examining a few high bits of the operands, as done by GMP in
3238 mpq_cmp. flonum/frac compares likewise, but with the slight complication
3239 of the float exponent to take into account. */
3240
3241 SCM_GPROC1 (s_less_p, "<", scm_tc7_rpsubr, scm_less_p, g_less_p);
3242 /* "Return @code{#t} if the list of parameters is monotonically\n"
3243 * "increasing."
3244 */
3245 SCM
3246 scm_less_p (SCM x, SCM y)
3247 {
3248 again:
3249 if (SCM_INUMP (x))
3250 {
3251 long xx = SCM_INUM (x);
3252 if (SCM_INUMP (y))
3253 {
3254 long yy = SCM_INUM (y);
3255 return SCM_BOOL (xx < yy);
3256 }
3257 else if (SCM_BIGP (y))
3258 {
3259 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
3260 scm_remember_upto_here_1 (y);
3261 return SCM_BOOL (sgn > 0);
3262 }
3263 else if (SCM_REALP (y))
3264 return SCM_BOOL ((double) xx < SCM_REAL_VALUE (y));
3265 else if (SCM_FRACTIONP (y))
3266 {
3267 /* "x < a/b" becomes "x*b < a" */
3268 int_frac:
3269 x = scm_product (x, SCM_FRACTION_DENOMINATOR (y));
3270 y = SCM_FRACTION_NUMERATOR (y);
3271 goto again;
3272 }
3273 else
3274 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3275 }
3276 else if (SCM_BIGP (x))
3277 {
3278 if (SCM_INUMP (y))
3279 {
3280 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3281 scm_remember_upto_here_1 (x);
3282 return SCM_BOOL (sgn < 0);
3283 }
3284 else if (SCM_BIGP (y))
3285 {
3286 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3287 scm_remember_upto_here_2 (x, y);
3288 return SCM_BOOL (cmp < 0);
3289 }
3290 else if (SCM_REALP (y))
3291 {
3292 int cmp;
3293 if (xisnan (SCM_REAL_VALUE (y)))
3294 return SCM_BOOL_F;
3295 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
3296 scm_remember_upto_here_1 (x);
3297 return SCM_BOOL (cmp < 0);
3298 }
3299 else if (SCM_FRACTIONP (y))
3300 goto int_frac;
3301 else
3302 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3303 }
3304 else if (SCM_REALP (x))
3305 {
3306 if (SCM_INUMP (y))
3307 return SCM_BOOL (SCM_REAL_VALUE (x) < (double) SCM_INUM (y));
3308 else if (SCM_BIGP (y))
3309 {
3310 int cmp;
3311 if (xisnan (SCM_REAL_VALUE (x)))
3312 return SCM_BOOL_F;
3313 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
3314 scm_remember_upto_here_1 (y);
3315 return SCM_BOOL (cmp > 0);
3316 }
3317 else if (SCM_REALP (y))
3318 return SCM_BOOL (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
3319 else if (SCM_FRACTIONP (y))
3320 {
3321 double xx = SCM_REAL_VALUE (x);
3322 if (xisnan (xx))
3323 return SCM_BOOL_F;
3324 if (xisinf (xx))
3325 return SCM_BOOL (xx < 0.0);
3326 x = scm_inexact_to_exact (x); /* with x as frac or int */
3327 goto again;
3328 }
3329 else
3330 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3331 }
3332 else if (SCM_FRACTIONP (x))
3333 {
3334 if (SCM_INUMP (y) || SCM_BIGP (y))
3335 {
3336 /* "a/b < y" becomes "a < y*b" */
3337 y = scm_product (y, SCM_FRACTION_DENOMINATOR (x));
3338 x = SCM_FRACTION_NUMERATOR (x);
3339 goto again;
3340 }
3341 else if (SCM_REALP (y))
3342 {
3343 double yy = SCM_REAL_VALUE (y);
3344 if (xisnan (yy))
3345 return SCM_BOOL_F;
3346 if (xisinf (yy))
3347 return SCM_BOOL (0.0 < yy);
3348 y = scm_inexact_to_exact (y); /* with y as frac or int */
3349 goto again;
3350 }
3351 else if (SCM_FRACTIONP (y))
3352 {
3353 /* "a/b < c/d" becomes "a*d < c*b" */
3354 SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x),
3355 SCM_FRACTION_DENOMINATOR (y));
3356 SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y),
3357 SCM_FRACTION_DENOMINATOR (x));
3358 x = new_x;
3359 y = new_y;
3360 goto again;
3361 }
3362 else
3363 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3364 }
3365 else
3366 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARG1, s_less_p);
3367 }
3368
3369
3370 SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p);
3371 /* "Return @code{#t} if the list of parameters is monotonically\n"
3372 * "decreasing."
3373 */
3374 #define FUNC_NAME s_scm_gr_p
3375 SCM
3376 scm_gr_p (SCM x, SCM y)
3377 {
3378 if (!SCM_NUMBERP (x))
3379 SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG1, FUNC_NAME);
3380 else if (!SCM_NUMBERP (y))
3381 SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG2, FUNC_NAME);
3382 else
3383 return scm_less_p (y, x);
3384 }
3385 #undef FUNC_NAME
3386
3387
3388 SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p);
3389 /* "Return @code{#t} if the list of parameters is monotonically\n"
3390 * "non-decreasing."
3391 */
3392 #define FUNC_NAME s_scm_leq_p
3393 SCM
3394 scm_leq_p (SCM x, SCM y)
3395 {
3396 if (!SCM_NUMBERP (x))
3397 SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG1, FUNC_NAME);
3398 else if (!SCM_NUMBERP (y))
3399 SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG2, FUNC_NAME);
3400 else if (SCM_NFALSEP (scm_nan_p (x)) || SCM_NFALSEP (scm_nan_p (y)))
3401 return SCM_BOOL_F;
3402 else
3403 return SCM_BOOL_NOT (scm_less_p (y, x));
3404 }
3405 #undef FUNC_NAME
3406
3407
3408 SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p);
3409 /* "Return @code{#t} if the list of parameters is monotonically\n"
3410 * "non-increasing."
3411 */
3412 #define FUNC_NAME s_scm_geq_p
3413 SCM
3414 scm_geq_p (SCM x, SCM y)
3415 {
3416 if (!SCM_NUMBERP (x))
3417 SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG1, FUNC_NAME);
3418 else if (!SCM_NUMBERP (y))
3419 SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG2, FUNC_NAME);
3420 else if (SCM_NFALSEP (scm_nan_p (x)) || SCM_NFALSEP (scm_nan_p (y)))
3421 return SCM_BOOL_F;
3422 else
3423 return SCM_BOOL_NOT (scm_less_p (x, y));
3424 }
3425 #undef FUNC_NAME
3426
3427
3428 SCM_GPROC (s_zero_p, "zero?", 1, 0, 0, scm_zero_p, g_zero_p);
3429 /* "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
3430 * "zero."
3431 */
3432 SCM
3433 scm_zero_p (SCM z)
3434 {
3435 if (SCM_INUMP (z))
3436 return SCM_BOOL (SCM_EQ_P (z, SCM_INUM0));
3437 else if (SCM_BIGP (z))
3438 return SCM_BOOL_F;
3439 else if (SCM_REALP (z))
3440 return SCM_BOOL (SCM_REAL_VALUE (z) == 0.0);
3441 else if (SCM_COMPLEXP (z))
3442 return SCM_BOOL (SCM_COMPLEX_REAL (z) == 0.0
3443 && SCM_COMPLEX_IMAG (z) == 0.0);
3444 else if (SCM_FRACTIONP (z))
3445 return SCM_BOOL_F;
3446 else
3447 SCM_WTA_DISPATCH_1 (g_zero_p, z, SCM_ARG1, s_zero_p);
3448 }
3449
3450
3451 SCM_GPROC (s_positive_p, "positive?", 1, 0, 0, scm_positive_p, g_positive_p);
3452 /* "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
3453 * "zero."
3454 */
3455 SCM
3456 scm_positive_p (SCM x)
3457 {
3458 if (SCM_INUMP (x))
3459 return SCM_BOOL (SCM_INUM (x) > 0);
3460 else if (SCM_BIGP (x))
3461 {
3462 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3463 scm_remember_upto_here_1 (x);
3464 return SCM_BOOL (sgn > 0);
3465 }
3466 else if (SCM_REALP (x))
3467 return SCM_BOOL(SCM_REAL_VALUE (x) > 0.0);
3468 else if (SCM_FRACTIONP (x))
3469 return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
3470 else
3471 SCM_WTA_DISPATCH_1 (g_positive_p, x, SCM_ARG1, s_positive_p);
3472 }
3473
3474
3475 SCM_GPROC (s_negative_p, "negative?", 1, 0, 0, scm_negative_p, g_negative_p);
3476 /* "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
3477 * "zero."
3478 */
3479 SCM
3480 scm_negative_p (SCM x)
3481 {
3482 if (SCM_INUMP (x))
3483 return SCM_BOOL (SCM_INUM (x) < 0);
3484 else if (SCM_BIGP (x))
3485 {
3486 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3487 scm_remember_upto_here_1 (x);
3488 return SCM_BOOL (sgn < 0);
3489 }
3490 else if (SCM_REALP (x))
3491 return SCM_BOOL(SCM_REAL_VALUE (x) < 0.0);
3492 else if (SCM_FRACTIONP (x))
3493 return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
3494 else
3495 SCM_WTA_DISPATCH_1 (g_negative_p, x, SCM_ARG1, s_negative_p);
3496 }
3497
3498
3499 /* scm_min and scm_max return an inexact when either argument is inexact, as
3500 required by r5rs. On that basis, for exact/inexact combinations the
3501 exact is converted to inexact to compare and possibly return. This is
3502 unlike scm_less_p above which takes some trouble to preserve all bits in
3503 its test, such trouble is not required for min and max. */
3504
3505 SCM_GPROC1 (s_max, "max", scm_tc7_asubr, scm_max, g_max);
3506 /* "Return the maximum of all parameter values."
3507 */
3508 SCM
3509 scm_max (SCM x, SCM y)
3510 {
3511 if (SCM_UNBNDP (y))
3512 {
3513 if (SCM_UNBNDP (x))
3514 SCM_WTA_DISPATCH_0 (g_max, s_max);
3515 else if (SCM_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
3516 return x;
3517 else
3518 SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max);
3519 }
3520
3521 if (SCM_INUMP (x))
3522 {
3523 long xx = SCM_INUM (x);
3524 if (SCM_INUMP (y))
3525 {
3526 long yy = SCM_INUM (y);
3527 return (xx < yy) ? y : x;
3528 }
3529 else if (SCM_BIGP (y))
3530 {
3531 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
3532 scm_remember_upto_here_1 (y);
3533 return (sgn < 0) ? x : y;
3534 }
3535 else if (SCM_REALP (y))
3536 {
3537 double z = xx;
3538 /* if y==NaN then ">" is false and we return NaN */
3539 return (z > SCM_REAL_VALUE (y)) ? scm_make_real (z) : y;
3540 }
3541 else if (SCM_FRACTIONP (y))
3542 {
3543 use_less:
3544 return (SCM_FALSEP (scm_less_p (x, y)) ? x : y);
3545 }
3546 else
3547 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3548 }
3549 else if (SCM_BIGP (x))
3550 {
3551 if (SCM_INUMP (y))
3552 {
3553 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3554 scm_remember_upto_here_1 (x);
3555 return (sgn < 0) ? y : x;
3556 }
3557 else if (SCM_BIGP (y))
3558 {
3559 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3560 scm_remember_upto_here_2 (x, y);
3561 return (cmp > 0) ? x : y;
3562 }
3563 else if (SCM_REALP (y))
3564 {
3565 /* if y==NaN then xx>yy is false, so we return the NaN y */
3566 double xx, yy;
3567 big_real:
3568 xx = scm_i_big2dbl (x);
3569 yy = SCM_REAL_VALUE (y);
3570 return (xx > yy ? scm_make_real (xx) : y);
3571 }
3572 else if (SCM_FRACTIONP (y))
3573 {
3574 goto use_less;
3575 }
3576 else
3577 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3578 }
3579 else if (SCM_REALP (x))
3580 {
3581 if (SCM_INUMP (y))
3582 {
3583 double z = SCM_INUM (y);
3584 /* if x==NaN then "<" is false and we return NaN */
3585 return (SCM_REAL_VALUE (x) < z) ? scm_make_real (z) : x;
3586 }
3587 else if (SCM_BIGP (y))
3588 {
3589 SCM_SWAP (x, y);
3590 goto big_real;
3591 }
3592 else if (SCM_REALP (y))
3593 {
3594 /* if x==NaN then our explicit check means we return NaN
3595 if y==NaN then ">" is false and we return NaN
3596 calling isnan is unavoidable, since it's the only way to know
3597 which of x or y causes any compares to be false */
3598 double xx = SCM_REAL_VALUE (x);
3599 return (xisnan (xx) || xx > SCM_REAL_VALUE (y)) ? x : y;
3600 }
3601 else if (SCM_FRACTIONP (y))
3602 {
3603 double yy = scm_i_fraction2double (y);
3604 double xx = SCM_REAL_VALUE (x);
3605 return (xx < yy) ? scm_make_real (yy) : x;
3606 }
3607 else
3608 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3609 }
3610 else if (SCM_FRACTIONP (x))
3611 {
3612 if (SCM_INUMP (y))
3613 {
3614 goto use_less;
3615 }
3616 else if (SCM_BIGP (y))
3617 {
3618 goto use_less;
3619 }
3620 else if (SCM_REALP (y))
3621 {
3622 double xx = scm_i_fraction2double (x);
3623 return (xx < SCM_REAL_VALUE (y)) ? y : scm_make_real (xx);
3624 }
3625 else if (SCM_FRACTIONP (y))
3626 {
3627 goto use_less;
3628 }
3629 else
3630 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3631 }
3632 else
3633 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max);
3634 }
3635
3636
3637 SCM_GPROC1 (s_min, "min", scm_tc7_asubr, scm_min, g_min);
3638 /* "Return the minium of all parameter values."
3639 */
3640 SCM
3641 scm_min (SCM x, SCM y)
3642 {
3643 if (SCM_UNBNDP (y))
3644 {
3645 if (SCM_UNBNDP (x))
3646 SCM_WTA_DISPATCH_0 (g_min, s_min);
3647 else if (SCM_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
3648 return x;
3649 else
3650 SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min);
3651 }
3652
3653 if (SCM_INUMP (x))
3654 {
3655 long xx = SCM_INUM (x);
3656 if (SCM_INUMP (y))
3657 {
3658 long yy = SCM_INUM (y);
3659 return (xx < yy) ? x : y;
3660 }
3661 else if (SCM_BIGP (y))
3662 {
3663 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
3664 scm_remember_upto_here_1 (y);
3665 return (sgn < 0) ? y : x;
3666 }
3667 else if (SCM_REALP (y))
3668 {
3669 double z = xx;
3670 /* if y==NaN then "<" is false and we return NaN */
3671 return (z < SCM_REAL_VALUE (y)) ? scm_make_real (z) : y;
3672 }
3673 else if (SCM_FRACTIONP (y))
3674 {
3675 use_less:
3676 return (SCM_FALSEP (scm_less_p (x, y)) ? y : x);
3677 }
3678 else
3679 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
3680 }
3681 else if (SCM_BIGP (x))
3682 {
3683 if (SCM_INUMP (y))
3684 {
3685 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3686 scm_remember_upto_here_1 (x);
3687 return (sgn < 0) ? x : y;
3688 }
3689 else if (SCM_BIGP (y))
3690 {
3691 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3692 scm_remember_upto_here_2 (x, y);
3693 return (cmp > 0) ? y : x;
3694 }
3695 else if (SCM_REALP (y))
3696 {
3697 /* if y==NaN then xx<yy is false, so we return the NaN y */
3698 double xx, yy;
3699 big_real:
3700 xx = scm_i_big2dbl (x);
3701 yy = SCM_REAL_VALUE (y);
3702 return (xx < yy ? scm_make_real (xx) : y);
3703 }
3704 else if (SCM_FRACTIONP (y))
3705 {
3706 goto use_less;
3707 }
3708 else
3709 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
3710 }
3711 else if (SCM_REALP (x))
3712 {
3713 if (SCM_INUMP (y))
3714 {
3715 double z = SCM_INUM (y);
3716 /* if x==NaN then "<" is false and we return NaN */
3717 return (z < SCM_REAL_VALUE (x)) ? scm_make_real (z) : x;
3718 }
3719 else if (SCM_BIGP (y))
3720 {
3721 SCM_SWAP (x, y);
3722 goto big_real;
3723 }
3724 else if (SCM_REALP (y))
3725 {
3726 /* if x==NaN then our explicit check means we return NaN
3727 if y==NaN then "<" is false and we return NaN
3728 calling isnan is unavoidable, since it's the only way to know
3729 which of x or y causes any compares to be false */
3730 double xx = SCM_REAL_VALUE (x);
3731 return (xisnan (xx) || xx < SCM_REAL_VALUE (y)) ? x : y;
3732 }
3733 else if (SCM_FRACTIONP (y))
3734 {
3735 double yy = scm_i_fraction2double (y);
3736 double xx = SCM_REAL_VALUE (x);
3737 return (yy < xx) ? scm_make_real (yy) : x;
3738 }
3739 else
3740 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
3741 }
3742 else if (SCM_FRACTIONP (x))
3743 {
3744 if (SCM_INUMP (y))
3745 {
3746 goto use_less;
3747 }
3748 else if (SCM_BIGP (y))
3749 {
3750 goto use_less;
3751 }
3752 else if (SCM_REALP (y))
3753 {
3754 double xx = scm_i_fraction2double (x);
3755 return (SCM_REAL_VALUE (y) < xx) ? y : scm_make_real (xx);
3756 }
3757 else if (SCM_FRACTIONP (y))
3758 {
3759 goto use_less;
3760 }
3761 else
3762 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3763 }
3764 else
3765 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min);
3766 }
3767
3768
3769 SCM_GPROC1 (s_sum, "+", scm_tc7_asubr, scm_sum, g_sum);
3770 /* "Return the sum of all parameter values. Return 0 if called without\n"
3771 * "any parameters."
3772 */
3773 SCM
3774 scm_sum (SCM x, SCM y)
3775 {
3776 if (SCM_UNBNDP (y))
3777 {
3778 if (SCM_NUMBERP (x)) return x;
3779 if (SCM_UNBNDP (x)) return SCM_INUM0;
3780 SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum);
3781 }
3782
3783 if (SCM_INUMP (x))
3784 {
3785 if (SCM_INUMP (y))
3786 {
3787 long xx = SCM_INUM (x);
3788 long yy = SCM_INUM (y);
3789 long int z = xx + yy;
3790 return SCM_FIXABLE (z) ? SCM_MAKINUM (z) : scm_i_long2big (z);
3791 }
3792 else if (SCM_BIGP (y))
3793 {
3794 SCM_SWAP (x, y);
3795 goto add_big_inum;
3796 }
3797 else if (SCM_REALP (y))
3798 {
3799 long int xx = SCM_INUM (x);
3800 return scm_make_real (xx + SCM_REAL_VALUE (y));
3801 }
3802 else if (SCM_COMPLEXP (y))
3803 {
3804 long int xx = SCM_INUM (x);
3805 return scm_make_complex (xx + SCM_COMPLEX_REAL (y),
3806 SCM_COMPLEX_IMAG (y));
3807 }
3808 else if (SCM_FRACTIONP (y))
3809 return scm_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
3810 scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
3811 SCM_FRACTION_DENOMINATOR (y));
3812 else
3813 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
3814 } else if (SCM_BIGP (x))
3815 {
3816 if (SCM_INUMP (y))
3817 {
3818 long int inum;
3819 int bigsgn;
3820 add_big_inum:
3821 inum = SCM_INUM (y);
3822 if (inum == 0)
3823 return x;
3824 bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3825 if (inum < 0)
3826 {
3827 SCM result = scm_i_mkbig ();
3828 mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum);
3829 scm_remember_upto_here_1 (x);
3830 /* we know the result will have to be a bignum */
3831 if (bigsgn == -1)
3832 return result;
3833 return scm_i_normbig (result);
3834 }
3835 else
3836 {
3837 SCM result = scm_i_mkbig ();
3838 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum);
3839 scm_remember_upto_here_1 (x);
3840 /* we know the result will have to be a bignum */
3841 if (bigsgn == 1)
3842 return result;
3843 return scm_i_normbig (result);
3844 }
3845 }
3846 else if (SCM_BIGP (y))
3847 {
3848 SCM result = scm_i_mkbig ();
3849 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
3850 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
3851 mpz_add (SCM_I_BIG_MPZ (result),
3852 SCM_I_BIG_MPZ (x),
3853 SCM_I_BIG_MPZ (y));
3854 scm_remember_upto_here_2 (x, y);
3855 /* we know the result will have to be a bignum */
3856 if (sgn_x == sgn_y)
3857 return result;
3858 return scm_i_normbig (result);
3859 }
3860 else if (SCM_REALP (y))
3861 {
3862 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y);
3863 scm_remember_upto_here_1 (x);
3864 return scm_make_real (result);
3865 }
3866 else if (SCM_COMPLEXP (y))
3867 {
3868 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
3869 + SCM_COMPLEX_REAL (y));
3870 scm_remember_upto_here_1 (x);
3871 return scm_make_complex (real_part, SCM_COMPLEX_IMAG (y));
3872 }
3873 else if (SCM_FRACTIONP (y))
3874 return scm_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
3875 scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
3876 SCM_FRACTION_DENOMINATOR (y));
3877 else
3878 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
3879 }
3880 else if (SCM_REALP (x))
3881 {
3882 if (SCM_INUMP (y))
3883 return scm_make_real (SCM_REAL_VALUE (x) + SCM_INUM (y));
3884 else if (SCM_BIGP (y))
3885 {
3886 double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x);
3887 scm_remember_upto_here_1 (y);
3888 return scm_make_real (result);
3889 }
3890 else if (SCM_REALP (y))
3891 return scm_make_real (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
3892 else if (SCM_COMPLEXP (y))
3893 return scm_make_complex (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
3894 SCM_COMPLEX_IMAG (y));
3895 else if (SCM_FRACTIONP (y))
3896 return scm_make_real (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
3897 else
3898 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
3899 }
3900 else if (SCM_COMPLEXP (x))
3901 {
3902 if (SCM_INUMP (y))
3903 return scm_make_complex (SCM_COMPLEX_REAL (x) + SCM_INUM (y),
3904 SCM_COMPLEX_IMAG (x));
3905 else if (SCM_BIGP (y))
3906 {
3907 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y))
3908 + SCM_COMPLEX_REAL (x));
3909 scm_remember_upto_here_1 (y);
3910 return scm_make_complex (real_part, SCM_COMPLEX_IMAG (x));
3911 }
3912 else if (SCM_REALP (y))
3913 return scm_make_complex (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
3914 SCM_COMPLEX_IMAG (x));
3915 else if (SCM_COMPLEXP (y))
3916 return scm_make_complex (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
3917 SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
3918 else if (SCM_FRACTIONP (y))
3919 return scm_make_complex (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
3920 SCM_COMPLEX_IMAG (x));
3921 else
3922 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
3923 }
3924 else if (SCM_FRACTIONP (x))
3925 {
3926 if (SCM_INUMP (y))
3927 return scm_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
3928 scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
3929 SCM_FRACTION_DENOMINATOR (x));
3930 else if (SCM_BIGP (y))
3931 return scm_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
3932 scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
3933 SCM_FRACTION_DENOMINATOR (x));
3934 else if (SCM_REALP (y))
3935 return scm_make_real (SCM_REAL_VALUE (y) + scm_i_fraction2double (x));
3936 else if (SCM_COMPLEXP (y))
3937 return scm_make_complex (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x),
3938 SCM_COMPLEX_IMAG (y));
3939 else if (SCM_FRACTIONP (y))
3940 /* a/b + c/d = (ad + bc) / bd */
3941 return scm_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
3942 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
3943 scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
3944 else
3945 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
3946 }
3947 else
3948 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum);
3949 }
3950
3951
3952 SCM_GPROC1 (s_difference, "-", scm_tc7_asubr, scm_difference, g_difference);
3953 /* If called with one argument @var{z1}, -@var{z1} returned. Otherwise
3954 * the sum of all but the first argument are subtracted from the first
3955 * argument. */
3956 #define FUNC_NAME s_difference
3957 SCM
3958 scm_difference (SCM x, SCM y)
3959 {
3960 if (SCM_UNBNDP (y))
3961 {
3962 if (SCM_UNBNDP (x))
3963 SCM_WTA_DISPATCH_0 (g_difference, s_difference);
3964 else
3965 if (SCM_INUMP (x))
3966 {
3967 long xx = -SCM_INUM (x);
3968 if (SCM_FIXABLE (xx))
3969 return SCM_MAKINUM (xx);
3970 else
3971 return scm_i_long2big (xx);
3972 }
3973 else if (SCM_BIGP (x))
3974 /* FIXME: do we really need to normalize here? */
3975 return scm_i_normbig (scm_i_clonebig (x, 0));
3976 else if (SCM_REALP (x))
3977 return scm_make_real (-SCM_REAL_VALUE (x));
3978 else if (SCM_COMPLEXP (x))
3979 return scm_make_complex (-SCM_COMPLEX_REAL (x),
3980 -SCM_COMPLEX_IMAG (x));
3981 else if (SCM_FRACTIONP (x))
3982 return scm_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
3983 SCM_FRACTION_DENOMINATOR (x));
3984 else
3985 SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference);
3986 }
3987
3988 if (SCM_INUMP (x))
3989 {
3990 if (SCM_INUMP (y))
3991 {
3992 long int xx = SCM_INUM (x);
3993 long int yy = SCM_INUM (y);
3994 long int z = xx - yy;
3995 if (SCM_FIXABLE (z))
3996 return SCM_MAKINUM (z);
3997 else
3998 return scm_i_long2big (z);
3999 }
4000 else if (SCM_BIGP (y))
4001 {
4002 /* inum-x - big-y */
4003 long xx = SCM_INUM (x);
4004
4005 if (xx == 0)
4006 return scm_i_clonebig (y, 0);
4007 else
4008 {
4009 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
4010 SCM result = scm_i_mkbig ();
4011
4012 if (xx >= 0)
4013 mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y));
4014 else
4015 {
4016 /* x - y == -(y + -x) */
4017 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx);
4018 mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
4019 }
4020 scm_remember_upto_here_1 (y);
4021
4022 if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0))
4023 /* we know the result will have to be a bignum */
4024 return result;
4025 else
4026 return scm_i_normbig (result);
4027 }
4028 }
4029 else if (SCM_REALP (y))
4030 {
4031 long int xx = SCM_INUM (x);
4032 return scm_make_real (xx - SCM_REAL_VALUE (y));
4033 }
4034 else if (SCM_COMPLEXP (y))
4035 {
4036 long int xx = SCM_INUM (x);
4037 return scm_make_complex (xx - SCM_COMPLEX_REAL (y),
4038 - SCM_COMPLEX_IMAG (y));
4039 }
4040 else if (SCM_FRACTIONP (y))
4041 /* a - b/c = (ac - b) / c */
4042 return scm_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4043 SCM_FRACTION_NUMERATOR (y)),
4044 SCM_FRACTION_DENOMINATOR (y));
4045 else
4046 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4047 }
4048 else if (SCM_BIGP (x))
4049 {
4050 if (SCM_INUMP (y))
4051 {
4052 /* big-x - inum-y */
4053 long yy = SCM_INUM (y);
4054 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
4055
4056 scm_remember_upto_here_1 (x);
4057 if (sgn_x == 0)
4058 return SCM_FIXABLE (-yy) ? SCM_MAKINUM (-yy) : scm_long2num (-yy);
4059 else
4060 {
4061 SCM result = scm_i_mkbig ();
4062
4063 if (yy >= 0)
4064 mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
4065 else
4066 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy);
4067 scm_remember_upto_here_1 (x);
4068
4069 if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0))
4070 /* we know the result will have to be a bignum */
4071 return result;
4072 else
4073 return scm_i_normbig (result);
4074 }
4075 }
4076 else if (SCM_BIGP (y))
4077 {
4078 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
4079 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
4080 SCM result = scm_i_mkbig ();
4081 mpz_sub (SCM_I_BIG_MPZ (result),
4082 SCM_I_BIG_MPZ (x),
4083 SCM_I_BIG_MPZ (y));
4084 scm_remember_upto_here_2 (x, y);
4085 /* we know the result will have to be a bignum */
4086 if ((sgn_x == 1) && (sgn_y == -1))
4087 return result;
4088 if ((sgn_x == -1) && (sgn_y == 1))
4089 return result;
4090 return scm_i_normbig (result);
4091 }
4092 else if (SCM_REALP (y))
4093 {
4094 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y);
4095 scm_remember_upto_here_1 (x);
4096 return scm_make_real (result);
4097 }
4098 else if (SCM_COMPLEXP (y))
4099 {
4100 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
4101 - SCM_COMPLEX_REAL (y));
4102 scm_remember_upto_here_1 (x);
4103 return scm_make_complex (real_part, - SCM_COMPLEX_IMAG (y));
4104 }
4105 else if (SCM_FRACTIONP (y))
4106 return scm_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4107 SCM_FRACTION_NUMERATOR (y)),
4108 SCM_FRACTION_DENOMINATOR (y));
4109 else SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4110 }
4111 else if (SCM_REALP (x))
4112 {
4113 if (SCM_INUMP (y))
4114 return scm_make_real (SCM_REAL_VALUE (x) - SCM_INUM (y));
4115 else if (SCM_BIGP (y))
4116 {
4117 double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y));
4118 scm_remember_upto_here_1 (x);
4119 return scm_make_real (result);
4120 }
4121 else if (SCM_REALP (y))
4122 return scm_make_real (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
4123 else if (SCM_COMPLEXP (y))
4124 return scm_make_complex (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
4125 -SCM_COMPLEX_IMAG (y));
4126 else if (SCM_FRACTIONP (y))
4127 return scm_make_real (SCM_REAL_VALUE (x) - scm_i_fraction2double (y));
4128 else
4129 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4130 }
4131 else if (SCM_COMPLEXP (x))
4132 {
4133 if (SCM_INUMP (y))
4134 return scm_make_complex (SCM_COMPLEX_REAL (x) - SCM_INUM (y),
4135 SCM_COMPLEX_IMAG (x));
4136 else if (SCM_BIGP (y))
4137 {
4138 double real_part = (SCM_COMPLEX_REAL (x)
4139 - mpz_get_d (SCM_I_BIG_MPZ (y)));
4140 scm_remember_upto_here_1 (x);
4141 return scm_make_complex (real_part, SCM_COMPLEX_IMAG (y));
4142 }
4143 else if (SCM_REALP (y))
4144 return scm_make_complex (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
4145 SCM_COMPLEX_IMAG (x));
4146 else if (SCM_COMPLEXP (y))
4147 return scm_make_complex (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
4148 SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
4149 else if (SCM_FRACTIONP (y))
4150 return scm_make_complex (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y),
4151 SCM_COMPLEX_IMAG (x));
4152 else
4153 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4154 }
4155 else if (SCM_FRACTIONP (x))
4156 {
4157 if (SCM_INUMP (y))
4158 /* a/b - c = (a - cb) / b */
4159 return scm_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
4160 scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
4161 SCM_FRACTION_DENOMINATOR (x));
4162 else if (SCM_BIGP (y))
4163 return scm_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
4164 scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
4165 SCM_FRACTION_DENOMINATOR (x));
4166 else if (SCM_REALP (y))
4167 return scm_make_real (scm_i_fraction2double (x) - SCM_REAL_VALUE (y));
4168 else if (SCM_COMPLEXP (y))
4169 return scm_make_complex (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y),
4170 -SCM_COMPLEX_IMAG (y));
4171 else if (SCM_FRACTIONP (y))
4172 /* a/b - c/d = (ad - bc) / bd */
4173 return scm_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
4174 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
4175 scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
4176 else
4177 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4178 }
4179 else
4180 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference);
4181 }
4182 #undef FUNC_NAME
4183
4184
4185 SCM_GPROC1 (s_product, "*", scm_tc7_asubr, scm_product, g_product);
4186 /* "Return the product of all arguments. If called without arguments,\n"
4187 * "1 is returned."
4188 */
4189 SCM
4190 scm_product (SCM x, SCM y)
4191 {
4192 if (SCM_UNBNDP (y))
4193 {
4194 if (SCM_UNBNDP (x))
4195 return SCM_MAKINUM (1L);
4196 else if (SCM_NUMBERP (x))
4197 return x;
4198 else
4199 SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product);
4200 }
4201
4202 if (SCM_INUMP (x))
4203 {
4204 long xx;
4205
4206 intbig:
4207 xx = SCM_INUM (x);
4208
4209 switch (xx)
4210 {
4211 case 0: return x; break;
4212 case 1: return y; break;
4213 }
4214
4215 if (SCM_INUMP (y))
4216 {
4217 long yy = SCM_INUM (y);
4218 long kk = xx * yy;
4219 SCM k = SCM_MAKINUM (kk);
4220 if ((kk == SCM_INUM (k)) && (kk / xx == yy))
4221 return k;
4222 else
4223 {
4224 SCM result = scm_i_long2big (xx);
4225 mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy);
4226 return scm_i_normbig (result);
4227 }
4228 }
4229 else if (SCM_BIGP (y))
4230 {
4231 SCM result = scm_i_mkbig ();
4232 mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx);
4233 scm_remember_upto_here_1 (y);
4234 return result;
4235 }
4236 else if (SCM_REALP (y))
4237 return scm_make_real (xx * SCM_REAL_VALUE (y));
4238 else if (SCM_COMPLEXP (y))
4239 return scm_make_complex (xx * SCM_COMPLEX_REAL (y),
4240 xx * SCM_COMPLEX_IMAG (y));
4241 else if (SCM_FRACTIONP (y))
4242 return scm_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
4243 SCM_FRACTION_DENOMINATOR (y));
4244 else
4245 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4246 }
4247 else if (SCM_BIGP (x))
4248 {
4249 if (SCM_INUMP (y))
4250 {
4251 SCM_SWAP (x, y);
4252 goto intbig;
4253 }
4254 else if (SCM_BIGP (y))
4255 {
4256 SCM result = scm_i_mkbig ();
4257 mpz_mul (SCM_I_BIG_MPZ (result),
4258 SCM_I_BIG_MPZ (x),
4259 SCM_I_BIG_MPZ (y));
4260 scm_remember_upto_here_2 (x, y);
4261 return result;
4262 }
4263 else if (SCM_REALP (y))
4264 {
4265 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y);
4266 scm_remember_upto_here_1 (x);
4267 return scm_make_real (result);
4268 }
4269 else if (SCM_COMPLEXP (y))
4270 {
4271 double z = mpz_get_d (SCM_I_BIG_MPZ (x));
4272 scm_remember_upto_here_1 (x);
4273 return scm_make_complex (z * SCM_COMPLEX_REAL (y),
4274 z * SCM_COMPLEX_IMAG (y));
4275 }
4276 else if (SCM_FRACTIONP (y))
4277 return scm_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
4278 SCM_FRACTION_DENOMINATOR (y));
4279 else
4280 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4281 }
4282 else if (SCM_REALP (x))
4283 {
4284 if (SCM_INUMP (y))
4285 return scm_make_real (SCM_INUM (y) * SCM_REAL_VALUE (x));
4286 else if (SCM_BIGP (y))
4287 {
4288 double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x);
4289 scm_remember_upto_here_1 (y);
4290 return scm_make_real (result);
4291 }
4292 else if (SCM_REALP (y))
4293 return scm_make_real (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
4294 else if (SCM_COMPLEXP (y))
4295 return scm_make_complex (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
4296 SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
4297 else if (SCM_FRACTIONP (y))
4298 return scm_make_real (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
4299 else
4300 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4301 }
4302 else if (SCM_COMPLEXP (x))
4303 {
4304 if (SCM_INUMP (y))
4305 return scm_make_complex (SCM_INUM (y) * SCM_COMPLEX_REAL (x),
4306 SCM_INUM (y) * SCM_COMPLEX_IMAG (x));
4307 else if (SCM_BIGP (y))
4308 {
4309 double z = mpz_get_d (SCM_I_BIG_MPZ (y));
4310 scm_remember_upto_here_1 (y);
4311 return scm_make_complex (z * SCM_COMPLEX_REAL (x),
4312 z * SCM_COMPLEX_IMAG (x));
4313 }
4314 else if (SCM_REALP (y))
4315 return scm_make_complex (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
4316 SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
4317 else if (SCM_COMPLEXP (y))
4318 {
4319 return scm_make_complex (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
4320 - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
4321 SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
4322 + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
4323 }
4324 else if (SCM_FRACTIONP (y))
4325 {
4326 double yy = scm_i_fraction2double (y);
4327 return scm_make_complex (yy * SCM_COMPLEX_REAL (x),
4328 yy * SCM_COMPLEX_IMAG (x));
4329 }
4330 else
4331 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4332 }
4333 else if (SCM_FRACTIONP (x))
4334 {
4335 if (SCM_INUMP (y))
4336 return scm_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
4337 SCM_FRACTION_DENOMINATOR (x));
4338 else if (SCM_BIGP (y))
4339 return scm_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
4340 SCM_FRACTION_DENOMINATOR (x));
4341 else if (SCM_REALP (y))
4342 return scm_make_real (scm_i_fraction2double (x) * SCM_REAL_VALUE (y));
4343 else if (SCM_COMPLEXP (y))
4344 {
4345 double xx = scm_i_fraction2double (x);
4346 return scm_make_complex (xx * SCM_COMPLEX_REAL (y),
4347 xx * SCM_COMPLEX_IMAG (y));
4348 }
4349 else if (SCM_FRACTIONP (y))
4350 /* a/b * c/d = ac / bd */
4351 return scm_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
4352 SCM_FRACTION_NUMERATOR (y)),
4353 scm_product (SCM_FRACTION_DENOMINATOR (x),
4354 SCM_FRACTION_DENOMINATOR (y)));
4355 else
4356 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4357 }
4358 else
4359 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product);
4360 }
4361
4362 double
4363 scm_num2dbl (SCM a, const char *why)
4364 #define FUNC_NAME why
4365 {
4366 if (SCM_INUMP (a))
4367 return (double) SCM_INUM (a);
4368 else if (SCM_BIGP (a))
4369 {
4370 double result = mpz_get_d (SCM_I_BIG_MPZ (a));
4371 scm_remember_upto_here_1 (a);
4372 return result;
4373 }
4374 else if (SCM_REALP (a))
4375 return (SCM_REAL_VALUE (a));
4376 else if (SCM_FRACTIONP (a))
4377 return scm_i_fraction2double (a);
4378 else
4379 SCM_WRONG_TYPE_ARG (SCM_ARGn, a);
4380 }
4381 #undef FUNC_NAME
4382
4383 #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \
4384 || (defined (HAVE_FINITE) && defined (HAVE_ISNAN)))
4385 #define ALLOW_DIVIDE_BY_ZERO
4386 /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */
4387 #endif
4388
4389 /* The code below for complex division is adapted from the GNU
4390 libstdc++, which adapted it from f2c's libF77, and is subject to
4391 this copyright: */
4392
4393 /****************************************************************
4394 Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
4395
4396 Permission to use, copy, modify, and distribute this software
4397 and its documentation for any purpose and without fee is hereby
4398 granted, provided that the above copyright notice appear in all
4399 copies and that both that the copyright notice and this
4400 permission notice and warranty disclaimer appear in supporting
4401 documentation, and that the names of AT&T Bell Laboratories or
4402 Bellcore or any of their entities not be used in advertising or
4403 publicity pertaining to distribution of the software without
4404 specific, written prior permission.
4405
4406 AT&T and Bellcore disclaim all warranties with regard to this
4407 software, including all implied warranties of merchantability
4408 and fitness. In no event shall AT&T or Bellcore be liable for
4409 any special, indirect or consequential damages or any damages
4410 whatsoever resulting from loss of use, data or profits, whether
4411 in an action of contract, negligence or other tortious action,
4412 arising out of or in connection with the use or performance of
4413 this software.
4414 ****************************************************************/
4415
4416 SCM_GPROC1 (s_divide, "/", scm_tc7_asubr, scm_divide, g_divide);
4417 /* Divide the first argument by the product of the remaining
4418 arguments. If called with one argument @var{z1}, 1/@var{z1} is
4419 returned. */
4420 #define FUNC_NAME s_divide
4421 static SCM
4422 scm_i_divide (SCM x, SCM y, int inexact)
4423 {
4424 double a;
4425
4426 if (SCM_UNBNDP (y))
4427 {
4428 if (SCM_UNBNDP (x))
4429 SCM_WTA_DISPATCH_0 (g_divide, s_divide);
4430 else if (SCM_INUMP (x))
4431 {
4432 long xx = SCM_INUM (x);
4433 if (xx == 1 || xx == -1)
4434 return x;
4435 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4436 else if (xx == 0)
4437 scm_num_overflow (s_divide);
4438 #endif
4439 else
4440 {
4441 if (inexact)
4442 return scm_make_real (1.0 / (double) xx);
4443 else return scm_make_ratio (SCM_MAKINUM(1), x);
4444 }
4445 }
4446 else if (SCM_BIGP (x))
4447 {
4448 if (inexact)
4449 return scm_make_real (1.0 / scm_i_big2dbl (x));
4450 else return scm_make_ratio (SCM_MAKINUM(1), x);
4451 }
4452 else if (SCM_REALP (x))
4453 {
4454 double xx = SCM_REAL_VALUE (x);
4455 #ifndef ALLOW_DIVIDE_BY_ZERO
4456 if (xx == 0.0)
4457 scm_num_overflow (s_divide);
4458 else
4459 #endif
4460 return scm_make_real (1.0 / xx);
4461 }
4462 else if (SCM_COMPLEXP (x))
4463 {
4464 double r = SCM_COMPLEX_REAL (x);
4465 double i = SCM_COMPLEX_IMAG (x);
4466 if (r <= i)
4467 {
4468 double t = r / i;
4469 double d = i * (1.0 + t * t);
4470 return scm_make_complex (t / d, -1.0 / d);
4471 }
4472 else
4473 {
4474 double t = i / r;
4475 double d = r * (1.0 + t * t);
4476 return scm_make_complex (1.0 / d, -t / d);
4477 }
4478 }
4479 else if (SCM_FRACTIONP (x))
4480 return scm_make_ratio (SCM_FRACTION_DENOMINATOR (x),
4481 SCM_FRACTION_NUMERATOR (x));
4482 else
4483 SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide);
4484 }
4485
4486 if (SCM_INUMP (x))
4487 {
4488 long xx = SCM_INUM (x);
4489 if (SCM_INUMP (y))
4490 {
4491 long yy = SCM_INUM (y);
4492 if (yy == 0)
4493 {
4494 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4495 scm_num_overflow (s_divide);
4496 #else
4497 return scm_make_real ((double) xx / (double) yy);
4498 #endif
4499 }
4500 else if (xx % yy != 0)
4501 {
4502 if (inexact)
4503 return scm_make_real ((double) xx / (double) yy);
4504 else return scm_make_ratio (x, y);
4505 }
4506 else
4507 {
4508 long z = xx / yy;
4509 if (SCM_FIXABLE (z))
4510 return SCM_MAKINUM (z);
4511 else
4512 return scm_i_long2big (z);
4513 }
4514 }
4515 else if (SCM_BIGP (y))
4516 {
4517 if (inexact)
4518 return scm_make_real ((double) xx / scm_i_big2dbl (y));
4519 else return scm_make_ratio (x, y);
4520 }
4521 else if (SCM_REALP (y))
4522 {
4523 double yy = SCM_REAL_VALUE (y);
4524 #ifndef ALLOW_DIVIDE_BY_ZERO
4525 if (yy == 0.0)
4526 scm_num_overflow (s_divide);
4527 else
4528 #endif
4529 return scm_make_real ((double) xx / yy);
4530 }
4531 else if (SCM_COMPLEXP (y))
4532 {
4533 a = xx;
4534 complex_div: /* y _must_ be a complex number */
4535 {
4536 double r = SCM_COMPLEX_REAL (y);
4537 double i = SCM_COMPLEX_IMAG (y);
4538 if (r <= i)
4539 {
4540 double t = r / i;
4541 double d = i * (1.0 + t * t);
4542 return scm_make_complex ((a * t) / d, -a / d);
4543 }
4544 else
4545 {
4546 double t = i / r;
4547 double d = r * (1.0 + t * t);
4548 return scm_make_complex (a / d, -(a * t) / d);
4549 }
4550 }
4551 }
4552 else if (SCM_FRACTIONP (y))
4553 /* a / b/c = ac / b */
4554 return scm_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4555 SCM_FRACTION_NUMERATOR (y));
4556 else
4557 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4558 }
4559 else if (SCM_BIGP (x))
4560 {
4561 if (SCM_INUMP (y))
4562 {
4563 long int yy = SCM_INUM (y);
4564 if (yy == 0)
4565 {
4566 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4567 scm_num_overflow (s_divide);
4568 #else
4569 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
4570 scm_remember_upto_here_1 (x);
4571 return (sgn == 0) ? scm_nan () : scm_inf ();
4572 #endif
4573 }
4574 else if (yy == 1)
4575 return x;
4576 else
4577 {
4578 /* FIXME: HMM, what are the relative performance issues here?
4579 We need to test. Is it faster on average to test
4580 divisible_p, then perform whichever operation, or is it
4581 faster to perform the integer div opportunistically and
4582 switch to real if there's a remainder? For now we take the
4583 middle ground: test, then if divisible, use the faster div
4584 func. */
4585
4586 long abs_yy = yy < 0 ? -yy : yy;
4587 int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy);
4588
4589 if (divisible_p)
4590 {
4591 SCM result = scm_i_mkbig ();
4592 mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy);
4593 scm_remember_upto_here_1 (x);
4594 if (yy < 0)
4595 mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
4596 return scm_i_normbig (result);
4597 }
4598 else
4599 {
4600 if (inexact)
4601 return scm_make_real (scm_i_big2dbl (x) / (double) yy);
4602 else return scm_make_ratio (x, y);
4603 }
4604 }
4605 }
4606 else if (SCM_BIGP (y))
4607 {
4608 int y_is_zero = (mpz_sgn (SCM_I_BIG_MPZ (y)) == 0);
4609 if (y_is_zero)
4610 {
4611 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4612 scm_num_overflow (s_divide);
4613 #else
4614 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
4615 scm_remember_upto_here_1 (x);
4616 return (sgn == 0) ? scm_nan () : scm_inf ();
4617 #endif
4618 }
4619 else
4620 {
4621 /* big_x / big_y */
4622 int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x),
4623 SCM_I_BIG_MPZ (y));
4624 if (divisible_p)
4625 {
4626 SCM result = scm_i_mkbig ();
4627 mpz_divexact (SCM_I_BIG_MPZ (result),
4628 SCM_I_BIG_MPZ (x),
4629 SCM_I_BIG_MPZ (y));
4630 scm_remember_upto_here_2 (x, y);
4631 return scm_i_normbig (result);
4632 }
4633 else
4634 {
4635 if (inexact)
4636 {
4637 double dbx = mpz_get_d (SCM_I_BIG_MPZ (x));
4638 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
4639 scm_remember_upto_here_2 (x, y);
4640 return scm_make_real (dbx / dby);
4641 }
4642 else return scm_make_ratio (x, y);
4643 }
4644 }
4645 }
4646 else if (SCM_REALP (y))
4647 {
4648 double yy = SCM_REAL_VALUE (y);
4649 #ifndef ALLOW_DIVIDE_BY_ZERO
4650 if (yy == 0.0)
4651 scm_num_overflow (s_divide);
4652 else
4653 #endif
4654 return scm_make_real (scm_i_big2dbl (x) / yy);
4655 }
4656 else if (SCM_COMPLEXP (y))
4657 {
4658 a = scm_i_big2dbl (x);
4659 goto complex_div;
4660 }
4661 else if (SCM_FRACTIONP (y))
4662 return scm_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4663 SCM_FRACTION_NUMERATOR (y));
4664 else
4665 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4666 }
4667 else if (SCM_REALP (x))
4668 {
4669 double rx = SCM_REAL_VALUE (x);
4670 if (SCM_INUMP (y))
4671 {
4672 long int yy = SCM_INUM (y);
4673 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4674 if (yy == 0)
4675 scm_num_overflow (s_divide);
4676 else
4677 #endif
4678 return scm_make_real (rx / (double) yy);
4679 }
4680 else if (SCM_BIGP (y))
4681 {
4682 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
4683 scm_remember_upto_here_1 (y);
4684 return scm_make_real (rx / dby);
4685 }
4686 else if (SCM_REALP (y))
4687 {
4688 double yy = SCM_REAL_VALUE (y);
4689 #ifndef ALLOW_DIVIDE_BY_ZERO
4690 if (yy == 0.0)
4691 scm_num_overflow (s_divide);
4692 else
4693 #endif
4694 return scm_make_real (rx / yy);
4695 }
4696 else if (SCM_COMPLEXP (y))
4697 {
4698 a = rx;
4699 goto complex_div;
4700 }
4701 else if (SCM_FRACTIONP (y))
4702 return scm_make_real (rx / scm_i_fraction2double (y));
4703 else
4704 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4705 }
4706 else if (SCM_COMPLEXP (x))
4707 {
4708 double rx = SCM_COMPLEX_REAL (x);
4709 double ix = SCM_COMPLEX_IMAG (x);
4710 if (SCM_INUMP (y))
4711 {
4712 long int yy = SCM_INUM (y);
4713 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4714 if (yy == 0)
4715 scm_num_overflow (s_divide);
4716 else
4717 #endif
4718 {
4719 double d = yy;
4720 return scm_make_complex (rx / d, ix / d);
4721 }
4722 }
4723 else if (SCM_BIGP (y))
4724 {
4725 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
4726 scm_remember_upto_here_1 (y);
4727 return scm_make_complex (rx / dby, ix / dby);
4728 }
4729 else if (SCM_REALP (y))
4730 {
4731 double yy = SCM_REAL_VALUE (y);
4732 #ifndef ALLOW_DIVIDE_BY_ZERO
4733 if (yy == 0.0)
4734 scm_num_overflow (s_divide);
4735 else
4736 #endif
4737 return scm_make_complex (rx / yy, ix / yy);
4738 }
4739 else if (SCM_COMPLEXP (y))
4740 {
4741 double ry = SCM_COMPLEX_REAL (y);
4742 double iy = SCM_COMPLEX_IMAG (y);
4743 if (ry <= iy)
4744 {
4745 double t = ry / iy;
4746 double d = iy * (1.0 + t * t);
4747 return scm_make_complex ((rx * t + ix) / d, (ix * t - rx) / d);
4748 }
4749 else
4750 {
4751 double t = iy / ry;
4752 double d = ry * (1.0 + t * t);
4753 return scm_make_complex ((rx + ix * t) / d, (ix - rx * t) / d);
4754 }
4755 }
4756 else if (SCM_FRACTIONP (y))
4757 {
4758 double yy = scm_i_fraction2double (y);
4759 return scm_make_complex (rx / yy, ix / yy);
4760 }
4761 else
4762 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4763 }
4764 else if (SCM_FRACTIONP (x))
4765 {
4766 if (SCM_INUMP (y))
4767 {
4768 long int yy = SCM_INUM (y);
4769 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4770 if (yy == 0)
4771 scm_num_overflow (s_divide);
4772 else
4773 #endif
4774 return scm_make_ratio (SCM_FRACTION_NUMERATOR (x),
4775 scm_product (SCM_FRACTION_DENOMINATOR (x), y));
4776 }
4777 else if (SCM_BIGP (y))
4778 {
4779 return scm_make_ratio (SCM_FRACTION_NUMERATOR (x),
4780 scm_product (SCM_FRACTION_DENOMINATOR (x), y));
4781 }
4782 else if (SCM_REALP (y))
4783 {
4784 double yy = SCM_REAL_VALUE (y);
4785 #ifndef ALLOW_DIVIDE_BY_ZERO
4786 if (yy == 0.0)
4787 scm_num_overflow (s_divide);
4788 else
4789 #endif
4790 return scm_make_real (scm_i_fraction2double (x) / yy);
4791 }
4792 else if (SCM_COMPLEXP (y))
4793 {
4794 a = scm_i_fraction2double (x);
4795 goto complex_div;
4796 }
4797 else if (SCM_FRACTIONP (y))
4798 return scm_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
4799 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x)));
4800 else
4801 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4802 }
4803 else
4804 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide);
4805 }
4806
4807 SCM
4808 scm_divide (SCM x, SCM y)
4809 {
4810 return scm_i_divide (x, y, 0);
4811 }
4812
4813 static SCM scm_divide2real (SCM x, SCM y)
4814 {
4815 return scm_i_divide (x, y, 1);
4816 }
4817 #undef FUNC_NAME
4818
4819
4820 double
4821 scm_asinh (double x)
4822 {
4823 #if HAVE_ASINH
4824 return asinh (x);
4825 #else
4826 #define asinh scm_asinh
4827 return log (x + sqrt (x * x + 1));
4828 #endif
4829 }
4830 SCM_GPROC1 (s_asinh, "$asinh", scm_tc7_dsubr, (SCM (*)()) asinh, g_asinh);
4831 /* "Return the inverse hyperbolic sine of @var{x}."
4832 */
4833
4834
4835 double
4836 scm_acosh (double x)
4837 {
4838 #if HAVE_ACOSH
4839 return acosh (x);
4840 #else
4841 #define acosh scm_acosh
4842 return log (x + sqrt (x * x - 1));
4843 #endif
4844 }
4845 SCM_GPROC1 (s_acosh, "$acosh", scm_tc7_dsubr, (SCM (*)()) acosh, g_acosh);
4846 /* "Return the inverse hyperbolic cosine of @var{x}."
4847 */
4848
4849
4850 double
4851 scm_atanh (double x)
4852 {
4853 #if HAVE_ATANH
4854 return atanh (x);
4855 #else
4856 #define atanh scm_atanh
4857 return 0.5 * log ((1 + x) / (1 - x));
4858 #endif
4859 }
4860 SCM_GPROC1 (s_atanh, "$atanh", scm_tc7_dsubr, (SCM (*)()) atanh, g_atanh);
4861 /* "Return the inverse hyperbolic tangent of @var{x}."
4862 */
4863
4864
4865 /* XXX - eventually, we should remove this definition of scm_round and
4866 rename scm_round_number to scm_round. Likewise for scm_truncate
4867 and scm_truncate_number.
4868 */
4869
4870 double
4871 scm_truncate (double x)
4872 {
4873 #if HAVE_TRUNC
4874 return trunc (x);
4875 #else
4876 #define trunc scm_truncate
4877 if (x < 0.0)
4878 return -floor (-x);
4879 return floor (x);
4880 #endif
4881 }
4882
4883 /* scm_round is done using floor(x+0.5) to round to nearest and with
4884 half-way case (ie. when x is an integer plus 0.5) going upwards. Then
4885 half-way cases are identified and adjusted down if the round-upwards
4886 didn't give the desired even integer.
4887
4888 "plus_half == result" identifies a half-way case. If plus_half, which is
4889 x + 0.5, is an integer then x must be an integer plus 0.5.
4890
4891 An odd "result" value is identified with result/2 != floor(result/2).
4892 This is done with plus_half, since that value is ready for use sooner in
4893 a pipelined cpu, and we're already requiring plus_half == result.
4894
4895 Note however that we need to be careful when x is big and already an
4896 integer. In that case "x+0.5" may round to an adjacent integer, causing
4897 us to return such a value, incorrectly. For instance if the hardware is
4898 in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
4899 (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
4900 returned. Or if the hardware is in round-upwards mode, then other bigger
4901 values like say x == 2^128 will see x+0.5 rounding up to the next higher
4902 representable value, 2^128+2^76 (or whatever), again incorrect.
4903
4904 These bad roundings of x+0.5 are avoided by testing at the start whether
4905 x is already an integer. If it is then clearly that's the desired result
4906 already. And if it's not then the exponent must be small enough to allow
4907 an 0.5 to be represented, and hence added without a bad rounding. */
4908
4909 double
4910 scm_round (double x)
4911 {
4912 double plus_half, result;
4913
4914 if (x == floor (x))
4915 return x;
4916
4917 plus_half = x + 0.5;
4918 result = floor (plus_half);
4919 /* Adjust so that the scm_round is towards even. */
4920 return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
4921 ? result - 1
4922 : result);
4923 }
4924
4925 SCM_DEFINE (scm_truncate_number, "truncate", 1, 0, 0,
4926 (SCM x),
4927 "Round the number @var{x} towards zero.")
4928 #define FUNC_NAME s_scm_truncate_number
4929 {
4930 if (SCM_FALSEP (scm_negative_p (x)))
4931 return scm_floor (x);
4932 else
4933 return scm_ceiling (x);
4934 }
4935 #undef FUNC_NAME
4936
4937 static SCM exactly_one_half;
4938
4939 SCM_DEFINE (scm_round_number, "round", 1, 0, 0,
4940 (SCM x),
4941 "Round the number @var{x} towards the nearest integer. "
4942 "When it is exactly halfway between two integers, "
4943 "round towards the even one.")
4944 #define FUNC_NAME s_scm_round_number
4945 {
4946 SCM plus_half = scm_sum (x, exactly_one_half);
4947 SCM result = scm_floor (plus_half);
4948 /* Adjust so that the scm_round is towards even. */
4949 if (!SCM_FALSEP (scm_num_eq_p (plus_half, result))
4950 && !SCM_FALSEP (scm_odd_p (result)))
4951 return scm_difference (result, SCM_MAKINUM (1));
4952 else
4953 return result;
4954 }
4955 #undef FUNC_NAME
4956
4957 SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
4958 (SCM x),
4959 "Round the number @var{x} towards minus infinity.")
4960 #define FUNC_NAME s_scm_floor
4961 {
4962 if (SCM_INUMP (x) || SCM_BIGP (x))
4963 return x;
4964 else if (SCM_REALP (x))
4965 return scm_make_real (floor (SCM_REAL_VALUE (x)));
4966 else if (SCM_FRACTIONP (x))
4967 {
4968 SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
4969 SCM_FRACTION_DENOMINATOR (x));
4970 if (SCM_FALSEP (scm_negative_p (x)))
4971 {
4972 /* For positive x, rounding towards zero is correct. */
4973 return q;
4974 }
4975 else
4976 {
4977 /* For negative x, we need to return q-1 unless x is an
4978 integer. But fractions are never integer, per our
4979 assumptions. */
4980 return scm_difference (q, SCM_MAKINUM (1));
4981 }
4982 }
4983 else
4984 SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor);
4985 }
4986 #undef FUNC_NAME
4987
4988 SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
4989 (SCM x),
4990 "Round the number @var{x} towards infinity.")
4991 #define FUNC_NAME s_scm_ceiling
4992 {
4993 if (SCM_INUMP (x) || SCM_BIGP (x))
4994 return x;
4995 else if (SCM_REALP (x))
4996 return scm_make_real (ceil (SCM_REAL_VALUE (x)));
4997 else if (SCM_FRACTIONP (x))
4998 {
4999 SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
5000 SCM_FRACTION_DENOMINATOR (x));
5001 if (SCM_FALSEP (scm_positive_p (x)))
5002 {
5003 /* For negative x, rounding towards zero is correct. */
5004 return q;
5005 }
5006 else
5007 {
5008 /* For positive x, we need to return q+1 unless x is an
5009 integer. But fractions are never integer, per our
5010 assumptions. */
5011 return scm_sum (q, SCM_MAKINUM (1));
5012 }
5013 }
5014 else
5015 SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
5016 }
5017 #undef FUNC_NAME
5018
5019 SCM_GPROC1 (s_i_sqrt, "$sqrt", scm_tc7_dsubr, (SCM (*)()) sqrt, g_i_sqrt);
5020 /* "Return the square root of the real number @var{x}."
5021 */
5022 SCM_GPROC1 (s_i_abs, "$abs", scm_tc7_dsubr, (SCM (*)()) fabs, g_i_abs);
5023 /* "Return the absolute value of the real number @var{x}."
5024 */
5025 SCM_GPROC1 (s_i_exp, "$exp", scm_tc7_dsubr, (SCM (*)()) exp, g_i_exp);
5026 /* "Return the @var{x}th power of e."
5027 */
5028 SCM_GPROC1 (s_i_log, "$log", scm_tc7_dsubr, (SCM (*)()) log, g_i_log);
5029 /* "Return the natural logarithm of the real number @var{x}."
5030 */
5031 SCM_GPROC1 (s_i_sin, "$sin", scm_tc7_dsubr, (SCM (*)()) sin, g_i_sin);
5032 /* "Return the sine of the real number @var{x}."
5033 */
5034 SCM_GPROC1 (s_i_cos, "$cos", scm_tc7_dsubr, (SCM (*)()) cos, g_i_cos);
5035 /* "Return the cosine of the real number @var{x}."
5036 */
5037 SCM_GPROC1 (s_i_tan, "$tan", scm_tc7_dsubr, (SCM (*)()) tan, g_i_tan);
5038 /* "Return the tangent of the real number @var{x}."
5039 */
5040 SCM_GPROC1 (s_i_asin, "$asin", scm_tc7_dsubr, (SCM (*)()) asin, g_i_asin);
5041 /* "Return the arc sine of the real number @var{x}."
5042 */
5043 SCM_GPROC1 (s_i_acos, "$acos", scm_tc7_dsubr, (SCM (*)()) acos, g_i_acos);
5044 /* "Return the arc cosine of the real number @var{x}."
5045 */
5046 SCM_GPROC1 (s_i_atan, "$atan", scm_tc7_dsubr, (SCM (*)()) atan, g_i_atan);
5047 /* "Return the arc tangent of the real number @var{x}."
5048 */
5049 SCM_GPROC1 (s_i_sinh, "$sinh", scm_tc7_dsubr, (SCM (*)()) sinh, g_i_sinh);
5050 /* "Return the hyperbolic sine of the real number @var{x}."
5051 */
5052 SCM_GPROC1 (s_i_cosh, "$cosh", scm_tc7_dsubr, (SCM (*)()) cosh, g_i_cosh);
5053 /* "Return the hyperbolic cosine of the real number @var{x}."
5054 */
5055 SCM_GPROC1 (s_i_tanh, "$tanh", scm_tc7_dsubr, (SCM (*)()) tanh, g_i_tanh);
5056 /* "Return the hyperbolic tangent of the real number @var{x}."
5057 */
5058
5059 struct dpair
5060 {
5061 double x, y;
5062 };
5063
5064 static void scm_two_doubles (SCM x,
5065 SCM y,
5066 const char *sstring,
5067 struct dpair * xy);
5068
5069 static void
5070 scm_two_doubles (SCM x, SCM y, const char *sstring, struct dpair *xy)
5071 {
5072 if (SCM_INUMP (x))
5073 xy->x = SCM_INUM (x);
5074 else if (SCM_BIGP (x))
5075 xy->x = scm_i_big2dbl (x);
5076 else if (SCM_REALP (x))
5077 xy->x = SCM_REAL_VALUE (x);
5078 else if (SCM_FRACTIONP (x))
5079 xy->x = scm_i_fraction2double (x);
5080 else
5081 scm_wrong_type_arg (sstring, SCM_ARG1, x);
5082
5083 if (SCM_INUMP (y))
5084 xy->y = SCM_INUM (y);
5085 else if (SCM_BIGP (y))
5086 xy->y = scm_i_big2dbl (y);
5087 else if (SCM_REALP (y))
5088 xy->y = SCM_REAL_VALUE (y);
5089 else if (SCM_FRACTIONP (y))
5090 xy->y = scm_i_fraction2double (y);
5091 else
5092 scm_wrong_type_arg (sstring, SCM_ARG2, y);
5093 }
5094
5095
5096 SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
5097 (SCM x, SCM y),
5098 "Return @var{x} raised to the power of @var{y}. This\n"
5099 "procedure does not accept complex arguments.")
5100 #define FUNC_NAME s_scm_sys_expt
5101 {
5102 struct dpair xy;
5103 scm_two_doubles (x, y, FUNC_NAME, &xy);
5104 return scm_make_real (pow (xy.x, xy.y));
5105 }
5106 #undef FUNC_NAME
5107
5108
5109 SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
5110 (SCM x, SCM y),
5111 "Return the arc tangent of the two arguments @var{x} and\n"
5112 "@var{y}. This is similar to calculating the arc tangent of\n"
5113 "@var{x} / @var{y}, except that the signs of both arguments\n"
5114 "are used to determine the quadrant of the result. This\n"
5115 "procedure does not accept complex arguments.")
5116 #define FUNC_NAME s_scm_sys_atan2
5117 {
5118 struct dpair xy;
5119 scm_two_doubles (x, y, FUNC_NAME, &xy);
5120 return scm_make_real (atan2 (xy.x, xy.y));
5121 }
5122 #undef FUNC_NAME
5123
5124
5125 SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
5126 (SCM real, SCM imaginary),
5127 "Return a complex number constructed of the given @var{real} and\n"
5128 "@var{imaginary} parts.")
5129 #define FUNC_NAME s_scm_make_rectangular
5130 {
5131 struct dpair xy;
5132 scm_two_doubles (real, imaginary, FUNC_NAME, &xy);
5133 return scm_make_complex (xy.x, xy.y);
5134 }
5135 #undef FUNC_NAME
5136
5137
5138
5139 SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
5140 (SCM x, SCM y),
5141 "Return the complex number @var{x} * e^(i * @var{y}).")
5142 #define FUNC_NAME s_scm_make_polar
5143 {
5144 struct dpair xy;
5145 double s, c;
5146 scm_two_doubles (x, y, FUNC_NAME, &xy);
5147 #if HAVE_SINCOS
5148 sincos (xy.y, &s, &c);
5149 #else
5150 s = sin (xy.y);
5151 c = cos (xy.y);
5152 #endif
5153 return scm_make_complex (xy.x * c, xy.x * s);
5154 }
5155 #undef FUNC_NAME
5156
5157
5158 SCM_GPROC (s_real_part, "real-part", 1, 0, 0, scm_real_part, g_real_part);
5159 /* "Return the real part of the number @var{z}."
5160 */
5161 SCM
5162 scm_real_part (SCM z)
5163 {
5164 if (SCM_INUMP (z))
5165 return z;
5166 else if (SCM_BIGP (z))
5167 return z;
5168 else if (SCM_REALP (z))
5169 return z;
5170 else if (SCM_COMPLEXP (z))
5171 return scm_make_real (SCM_COMPLEX_REAL (z));
5172 else if (SCM_FRACTIONP (z))
5173 return z;
5174 else
5175 SCM_WTA_DISPATCH_1 (g_real_part, z, SCM_ARG1, s_real_part);
5176 }
5177
5178
5179 SCM_GPROC (s_imag_part, "imag-part", 1, 0, 0, scm_imag_part, g_imag_part);
5180 /* "Return the imaginary part of the number @var{z}."
5181 */
5182 SCM
5183 scm_imag_part (SCM z)
5184 {
5185 if (SCM_INUMP (z))
5186 return SCM_INUM0;
5187 else if (SCM_BIGP (z))
5188 return SCM_INUM0;
5189 else if (SCM_REALP (z))
5190 return scm_flo0;
5191 else if (SCM_COMPLEXP (z))
5192 return scm_make_real (SCM_COMPLEX_IMAG (z));
5193 else if (SCM_FRACTIONP (z))
5194 return SCM_INUM0;
5195 else
5196 SCM_WTA_DISPATCH_1 (g_imag_part, z, SCM_ARG1, s_imag_part);
5197 }
5198
5199 SCM_GPROC (s_numerator, "numerator", 1, 0, 0, scm_numerator, g_numerator);
5200 /* "Return the numerator of the number @var{z}."
5201 */
5202 SCM
5203 scm_numerator (SCM z)
5204 {
5205 if (SCM_INUMP (z))
5206 return z;
5207 else if (SCM_BIGP (z))
5208 return z;
5209 else if (SCM_FRACTIONP (z))
5210 {
5211 scm_i_fraction_reduce (z);
5212 return SCM_FRACTION_NUMERATOR (z);
5213 }
5214 else if (SCM_REALP (z))
5215 return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
5216 else
5217 SCM_WTA_DISPATCH_1 (g_numerator, z, SCM_ARG1, s_numerator);
5218 }
5219
5220
5221 SCM_GPROC (s_denominator, "denominator", 1, 0, 0, scm_denominator, g_denominator);
5222 /* "Return the denominator of the number @var{z}."
5223 */
5224 SCM
5225 scm_denominator (SCM z)
5226 {
5227 if (SCM_INUMP (z))
5228 return SCM_MAKINUM (1);
5229 else if (SCM_BIGP (z))
5230 return SCM_MAKINUM (1);
5231 else if (SCM_FRACTIONP (z))
5232 {
5233 scm_i_fraction_reduce (z);
5234 return SCM_FRACTION_DENOMINATOR (z);
5235 }
5236 else if (SCM_REALP (z))
5237 return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
5238 else
5239 SCM_WTA_DISPATCH_1 (g_denominator, z, SCM_ARG1, s_denominator);
5240 }
5241
5242 SCM_GPROC (s_magnitude, "magnitude", 1, 0, 0, scm_magnitude, g_magnitude);
5243 /* "Return the magnitude of the number @var{z}. This is the same as\n"
5244 * "@code{abs} for real arguments, but also allows complex numbers."
5245 */
5246 SCM
5247 scm_magnitude (SCM z)
5248 {
5249 if (SCM_INUMP (z))
5250 {
5251 long int zz = SCM_INUM (z);
5252 if (zz >= 0)
5253 return z;
5254 else if (SCM_POSFIXABLE (-zz))
5255 return SCM_MAKINUM (-zz);
5256 else
5257 return scm_i_long2big (-zz);
5258 }
5259 else if (SCM_BIGP (z))
5260 {
5261 int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
5262 scm_remember_upto_here_1 (z);
5263 if (sgn < 0)
5264 return scm_i_clonebig (z, 0);
5265 else
5266 return z;
5267 }
5268 else if (SCM_REALP (z))
5269 return scm_make_real (fabs (SCM_REAL_VALUE (z)));
5270 else if (SCM_COMPLEXP (z))
5271 return scm_make_real (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
5272 else if (SCM_FRACTIONP (z))
5273 {
5274 if (SCM_FALSEP (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
5275 return z;
5276 return scm_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
5277 SCM_FRACTION_DENOMINATOR (z));
5278 }
5279 else
5280 SCM_WTA_DISPATCH_1 (g_magnitude, z, SCM_ARG1, s_magnitude);
5281 }
5282
5283
5284 SCM_GPROC (s_angle, "angle", 1, 0, 0, scm_angle, g_angle);
5285 /* "Return the angle of the complex number @var{z}."
5286 */
5287 SCM
5288 scm_angle (SCM z)
5289 {
5290 /* atan(0,-1) is pi and it'd be possible to have that as a constant like
5291 scm_flo0 to save allocating a new flonum with scm_make_real each time.
5292 But if atan2 follows the floating point rounding mode, then the value
5293 is not a constant. Maybe it'd be close enough though. */
5294 if (SCM_INUMP (z))
5295 {
5296 if (SCM_INUM (z) >= 0)
5297 return scm_flo0;
5298 else
5299 return scm_make_real (atan2 (0.0, -1.0));
5300 }
5301 else if (SCM_BIGP (z))
5302 {
5303 int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
5304 scm_remember_upto_here_1 (z);
5305 if (sgn < 0)
5306 return scm_make_real (atan2 (0.0, -1.0));
5307 else
5308 return scm_flo0;
5309 }
5310 else if (SCM_REALP (z))
5311 {
5312 if (SCM_REAL_VALUE (z) >= 0)
5313 return scm_flo0;
5314 else
5315 return scm_make_real (atan2 (0.0, -1.0));
5316 }
5317 else if (SCM_COMPLEXP (z))
5318 return scm_make_real (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
5319 else if (SCM_FRACTIONP (z))
5320 {
5321 if (SCM_FALSEP (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
5322 return scm_flo0;
5323 else return scm_make_real (atan2 (0.0, -1.0));
5324 }
5325 else
5326 SCM_WTA_DISPATCH_1 (g_angle, z, SCM_ARG1, s_angle);
5327 }
5328
5329
5330 SCM_GPROC (s_exact_to_inexact, "exact->inexact", 1, 0, 0, scm_exact_to_inexact, g_exact_to_inexact);
5331 /* Convert the number @var{x} to its inexact representation.\n"
5332 */
5333 SCM
5334 scm_exact_to_inexact (SCM z)
5335 {
5336 if (SCM_INUMP (z))
5337 return scm_make_real ((double) SCM_INUM (z));
5338 else if (SCM_BIGP (z))
5339 return scm_make_real (scm_i_big2dbl (z));
5340 else if (SCM_FRACTIONP (z))
5341 return scm_make_real (scm_i_fraction2double (z));
5342 else if (SCM_INEXACTP (z))
5343 return z;
5344 else
5345 SCM_WTA_DISPATCH_1 (g_exact_to_inexact, z, 1, s_exact_to_inexact);
5346 }
5347
5348
5349 SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
5350 (SCM z),
5351 "Return an exact number that is numerically closest to @var{z}.")
5352 #define FUNC_NAME s_scm_inexact_to_exact
5353 {
5354 if (SCM_INUMP (z))
5355 return z;
5356 else if (SCM_BIGP (z))
5357 return z;
5358 else if (SCM_REALP (z))
5359 {
5360 if (xisinf (SCM_REAL_VALUE (z)) || xisnan (SCM_REAL_VALUE (z)))
5361 SCM_OUT_OF_RANGE (1, z);
5362 else
5363 {
5364 mpq_t frac;
5365 SCM q;
5366
5367 mpq_init (frac);
5368 mpq_set_d (frac, SCM_REAL_VALUE (z));
5369 q = scm_make_ratio (scm_i_mpz2num (mpq_numref (frac)),
5370 scm_i_mpz2num (mpq_denref (frac)));
5371
5372 /* When scm_make_ratio throws, we leak the memory allocated
5373 for frac...
5374 */
5375 mpq_clear (frac);
5376 return q;
5377 }
5378 }
5379 else if (SCM_FRACTIONP (z))
5380 return z;
5381 else
5382 SCM_WRONG_TYPE_ARG (1, z);
5383 }
5384 #undef FUNC_NAME
5385
5386 SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
5387 (SCM x, SCM err),
5388 "Return an exact number that is within @var{err} of @var{x}.")
5389 #define FUNC_NAME s_scm_rationalize
5390 {
5391 if (SCM_INUMP (x))
5392 return x;
5393 else if (SCM_BIGP (x))
5394 return x;
5395 else if ((SCM_REALP (x)) || SCM_FRACTIONP (x))
5396 {
5397 /* Use continued fractions to find closest ratio. All
5398 arithmetic is done with exact numbers.
5399 */
5400
5401 SCM ex = scm_inexact_to_exact (x);
5402 SCM int_part = scm_floor (ex);
5403 SCM tt = SCM_MAKINUM (1);
5404 SCM a1 = SCM_MAKINUM (0), a2 = SCM_MAKINUM (1), a = SCM_MAKINUM (0);
5405 SCM b1 = SCM_MAKINUM (1), b2 = SCM_MAKINUM (0), b = SCM_MAKINUM (0);
5406 SCM rx;
5407 int i = 0;
5408
5409 if (!SCM_FALSEP (scm_num_eq_p (ex, int_part)))
5410 return ex;
5411
5412 ex = scm_difference (ex, int_part); /* x = x-int_part */
5413 rx = scm_divide (ex, SCM_UNDEFINED); /* rx = 1/x */
5414
5415 /* We stop after a million iterations just to be absolutely sure
5416 that we don't go into an infinite loop. The process normally
5417 converges after less than a dozen iterations.
5418 */
5419
5420 err = scm_abs (err);
5421 while (++i < 1000000)
5422 {
5423 a = scm_sum (scm_product (a1, tt), a2); /* a = a1*tt + a2 */
5424 b = scm_sum (scm_product (b1, tt), b2); /* b = b1*tt + b2 */
5425 if (SCM_FALSEP (scm_zero_p (b)) && /* b != 0 */
5426 SCM_FALSEP
5427 (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))),
5428 err))) /* abs(x-a/b) <= err */
5429 {
5430 SCM res = scm_sum (int_part, scm_divide (a, b));
5431 if (SCM_FALSEP (scm_exact_p (x))
5432 || SCM_FALSEP (scm_exact_p (err)))
5433 return scm_exact_to_inexact (res);
5434 else
5435 return res;
5436 }
5437 rx = scm_divide (scm_difference (rx, tt), /* rx = 1/(rx - tt) */
5438 SCM_UNDEFINED);
5439 tt = scm_floor (rx); /* tt = floor (rx) */
5440 a2 = a1;
5441 b2 = b1;
5442 a1 = a;
5443 b1 = b;
5444 }
5445 scm_num_overflow (s_scm_rationalize);
5446 }
5447 else
5448 SCM_WRONG_TYPE_ARG (1, x);
5449 }
5450 #undef FUNC_NAME
5451
5452 /* if you need to change this, change test-num2integral.c as well */
5453 #if SCM_SIZEOF_LONG_LONG != 0
5454 # ifndef LLONG_MAX
5455 # define ULLONG_MAX ((unsigned long long) (-1))
5456 # define LLONG_MAX ((long long) (ULLONG_MAX >> 1))
5457 # define LLONG_MIN (~LLONG_MAX)
5458 # endif
5459 #endif
5460
5461 /* Parameters for creating integer conversion routines.
5462
5463 Define the following preprocessor macros before including
5464 "libguile/num2integral.i.c":
5465
5466 NUM2INTEGRAL - the name of the function for converting from a
5467 Scheme object to the integral type. This function will be
5468 defined when including "num2integral.i.c".
5469
5470 INTEGRAL2NUM - the name of the function for converting from the
5471 integral type to a Scheme object. This function will be defined.
5472
5473 INTEGRAL2BIG - the name of an internal function that createas a
5474 bignum from the integral type. This function will be defined.
5475 The name should start with "scm_i_".
5476
5477 ITYPE - the name of the integral type.
5478
5479 UNSIGNED - Define this to 1 when ITYPE is an unsigned type. Define
5480 it to 0 otherwise.
5481
5482 UNSIGNED_ITYPE - the name of the the unsigned variant of the
5483 integral type. If you don't define this, it defaults to
5484 "unsigned ITYPE" for signed types and simply "ITYPE" for unsigned
5485 ones.
5486
5487 SIZEOF_ITYPE - an expression giving the size of the integral type
5488 in bytes. This expression must be computable by the
5489 preprocessor. (SIZEOF_FOO values are calculated by configure.in
5490 for common types).
5491
5492 */
5493
5494 #define NUM2INTEGRAL scm_num2short
5495 #define INTEGRAL2NUM scm_short2num
5496 #define INTEGRAL2BIG scm_i_short2big
5497 #define UNSIGNED 0
5498 #define ITYPE short
5499 #define SIZEOF_ITYPE SIZEOF_SHORT
5500 #include "libguile/num2integral.i.c"
5501
5502 #define NUM2INTEGRAL scm_num2ushort
5503 #define INTEGRAL2NUM scm_ushort2num
5504 #define INTEGRAL2BIG scm_i_ushort2big
5505 #define UNSIGNED 1
5506 #define ITYPE unsigned short
5507 #define SIZEOF_ITYPE SIZEOF_UNSIGNED_SHORT
5508 #include "libguile/num2integral.i.c"
5509
5510 #define NUM2INTEGRAL scm_num2int
5511 #define INTEGRAL2NUM scm_int2num
5512 #define INTEGRAL2BIG scm_i_int2big
5513 #define UNSIGNED 0
5514 #define ITYPE int
5515 #define SIZEOF_ITYPE SIZEOF_INT
5516 #include "libguile/num2integral.i.c"
5517
5518 #define NUM2INTEGRAL scm_num2uint
5519 #define INTEGRAL2NUM scm_uint2num
5520 #define INTEGRAL2BIG scm_i_uint2big
5521 #define UNSIGNED 1
5522 #define ITYPE unsigned int
5523 #define SIZEOF_ITYPE SIZEOF_UNSIGNED_INT
5524 #include "libguile/num2integral.i.c"
5525
5526 #define NUM2INTEGRAL scm_num2long
5527 #define INTEGRAL2NUM scm_long2num
5528 #define INTEGRAL2BIG scm_i_long2big
5529 #define UNSIGNED 0
5530 #define ITYPE long
5531 #define SIZEOF_ITYPE SIZEOF_LONG
5532 #include "libguile/num2integral.i.c"
5533
5534 #define NUM2INTEGRAL scm_num2ulong
5535 #define INTEGRAL2NUM scm_ulong2num
5536 #define INTEGRAL2BIG scm_i_ulong2big
5537 #define UNSIGNED 1
5538 #define ITYPE unsigned long
5539 #define SIZEOF_ITYPE SIZEOF_UNSIGNED_LONG
5540 #include "libguile/num2integral.i.c"
5541
5542 #define NUM2INTEGRAL scm_num2ptrdiff
5543 #define INTEGRAL2NUM scm_ptrdiff2num
5544 #define INTEGRAL2BIG scm_i_ptrdiff2big
5545 #define UNSIGNED 0
5546 #define ITYPE scm_t_ptrdiff
5547 #define UNSIGNED_ITYPE size_t
5548 #define SIZEOF_ITYPE SCM_SIZEOF_SCM_T_PTRDIFF
5549 #include "libguile/num2integral.i.c"
5550
5551 #define NUM2INTEGRAL scm_num2size
5552 #define INTEGRAL2NUM scm_size2num
5553 #define INTEGRAL2BIG scm_i_size2big
5554 #define UNSIGNED 1
5555 #define ITYPE size_t
5556 #define SIZEOF_ITYPE SIZEOF_SIZE_T
5557 #include "libguile/num2integral.i.c"
5558
5559 #if SCM_SIZEOF_LONG_LONG != 0
5560
5561 #ifndef ULONG_LONG_MAX
5562 #define ULONG_LONG_MAX (~0ULL)
5563 #endif
5564
5565 #define NUM2INTEGRAL scm_num2long_long
5566 #define INTEGRAL2NUM scm_long_long2num
5567 #define INTEGRAL2BIG scm_i_long_long2big
5568 #define UNSIGNED 0
5569 #define ITYPE long long
5570 #define SIZEOF_ITYPE SIZEOF_LONG_LONG
5571 #include "libguile/num2integral.i.c"
5572
5573 #define NUM2INTEGRAL scm_num2ulong_long
5574 #define INTEGRAL2NUM scm_ulong_long2num
5575 #define INTEGRAL2BIG scm_i_ulong_long2big
5576 #define UNSIGNED 1
5577 #define ITYPE unsigned long long
5578 #define SIZEOF_ITYPE SIZEOF_UNSIGNED_LONG_LONG
5579 #include "libguile/num2integral.i.c"
5580
5581 #endif /* SCM_SIZEOF_LONG_LONG != 0 */
5582
5583 #define NUM2FLOAT scm_num2float
5584 #define FLOAT2NUM scm_float2num
5585 #define FTYPE float
5586 #include "libguile/num2float.i.c"
5587
5588 #define NUM2FLOAT scm_num2double
5589 #define FLOAT2NUM scm_double2num
5590 #define FTYPE double
5591 #include "libguile/num2float.i.c"
5592
5593 #ifdef GUILE_DEBUG
5594
5595 #ifndef SIZE_MAX
5596 #define SIZE_MAX ((size_t) (-1))
5597 #endif
5598 #ifndef PTRDIFF_MIN
5599 #define PTRDIFF_MIN \
5600 ((scm_t_ptrdiff) ((scm_t_ptrdiff) 1 \
5601 << ((sizeof (scm_t_ptrdiff) * SCM_CHAR_BIT) - 1)))
5602 #endif
5603 #ifndef PTRDIFF_MAX
5604 #define PTRDIFF_MAX (~ PTRDIFF_MIN)
5605 #endif
5606
5607 #define CHECK(type, v) \
5608 do \
5609 { \
5610 if ((v) != scm_num2##type (scm_##type##2num (v), 1, "check_sanity")) \
5611 abort (); \
5612 } \
5613 while (0)
5614
5615 static void
5616 check_sanity ()
5617 {
5618 CHECK (short, 0);
5619 CHECK (ushort, 0U);
5620 CHECK (int, 0);
5621 CHECK (uint, 0U);
5622 CHECK (long, 0L);
5623 CHECK (ulong, 0UL);
5624 CHECK (size, 0);
5625 CHECK (ptrdiff, 0);
5626
5627 CHECK (short, -1);
5628 CHECK (int, -1);
5629 CHECK (long, -1L);
5630 CHECK (ptrdiff, -1);
5631
5632 CHECK (short, SHRT_MAX);
5633 CHECK (short, SHRT_MIN);
5634 CHECK (ushort, USHRT_MAX);
5635 CHECK (int, INT_MAX);
5636 CHECK (int, INT_MIN);
5637 CHECK (uint, UINT_MAX);
5638 CHECK (long, LONG_MAX);
5639 CHECK (long, LONG_MIN);
5640 CHECK (ulong, ULONG_MAX);
5641 CHECK (size, SIZE_MAX);
5642 CHECK (ptrdiff, PTRDIFF_MAX);
5643 CHECK (ptrdiff, PTRDIFF_MIN);
5644
5645 #if SCM_SIZEOF_LONG_LONG != 0
5646 CHECK (long_long, 0LL);
5647 CHECK (ulong_long, 0ULL);
5648 CHECK (long_long, -1LL);
5649 CHECK (long_long, LLONG_MAX);
5650 CHECK (long_long, LLONG_MIN);
5651 CHECK (ulong_long, ULLONG_MAX);
5652 #endif
5653 }
5654
5655 #undef CHECK
5656
5657 #define CHECK \
5658 scm_internal_catch (SCM_BOOL_T, check_body, &data, check_handler, &data); \
5659 if (!SCM_FALSEP (data)) abort();
5660
5661 static SCM
5662 check_body (void *data)
5663 {
5664 SCM num = *(SCM *) data;
5665 scm_num2ulong (num, 1, NULL);
5666
5667 return SCM_UNSPECIFIED;
5668 }
5669
5670 static SCM
5671 check_handler (void *data, SCM tag, SCM throw_args)
5672 {
5673 SCM *num = (SCM *) data;
5674 *num = SCM_BOOL_F;
5675
5676 return SCM_UNSPECIFIED;
5677 }
5678
5679 SCM_DEFINE (scm_sys_check_number_conversions, "%check-number-conversions", 0, 0, 0,
5680 (void),
5681 "Number conversion sanity checking.")
5682 #define FUNC_NAME s_scm_sys_check_number_conversions
5683 {
5684 SCM data = SCM_MAKINUM (-1);
5685 CHECK;
5686 data = scm_int2num (INT_MIN);
5687 CHECK;
5688 data = scm_ulong2num (ULONG_MAX);
5689 data = scm_difference (SCM_INUM0, data);
5690 CHECK;
5691 data = scm_ulong2num (ULONG_MAX);
5692 data = scm_sum (SCM_MAKINUM (1), data); data = scm_difference (SCM_INUM0, data);
5693 CHECK;
5694 data = scm_int2num (-10000); data = scm_product (data, data); data = scm_product (data, data);
5695 CHECK;
5696
5697 return SCM_UNSPECIFIED;
5698 }
5699 #undef FUNC_NAME
5700
5701 #endif
5702
5703 void
5704 scm_init_numbers ()
5705 {
5706 mpz_init_set_si (z_negative_one, -1);
5707
5708 /* It may be possible to tune the performance of some algorithms by using
5709 * the following constants to avoid the creation of bignums. Please, before
5710 * using these values, remember the two rules of program optimization:
5711 * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
5712 scm_c_define ("most-positive-fixnum",
5713 SCM_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
5714 scm_c_define ("most-negative-fixnum",
5715 SCM_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
5716
5717 scm_add_feature ("complex");
5718 scm_add_feature ("inexact");
5719 scm_flo0 = scm_make_real (0.0);
5720 #ifdef DBL_DIG
5721 scm_dblprec = (DBL_DIG > 20) ? 20 : DBL_DIG;
5722 #else
5723 { /* determine floating point precision */
5724 double f = 0.1;
5725 double fsum = 1.0 + f;
5726 while (fsum != 1.0)
5727 {
5728 if (++scm_dblprec > 20)
5729 fsum = 1.0;
5730 else
5731 {
5732 f /= 10.0;
5733 fsum = f + 1.0;
5734 }
5735 }
5736 scm_dblprec = scm_dblprec - 1;
5737 }
5738 #endif /* DBL_DIG */
5739
5740 #ifdef GUILE_DEBUG
5741 check_sanity ();
5742 #endif
5743
5744 exactly_one_half = scm_permanent_object (scm_divide (SCM_MAKINUM (1),
5745 SCM_MAKINUM (2)));
5746 #include "libguile/numbers.x"
5747 }
5748
5749 /*
5750 Local Variables:
5751 c-file-style: "gnu"
5752 End:
5753 */