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