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