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