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