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