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