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