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