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