(scm_oneplus, scm_oneminus): New functions, converted from
[bpt/guile.git] / libguile / numbers.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005 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 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 scm_i_normbig (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 scm_i_normbig (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 (SCM_I_INUMP (n))
1786 {
1787 long nn = SCM_I_INUM (n);
1788
1789 if (bits_to_shift > 0)
1790 {
1791 /* Left shift of bits_to_shift >= SCM_I_FIXNUM_BIT-1 will always
1792 overflow a non-zero fixnum. For smaller shifts we check the
1793 bits going into positions above SCM_I_FIXNUM_BIT-1. If they're
1794 all 0s for nn>=0, or all 1s for nn<0 then there's no overflow.
1795 Those bits are "nn >> (SCM_I_FIXNUM_BIT-1 -
1796 bits_to_shift)". */
1797
1798 if (nn == 0)
1799 return n;
1800
1801 if (bits_to_shift < SCM_I_FIXNUM_BIT-1
1802 && ((unsigned long)
1803 (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - bits_to_shift)) + 1)
1804 <= 1))
1805 {
1806 return SCM_I_MAKINUM (nn << bits_to_shift);
1807 }
1808 else
1809 {
1810 SCM result = scm_i_long2big (nn);
1811 mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
1812 bits_to_shift);
1813 return result;
1814 }
1815 }
1816 else
1817 {
1818 bits_to_shift = -bits_to_shift;
1819 if (bits_to_shift >= SCM_LONG_BIT)
1820 return (nn >= 0 ? SCM_I_MAKINUM (0) : SCM_I_MAKINUM(-1));
1821 else
1822 return SCM_I_MAKINUM (SCM_SRS (nn, bits_to_shift));
1823 }
1824
1825 }
1826 else if (SCM_BIGP (n))
1827 {
1828 SCM result;
1829
1830 if (bits_to_shift == 0)
1831 return n;
1832
1833 result = scm_i_mkbig ();
1834 if (bits_to_shift >= 0)
1835 {
1836 mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
1837 bits_to_shift);
1838 return result;
1839 }
1840 else
1841 {
1842 /* GMP doesn't have an fdiv_q_2exp variant returning just a long, so
1843 we have to allocate a bignum even if the result is going to be a
1844 fixnum. */
1845 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
1846 -bits_to_shift);
1847 return scm_i_normbig (result);
1848 }
1849
1850 }
1851 else
1852 {
1853 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
1854 }
1855 }
1856 #undef FUNC_NAME
1857
1858
1859 SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
1860 (SCM n, SCM start, SCM end),
1861 "Return the integer composed of the @var{start} (inclusive)\n"
1862 "through @var{end} (exclusive) bits of @var{n}. The\n"
1863 "@var{start}th bit becomes the 0-th bit in the result.\n"
1864 "\n"
1865 "@lisp\n"
1866 "(number->string (bit-extract #b1101101010 0 4) 2)\n"
1867 " @result{} \"1010\"\n"
1868 "(number->string (bit-extract #b1101101010 4 9) 2)\n"
1869 " @result{} \"10110\"\n"
1870 "@end lisp")
1871 #define FUNC_NAME s_scm_bit_extract
1872 {
1873 unsigned long int istart, iend, bits;
1874 istart = scm_to_ulong (start);
1875 iend = scm_to_ulong (end);
1876 SCM_ASSERT_RANGE (3, end, (iend >= istart));
1877
1878 /* how many bits to keep */
1879 bits = iend - istart;
1880
1881 if (SCM_I_INUMP (n))
1882 {
1883 long int in = SCM_I_INUM (n);
1884
1885 /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
1886 SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */
1887 in = SCM_SRS (in, min (istart, SCM_I_FIXNUM_BIT-1));
1888
1889 if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
1890 {
1891 /* Since we emulate two's complement encoded numbers, this
1892 * special case requires us to produce a result that has
1893 * more bits than can be stored in a fixnum.
1894 */
1895 SCM result = scm_i_long2big (in);
1896 mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
1897 bits);
1898 return result;
1899 }
1900
1901 /* mask down to requisite bits */
1902 bits = min (bits, SCM_I_FIXNUM_BIT);
1903 return SCM_I_MAKINUM (in & ((1L << bits) - 1));
1904 }
1905 else if (SCM_BIGP (n))
1906 {
1907 SCM result;
1908 if (bits == 1)
1909 {
1910 result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart));
1911 }
1912 else
1913 {
1914 /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
1915 bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
1916 such bits into a ulong. */
1917 result = scm_i_mkbig ();
1918 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart);
1919 mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits);
1920 result = scm_i_normbig (result);
1921 }
1922 scm_remember_upto_here_1 (n);
1923 return result;
1924 }
1925 else
1926 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
1927 }
1928 #undef FUNC_NAME
1929
1930
1931 static const char scm_logtab[] = {
1932 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1933 };
1934
1935 SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
1936 (SCM n),
1937 "Return the number of bits in integer @var{n}. If integer is\n"
1938 "positive, the 1-bits in its binary representation are counted.\n"
1939 "If negative, the 0-bits in its two's-complement binary\n"
1940 "representation are counted. If 0, 0 is returned.\n"
1941 "\n"
1942 "@lisp\n"
1943 "(logcount #b10101010)\n"
1944 " @result{} 4\n"
1945 "(logcount 0)\n"
1946 " @result{} 0\n"
1947 "(logcount -2)\n"
1948 " @result{} 1\n"
1949 "@end lisp")
1950 #define FUNC_NAME s_scm_logcount
1951 {
1952 if (SCM_I_INUMP (n))
1953 {
1954 unsigned long int c = 0;
1955 long int nn = SCM_I_INUM (n);
1956 if (nn < 0)
1957 nn = -1 - nn;
1958 while (nn)
1959 {
1960 c += scm_logtab[15 & nn];
1961 nn >>= 4;
1962 }
1963 return SCM_I_MAKINUM (c);
1964 }
1965 else if (SCM_BIGP (n))
1966 {
1967 unsigned long count;
1968 if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0)
1969 count = mpz_popcount (SCM_I_BIG_MPZ (n));
1970 else
1971 count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one);
1972 scm_remember_upto_here_1 (n);
1973 return SCM_I_MAKINUM (count);
1974 }
1975 else
1976 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
1977 }
1978 #undef FUNC_NAME
1979
1980
1981 static const char scm_ilentab[] = {
1982 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
1983 };
1984
1985
1986 SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
1987 (SCM n),
1988 "Return the number of bits necessary to represent @var{n}.\n"
1989 "\n"
1990 "@lisp\n"
1991 "(integer-length #b10101010)\n"
1992 " @result{} 8\n"
1993 "(integer-length 0)\n"
1994 " @result{} 0\n"
1995 "(integer-length #b1111)\n"
1996 " @result{} 4\n"
1997 "@end lisp")
1998 #define FUNC_NAME s_scm_integer_length
1999 {
2000 if (SCM_I_INUMP (n))
2001 {
2002 unsigned long int c = 0;
2003 unsigned int l = 4;
2004 long int nn = SCM_I_INUM (n);
2005 if (nn < 0)
2006 nn = -1 - nn;
2007 while (nn)
2008 {
2009 c += 4;
2010 l = scm_ilentab [15 & nn];
2011 nn >>= 4;
2012 }
2013 return SCM_I_MAKINUM (c - 4 + l);
2014 }
2015 else if (SCM_BIGP (n))
2016 {
2017 /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
2018 want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
2019 1 too big, so check for that and adjust. */
2020 size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
2021 if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
2022 && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
2023 mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
2024 size--;
2025 scm_remember_upto_here_1 (n);
2026 return SCM_I_MAKINUM (size);
2027 }
2028 else
2029 SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
2030 }
2031 #undef FUNC_NAME
2032
2033 /*** NUMBERS -> STRINGS ***/
2034 #define SCM_MAX_DBL_PREC 60
2035 #define SCM_MAX_DBL_RADIX 36
2036
2037 /* this is an array starting with radix 2, and ending with radix SCM_MAX_DBL_RADIX */
2038 static int scm_dblprec[SCM_MAX_DBL_RADIX - 1];
2039 static double fx_per_radix[SCM_MAX_DBL_RADIX - 1][SCM_MAX_DBL_PREC];
2040
2041 static
2042 void init_dblprec(int *prec, int radix) {
2043 /* determine floating point precision by adding successively
2044 smaller increments to 1.0 until it is considered == 1.0 */
2045 double f = ((double)1.0)/radix;
2046 double fsum = 1.0 + f;
2047
2048 *prec = 0;
2049 while (fsum != 1.0)
2050 {
2051 if (++(*prec) > SCM_MAX_DBL_PREC)
2052 fsum = 1.0;
2053 else
2054 {
2055 f /= radix;
2056 fsum = f + 1.0;
2057 }
2058 }
2059 (*prec) -= 1;
2060 }
2061
2062 static
2063 void init_fx_radix(double *fx_list, int radix)
2064 {
2065 /* initialize a per-radix list of tolerances. When added
2066 to a number < 1.0, we can determine if we should raund
2067 up and quit converting a number to a string. */
2068 int i;
2069 fx_list[0] = 0.0;
2070 fx_list[1] = 0.5;
2071 for( i = 2 ; i < SCM_MAX_DBL_PREC; ++i )
2072 fx_list[i] = (fx_list[i-1] / radix);
2073 }
2074
2075 /* use this array as a way to generate a single digit */
2076 static const char*number_chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2077
2078 static size_t
2079 idbl2str (double f, char *a, int radix)
2080 {
2081 int efmt, dpt, d, i, wp;
2082 double *fx;
2083 #ifdef DBL_MIN_10_EXP
2084 double f_cpy;
2085 int exp_cpy;
2086 #endif /* DBL_MIN_10_EXP */
2087 size_t ch = 0;
2088 int exp = 0;
2089
2090 if(radix < 2 ||
2091 radix > SCM_MAX_DBL_RADIX)
2092 {
2093 /* revert to existing behavior */
2094 radix = 10;
2095 }
2096
2097 wp = scm_dblprec[radix-2];
2098 fx = fx_per_radix[radix-2];
2099
2100 if (f == 0.0)
2101 {
2102 #ifdef HAVE_COPYSIGN
2103 double sgn = copysign (1.0, f);
2104
2105 if (sgn < 0.0)
2106 a[ch++] = '-';
2107 #endif
2108 goto zero; /*{a[0]='0'; a[1]='.'; a[2]='0'; return 3;} */
2109 }
2110
2111 if (xisinf (f))
2112 {
2113 if (f < 0)
2114 strcpy (a, "-inf.0");
2115 else
2116 strcpy (a, "+inf.0");
2117 return ch+6;
2118 }
2119 else if (xisnan (f))
2120 {
2121 strcpy (a, "+nan.0");
2122 return ch+6;
2123 }
2124
2125 if (f < 0.0)
2126 {
2127 f = -f;
2128 a[ch++] = '-';
2129 }
2130
2131 #ifdef DBL_MIN_10_EXP /* Prevent unnormalized values, as from
2132 make-uniform-vector, from causing infinite loops. */
2133 /* just do the checking...if it passes, we do the conversion for our
2134 radix again below */
2135 f_cpy = f;
2136 exp_cpy = exp;
2137
2138 while (f_cpy < 1.0)
2139 {
2140 f_cpy *= 10.0;
2141 if (exp_cpy-- < DBL_MIN_10_EXP)
2142 {
2143 a[ch++] = '#';
2144 a[ch++] = '.';
2145 a[ch++] = '#';
2146 return ch;
2147 }
2148 }
2149 while (f_cpy > 10.0)
2150 {
2151 f_cpy *= 0.10;
2152 if (exp_cpy++ > DBL_MAX_10_EXP)
2153 {
2154 a[ch++] = '#';
2155 a[ch++] = '.';
2156 a[ch++] = '#';
2157 return ch;
2158 }
2159 }
2160 #endif
2161
2162 while (f < 1.0)
2163 {
2164 f *= radix;
2165 exp--;
2166 }
2167 while (f > radix)
2168 {
2169 f /= radix;
2170 exp++;
2171 }
2172
2173 if (f + fx[wp] >= radix)
2174 {
2175 f = 1.0;
2176 exp++;
2177 }
2178 zero:
2179 #ifdef ENGNOT
2180 /* adding 9999 makes this equivalent to abs(x) % 3 */
2181 dpt = (exp + 9999) % 3;
2182 exp -= dpt++;
2183 efmt = 1;
2184 #else
2185 efmt = (exp < -3) || (exp > wp + 2);
2186 if (!efmt)
2187 {
2188 if (exp < 0)
2189 {
2190 a[ch++] = '0';
2191 a[ch++] = '.';
2192 dpt = exp;
2193 while (++dpt)
2194 a[ch++] = '0';
2195 }
2196 else
2197 dpt = exp + 1;
2198 }
2199 else
2200 dpt = 1;
2201 #endif
2202
2203 do
2204 {
2205 d = f;
2206 f -= d;
2207 a[ch++] = number_chars[d];
2208 if (f < fx[wp])
2209 break;
2210 if (f + fx[wp] >= 1.0)
2211 {
2212 a[ch - 1] = number_chars[d+1];
2213 break;
2214 }
2215 f *= radix;
2216 if (!(--dpt))
2217 a[ch++] = '.';
2218 }
2219 while (wp--);
2220
2221 if (dpt > 0)
2222 {
2223 #ifndef ENGNOT
2224 if ((dpt > 4) && (exp > 6))
2225 {
2226 d = (a[0] == '-' ? 2 : 1);
2227 for (i = ch++; i > d; i--)
2228 a[i] = a[i - 1];
2229 a[d] = '.';
2230 efmt = 1;
2231 }
2232 else
2233 #endif
2234 {
2235 while (--dpt)
2236 a[ch++] = '0';
2237 a[ch++] = '.';
2238 }
2239 }
2240 if (a[ch - 1] == '.')
2241 a[ch++] = '0'; /* trailing zero */
2242 if (efmt && exp)
2243 {
2244 a[ch++] = 'e';
2245 if (exp < 0)
2246 {
2247 exp = -exp;
2248 a[ch++] = '-';
2249 }
2250 for (i = radix; i <= exp; i *= radix);
2251 for (i /= radix; i; i /= radix)
2252 {
2253 a[ch++] = number_chars[exp / i];
2254 exp %= i;
2255 }
2256 }
2257 return ch;
2258 }
2259
2260
2261 static size_t
2262 icmplx2str (double real, double imag, char *str, int radix)
2263 {
2264 size_t i;
2265
2266 i = idbl2str (real, str, radix);
2267 if (imag != 0.0)
2268 {
2269 /* Don't output a '+' for negative numbers or for Inf and
2270 NaN. They will provide their own sign. */
2271 if (0 <= imag && !xisinf (imag) && !xisnan (imag))
2272 str[i++] = '+';
2273 i += idbl2str (imag, &str[i], radix);
2274 str[i++] = 'i';
2275 }
2276 return i;
2277 }
2278
2279 static size_t
2280 iflo2str (SCM flt, char *str, int radix)
2281 {
2282 size_t i;
2283 if (SCM_REALP (flt))
2284 i = idbl2str (SCM_REAL_VALUE (flt), str, radix);
2285 else
2286 i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt),
2287 str, radix);
2288 return i;
2289 }
2290
2291 /* convert a scm_t_intmax to a string (unterminated). returns the number of
2292 characters in the result.
2293 rad is output base
2294 p is destination: worst case (base 2) is SCM_INTBUFLEN */
2295 size_t
2296 scm_iint2str (scm_t_intmax num, int rad, char *p)
2297 {
2298 if (num < 0)
2299 {
2300 *p++ = '-';
2301 return scm_iuint2str (-num, rad, p) + 1;
2302 }
2303 else
2304 return scm_iuint2str (num, rad, p);
2305 }
2306
2307 /* convert a scm_t_intmax to a string (unterminated). returns the number of
2308 characters in the result.
2309 rad is output base
2310 p is destination: worst case (base 2) is SCM_INTBUFLEN */
2311 size_t
2312 scm_iuint2str (scm_t_uintmax num, int rad, char *p)
2313 {
2314 size_t j = 1;
2315 size_t i;
2316 scm_t_uintmax n = num;
2317
2318 for (n /= rad; n > 0; n /= rad)
2319 j++;
2320
2321 i = j;
2322 n = num;
2323 while (i--)
2324 {
2325 int d = n % rad;
2326
2327 n /= rad;
2328 p[i] = d + ((d < 10) ? '0' : 'a' - 10);
2329 }
2330 return j;
2331 }
2332
2333 SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
2334 (SCM n, SCM radix),
2335 "Return a string holding the external representation of the\n"
2336 "number @var{n} in the given @var{radix}. If @var{n} is\n"
2337 "inexact, a radix of 10 will be used.")
2338 #define FUNC_NAME s_scm_number_to_string
2339 {
2340 int base;
2341
2342 if (SCM_UNBNDP (radix))
2343 base = 10;
2344 else
2345 base = scm_to_signed_integer (radix, 2, 36);
2346
2347 if (SCM_I_INUMP (n))
2348 {
2349 char num_buf [SCM_INTBUFLEN];
2350 size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf);
2351 return scm_from_locale_stringn (num_buf, length);
2352 }
2353 else if (SCM_BIGP (n))
2354 {
2355 char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
2356 scm_remember_upto_here_1 (n);
2357 return scm_take_locale_string (str);
2358 }
2359 else if (SCM_FRACTIONP (n))
2360 {
2361 scm_i_fraction_reduce (n);
2362 return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
2363 scm_from_locale_string ("/"),
2364 scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
2365 }
2366 else if (SCM_INEXACTP (n))
2367 {
2368 char num_buf [FLOBUFLEN];
2369 return scm_from_locale_stringn (num_buf, iflo2str (n, num_buf, base));
2370 }
2371 else
2372 SCM_WRONG_TYPE_ARG (1, n);
2373 }
2374 #undef FUNC_NAME
2375
2376
2377 /* These print routines used to be stubbed here so that scm_repl.c
2378 wouldn't need SCM_BIGDIG conditionals (pre GMP) */
2379
2380 int
2381 scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
2382 {
2383 char num_buf[FLOBUFLEN];
2384 scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
2385 return !0;
2386 }
2387
2388 void
2389 scm_i_print_double (double val, SCM port)
2390 {
2391 char num_buf[FLOBUFLEN];
2392 scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port);
2393 }
2394
2395 int
2396 scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
2397
2398 {
2399 char num_buf[FLOBUFLEN];
2400 scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
2401 return !0;
2402 }
2403
2404 void
2405 scm_i_print_complex (double real, double imag, SCM port)
2406 {
2407 char num_buf[FLOBUFLEN];
2408 scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port);
2409 }
2410
2411 int
2412 scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
2413 {
2414 SCM str;
2415 scm_i_fraction_reduce (sexp);
2416 str = scm_number_to_string (sexp, SCM_UNDEFINED);
2417 scm_lfwrite (scm_i_string_chars (str), scm_i_string_length (str), port);
2418 scm_remember_upto_here_1 (str);
2419 return !0;
2420 }
2421
2422 int
2423 scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
2424 {
2425 char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp));
2426 scm_remember_upto_here_1 (exp);
2427 scm_lfwrite (str, (size_t) strlen (str), port);
2428 free (str);
2429 return !0;
2430 }
2431 /*** END nums->strs ***/
2432
2433
2434 /*** STRINGS -> NUMBERS ***/
2435
2436 /* The following functions implement the conversion from strings to numbers.
2437 * The implementation somehow follows the grammar for numbers as it is given
2438 * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
2439 * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
2440 * points should be noted about the implementation:
2441 * * Each function keeps a local index variable 'idx' that points at the
2442 * current position within the parsed string. The global index is only
2443 * updated if the function could parse the corresponding syntactic unit
2444 * successfully.
2445 * * Similarly, the functions keep track of indicators of inexactness ('#',
2446 * '.' or exponents) using local variables ('hash_seen', 'x'). Again, the
2447 * global exactness information is only updated after each part has been
2448 * successfully parsed.
2449 * * Sequences of digits are parsed into temporary variables holding fixnums.
2450 * Only if these fixnums would overflow, the result variables are updated
2451 * using the standard functions scm_add, scm_product, scm_divide etc. Then,
2452 * the temporary variables holding the fixnums are cleared, and the process
2453 * starts over again. If for example fixnums were able to store five decimal
2454 * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
2455 * and the result was computed as 12345 * 100000 + 67890. In other words,
2456 * only every five digits two bignum operations were performed.
2457 */
2458
2459 enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
2460
2461 /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
2462
2463 /* In non ASCII-style encodings the following macro might not work. */
2464 #define XDIGIT2UINT(d) \
2465 (isdigit ((int) (unsigned char) d) \
2466 ? (d) - '0' \
2467 : tolower ((int) (unsigned char) d) - 'a' + 10)
2468
2469 static SCM
2470 mem2uinteger (const char* mem, size_t len, unsigned int *p_idx,
2471 unsigned int radix, enum t_exactness *p_exactness)
2472 {
2473 unsigned int idx = *p_idx;
2474 unsigned int hash_seen = 0;
2475 scm_t_bits shift = 1;
2476 scm_t_bits add = 0;
2477 unsigned int digit_value;
2478 SCM result;
2479 char c;
2480
2481 if (idx == len)
2482 return SCM_BOOL_F;
2483
2484 c = mem[idx];
2485 if (!isxdigit ((int) (unsigned char) c))
2486 return SCM_BOOL_F;
2487 digit_value = XDIGIT2UINT (c);
2488 if (digit_value >= radix)
2489 return SCM_BOOL_F;
2490
2491 idx++;
2492 result = SCM_I_MAKINUM (digit_value);
2493 while (idx != len)
2494 {
2495 char c = mem[idx];
2496 if (isxdigit ((int) (unsigned char) c))
2497 {
2498 if (hash_seen)
2499 break;
2500 digit_value = XDIGIT2UINT (c);
2501 if (digit_value >= radix)
2502 break;
2503 }
2504 else if (c == '#')
2505 {
2506 hash_seen = 1;
2507 digit_value = 0;
2508 }
2509 else
2510 break;
2511
2512 idx++;
2513 if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
2514 {
2515 result = scm_product (result, SCM_I_MAKINUM (shift));
2516 if (add > 0)
2517 result = scm_sum (result, SCM_I_MAKINUM (add));
2518
2519 shift = radix;
2520 add = digit_value;
2521 }
2522 else
2523 {
2524 shift = shift * radix;
2525 add = add * radix + digit_value;
2526 }
2527 };
2528
2529 if (shift > 1)
2530 result = scm_product (result, SCM_I_MAKINUM (shift));
2531 if (add > 0)
2532 result = scm_sum (result, SCM_I_MAKINUM (add));
2533
2534 *p_idx = idx;
2535 if (hash_seen)
2536 *p_exactness = INEXACT;
2537
2538 return result;
2539 }
2540
2541
2542 /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
2543 * covers the parts of the rules that start at a potential point. The value
2544 * of the digits up to the point have been parsed by the caller and are given
2545 * in variable result. The content of *p_exactness indicates, whether a hash
2546 * has already been seen in the digits before the point.
2547 */
2548
2549 /* In non ASCII-style encodings the following macro might not work. */
2550 #define DIGIT2UINT(d) ((d) - '0')
2551
2552 static SCM
2553 mem2decimal_from_point (SCM result, const char* mem, size_t len,
2554 unsigned int *p_idx, enum t_exactness *p_exactness)
2555 {
2556 unsigned int idx = *p_idx;
2557 enum t_exactness x = *p_exactness;
2558
2559 if (idx == len)
2560 return result;
2561
2562 if (mem[idx] == '.')
2563 {
2564 scm_t_bits shift = 1;
2565 scm_t_bits add = 0;
2566 unsigned int digit_value;
2567 SCM big_shift = SCM_I_MAKINUM (1);
2568
2569 idx++;
2570 while (idx != len)
2571 {
2572 char c = mem[idx];
2573 if (isdigit ((int) (unsigned char) c))
2574 {
2575 if (x == INEXACT)
2576 return SCM_BOOL_F;
2577 else
2578 digit_value = DIGIT2UINT (c);
2579 }
2580 else if (c == '#')
2581 {
2582 x = INEXACT;
2583 digit_value = 0;
2584 }
2585 else
2586 break;
2587
2588 idx++;
2589 if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
2590 {
2591 big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
2592 result = scm_product (result, SCM_I_MAKINUM (shift));
2593 if (add > 0)
2594 result = scm_sum (result, SCM_I_MAKINUM (add));
2595
2596 shift = 10;
2597 add = digit_value;
2598 }
2599 else
2600 {
2601 shift = shift * 10;
2602 add = add * 10 + digit_value;
2603 }
2604 };
2605
2606 if (add > 0)
2607 {
2608 big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
2609 result = scm_product (result, SCM_I_MAKINUM (shift));
2610 result = scm_sum (result, SCM_I_MAKINUM (add));
2611 }
2612
2613 result = scm_divide (result, big_shift);
2614
2615 /* We've seen a decimal point, thus the value is implicitly inexact. */
2616 x = INEXACT;
2617 }
2618
2619 if (idx != len)
2620 {
2621 int sign = 1;
2622 unsigned int start;
2623 char c;
2624 int exponent;
2625 SCM e;
2626
2627 /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
2628
2629 switch (mem[idx])
2630 {
2631 case 'd': case 'D':
2632 case 'e': case 'E':
2633 case 'f': case 'F':
2634 case 'l': case 'L':
2635 case 's': case 'S':
2636 idx++;
2637 start = idx;
2638 c = mem[idx];
2639 if (c == '-')
2640 {
2641 idx++;
2642 sign = -1;
2643 c = mem[idx];
2644 }
2645 else if (c == '+')
2646 {
2647 idx++;
2648 sign = 1;
2649 c = mem[idx];
2650 }
2651 else
2652 sign = 1;
2653
2654 if (!isdigit ((int) (unsigned char) c))
2655 return SCM_BOOL_F;
2656
2657 idx++;
2658 exponent = DIGIT2UINT (c);
2659 while (idx != len)
2660 {
2661 char c = mem[idx];
2662 if (isdigit ((int) (unsigned char) c))
2663 {
2664 idx++;
2665 if (exponent <= SCM_MAXEXP)
2666 exponent = exponent * 10 + DIGIT2UINT (c);
2667 }
2668 else
2669 break;
2670 }
2671
2672 if (exponent > SCM_MAXEXP)
2673 {
2674 size_t exp_len = idx - start;
2675 SCM exp_string = scm_from_locale_stringn (&mem[start], exp_len);
2676 SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
2677 scm_out_of_range ("string->number", exp_num);
2678 }
2679
2680 e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent));
2681 if (sign == 1)
2682 result = scm_product (result, e);
2683 else
2684 result = scm_divide2real (result, e);
2685
2686 /* We've seen an exponent, thus the value is implicitly inexact. */
2687 x = INEXACT;
2688
2689 break;
2690
2691 default:
2692 break;
2693 }
2694 }
2695
2696 *p_idx = idx;
2697 if (x == INEXACT)
2698 *p_exactness = x;
2699
2700 return result;
2701 }
2702
2703
2704 /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
2705
2706 static SCM
2707 mem2ureal (const char* mem, size_t len, unsigned int *p_idx,
2708 unsigned int radix, enum t_exactness *p_exactness)
2709 {
2710 unsigned int idx = *p_idx;
2711 SCM result;
2712
2713 if (idx == len)
2714 return SCM_BOOL_F;
2715
2716 if (idx+5 <= len && !strncmp (mem+idx, "inf.0", 5))
2717 {
2718 *p_idx = idx+5;
2719 return scm_inf ();
2720 }
2721
2722 if (idx+4 < len && !strncmp (mem+idx, "nan.", 4))
2723 {
2724 enum t_exactness x = EXACT;
2725
2726 /* Cobble up the fractional part. We might want to set the
2727 NaN's mantissa from it. */
2728 idx += 4;
2729 mem2uinteger (mem, len, &idx, 10, &x);
2730 *p_idx = idx;
2731 return scm_nan ();
2732 }
2733
2734 if (mem[idx] == '.')
2735 {
2736 if (radix != 10)
2737 return SCM_BOOL_F;
2738 else if (idx + 1 == len)
2739 return SCM_BOOL_F;
2740 else if (!isdigit ((int) (unsigned char) mem[idx + 1]))
2741 return SCM_BOOL_F;
2742 else
2743 result = mem2decimal_from_point (SCM_I_MAKINUM (0), mem, len,
2744 p_idx, p_exactness);
2745 }
2746 else
2747 {
2748 enum t_exactness x = EXACT;
2749 SCM uinteger;
2750
2751 uinteger = mem2uinteger (mem, len, &idx, radix, &x);
2752 if (scm_is_false (uinteger))
2753 return SCM_BOOL_F;
2754
2755 if (idx == len)
2756 result = uinteger;
2757 else if (mem[idx] == '/')
2758 {
2759 SCM divisor;
2760
2761 idx++;
2762
2763 divisor = mem2uinteger (mem, len, &idx, radix, &x);
2764 if (scm_is_false (divisor))
2765 return SCM_BOOL_F;
2766
2767 /* both are int/big here, I assume */
2768 result = scm_i_make_ratio (uinteger, divisor);
2769 }
2770 else if (radix == 10)
2771 {
2772 result = mem2decimal_from_point (uinteger, mem, len, &idx, &x);
2773 if (scm_is_false (result))
2774 return SCM_BOOL_F;
2775 }
2776 else
2777 result = uinteger;
2778
2779 *p_idx = idx;
2780 if (x == INEXACT)
2781 *p_exactness = x;
2782 }
2783
2784 /* When returning an inexact zero, make sure it is represented as a
2785 floating point value so that we can change its sign.
2786 */
2787 if (scm_is_eq (result, SCM_I_MAKINUM(0)) && *p_exactness == INEXACT)
2788 result = scm_from_double (0.0);
2789
2790 return result;
2791 }
2792
2793
2794 /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
2795
2796 static SCM
2797 mem2complex (const char* mem, size_t len, unsigned int idx,
2798 unsigned int radix, enum t_exactness *p_exactness)
2799 {
2800 char c;
2801 int sign = 0;
2802 SCM ureal;
2803
2804 if (idx == len)
2805 return SCM_BOOL_F;
2806
2807 c = mem[idx];
2808 if (c == '+')
2809 {
2810 idx++;
2811 sign = 1;
2812 }
2813 else if (c == '-')
2814 {
2815 idx++;
2816 sign = -1;
2817 }
2818
2819 if (idx == len)
2820 return SCM_BOOL_F;
2821
2822 ureal = mem2ureal (mem, len, &idx, radix, p_exactness);
2823 if (scm_is_false (ureal))
2824 {
2825 /* input must be either +i or -i */
2826
2827 if (sign == 0)
2828 return SCM_BOOL_F;
2829
2830 if (mem[idx] == 'i' || mem[idx] == 'I')
2831 {
2832 idx++;
2833 if (idx != len)
2834 return SCM_BOOL_F;
2835
2836 return scm_make_rectangular (SCM_I_MAKINUM (0), SCM_I_MAKINUM (sign));
2837 }
2838 else
2839 return SCM_BOOL_F;
2840 }
2841 else
2842 {
2843 if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
2844 ureal = scm_difference (ureal, SCM_UNDEFINED);
2845
2846 if (idx == len)
2847 return ureal;
2848
2849 c = mem[idx];
2850 switch (c)
2851 {
2852 case 'i': case 'I':
2853 /* either +<ureal>i or -<ureal>i */
2854
2855 idx++;
2856 if (sign == 0)
2857 return SCM_BOOL_F;
2858 if (idx != len)
2859 return SCM_BOOL_F;
2860 return scm_make_rectangular (SCM_I_MAKINUM (0), ureal);
2861
2862 case '@':
2863 /* polar input: <real>@<real>. */
2864
2865 idx++;
2866 if (idx == len)
2867 return SCM_BOOL_F;
2868 else
2869 {
2870 int sign;
2871 SCM angle;
2872 SCM result;
2873
2874 c = mem[idx];
2875 if (c == '+')
2876 {
2877 idx++;
2878 sign = 1;
2879 }
2880 else if (c == '-')
2881 {
2882 idx++;
2883 sign = -1;
2884 }
2885 else
2886 sign = 1;
2887
2888 angle = mem2ureal (mem, len, &idx, radix, p_exactness);
2889 if (scm_is_false (angle))
2890 return SCM_BOOL_F;
2891 if (idx != len)
2892 return SCM_BOOL_F;
2893
2894 if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
2895 angle = scm_difference (angle, SCM_UNDEFINED);
2896
2897 result = scm_make_polar (ureal, angle);
2898 return result;
2899 }
2900 case '+':
2901 case '-':
2902 /* expecting input matching <real>[+-]<ureal>?i */
2903
2904 idx++;
2905 if (idx == len)
2906 return SCM_BOOL_F;
2907 else
2908 {
2909 int sign = (c == '+') ? 1 : -1;
2910 SCM imag = mem2ureal (mem, len, &idx, radix, p_exactness);
2911
2912 if (scm_is_false (imag))
2913 imag = SCM_I_MAKINUM (sign);
2914 else if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
2915 imag = scm_difference (imag, SCM_UNDEFINED);
2916
2917 if (idx == len)
2918 return SCM_BOOL_F;
2919 if (mem[idx] != 'i' && mem[idx] != 'I')
2920 return SCM_BOOL_F;
2921
2922 idx++;
2923 if (idx != len)
2924 return SCM_BOOL_F;
2925
2926 return scm_make_rectangular (ureal, imag);
2927 }
2928 default:
2929 return SCM_BOOL_F;
2930 }
2931 }
2932 }
2933
2934
2935 /* R5RS, section 7.1.1, lexical structure of numbers: <number> */
2936
2937 enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
2938
2939 SCM
2940 scm_i_mem2number (const char* mem, size_t len, unsigned int default_radix)
2941 {
2942 unsigned int idx = 0;
2943 unsigned int radix = NO_RADIX;
2944 enum t_exactness forced_x = NO_EXACTNESS;
2945 enum t_exactness implicit_x = EXACT;
2946 SCM result;
2947
2948 /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
2949 while (idx + 2 < len && mem[idx] == '#')
2950 {
2951 switch (mem[idx + 1])
2952 {
2953 case 'b': case 'B':
2954 if (radix != NO_RADIX)
2955 return SCM_BOOL_F;
2956 radix = DUAL;
2957 break;
2958 case 'd': case 'D':
2959 if (radix != NO_RADIX)
2960 return SCM_BOOL_F;
2961 radix = DEC;
2962 break;
2963 case 'i': case 'I':
2964 if (forced_x != NO_EXACTNESS)
2965 return SCM_BOOL_F;
2966 forced_x = INEXACT;
2967 break;
2968 case 'e': case 'E':
2969 if (forced_x != NO_EXACTNESS)
2970 return SCM_BOOL_F;
2971 forced_x = EXACT;
2972 break;
2973 case 'o': case 'O':
2974 if (radix != NO_RADIX)
2975 return SCM_BOOL_F;
2976 radix = OCT;
2977 break;
2978 case 'x': case 'X':
2979 if (radix != NO_RADIX)
2980 return SCM_BOOL_F;
2981 radix = HEX;
2982 break;
2983 default:
2984 return SCM_BOOL_F;
2985 }
2986 idx += 2;
2987 }
2988
2989 /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
2990 if (radix == NO_RADIX)
2991 result = mem2complex (mem, len, idx, default_radix, &implicit_x);
2992 else
2993 result = mem2complex (mem, len, idx, (unsigned int) radix, &implicit_x);
2994
2995 if (scm_is_false (result))
2996 return SCM_BOOL_F;
2997
2998 switch (forced_x)
2999 {
3000 case EXACT:
3001 if (SCM_INEXACTP (result))
3002 return scm_inexact_to_exact (result);
3003 else
3004 return result;
3005 case INEXACT:
3006 if (SCM_INEXACTP (result))
3007 return result;
3008 else
3009 return scm_exact_to_inexact (result);
3010 case NO_EXACTNESS:
3011 default:
3012 if (implicit_x == INEXACT)
3013 {
3014 if (SCM_INEXACTP (result))
3015 return result;
3016 else
3017 return scm_exact_to_inexact (result);
3018 }
3019 else
3020 return result;
3021 }
3022 }
3023
3024
3025 SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
3026 (SCM string, SCM radix),
3027 "Return a number of the maximally precise representation\n"
3028 "expressed by the given @var{string}. @var{radix} must be an\n"
3029 "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
3030 "is a default radix that may be overridden by an explicit radix\n"
3031 "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
3032 "supplied, then the default radix is 10. If string is not a\n"
3033 "syntactically valid notation for a number, then\n"
3034 "@code{string->number} returns @code{#f}.")
3035 #define FUNC_NAME s_scm_string_to_number
3036 {
3037 SCM answer;
3038 unsigned int base;
3039 SCM_VALIDATE_STRING (1, string);
3040
3041 if (SCM_UNBNDP (radix))
3042 base = 10;
3043 else
3044 base = scm_to_unsigned_integer (radix, 2, INT_MAX);
3045
3046 answer = scm_i_mem2number (scm_i_string_chars (string),
3047 scm_i_string_length (string),
3048 base);
3049 scm_remember_upto_here_1 (string);
3050 return answer;
3051 }
3052 #undef FUNC_NAME
3053
3054
3055 /*** END strs->nums ***/
3056
3057
3058 SCM
3059 scm_bigequal (SCM x, SCM y)
3060 {
3061 int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3062 scm_remember_upto_here_2 (x, y);
3063 return scm_from_bool (0 == result);
3064 }
3065
3066 SCM
3067 scm_real_equalp (SCM x, SCM y)
3068 {
3069 return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
3070 }
3071
3072 SCM
3073 scm_complex_equalp (SCM x, SCM y)
3074 {
3075 return scm_from_bool (SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)
3076 && SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y));
3077 }
3078
3079 SCM
3080 scm_i_fraction_equalp (SCM x, SCM y)
3081 {
3082 scm_i_fraction_reduce (x);
3083 scm_i_fraction_reduce (y);
3084 if (scm_is_false (scm_equal_p (SCM_FRACTION_NUMERATOR (x),
3085 SCM_FRACTION_NUMERATOR (y)))
3086 || scm_is_false (scm_equal_p (SCM_FRACTION_DENOMINATOR (x),
3087 SCM_FRACTION_DENOMINATOR (y))))
3088 return SCM_BOOL_F;
3089 else
3090 return SCM_BOOL_T;
3091 }
3092
3093
3094 SCM_DEFINE (scm_number_p, "number?", 1, 0, 0,
3095 (SCM x),
3096 "Return @code{#t} if @var{x} is a number, @code{#f}\n"
3097 "otherwise.")
3098 #define FUNC_NAME s_scm_number_p
3099 {
3100 return scm_from_bool (SCM_NUMBERP (x));
3101 }
3102 #undef FUNC_NAME
3103
3104 SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0,
3105 (SCM x),
3106 "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
3107 "otherwise. Note that the sets of real, rational and integer\n"
3108 "values form subsets of the set of complex numbers, i. e. the\n"
3109 "predicate will also be fulfilled if @var{x} is a real,\n"
3110 "rational or integer number.")
3111 #define FUNC_NAME s_scm_complex_p
3112 {
3113 /* all numbers are complex. */
3114 return scm_number_p (x);
3115 }
3116 #undef FUNC_NAME
3117
3118 SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
3119 (SCM x),
3120 "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
3121 "otherwise. Note that the set of integer values forms a subset of\n"
3122 "the set of real numbers, i. e. the predicate will also be\n"
3123 "fulfilled if @var{x} is an integer number.")
3124 #define FUNC_NAME s_scm_real_p
3125 {
3126 /* we can't represent irrational numbers. */
3127 return scm_rational_p (x);
3128 }
3129 #undef FUNC_NAME
3130
3131 SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
3132 (SCM x),
3133 "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
3134 "otherwise. Note that the set of integer values forms a subset of\n"
3135 "the set of rational numbers, i. e. the predicate will also be\n"
3136 "fulfilled if @var{x} is an integer number.")
3137 #define FUNC_NAME s_scm_rational_p
3138 {
3139 if (SCM_I_INUMP (x))
3140 return SCM_BOOL_T;
3141 else if (SCM_IMP (x))
3142 return SCM_BOOL_F;
3143 else if (SCM_BIGP (x))
3144 return SCM_BOOL_T;
3145 else if (SCM_FRACTIONP (x))
3146 return SCM_BOOL_T;
3147 else if (SCM_REALP (x))
3148 /* due to their limited precision, all floating point numbers are
3149 rational as well. */
3150 return SCM_BOOL_T;
3151 else
3152 return SCM_BOOL_F;
3153 }
3154 #undef FUNC_NAME
3155
3156 SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
3157 (SCM x),
3158 "Return @code{#t} if @var{x} is an integer number, @code{#f}\n"
3159 "else.")
3160 #define FUNC_NAME s_scm_integer_p
3161 {
3162 double r;
3163 if (SCM_I_INUMP (x))
3164 return SCM_BOOL_T;
3165 if (SCM_IMP (x))
3166 return SCM_BOOL_F;
3167 if (SCM_BIGP (x))
3168 return SCM_BOOL_T;
3169 if (!SCM_INEXACTP (x))
3170 return SCM_BOOL_F;
3171 if (SCM_COMPLEXP (x))
3172 return SCM_BOOL_F;
3173 r = SCM_REAL_VALUE (x);
3174 /* +/-inf passes r==floor(r), making those #t */
3175 if (r == floor (r))
3176 return SCM_BOOL_T;
3177 return SCM_BOOL_F;
3178 }
3179 #undef FUNC_NAME
3180
3181
3182 SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0,
3183 (SCM x),
3184 "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
3185 "else.")
3186 #define FUNC_NAME s_scm_inexact_p
3187 {
3188 if (SCM_INEXACTP (x))
3189 return SCM_BOOL_T;
3190 if (SCM_NUMBERP (x))
3191 return SCM_BOOL_F;
3192 SCM_WRONG_TYPE_ARG (1, x);
3193 }
3194 #undef FUNC_NAME
3195
3196
3197 SCM_GPROC1 (s_eq_p, "=", scm_tc7_rpsubr, scm_num_eq_p, g_eq_p);
3198 /* "Return @code{#t} if all parameters are numerically equal." */
3199 SCM
3200 scm_num_eq_p (SCM x, SCM y)
3201 {
3202 again:
3203 if (SCM_I_INUMP (x))
3204 {
3205 long xx = SCM_I_INUM (x);
3206 if (SCM_I_INUMP (y))
3207 {
3208 long yy = SCM_I_INUM (y);
3209 return scm_from_bool (xx == yy);
3210 }
3211 else if (SCM_BIGP (y))
3212 return SCM_BOOL_F;
3213 else if (SCM_REALP (y))
3214 {
3215 /* On a 32-bit system an inum fits a double, we can cast the inum
3216 to a double and compare.
3217
3218 But on a 64-bit system an inum is bigger than a double and
3219 casting it to a double (call that dxx) will round. dxx is at
3220 worst 1 bigger or smaller than xx, so if dxx==yy we know yy is
3221 an integer and fits a long. So we cast yy to a long and
3222 compare with plain xx.
3223
3224 An alternative (for any size system actually) would be to check
3225 yy is an integer (with floor) and is in range of an inum
3226 (compare against appropriate powers of 2) then test
3227 xx==(long)yy. It's just a matter of which casts/comparisons
3228 might be fastest or easiest for the cpu. */
3229
3230 double yy = SCM_REAL_VALUE (y);
3231 return SCM_BOOL ((double) xx == yy
3232 && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
3233 || xx == (long) yy));
3234 }
3235 else if (SCM_COMPLEXP (y))
3236 return scm_from_bool (((double) xx == SCM_COMPLEX_REAL (y))
3237 && (0.0 == SCM_COMPLEX_IMAG (y)));
3238 else if (SCM_FRACTIONP (y))
3239 return SCM_BOOL_F;
3240 else
3241 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3242 }
3243 else if (SCM_BIGP (x))
3244 {
3245 if (SCM_I_INUMP (y))
3246 return SCM_BOOL_F;
3247 else if (SCM_BIGP (y))
3248 {
3249 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3250 scm_remember_upto_here_2 (x, y);
3251 return scm_from_bool (0 == cmp);
3252 }
3253 else if (SCM_REALP (y))
3254 {
3255 int cmp;
3256 if (xisnan (SCM_REAL_VALUE (y)))
3257 return SCM_BOOL_F;
3258 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
3259 scm_remember_upto_here_1 (x);
3260 return scm_from_bool (0 == cmp);
3261 }
3262 else if (SCM_COMPLEXP (y))
3263 {
3264 int cmp;
3265 if (0.0 != SCM_COMPLEX_IMAG (y))
3266 return SCM_BOOL_F;
3267 if (xisnan (SCM_COMPLEX_REAL (y)))
3268 return SCM_BOOL_F;
3269 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y));
3270 scm_remember_upto_here_1 (x);
3271 return scm_from_bool (0 == cmp);
3272 }
3273 else if (SCM_FRACTIONP (y))
3274 return SCM_BOOL_F;
3275 else
3276 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3277 }
3278 else if (SCM_REALP (x))
3279 {
3280 double xx = SCM_REAL_VALUE (x);
3281 if (SCM_I_INUMP (y))
3282 {
3283 /* see comments with inum/real above */
3284 long yy = SCM_I_INUM (y);
3285 return SCM_BOOL (xx == (double) yy
3286 && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
3287 || (long) xx == yy));
3288 }
3289 else if (SCM_BIGP (y))
3290 {
3291 int cmp;
3292 if (xisnan (SCM_REAL_VALUE (x)))
3293 return SCM_BOOL_F;
3294 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
3295 scm_remember_upto_here_1 (y);
3296 return scm_from_bool (0 == cmp);
3297 }
3298 else if (SCM_REALP (y))
3299 return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
3300 else if (SCM_COMPLEXP (y))
3301 return scm_from_bool ((SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y))
3302 && (0.0 == SCM_COMPLEX_IMAG (y)));
3303 else if (SCM_FRACTIONP (y))
3304 {
3305 double xx = SCM_REAL_VALUE (x);
3306 if (xisnan (xx))
3307 return SCM_BOOL_F;
3308 if (xisinf (xx))
3309 return scm_from_bool (xx < 0.0);
3310 x = scm_inexact_to_exact (x); /* with x as frac or int */
3311 goto again;
3312 }
3313 else
3314 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3315 }
3316 else if (SCM_COMPLEXP (x))
3317 {
3318 if (SCM_I_INUMP (y))
3319 return scm_from_bool ((SCM_COMPLEX_REAL (x) == (double) SCM_I_INUM (y))
3320 && (SCM_COMPLEX_IMAG (x) == 0.0));
3321 else if (SCM_BIGP (y))
3322 {
3323 int cmp;
3324 if (0.0 != SCM_COMPLEX_IMAG (x))
3325 return SCM_BOOL_F;
3326 if (xisnan (SCM_COMPLEX_REAL (x)))
3327 return SCM_BOOL_F;
3328 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x));
3329 scm_remember_upto_here_1 (y);
3330 return scm_from_bool (0 == cmp);
3331 }
3332 else if (SCM_REALP (y))
3333 return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
3334 && (SCM_COMPLEX_IMAG (x) == 0.0));
3335 else if (SCM_COMPLEXP (y))
3336 return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
3337 && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
3338 else if (SCM_FRACTIONP (y))
3339 {
3340 double xx;
3341 if (SCM_COMPLEX_IMAG (x) != 0.0)
3342 return SCM_BOOL_F;
3343 xx = SCM_COMPLEX_REAL (x);
3344 if (xisnan (xx))
3345 return SCM_BOOL_F;
3346 if (xisinf (xx))
3347 return scm_from_bool (xx < 0.0);
3348 x = scm_inexact_to_exact (x); /* with x as frac or int */
3349 goto again;
3350 }
3351 else
3352 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3353 }
3354 else if (SCM_FRACTIONP (x))
3355 {
3356 if (SCM_I_INUMP (y))
3357 return SCM_BOOL_F;
3358 else if (SCM_BIGP (y))
3359 return SCM_BOOL_F;
3360 else if (SCM_REALP (y))
3361 {
3362 double yy = SCM_REAL_VALUE (y);
3363 if (xisnan (yy))
3364 return SCM_BOOL_F;
3365 if (xisinf (yy))
3366 return scm_from_bool (0.0 < yy);
3367 y = scm_inexact_to_exact (y); /* with y as frac or int */
3368 goto again;
3369 }
3370 else if (SCM_COMPLEXP (y))
3371 {
3372 double yy;
3373 if (SCM_COMPLEX_IMAG (y) != 0.0)
3374 return SCM_BOOL_F;
3375 yy = SCM_COMPLEX_REAL (y);
3376 if (xisnan (yy))
3377 return SCM_BOOL_F;
3378 if (xisinf (yy))
3379 return scm_from_bool (0.0 < yy);
3380 y = scm_inexact_to_exact (y); /* with y as frac or int */
3381 goto again;
3382 }
3383 else if (SCM_FRACTIONP (y))
3384 return scm_i_fraction_equalp (x, y);
3385 else
3386 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
3387 }
3388 else
3389 SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARG1, s_eq_p);
3390 }
3391
3392
3393 /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
3394 done are good for inums, but for bignums an answer can almost always be
3395 had by just examining a few high bits of the operands, as done by GMP in
3396 mpq_cmp. flonum/frac compares likewise, but with the slight complication
3397 of the float exponent to take into account. */
3398
3399 SCM_GPROC1 (s_less_p, "<", scm_tc7_rpsubr, scm_less_p, g_less_p);
3400 /* "Return @code{#t} if the list of parameters is monotonically\n"
3401 * "increasing."
3402 */
3403 SCM
3404 scm_less_p (SCM x, SCM y)
3405 {
3406 again:
3407 if (SCM_I_INUMP (x))
3408 {
3409 long xx = SCM_I_INUM (x);
3410 if (SCM_I_INUMP (y))
3411 {
3412 long yy = SCM_I_INUM (y);
3413 return scm_from_bool (xx < yy);
3414 }
3415 else if (SCM_BIGP (y))
3416 {
3417 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
3418 scm_remember_upto_here_1 (y);
3419 return scm_from_bool (sgn > 0);
3420 }
3421 else if (SCM_REALP (y))
3422 return scm_from_bool ((double) xx < SCM_REAL_VALUE (y));
3423 else if (SCM_FRACTIONP (y))
3424 {
3425 /* "x < a/b" becomes "x*b < a" */
3426 int_frac:
3427 x = scm_product (x, SCM_FRACTION_DENOMINATOR (y));
3428 y = SCM_FRACTION_NUMERATOR (y);
3429 goto again;
3430 }
3431 else
3432 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3433 }
3434 else if (SCM_BIGP (x))
3435 {
3436 if (SCM_I_INUMP (y))
3437 {
3438 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3439 scm_remember_upto_here_1 (x);
3440 return scm_from_bool (sgn < 0);
3441 }
3442 else if (SCM_BIGP (y))
3443 {
3444 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3445 scm_remember_upto_here_2 (x, y);
3446 return scm_from_bool (cmp < 0);
3447 }
3448 else if (SCM_REALP (y))
3449 {
3450 int cmp;
3451 if (xisnan (SCM_REAL_VALUE (y)))
3452 return SCM_BOOL_F;
3453 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
3454 scm_remember_upto_here_1 (x);
3455 return scm_from_bool (cmp < 0);
3456 }
3457 else if (SCM_FRACTIONP (y))
3458 goto int_frac;
3459 else
3460 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3461 }
3462 else if (SCM_REALP (x))
3463 {
3464 if (SCM_I_INUMP (y))
3465 return scm_from_bool (SCM_REAL_VALUE (x) < (double) SCM_I_INUM (y));
3466 else if (SCM_BIGP (y))
3467 {
3468 int cmp;
3469 if (xisnan (SCM_REAL_VALUE (x)))
3470 return SCM_BOOL_F;
3471 cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
3472 scm_remember_upto_here_1 (y);
3473 return scm_from_bool (cmp > 0);
3474 }
3475 else if (SCM_REALP (y))
3476 return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
3477 else if (SCM_FRACTIONP (y))
3478 {
3479 double xx = SCM_REAL_VALUE (x);
3480 if (xisnan (xx))
3481 return SCM_BOOL_F;
3482 if (xisinf (xx))
3483 return scm_from_bool (xx < 0.0);
3484 x = scm_inexact_to_exact (x); /* with x as frac or int */
3485 goto again;
3486 }
3487 else
3488 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3489 }
3490 else if (SCM_FRACTIONP (x))
3491 {
3492 if (SCM_I_INUMP (y) || SCM_BIGP (y))
3493 {
3494 /* "a/b < y" becomes "a < y*b" */
3495 y = scm_product (y, SCM_FRACTION_DENOMINATOR (x));
3496 x = SCM_FRACTION_NUMERATOR (x);
3497 goto again;
3498 }
3499 else if (SCM_REALP (y))
3500 {
3501 double yy = SCM_REAL_VALUE (y);
3502 if (xisnan (yy))
3503 return SCM_BOOL_F;
3504 if (xisinf (yy))
3505 return scm_from_bool (0.0 < yy);
3506 y = scm_inexact_to_exact (y); /* with y as frac or int */
3507 goto again;
3508 }
3509 else if (SCM_FRACTIONP (y))
3510 {
3511 /* "a/b < c/d" becomes "a*d < c*b" */
3512 SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x),
3513 SCM_FRACTION_DENOMINATOR (y));
3514 SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y),
3515 SCM_FRACTION_DENOMINATOR (x));
3516 x = new_x;
3517 y = new_y;
3518 goto again;
3519 }
3520 else
3521 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
3522 }
3523 else
3524 SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARG1, s_less_p);
3525 }
3526
3527
3528 SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p);
3529 /* "Return @code{#t} if the list of parameters is monotonically\n"
3530 * "decreasing."
3531 */
3532 #define FUNC_NAME s_scm_gr_p
3533 SCM
3534 scm_gr_p (SCM x, SCM y)
3535 {
3536 if (!SCM_NUMBERP (x))
3537 SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG1, FUNC_NAME);
3538 else if (!SCM_NUMBERP (y))
3539 SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG2, FUNC_NAME);
3540 else
3541 return scm_less_p (y, x);
3542 }
3543 #undef FUNC_NAME
3544
3545
3546 SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p);
3547 /* "Return @code{#t} if the list of parameters is monotonically\n"
3548 * "non-decreasing."
3549 */
3550 #define FUNC_NAME s_scm_leq_p
3551 SCM
3552 scm_leq_p (SCM x, SCM y)
3553 {
3554 if (!SCM_NUMBERP (x))
3555 SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG1, FUNC_NAME);
3556 else if (!SCM_NUMBERP (y))
3557 SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG2, FUNC_NAME);
3558 else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
3559 return SCM_BOOL_F;
3560 else
3561 return scm_not (scm_less_p (y, x));
3562 }
3563 #undef FUNC_NAME
3564
3565
3566 SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p);
3567 /* "Return @code{#t} if the list of parameters is monotonically\n"
3568 * "non-increasing."
3569 */
3570 #define FUNC_NAME s_scm_geq_p
3571 SCM
3572 scm_geq_p (SCM x, SCM y)
3573 {
3574 if (!SCM_NUMBERP (x))
3575 SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG1, FUNC_NAME);
3576 else if (!SCM_NUMBERP (y))
3577 SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG2, FUNC_NAME);
3578 else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
3579 return SCM_BOOL_F;
3580 else
3581 return scm_not (scm_less_p (x, y));
3582 }
3583 #undef FUNC_NAME
3584
3585
3586 SCM_GPROC (s_zero_p, "zero?", 1, 0, 0, scm_zero_p, g_zero_p);
3587 /* "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
3588 * "zero."
3589 */
3590 SCM
3591 scm_zero_p (SCM z)
3592 {
3593 if (SCM_I_INUMP (z))
3594 return scm_from_bool (scm_is_eq (z, SCM_INUM0));
3595 else if (SCM_BIGP (z))
3596 return SCM_BOOL_F;
3597 else if (SCM_REALP (z))
3598 return scm_from_bool (SCM_REAL_VALUE (z) == 0.0);
3599 else if (SCM_COMPLEXP (z))
3600 return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0
3601 && SCM_COMPLEX_IMAG (z) == 0.0);
3602 else if (SCM_FRACTIONP (z))
3603 return SCM_BOOL_F;
3604 else
3605 SCM_WTA_DISPATCH_1 (g_zero_p, z, SCM_ARG1, s_zero_p);
3606 }
3607
3608
3609 SCM_GPROC (s_positive_p, "positive?", 1, 0, 0, scm_positive_p, g_positive_p);
3610 /* "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
3611 * "zero."
3612 */
3613 SCM
3614 scm_positive_p (SCM x)
3615 {
3616 if (SCM_I_INUMP (x))
3617 return scm_from_bool (SCM_I_INUM (x) > 0);
3618 else if (SCM_BIGP (x))
3619 {
3620 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3621 scm_remember_upto_here_1 (x);
3622 return scm_from_bool (sgn > 0);
3623 }
3624 else if (SCM_REALP (x))
3625 return scm_from_bool(SCM_REAL_VALUE (x) > 0.0);
3626 else if (SCM_FRACTIONP (x))
3627 return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
3628 else
3629 SCM_WTA_DISPATCH_1 (g_positive_p, x, SCM_ARG1, s_positive_p);
3630 }
3631
3632
3633 SCM_GPROC (s_negative_p, "negative?", 1, 0, 0, scm_negative_p, g_negative_p);
3634 /* "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
3635 * "zero."
3636 */
3637 SCM
3638 scm_negative_p (SCM x)
3639 {
3640 if (SCM_I_INUMP (x))
3641 return scm_from_bool (SCM_I_INUM (x) < 0);
3642 else if (SCM_BIGP (x))
3643 {
3644 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3645 scm_remember_upto_here_1 (x);
3646 return scm_from_bool (sgn < 0);
3647 }
3648 else if (SCM_REALP (x))
3649 return scm_from_bool(SCM_REAL_VALUE (x) < 0.0);
3650 else if (SCM_FRACTIONP (x))
3651 return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
3652 else
3653 SCM_WTA_DISPATCH_1 (g_negative_p, x, SCM_ARG1, s_negative_p);
3654 }
3655
3656
3657 /* scm_min and scm_max return an inexact when either argument is inexact, as
3658 required by r5rs. On that basis, for exact/inexact combinations the
3659 exact is converted to inexact to compare and possibly return. This is
3660 unlike scm_less_p above which takes some trouble to preserve all bits in
3661 its test, such trouble is not required for min and max. */
3662
3663 SCM_GPROC1 (s_max, "max", scm_tc7_asubr, scm_max, g_max);
3664 /* "Return the maximum of all parameter values."
3665 */
3666 SCM
3667 scm_max (SCM x, SCM y)
3668 {
3669 if (SCM_UNBNDP (y))
3670 {
3671 if (SCM_UNBNDP (x))
3672 SCM_WTA_DISPATCH_0 (g_max, s_max);
3673 else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
3674 return x;
3675 else
3676 SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max);
3677 }
3678
3679 if (SCM_I_INUMP (x))
3680 {
3681 long xx = SCM_I_INUM (x);
3682 if (SCM_I_INUMP (y))
3683 {
3684 long yy = SCM_I_INUM (y);
3685 return (xx < yy) ? y : x;
3686 }
3687 else if (SCM_BIGP (y))
3688 {
3689 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
3690 scm_remember_upto_here_1 (y);
3691 return (sgn < 0) ? x : y;
3692 }
3693 else if (SCM_REALP (y))
3694 {
3695 double z = xx;
3696 /* if y==NaN then ">" is false and we return NaN */
3697 return (z > SCM_REAL_VALUE (y)) ? scm_from_double (z) : y;
3698 }
3699 else if (SCM_FRACTIONP (y))
3700 {
3701 use_less:
3702 return (scm_is_false (scm_less_p (x, y)) ? x : y);
3703 }
3704 else
3705 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3706 }
3707 else if (SCM_BIGP (x))
3708 {
3709 if (SCM_I_INUMP (y))
3710 {
3711 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3712 scm_remember_upto_here_1 (x);
3713 return (sgn < 0) ? y : x;
3714 }
3715 else if (SCM_BIGP (y))
3716 {
3717 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3718 scm_remember_upto_here_2 (x, y);
3719 return (cmp > 0) ? x : y;
3720 }
3721 else if (SCM_REALP (y))
3722 {
3723 /* if y==NaN then xx>yy is false, so we return the NaN y */
3724 double xx, yy;
3725 big_real:
3726 xx = scm_i_big2dbl (x);
3727 yy = SCM_REAL_VALUE (y);
3728 return (xx > yy ? scm_from_double (xx) : y);
3729 }
3730 else if (SCM_FRACTIONP (y))
3731 {
3732 goto use_less;
3733 }
3734 else
3735 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3736 }
3737 else if (SCM_REALP (x))
3738 {
3739 if (SCM_I_INUMP (y))
3740 {
3741 double z = SCM_I_INUM (y);
3742 /* if x==NaN then "<" is false and we return NaN */
3743 return (SCM_REAL_VALUE (x) < z) ? scm_from_double (z) : x;
3744 }
3745 else if (SCM_BIGP (y))
3746 {
3747 SCM_SWAP (x, y);
3748 goto big_real;
3749 }
3750 else if (SCM_REALP (y))
3751 {
3752 /* if x==NaN then our explicit check means we return NaN
3753 if y==NaN then ">" is false and we return NaN
3754 calling isnan is unavoidable, since it's the only way to know
3755 which of x or y causes any compares to be false */
3756 double xx = SCM_REAL_VALUE (x);
3757 return (xisnan (xx) || xx > SCM_REAL_VALUE (y)) ? x : y;
3758 }
3759 else if (SCM_FRACTIONP (y))
3760 {
3761 double yy = scm_i_fraction2double (y);
3762 double xx = SCM_REAL_VALUE (x);
3763 return (xx < yy) ? scm_from_double (yy) : x;
3764 }
3765 else
3766 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3767 }
3768 else if (SCM_FRACTIONP (x))
3769 {
3770 if (SCM_I_INUMP (y))
3771 {
3772 goto use_less;
3773 }
3774 else if (SCM_BIGP (y))
3775 {
3776 goto use_less;
3777 }
3778 else if (SCM_REALP (y))
3779 {
3780 double xx = scm_i_fraction2double (x);
3781 return (xx < SCM_REAL_VALUE (y)) ? y : scm_from_double (xx);
3782 }
3783 else if (SCM_FRACTIONP (y))
3784 {
3785 goto use_less;
3786 }
3787 else
3788 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3789 }
3790 else
3791 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max);
3792 }
3793
3794
3795 SCM_GPROC1 (s_min, "min", scm_tc7_asubr, scm_min, g_min);
3796 /* "Return the minium of all parameter values."
3797 */
3798 SCM
3799 scm_min (SCM x, SCM y)
3800 {
3801 if (SCM_UNBNDP (y))
3802 {
3803 if (SCM_UNBNDP (x))
3804 SCM_WTA_DISPATCH_0 (g_min, s_min);
3805 else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
3806 return x;
3807 else
3808 SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min);
3809 }
3810
3811 if (SCM_I_INUMP (x))
3812 {
3813 long xx = SCM_I_INUM (x);
3814 if (SCM_I_INUMP (y))
3815 {
3816 long yy = SCM_I_INUM (y);
3817 return (xx < yy) ? x : y;
3818 }
3819 else if (SCM_BIGP (y))
3820 {
3821 int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
3822 scm_remember_upto_here_1 (y);
3823 return (sgn < 0) ? y : x;
3824 }
3825 else if (SCM_REALP (y))
3826 {
3827 double z = xx;
3828 /* if y==NaN then "<" is false and we return NaN */
3829 return (z < SCM_REAL_VALUE (y)) ? scm_from_double (z) : y;
3830 }
3831 else if (SCM_FRACTIONP (y))
3832 {
3833 use_less:
3834 return (scm_is_false (scm_less_p (x, y)) ? y : x);
3835 }
3836 else
3837 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
3838 }
3839 else if (SCM_BIGP (x))
3840 {
3841 if (SCM_I_INUMP (y))
3842 {
3843 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3844 scm_remember_upto_here_1 (x);
3845 return (sgn < 0) ? x : y;
3846 }
3847 else if (SCM_BIGP (y))
3848 {
3849 int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
3850 scm_remember_upto_here_2 (x, y);
3851 return (cmp > 0) ? y : x;
3852 }
3853 else if (SCM_REALP (y))
3854 {
3855 /* if y==NaN then xx<yy is false, so we return the NaN y */
3856 double xx, yy;
3857 big_real:
3858 xx = scm_i_big2dbl (x);
3859 yy = SCM_REAL_VALUE (y);
3860 return (xx < yy ? scm_from_double (xx) : y);
3861 }
3862 else if (SCM_FRACTIONP (y))
3863 {
3864 goto use_less;
3865 }
3866 else
3867 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
3868 }
3869 else if (SCM_REALP (x))
3870 {
3871 if (SCM_I_INUMP (y))
3872 {
3873 double z = SCM_I_INUM (y);
3874 /* if x==NaN then "<" is false and we return NaN */
3875 return (z < SCM_REAL_VALUE (x)) ? scm_from_double (z) : x;
3876 }
3877 else if (SCM_BIGP (y))
3878 {
3879 SCM_SWAP (x, y);
3880 goto big_real;
3881 }
3882 else if (SCM_REALP (y))
3883 {
3884 /* if x==NaN then our explicit check means we return NaN
3885 if y==NaN then "<" is false and we return NaN
3886 calling isnan is unavoidable, since it's the only way to know
3887 which of x or y causes any compares to be false */
3888 double xx = SCM_REAL_VALUE (x);
3889 return (xisnan (xx) || xx < SCM_REAL_VALUE (y)) ? x : y;
3890 }
3891 else if (SCM_FRACTIONP (y))
3892 {
3893 double yy = scm_i_fraction2double (y);
3894 double xx = SCM_REAL_VALUE (x);
3895 return (yy < xx) ? scm_from_double (yy) : x;
3896 }
3897 else
3898 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
3899 }
3900 else if (SCM_FRACTIONP (x))
3901 {
3902 if (SCM_I_INUMP (y))
3903 {
3904 goto use_less;
3905 }
3906 else if (SCM_BIGP (y))
3907 {
3908 goto use_less;
3909 }
3910 else if (SCM_REALP (y))
3911 {
3912 double xx = scm_i_fraction2double (x);
3913 return (SCM_REAL_VALUE (y) < xx) ? y : scm_from_double (xx);
3914 }
3915 else if (SCM_FRACTIONP (y))
3916 {
3917 goto use_less;
3918 }
3919 else
3920 SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
3921 }
3922 else
3923 SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min);
3924 }
3925
3926
3927 SCM_GPROC1 (s_sum, "+", scm_tc7_asubr, scm_sum, g_sum);
3928 /* "Return the sum of all parameter values. Return 0 if called without\n"
3929 * "any parameters."
3930 */
3931 SCM
3932 scm_sum (SCM x, SCM y)
3933 {
3934 if (SCM_UNBNDP (y))
3935 {
3936 if (SCM_NUMBERP (x)) return x;
3937 if (SCM_UNBNDP (x)) return SCM_INUM0;
3938 SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum);
3939 }
3940
3941 if (SCM_I_INUMP (x))
3942 {
3943 if (SCM_I_INUMP (y))
3944 {
3945 long xx = SCM_I_INUM (x);
3946 long yy = SCM_I_INUM (y);
3947 long int z = xx + yy;
3948 return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_long2big (z);
3949 }
3950 else if (SCM_BIGP (y))
3951 {
3952 SCM_SWAP (x, y);
3953 goto add_big_inum;
3954 }
3955 else if (SCM_REALP (y))
3956 {
3957 long int xx = SCM_I_INUM (x);
3958 return scm_from_double (xx + SCM_REAL_VALUE (y));
3959 }
3960 else if (SCM_COMPLEXP (y))
3961 {
3962 long int xx = SCM_I_INUM (x);
3963 return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y),
3964 SCM_COMPLEX_IMAG (y));
3965 }
3966 else if (SCM_FRACTIONP (y))
3967 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
3968 scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
3969 SCM_FRACTION_DENOMINATOR (y));
3970 else
3971 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
3972 } else if (SCM_BIGP (x))
3973 {
3974 if (SCM_I_INUMP (y))
3975 {
3976 long int inum;
3977 int bigsgn;
3978 add_big_inum:
3979 inum = SCM_I_INUM (y);
3980 if (inum == 0)
3981 return x;
3982 bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x));
3983 if (inum < 0)
3984 {
3985 SCM result = scm_i_mkbig ();
3986 mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum);
3987 scm_remember_upto_here_1 (x);
3988 /* we know the result will have to be a bignum */
3989 if (bigsgn == -1)
3990 return result;
3991 return scm_i_normbig (result);
3992 }
3993 else
3994 {
3995 SCM result = scm_i_mkbig ();
3996 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum);
3997 scm_remember_upto_here_1 (x);
3998 /* we know the result will have to be a bignum */
3999 if (bigsgn == 1)
4000 return result;
4001 return scm_i_normbig (result);
4002 }
4003 }
4004 else if (SCM_BIGP (y))
4005 {
4006 SCM result = scm_i_mkbig ();
4007 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
4008 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
4009 mpz_add (SCM_I_BIG_MPZ (result),
4010 SCM_I_BIG_MPZ (x),
4011 SCM_I_BIG_MPZ (y));
4012 scm_remember_upto_here_2 (x, y);
4013 /* we know the result will have to be a bignum */
4014 if (sgn_x == sgn_y)
4015 return result;
4016 return scm_i_normbig (result);
4017 }
4018 else if (SCM_REALP (y))
4019 {
4020 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y);
4021 scm_remember_upto_here_1 (x);
4022 return scm_from_double (result);
4023 }
4024 else if (SCM_COMPLEXP (y))
4025 {
4026 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
4027 + SCM_COMPLEX_REAL (y));
4028 scm_remember_upto_here_1 (x);
4029 return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
4030 }
4031 else if (SCM_FRACTIONP (y))
4032 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
4033 scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
4034 SCM_FRACTION_DENOMINATOR (y));
4035 else
4036 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
4037 }
4038 else if (SCM_REALP (x))
4039 {
4040 if (SCM_I_INUMP (y))
4041 return scm_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y));
4042 else if (SCM_BIGP (y))
4043 {
4044 double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x);
4045 scm_remember_upto_here_1 (y);
4046 return scm_from_double (result);
4047 }
4048 else if (SCM_REALP (y))
4049 return scm_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
4050 else if (SCM_COMPLEXP (y))
4051 return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
4052 SCM_COMPLEX_IMAG (y));
4053 else if (SCM_FRACTIONP (y))
4054 return scm_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
4055 else
4056 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
4057 }
4058 else if (SCM_COMPLEXP (x))
4059 {
4060 if (SCM_I_INUMP (y))
4061 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y),
4062 SCM_COMPLEX_IMAG (x));
4063 else if (SCM_BIGP (y))
4064 {
4065 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y))
4066 + SCM_COMPLEX_REAL (x));
4067 scm_remember_upto_here_1 (y);
4068 return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x));
4069 }
4070 else if (SCM_REALP (y))
4071 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
4072 SCM_COMPLEX_IMAG (x));
4073 else if (SCM_COMPLEXP (y))
4074 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
4075 SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
4076 else if (SCM_FRACTIONP (y))
4077 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
4078 SCM_COMPLEX_IMAG (x));
4079 else
4080 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
4081 }
4082 else if (SCM_FRACTIONP (x))
4083 {
4084 if (SCM_I_INUMP (y))
4085 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
4086 scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
4087 SCM_FRACTION_DENOMINATOR (x));
4088 else if (SCM_BIGP (y))
4089 return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
4090 scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
4091 SCM_FRACTION_DENOMINATOR (x));
4092 else if (SCM_REALP (y))
4093 return scm_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x));
4094 else if (SCM_COMPLEXP (y))
4095 return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x),
4096 SCM_COMPLEX_IMAG (y));
4097 else if (SCM_FRACTIONP (y))
4098 /* a/b + c/d = (ad + bc) / bd */
4099 return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
4100 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
4101 scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
4102 else
4103 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
4104 }
4105 else
4106 SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum);
4107 }
4108
4109
4110 SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0,
4111 (SCM x),
4112 "Return @math{@var{x}+1}.")
4113 #define FUNC_NAME s_scm_oneplus
4114 {
4115 return scm_sum (x, SCM_I_MAKINUM (1));
4116 }
4117 #undef FUNC_NAME
4118
4119
4120 SCM_GPROC1 (s_difference, "-", scm_tc7_asubr, scm_difference, g_difference);
4121 /* If called with one argument @var{z1}, -@var{z1} returned. Otherwise
4122 * the sum of all but the first argument are subtracted from the first
4123 * argument. */
4124 #define FUNC_NAME s_difference
4125 SCM
4126 scm_difference (SCM x, SCM y)
4127 {
4128 if (SCM_UNBNDP (y))
4129 {
4130 if (SCM_UNBNDP (x))
4131 SCM_WTA_DISPATCH_0 (g_difference, s_difference);
4132 else
4133 if (SCM_I_INUMP (x))
4134 {
4135 long xx = -SCM_I_INUM (x);
4136 if (SCM_FIXABLE (xx))
4137 return SCM_I_MAKINUM (xx);
4138 else
4139 return scm_i_long2big (xx);
4140 }
4141 else if (SCM_BIGP (x))
4142 /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
4143 bignum, but negating that gives a fixnum. */
4144 return scm_i_normbig (scm_i_clonebig (x, 0));
4145 else if (SCM_REALP (x))
4146 return scm_from_double (-SCM_REAL_VALUE (x));
4147 else if (SCM_COMPLEXP (x))
4148 return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x),
4149 -SCM_COMPLEX_IMAG (x));
4150 else if (SCM_FRACTIONP (x))
4151 return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
4152 SCM_FRACTION_DENOMINATOR (x));
4153 else
4154 SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference);
4155 }
4156
4157 if (SCM_I_INUMP (x))
4158 {
4159 if (SCM_I_INUMP (y))
4160 {
4161 long int xx = SCM_I_INUM (x);
4162 long int yy = SCM_I_INUM (y);
4163 long int z = xx - yy;
4164 if (SCM_FIXABLE (z))
4165 return SCM_I_MAKINUM (z);
4166 else
4167 return scm_i_long2big (z);
4168 }
4169 else if (SCM_BIGP (y))
4170 {
4171 /* inum-x - big-y */
4172 long xx = SCM_I_INUM (x);
4173
4174 if (xx == 0)
4175 return scm_i_clonebig (y, 0);
4176 else
4177 {
4178 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
4179 SCM result = scm_i_mkbig ();
4180
4181 if (xx >= 0)
4182 mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y));
4183 else
4184 {
4185 /* x - y == -(y + -x) */
4186 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx);
4187 mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
4188 }
4189 scm_remember_upto_here_1 (y);
4190
4191 if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0))
4192 /* we know the result will have to be a bignum */
4193 return result;
4194 else
4195 return scm_i_normbig (result);
4196 }
4197 }
4198 else if (SCM_REALP (y))
4199 {
4200 long int xx = SCM_I_INUM (x);
4201 return scm_from_double (xx - SCM_REAL_VALUE (y));
4202 }
4203 else if (SCM_COMPLEXP (y))
4204 {
4205 long int xx = SCM_I_INUM (x);
4206 return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y),
4207 - SCM_COMPLEX_IMAG (y));
4208 }
4209 else if (SCM_FRACTIONP (y))
4210 /* a - b/c = (ac - b) / c */
4211 return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4212 SCM_FRACTION_NUMERATOR (y)),
4213 SCM_FRACTION_DENOMINATOR (y));
4214 else
4215 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4216 }
4217 else if (SCM_BIGP (x))
4218 {
4219 if (SCM_I_INUMP (y))
4220 {
4221 /* big-x - inum-y */
4222 long yy = SCM_I_INUM (y);
4223 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
4224
4225 scm_remember_upto_here_1 (x);
4226 if (sgn_x == 0)
4227 return (SCM_FIXABLE (-yy) ?
4228 SCM_I_MAKINUM (-yy) : scm_from_long (-yy));
4229 else
4230 {
4231 SCM result = scm_i_mkbig ();
4232
4233 if (yy >= 0)
4234 mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
4235 else
4236 mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy);
4237 scm_remember_upto_here_1 (x);
4238
4239 if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0))
4240 /* we know the result will have to be a bignum */
4241 return result;
4242 else
4243 return scm_i_normbig (result);
4244 }
4245 }
4246 else if (SCM_BIGP (y))
4247 {
4248 int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
4249 int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
4250 SCM result = scm_i_mkbig ();
4251 mpz_sub (SCM_I_BIG_MPZ (result),
4252 SCM_I_BIG_MPZ (x),
4253 SCM_I_BIG_MPZ (y));
4254 scm_remember_upto_here_2 (x, y);
4255 /* we know the result will have to be a bignum */
4256 if ((sgn_x == 1) && (sgn_y == -1))
4257 return result;
4258 if ((sgn_x == -1) && (sgn_y == 1))
4259 return result;
4260 return scm_i_normbig (result);
4261 }
4262 else if (SCM_REALP (y))
4263 {
4264 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y);
4265 scm_remember_upto_here_1 (x);
4266 return scm_from_double (result);
4267 }
4268 else if (SCM_COMPLEXP (y))
4269 {
4270 double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
4271 - SCM_COMPLEX_REAL (y));
4272 scm_remember_upto_here_1 (x);
4273 return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y));
4274 }
4275 else if (SCM_FRACTIONP (y))
4276 return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4277 SCM_FRACTION_NUMERATOR (y)),
4278 SCM_FRACTION_DENOMINATOR (y));
4279 else SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4280 }
4281 else if (SCM_REALP (x))
4282 {
4283 if (SCM_I_INUMP (y))
4284 return scm_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y));
4285 else if (SCM_BIGP (y))
4286 {
4287 double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y));
4288 scm_remember_upto_here_1 (x);
4289 return scm_from_double (result);
4290 }
4291 else if (SCM_REALP (y))
4292 return scm_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
4293 else if (SCM_COMPLEXP (y))
4294 return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
4295 -SCM_COMPLEX_IMAG (y));
4296 else if (SCM_FRACTIONP (y))
4297 return scm_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y));
4298 else
4299 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4300 }
4301 else if (SCM_COMPLEXP (x))
4302 {
4303 if (SCM_I_INUMP (y))
4304 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y),
4305 SCM_COMPLEX_IMAG (x));
4306 else if (SCM_BIGP (y))
4307 {
4308 double real_part = (SCM_COMPLEX_REAL (x)
4309 - mpz_get_d (SCM_I_BIG_MPZ (y)));
4310 scm_remember_upto_here_1 (x);
4311 return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
4312 }
4313 else if (SCM_REALP (y))
4314 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
4315 SCM_COMPLEX_IMAG (x));
4316 else if (SCM_COMPLEXP (y))
4317 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
4318 SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
4319 else if (SCM_FRACTIONP (y))
4320 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y),
4321 SCM_COMPLEX_IMAG (x));
4322 else
4323 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4324 }
4325 else if (SCM_FRACTIONP (x))
4326 {
4327 if (SCM_I_INUMP (y))
4328 /* a/b - c = (a - cb) / b */
4329 return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
4330 scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
4331 SCM_FRACTION_DENOMINATOR (x));
4332 else if (SCM_BIGP (y))
4333 return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
4334 scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
4335 SCM_FRACTION_DENOMINATOR (x));
4336 else if (SCM_REALP (y))
4337 return scm_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y));
4338 else if (SCM_COMPLEXP (y))
4339 return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y),
4340 -SCM_COMPLEX_IMAG (y));
4341 else if (SCM_FRACTIONP (y))
4342 /* a/b - c/d = (ad - bc) / bd */
4343 return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
4344 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
4345 scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
4346 else
4347 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
4348 }
4349 else
4350 SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference);
4351 }
4352 #undef FUNC_NAME
4353
4354
4355 SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0,
4356 (SCM x),
4357 "Return @math{@var{x}-1}.")
4358 #define FUNC_NAME s_scm_oneminus
4359 {
4360 return scm_difference (x, SCM_I_MAKINUM (1));
4361 }
4362 #undef FUNC_NAME
4363
4364
4365 SCM_GPROC1 (s_product, "*", scm_tc7_asubr, scm_product, g_product);
4366 /* "Return the product of all arguments. If called without arguments,\n"
4367 * "1 is returned."
4368 */
4369 SCM
4370 scm_product (SCM x, SCM y)
4371 {
4372 if (SCM_UNBNDP (y))
4373 {
4374 if (SCM_UNBNDP (x))
4375 return SCM_I_MAKINUM (1L);
4376 else if (SCM_NUMBERP (x))
4377 return x;
4378 else
4379 SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product);
4380 }
4381
4382 if (SCM_I_INUMP (x))
4383 {
4384 long xx;
4385
4386 intbig:
4387 xx = SCM_I_INUM (x);
4388
4389 switch (xx)
4390 {
4391 case 0: return x; break;
4392 case 1: return y; break;
4393 }
4394
4395 if (SCM_I_INUMP (y))
4396 {
4397 long yy = SCM_I_INUM (y);
4398 long kk = xx * yy;
4399 SCM k = SCM_I_MAKINUM (kk);
4400 if ((kk == SCM_I_INUM (k)) && (kk / xx == yy))
4401 return k;
4402 else
4403 {
4404 SCM result = scm_i_long2big (xx);
4405 mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy);
4406 return scm_i_normbig (result);
4407 }
4408 }
4409 else if (SCM_BIGP (y))
4410 {
4411 SCM result = scm_i_mkbig ();
4412 mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx);
4413 scm_remember_upto_here_1 (y);
4414 return result;
4415 }
4416 else if (SCM_REALP (y))
4417 return scm_from_double (xx * SCM_REAL_VALUE (y));
4418 else if (SCM_COMPLEXP (y))
4419 return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
4420 xx * SCM_COMPLEX_IMAG (y));
4421 else if (SCM_FRACTIONP (y))
4422 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
4423 SCM_FRACTION_DENOMINATOR (y));
4424 else
4425 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4426 }
4427 else if (SCM_BIGP (x))
4428 {
4429 if (SCM_I_INUMP (y))
4430 {
4431 SCM_SWAP (x, y);
4432 goto intbig;
4433 }
4434 else if (SCM_BIGP (y))
4435 {
4436 SCM result = scm_i_mkbig ();
4437 mpz_mul (SCM_I_BIG_MPZ (result),
4438 SCM_I_BIG_MPZ (x),
4439 SCM_I_BIG_MPZ (y));
4440 scm_remember_upto_here_2 (x, y);
4441 return result;
4442 }
4443 else if (SCM_REALP (y))
4444 {
4445 double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y);
4446 scm_remember_upto_here_1 (x);
4447 return scm_from_double (result);
4448 }
4449 else if (SCM_COMPLEXP (y))
4450 {
4451 double z = mpz_get_d (SCM_I_BIG_MPZ (x));
4452 scm_remember_upto_here_1 (x);
4453 return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y),
4454 z * SCM_COMPLEX_IMAG (y));
4455 }
4456 else if (SCM_FRACTIONP (y))
4457 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
4458 SCM_FRACTION_DENOMINATOR (y));
4459 else
4460 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4461 }
4462 else if (SCM_REALP (x))
4463 {
4464 if (SCM_I_INUMP (y))
4465 return scm_from_double (SCM_I_INUM (y) * SCM_REAL_VALUE (x));
4466 else if (SCM_BIGP (y))
4467 {
4468 double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x);
4469 scm_remember_upto_here_1 (y);
4470 return scm_from_double (result);
4471 }
4472 else if (SCM_REALP (y))
4473 return scm_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
4474 else if (SCM_COMPLEXP (y))
4475 return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
4476 SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
4477 else if (SCM_FRACTIONP (y))
4478 return scm_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
4479 else
4480 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4481 }
4482 else if (SCM_COMPLEXP (x))
4483 {
4484 if (SCM_I_INUMP (y))
4485 return scm_c_make_rectangular (SCM_I_INUM (y) * SCM_COMPLEX_REAL (x),
4486 SCM_I_INUM (y) * SCM_COMPLEX_IMAG (x));
4487 else if (SCM_BIGP (y))
4488 {
4489 double z = mpz_get_d (SCM_I_BIG_MPZ (y));
4490 scm_remember_upto_here_1 (y);
4491 return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x),
4492 z * SCM_COMPLEX_IMAG (x));
4493 }
4494 else if (SCM_REALP (y))
4495 return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
4496 SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
4497 else if (SCM_COMPLEXP (y))
4498 {
4499 return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
4500 - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
4501 SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
4502 + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
4503 }
4504 else if (SCM_FRACTIONP (y))
4505 {
4506 double yy = scm_i_fraction2double (y);
4507 return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x),
4508 yy * SCM_COMPLEX_IMAG (x));
4509 }
4510 else
4511 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4512 }
4513 else if (SCM_FRACTIONP (x))
4514 {
4515 if (SCM_I_INUMP (y))
4516 return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
4517 SCM_FRACTION_DENOMINATOR (x));
4518 else if (SCM_BIGP (y))
4519 return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
4520 SCM_FRACTION_DENOMINATOR (x));
4521 else if (SCM_REALP (y))
4522 return scm_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y));
4523 else if (SCM_COMPLEXP (y))
4524 {
4525 double xx = scm_i_fraction2double (x);
4526 return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
4527 xx * SCM_COMPLEX_IMAG (y));
4528 }
4529 else if (SCM_FRACTIONP (y))
4530 /* a/b * c/d = ac / bd */
4531 return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
4532 SCM_FRACTION_NUMERATOR (y)),
4533 scm_product (SCM_FRACTION_DENOMINATOR (x),
4534 SCM_FRACTION_DENOMINATOR (y)));
4535 else
4536 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
4537 }
4538 else
4539 SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product);
4540 }
4541
4542 #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \
4543 || (defined (HAVE_FINITE) && defined (HAVE_ISNAN)))
4544 #define ALLOW_DIVIDE_BY_ZERO
4545 /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */
4546 #endif
4547
4548 /* The code below for complex division is adapted from the GNU
4549 libstdc++, which adapted it from f2c's libF77, and is subject to
4550 this copyright: */
4551
4552 /****************************************************************
4553 Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
4554
4555 Permission to use, copy, modify, and distribute this software
4556 and its documentation for any purpose and without fee is hereby
4557 granted, provided that the above copyright notice appear in all
4558 copies and that both that the copyright notice and this
4559 permission notice and warranty disclaimer appear in supporting
4560 documentation, and that the names of AT&T Bell Laboratories or
4561 Bellcore or any of their entities not be used in advertising or
4562 publicity pertaining to distribution of the software without
4563 specific, written prior permission.
4564
4565 AT&T and Bellcore disclaim all warranties with regard to this
4566 software, including all implied warranties of merchantability
4567 and fitness. In no event shall AT&T or Bellcore be liable for
4568 any special, indirect or consequential damages or any damages
4569 whatsoever resulting from loss of use, data or profits, whether
4570 in an action of contract, negligence or other tortious action,
4571 arising out of or in connection with the use or performance of
4572 this software.
4573 ****************************************************************/
4574
4575 SCM_GPROC1 (s_divide, "/", scm_tc7_asubr, scm_divide, g_divide);
4576 /* Divide the first argument by the product of the remaining
4577 arguments. If called with one argument @var{z1}, 1/@var{z1} is
4578 returned. */
4579 #define FUNC_NAME s_divide
4580 static SCM
4581 scm_i_divide (SCM x, SCM y, int inexact)
4582 {
4583 double a;
4584
4585 if (SCM_UNBNDP (y))
4586 {
4587 if (SCM_UNBNDP (x))
4588 SCM_WTA_DISPATCH_0 (g_divide, s_divide);
4589 else if (SCM_I_INUMP (x))
4590 {
4591 long xx = SCM_I_INUM (x);
4592 if (xx == 1 || xx == -1)
4593 return x;
4594 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4595 else if (xx == 0)
4596 scm_num_overflow (s_divide);
4597 #endif
4598 else
4599 {
4600 if (inexact)
4601 return scm_from_double (1.0 / (double) xx);
4602 else return scm_i_make_ratio (SCM_I_MAKINUM(1), x);
4603 }
4604 }
4605 else if (SCM_BIGP (x))
4606 {
4607 if (inexact)
4608 return scm_from_double (1.0 / scm_i_big2dbl (x));
4609 else return scm_i_make_ratio (SCM_I_MAKINUM(1), x);
4610 }
4611 else if (SCM_REALP (x))
4612 {
4613 double xx = SCM_REAL_VALUE (x);
4614 #ifndef ALLOW_DIVIDE_BY_ZERO
4615 if (xx == 0.0)
4616 scm_num_overflow (s_divide);
4617 else
4618 #endif
4619 return scm_from_double (1.0 / xx);
4620 }
4621 else if (SCM_COMPLEXP (x))
4622 {
4623 double r = SCM_COMPLEX_REAL (x);
4624 double i = SCM_COMPLEX_IMAG (x);
4625 if (r <= i)
4626 {
4627 double t = r / i;
4628 double d = i * (1.0 + t * t);
4629 return scm_c_make_rectangular (t / d, -1.0 / d);
4630 }
4631 else
4632 {
4633 double t = i / r;
4634 double d = r * (1.0 + t * t);
4635 return scm_c_make_rectangular (1.0 / d, -t / d);
4636 }
4637 }
4638 else if (SCM_FRACTIONP (x))
4639 return scm_i_make_ratio (SCM_FRACTION_DENOMINATOR (x),
4640 SCM_FRACTION_NUMERATOR (x));
4641 else
4642 SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide);
4643 }
4644
4645 if (SCM_I_INUMP (x))
4646 {
4647 long xx = SCM_I_INUM (x);
4648 if (SCM_I_INUMP (y))
4649 {
4650 long yy = SCM_I_INUM (y);
4651 if (yy == 0)
4652 {
4653 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4654 scm_num_overflow (s_divide);
4655 #else
4656 return scm_from_double ((double) xx / (double) yy);
4657 #endif
4658 }
4659 else if (xx % yy != 0)
4660 {
4661 if (inexact)
4662 return scm_from_double ((double) xx / (double) yy);
4663 else return scm_i_make_ratio (x, y);
4664 }
4665 else
4666 {
4667 long z = xx / yy;
4668 if (SCM_FIXABLE (z))
4669 return SCM_I_MAKINUM (z);
4670 else
4671 return scm_i_long2big (z);
4672 }
4673 }
4674 else if (SCM_BIGP (y))
4675 {
4676 if (inexact)
4677 return scm_from_double ((double) xx / scm_i_big2dbl (y));
4678 else return scm_i_make_ratio (x, y);
4679 }
4680 else if (SCM_REALP (y))
4681 {
4682 double yy = SCM_REAL_VALUE (y);
4683 #ifndef ALLOW_DIVIDE_BY_ZERO
4684 if (yy == 0.0)
4685 scm_num_overflow (s_divide);
4686 else
4687 #endif
4688 return scm_from_double ((double) xx / yy);
4689 }
4690 else if (SCM_COMPLEXP (y))
4691 {
4692 a = xx;
4693 complex_div: /* y _must_ be a complex number */
4694 {
4695 double r = SCM_COMPLEX_REAL (y);
4696 double i = SCM_COMPLEX_IMAG (y);
4697 if (r <= i)
4698 {
4699 double t = r / i;
4700 double d = i * (1.0 + t * t);
4701 return scm_c_make_rectangular ((a * t) / d, -a / d);
4702 }
4703 else
4704 {
4705 double t = i / r;
4706 double d = r * (1.0 + t * t);
4707 return scm_c_make_rectangular (a / d, -(a * t) / d);
4708 }
4709 }
4710 }
4711 else if (SCM_FRACTIONP (y))
4712 /* a / b/c = ac / b */
4713 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4714 SCM_FRACTION_NUMERATOR (y));
4715 else
4716 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4717 }
4718 else if (SCM_BIGP (x))
4719 {
4720 if (SCM_I_INUMP (y))
4721 {
4722 long int yy = SCM_I_INUM (y);
4723 if (yy == 0)
4724 {
4725 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4726 scm_num_overflow (s_divide);
4727 #else
4728 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
4729 scm_remember_upto_here_1 (x);
4730 return (sgn == 0) ? scm_nan () : scm_inf ();
4731 #endif
4732 }
4733 else if (yy == 1)
4734 return x;
4735 else
4736 {
4737 /* FIXME: HMM, what are the relative performance issues here?
4738 We need to test. Is it faster on average to test
4739 divisible_p, then perform whichever operation, or is it
4740 faster to perform the integer div opportunistically and
4741 switch to real if there's a remainder? For now we take the
4742 middle ground: test, then if divisible, use the faster div
4743 func. */
4744
4745 long abs_yy = yy < 0 ? -yy : yy;
4746 int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy);
4747
4748 if (divisible_p)
4749 {
4750 SCM result = scm_i_mkbig ();
4751 mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy);
4752 scm_remember_upto_here_1 (x);
4753 if (yy < 0)
4754 mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
4755 return scm_i_normbig (result);
4756 }
4757 else
4758 {
4759 if (inexact)
4760 return scm_from_double (scm_i_big2dbl (x) / (double) yy);
4761 else return scm_i_make_ratio (x, y);
4762 }
4763 }
4764 }
4765 else if (SCM_BIGP (y))
4766 {
4767 int y_is_zero = (mpz_sgn (SCM_I_BIG_MPZ (y)) == 0);
4768 if (y_is_zero)
4769 {
4770 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4771 scm_num_overflow (s_divide);
4772 #else
4773 int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
4774 scm_remember_upto_here_1 (x);
4775 return (sgn == 0) ? scm_nan () : scm_inf ();
4776 #endif
4777 }
4778 else
4779 {
4780 /* big_x / big_y */
4781 int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x),
4782 SCM_I_BIG_MPZ (y));
4783 if (divisible_p)
4784 {
4785 SCM result = scm_i_mkbig ();
4786 mpz_divexact (SCM_I_BIG_MPZ (result),
4787 SCM_I_BIG_MPZ (x),
4788 SCM_I_BIG_MPZ (y));
4789 scm_remember_upto_here_2 (x, y);
4790 return scm_i_normbig (result);
4791 }
4792 else
4793 {
4794 if (inexact)
4795 {
4796 double dbx = mpz_get_d (SCM_I_BIG_MPZ (x));
4797 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
4798 scm_remember_upto_here_2 (x, y);
4799 return scm_from_double (dbx / dby);
4800 }
4801 else return scm_i_make_ratio (x, y);
4802 }
4803 }
4804 }
4805 else if (SCM_REALP (y))
4806 {
4807 double yy = SCM_REAL_VALUE (y);
4808 #ifndef ALLOW_DIVIDE_BY_ZERO
4809 if (yy == 0.0)
4810 scm_num_overflow (s_divide);
4811 else
4812 #endif
4813 return scm_from_double (scm_i_big2dbl (x) / yy);
4814 }
4815 else if (SCM_COMPLEXP (y))
4816 {
4817 a = scm_i_big2dbl (x);
4818 goto complex_div;
4819 }
4820 else if (SCM_FRACTIONP (y))
4821 return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
4822 SCM_FRACTION_NUMERATOR (y));
4823 else
4824 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4825 }
4826 else if (SCM_REALP (x))
4827 {
4828 double rx = SCM_REAL_VALUE (x);
4829 if (SCM_I_INUMP (y))
4830 {
4831 long int yy = SCM_I_INUM (y);
4832 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4833 if (yy == 0)
4834 scm_num_overflow (s_divide);
4835 else
4836 #endif
4837 return scm_from_double (rx / (double) yy);
4838 }
4839 else if (SCM_BIGP (y))
4840 {
4841 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
4842 scm_remember_upto_here_1 (y);
4843 return scm_from_double (rx / dby);
4844 }
4845 else if (SCM_REALP (y))
4846 {
4847 double yy = SCM_REAL_VALUE (y);
4848 #ifndef ALLOW_DIVIDE_BY_ZERO
4849 if (yy == 0.0)
4850 scm_num_overflow (s_divide);
4851 else
4852 #endif
4853 return scm_from_double (rx / yy);
4854 }
4855 else if (SCM_COMPLEXP (y))
4856 {
4857 a = rx;
4858 goto complex_div;
4859 }
4860 else if (SCM_FRACTIONP (y))
4861 return scm_from_double (rx / scm_i_fraction2double (y));
4862 else
4863 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4864 }
4865 else if (SCM_COMPLEXP (x))
4866 {
4867 double rx = SCM_COMPLEX_REAL (x);
4868 double ix = SCM_COMPLEX_IMAG (x);
4869 if (SCM_I_INUMP (y))
4870 {
4871 long int yy = SCM_I_INUM (y);
4872 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4873 if (yy == 0)
4874 scm_num_overflow (s_divide);
4875 else
4876 #endif
4877 {
4878 double d = yy;
4879 return scm_c_make_rectangular (rx / d, ix / d);
4880 }
4881 }
4882 else if (SCM_BIGP (y))
4883 {
4884 double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
4885 scm_remember_upto_here_1 (y);
4886 return scm_c_make_rectangular (rx / dby, ix / dby);
4887 }
4888 else if (SCM_REALP (y))
4889 {
4890 double yy = SCM_REAL_VALUE (y);
4891 #ifndef ALLOW_DIVIDE_BY_ZERO
4892 if (yy == 0.0)
4893 scm_num_overflow (s_divide);
4894 else
4895 #endif
4896 return scm_c_make_rectangular (rx / yy, ix / yy);
4897 }
4898 else if (SCM_COMPLEXP (y))
4899 {
4900 double ry = SCM_COMPLEX_REAL (y);
4901 double iy = SCM_COMPLEX_IMAG (y);
4902 if (ry <= iy)
4903 {
4904 double t = ry / iy;
4905 double d = iy * (1.0 + t * t);
4906 return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d);
4907 }
4908 else
4909 {
4910 double t = iy / ry;
4911 double d = ry * (1.0 + t * t);
4912 return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d);
4913 }
4914 }
4915 else if (SCM_FRACTIONP (y))
4916 {
4917 double yy = scm_i_fraction2double (y);
4918 return scm_c_make_rectangular (rx / yy, ix / yy);
4919 }
4920 else
4921 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4922 }
4923 else if (SCM_FRACTIONP (x))
4924 {
4925 if (SCM_I_INUMP (y))
4926 {
4927 long int yy = SCM_I_INUM (y);
4928 #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
4929 if (yy == 0)
4930 scm_num_overflow (s_divide);
4931 else
4932 #endif
4933 return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
4934 scm_product (SCM_FRACTION_DENOMINATOR (x), y));
4935 }
4936 else if (SCM_BIGP (y))
4937 {
4938 return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
4939 scm_product (SCM_FRACTION_DENOMINATOR (x), y));
4940 }
4941 else if (SCM_REALP (y))
4942 {
4943 double yy = SCM_REAL_VALUE (y);
4944 #ifndef ALLOW_DIVIDE_BY_ZERO
4945 if (yy == 0.0)
4946 scm_num_overflow (s_divide);
4947 else
4948 #endif
4949 return scm_from_double (scm_i_fraction2double (x) / yy);
4950 }
4951 else if (SCM_COMPLEXP (y))
4952 {
4953 a = scm_i_fraction2double (x);
4954 goto complex_div;
4955 }
4956 else if (SCM_FRACTIONP (y))
4957 return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
4958 scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x)));
4959 else
4960 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
4961 }
4962 else
4963 SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide);
4964 }
4965
4966 SCM
4967 scm_divide (SCM x, SCM y)
4968 {
4969 return scm_i_divide (x, y, 0);
4970 }
4971
4972 static SCM scm_divide2real (SCM x, SCM y)
4973 {
4974 return scm_i_divide (x, y, 1);
4975 }
4976 #undef FUNC_NAME
4977
4978
4979 double
4980 scm_asinh (double x)
4981 {
4982 #if HAVE_ASINH
4983 return asinh (x);
4984 #else
4985 #define asinh scm_asinh
4986 return log (x + sqrt (x * x + 1));
4987 #endif
4988 }
4989 SCM_GPROC1 (s_asinh, "$asinh", scm_tc7_dsubr, (SCM (*)()) asinh, g_asinh);
4990 /* "Return the inverse hyperbolic sine of @var{x}."
4991 */
4992
4993
4994 double
4995 scm_acosh (double x)
4996 {
4997 #if HAVE_ACOSH
4998 return acosh (x);
4999 #else
5000 #define acosh scm_acosh
5001 return log (x + sqrt (x * x - 1));
5002 #endif
5003 }
5004 SCM_GPROC1 (s_acosh, "$acosh", scm_tc7_dsubr, (SCM (*)()) acosh, g_acosh);
5005 /* "Return the inverse hyperbolic cosine of @var{x}."
5006 */
5007
5008
5009 double
5010 scm_atanh (double x)
5011 {
5012 #if HAVE_ATANH
5013 return atanh (x);
5014 #else
5015 #define atanh scm_atanh
5016 return 0.5 * log ((1 + x) / (1 - x));
5017 #endif
5018 }
5019 SCM_GPROC1 (s_atanh, "$atanh", scm_tc7_dsubr, (SCM (*)()) atanh, g_atanh);
5020 /* "Return the inverse hyperbolic tangent of @var{x}."
5021 */
5022
5023
5024 double
5025 scm_c_truncate (double x)
5026 {
5027 #if HAVE_TRUNC
5028 return trunc (x);
5029 #else
5030 if (x < 0.0)
5031 return -floor (-x);
5032 return floor (x);
5033 #endif
5034 }
5035
5036 /* scm_c_round is done using floor(x+0.5) to round to nearest and with
5037 half-way case (ie. when x is an integer plus 0.5) going upwards.
5038 Then half-way cases are identified and adjusted down if the
5039 round-upwards didn't give the desired even integer.
5040
5041 "plus_half == result" identifies a half-way case. If plus_half, which is
5042 x + 0.5, is an integer then x must be an integer plus 0.5.
5043
5044 An odd "result" value is identified with result/2 != floor(result/2).
5045 This is done with plus_half, since that value is ready for use sooner in
5046 a pipelined cpu, and we're already requiring plus_half == result.
5047
5048 Note however that we need to be careful when x is big and already an
5049 integer. In that case "x+0.5" may round to an adjacent integer, causing
5050 us to return such a value, incorrectly. For instance if the hardware is
5051 in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
5052 (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
5053 returned. Or if the hardware is in round-upwards mode, then other bigger
5054 values like say x == 2^128 will see x+0.5 rounding up to the next higher
5055 representable value, 2^128+2^76 (or whatever), again incorrect.
5056
5057 These bad roundings of x+0.5 are avoided by testing at the start whether
5058 x is already an integer. If it is then clearly that's the desired result
5059 already. And if it's not then the exponent must be small enough to allow
5060 an 0.5 to be represented, and hence added without a bad rounding. */
5061
5062 double
5063 scm_c_round (double x)
5064 {
5065 double plus_half, result;
5066
5067 if (x == floor (x))
5068 return x;
5069
5070 plus_half = x + 0.5;
5071 result = floor (plus_half);
5072 /* Adjust so that the rounding is towards even. */
5073 return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
5074 ? result - 1
5075 : result);
5076 }
5077
5078 SCM_DEFINE (scm_truncate_number, "truncate", 1, 0, 0,
5079 (SCM x),
5080 "Round the number @var{x} towards zero.")
5081 #define FUNC_NAME s_scm_truncate_number
5082 {
5083 if (scm_is_false (scm_negative_p (x)))
5084 return scm_floor (x);
5085 else
5086 return scm_ceiling (x);
5087 }
5088 #undef FUNC_NAME
5089
5090 static SCM exactly_one_half;
5091
5092 SCM_DEFINE (scm_round_number, "round", 1, 0, 0,
5093 (SCM x),
5094 "Round the number @var{x} towards the nearest integer. "
5095 "When it is exactly halfway between two integers, "
5096 "round towards the even one.")
5097 #define FUNC_NAME s_scm_round_number
5098 {
5099 if (SCM_I_INUMP (x) || SCM_BIGP (x))
5100 return x;
5101 else if (SCM_REALP (x))
5102 return scm_from_double (scm_c_round (SCM_REAL_VALUE (x)));
5103 else
5104 {
5105 /* OPTIMIZE-ME: Fraction case could be done more efficiently by a
5106 single quotient+remainder division then examining to see which way
5107 the rounding should go. */
5108 SCM plus_half = scm_sum (x, exactly_one_half);
5109 SCM result = scm_floor (plus_half);
5110 /* Adjust so that the rounding is towards even. */
5111 if (scm_is_true (scm_num_eq_p (plus_half, result))
5112 && scm_is_true (scm_odd_p (result)))
5113 return scm_difference (result, SCM_I_MAKINUM (1));
5114 else
5115 return result;
5116 }
5117 }
5118 #undef FUNC_NAME
5119
5120 SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
5121 (SCM x),
5122 "Round the number @var{x} towards minus infinity.")
5123 #define FUNC_NAME s_scm_floor
5124 {
5125 if (SCM_I_INUMP (x) || SCM_BIGP (x))
5126 return x;
5127 else if (SCM_REALP (x))
5128 return scm_from_double (floor (SCM_REAL_VALUE (x)));
5129 else if (SCM_FRACTIONP (x))
5130 {
5131 SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
5132 SCM_FRACTION_DENOMINATOR (x));
5133 if (scm_is_false (scm_negative_p (x)))
5134 {
5135 /* For positive x, rounding towards zero is correct. */
5136 return q;
5137 }
5138 else
5139 {
5140 /* For negative x, we need to return q-1 unless x is an
5141 integer. But fractions are never integer, per our
5142 assumptions. */
5143 return scm_difference (q, SCM_I_MAKINUM (1));
5144 }
5145 }
5146 else
5147 SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor);
5148 }
5149 #undef FUNC_NAME
5150
5151 SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
5152 (SCM x),
5153 "Round the number @var{x} towards infinity.")
5154 #define FUNC_NAME s_scm_ceiling
5155 {
5156 if (SCM_I_INUMP (x) || SCM_BIGP (x))
5157 return x;
5158 else if (SCM_REALP (x))
5159 return scm_from_double (ceil (SCM_REAL_VALUE (x)));
5160 else if (SCM_FRACTIONP (x))
5161 {
5162 SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
5163 SCM_FRACTION_DENOMINATOR (x));
5164 if (scm_is_false (scm_positive_p (x)))
5165 {
5166 /* For negative x, rounding towards zero is correct. */
5167 return q;
5168 }
5169 else
5170 {
5171 /* For positive x, we need to return q+1 unless x is an
5172 integer. But fractions are never integer, per our
5173 assumptions. */
5174 return scm_sum (q, SCM_I_MAKINUM (1));
5175 }
5176 }
5177 else
5178 SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
5179 }
5180 #undef FUNC_NAME
5181
5182 SCM_GPROC1 (s_i_sqrt, "$sqrt", scm_tc7_dsubr, (SCM (*)()) sqrt, g_i_sqrt);
5183 /* "Return the square root of the real number @var{x}."
5184 */
5185 SCM_GPROC1 (s_i_abs, "$abs", scm_tc7_dsubr, (SCM (*)()) fabs, g_i_abs);
5186 /* "Return the absolute value of the real number @var{x}."
5187 */
5188 SCM_GPROC1 (s_i_exp, "$exp", scm_tc7_dsubr, (SCM (*)()) exp, g_i_exp);
5189 /* "Return the @var{x}th power of e."
5190 */
5191 SCM_GPROC1 (s_i_log, "$log", scm_tc7_dsubr, (SCM (*)()) log, g_i_log);
5192 /* "Return the natural logarithm of the real number @var{x}."
5193 */
5194 SCM_GPROC1 (s_i_sin, "$sin", scm_tc7_dsubr, (SCM (*)()) sin, g_i_sin);
5195 /* "Return the sine of the real number @var{x}."
5196 */
5197 SCM_GPROC1 (s_i_cos, "$cos", scm_tc7_dsubr, (SCM (*)()) cos, g_i_cos);
5198 /* "Return the cosine of the real number @var{x}."
5199 */
5200 SCM_GPROC1 (s_i_tan, "$tan", scm_tc7_dsubr, (SCM (*)()) tan, g_i_tan);
5201 /* "Return the tangent of the real number @var{x}."
5202 */
5203 SCM_GPROC1 (s_i_asin, "$asin", scm_tc7_dsubr, (SCM (*)()) asin, g_i_asin);
5204 /* "Return the arc sine of the real number @var{x}."
5205 */
5206 SCM_GPROC1 (s_i_acos, "$acos", scm_tc7_dsubr, (SCM (*)()) acos, g_i_acos);
5207 /* "Return the arc cosine of the real number @var{x}."
5208 */
5209 SCM_GPROC1 (s_i_atan, "$atan", scm_tc7_dsubr, (SCM (*)()) atan, g_i_atan);
5210 /* "Return the arc tangent of the real number @var{x}."
5211 */
5212 SCM_GPROC1 (s_i_sinh, "$sinh", scm_tc7_dsubr, (SCM (*)()) sinh, g_i_sinh);
5213 /* "Return the hyperbolic sine of the real number @var{x}."
5214 */
5215 SCM_GPROC1 (s_i_cosh, "$cosh", scm_tc7_dsubr, (SCM (*)()) cosh, g_i_cosh);
5216 /* "Return the hyperbolic cosine of the real number @var{x}."
5217 */
5218 SCM_GPROC1 (s_i_tanh, "$tanh", scm_tc7_dsubr, (SCM (*)()) tanh, g_i_tanh);
5219 /* "Return the hyperbolic tangent of the real number @var{x}."
5220 */
5221
5222 struct dpair
5223 {
5224 double x, y;
5225 };
5226
5227 static void scm_two_doubles (SCM x,
5228 SCM y,
5229 const char *sstring,
5230 struct dpair * xy);
5231
5232 static void
5233 scm_two_doubles (SCM x, SCM y, const char *sstring, struct dpair *xy)
5234 {
5235 if (SCM_I_INUMP (x))
5236 xy->x = SCM_I_INUM (x);
5237 else if (SCM_BIGP (x))
5238 xy->x = scm_i_big2dbl (x);
5239 else if (SCM_REALP (x))
5240 xy->x = SCM_REAL_VALUE (x);
5241 else if (SCM_FRACTIONP (x))
5242 xy->x = scm_i_fraction2double (x);
5243 else
5244 scm_wrong_type_arg (sstring, SCM_ARG1, x);
5245
5246 if (SCM_I_INUMP (y))
5247 xy->y = SCM_I_INUM (y);
5248 else if (SCM_BIGP (y))
5249 xy->y = scm_i_big2dbl (y);
5250 else if (SCM_REALP (y))
5251 xy->y = SCM_REAL_VALUE (y);
5252 else if (SCM_FRACTIONP (y))
5253 xy->y = scm_i_fraction2double (y);
5254 else
5255 scm_wrong_type_arg (sstring, SCM_ARG2, y);
5256 }
5257
5258
5259 SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
5260 (SCM x, SCM y),
5261 "Return @var{x} raised to the power of @var{y}. This\n"
5262 "procedure does not accept complex arguments.")
5263 #define FUNC_NAME s_scm_sys_expt
5264 {
5265 struct dpair xy;
5266 scm_two_doubles (x, y, FUNC_NAME, &xy);
5267 return scm_from_double (pow (xy.x, xy.y));
5268 }
5269 #undef FUNC_NAME
5270
5271
5272 SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
5273 (SCM x, SCM y),
5274 "Return the arc tangent of the two arguments @var{x} and\n"
5275 "@var{y}. This is similar to calculating the arc tangent of\n"
5276 "@var{x} / @var{y}, except that the signs of both arguments\n"
5277 "are used to determine the quadrant of the result. This\n"
5278 "procedure does not accept complex arguments.")
5279 #define FUNC_NAME s_scm_sys_atan2
5280 {
5281 struct dpair xy;
5282 scm_two_doubles (x, y, FUNC_NAME, &xy);
5283 return scm_from_double (atan2 (xy.x, xy.y));
5284 }
5285 #undef FUNC_NAME
5286
5287 SCM
5288 scm_c_make_rectangular (double re, double im)
5289 {
5290 if (im == 0.0)
5291 return scm_from_double (re);
5292 else
5293 {
5294 SCM z;
5295 SCM_NEWSMOB (z, scm_tc16_complex, scm_gc_malloc (sizeof (scm_t_complex),
5296 "complex"));
5297 SCM_COMPLEX_REAL (z) = re;
5298 SCM_COMPLEX_IMAG (z) = im;
5299 return z;
5300 }
5301 }
5302
5303 SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
5304 (SCM real, SCM imaginary),
5305 "Return a complex number constructed of the given @var{real} and\n"
5306 "@var{imaginary} parts.")
5307 #define FUNC_NAME s_scm_make_rectangular
5308 {
5309 struct dpair xy;
5310 scm_two_doubles (real, imaginary, FUNC_NAME, &xy);
5311 return scm_c_make_rectangular (xy.x, xy.y);
5312 }
5313 #undef FUNC_NAME
5314
5315 SCM
5316 scm_c_make_polar (double mag, double ang)
5317 {
5318 double s, c;
5319 #if HAVE_SINCOS
5320 sincos (ang, &s, &c);
5321 #else
5322 s = sin (ang);
5323 c = cos (ang);
5324 #endif
5325 return scm_c_make_rectangular (mag * c, mag * s);
5326 }
5327
5328 SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
5329 (SCM x, SCM y),
5330 "Return the complex number @var{x} * e^(i * @var{y}).")
5331 #define FUNC_NAME s_scm_make_polar
5332 {
5333 struct dpair xy;
5334 scm_two_doubles (x, y, FUNC_NAME, &xy);
5335 return scm_c_make_polar (xy.x, xy.y);
5336 }
5337 #undef FUNC_NAME
5338
5339
5340 SCM_GPROC (s_real_part, "real-part", 1, 0, 0, scm_real_part, g_real_part);
5341 /* "Return the real part of the number @var{z}."
5342 */
5343 SCM
5344 scm_real_part (SCM z)
5345 {
5346 if (SCM_I_INUMP (z))
5347 return z;
5348 else if (SCM_BIGP (z))
5349 return z;
5350 else if (SCM_REALP (z))
5351 return z;
5352 else if (SCM_COMPLEXP (z))
5353 return scm_from_double (SCM_COMPLEX_REAL (z));
5354 else if (SCM_FRACTIONP (z))
5355 return z;
5356 else
5357 SCM_WTA_DISPATCH_1 (g_real_part, z, SCM_ARG1, s_real_part);
5358 }
5359
5360
5361 SCM_GPROC (s_imag_part, "imag-part", 1, 0, 0, scm_imag_part, g_imag_part);
5362 /* "Return the imaginary part of the number @var{z}."
5363 */
5364 SCM
5365 scm_imag_part (SCM z)
5366 {
5367 if (SCM_I_INUMP (z))
5368 return SCM_INUM0;
5369 else if (SCM_BIGP (z))
5370 return SCM_INUM0;
5371 else if (SCM_REALP (z))
5372 return scm_flo0;
5373 else if (SCM_COMPLEXP (z))
5374 return scm_from_double (SCM_COMPLEX_IMAG (z));
5375 else if (SCM_FRACTIONP (z))
5376 return SCM_INUM0;
5377 else
5378 SCM_WTA_DISPATCH_1 (g_imag_part, z, SCM_ARG1, s_imag_part);
5379 }
5380
5381 SCM_GPROC (s_numerator, "numerator", 1, 0, 0, scm_numerator, g_numerator);
5382 /* "Return the numerator of the number @var{z}."
5383 */
5384 SCM
5385 scm_numerator (SCM z)
5386 {
5387 if (SCM_I_INUMP (z))
5388 return z;
5389 else if (SCM_BIGP (z))
5390 return z;
5391 else if (SCM_FRACTIONP (z))
5392 {
5393 scm_i_fraction_reduce (z);
5394 return SCM_FRACTION_NUMERATOR (z);
5395 }
5396 else if (SCM_REALP (z))
5397 return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
5398 else
5399 SCM_WTA_DISPATCH_1 (g_numerator, z, SCM_ARG1, s_numerator);
5400 }
5401
5402
5403 SCM_GPROC (s_denominator, "denominator", 1, 0, 0, scm_denominator, g_denominator);
5404 /* "Return the denominator of the number @var{z}."
5405 */
5406 SCM
5407 scm_denominator (SCM z)
5408 {
5409 if (SCM_I_INUMP (z))
5410 return SCM_I_MAKINUM (1);
5411 else if (SCM_BIGP (z))
5412 return SCM_I_MAKINUM (1);
5413 else if (SCM_FRACTIONP (z))
5414 {
5415 scm_i_fraction_reduce (z);
5416 return SCM_FRACTION_DENOMINATOR (z);
5417 }
5418 else if (SCM_REALP (z))
5419 return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
5420 else
5421 SCM_WTA_DISPATCH_1 (g_denominator, z, SCM_ARG1, s_denominator);
5422 }
5423
5424 SCM_GPROC (s_magnitude, "magnitude", 1, 0, 0, scm_magnitude, g_magnitude);
5425 /* "Return the magnitude of the number @var{z}. This is the same as\n"
5426 * "@code{abs} for real arguments, but also allows complex numbers."
5427 */
5428 SCM
5429 scm_magnitude (SCM z)
5430 {
5431 if (SCM_I_INUMP (z))
5432 {
5433 long int zz = SCM_I_INUM (z);
5434 if (zz >= 0)
5435 return z;
5436 else if (SCM_POSFIXABLE (-zz))
5437 return SCM_I_MAKINUM (-zz);
5438 else
5439 return scm_i_long2big (-zz);
5440 }
5441 else if (SCM_BIGP (z))
5442 {
5443 int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
5444 scm_remember_upto_here_1 (z);
5445 if (sgn < 0)
5446 return scm_i_clonebig (z, 0);
5447 else
5448 return z;
5449 }
5450 else if (SCM_REALP (z))
5451 return scm_from_double (fabs (SCM_REAL_VALUE (z)));
5452 else if (SCM_COMPLEXP (z))
5453 return scm_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
5454 else if (SCM_FRACTIONP (z))
5455 {
5456 if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
5457 return z;
5458 return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
5459 SCM_FRACTION_DENOMINATOR (z));
5460 }
5461 else
5462 SCM_WTA_DISPATCH_1 (g_magnitude, z, SCM_ARG1, s_magnitude);
5463 }
5464
5465
5466 SCM_GPROC (s_angle, "angle", 1, 0, 0, scm_angle, g_angle);
5467 /* "Return the angle of the complex number @var{z}."
5468 */
5469 SCM
5470 scm_angle (SCM z)
5471 {
5472 /* atan(0,-1) is pi and it'd be possible to have that as a constant like
5473 scm_flo0 to save allocating a new flonum with scm_from_double each time.
5474 But if atan2 follows the floating point rounding mode, then the value
5475 is not a constant. Maybe it'd be close enough though. */
5476 if (SCM_I_INUMP (z))
5477 {
5478 if (SCM_I_INUM (z) >= 0)
5479 return scm_flo0;
5480 else
5481 return scm_from_double (atan2 (0.0, -1.0));
5482 }
5483 else if (SCM_BIGP (z))
5484 {
5485 int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
5486 scm_remember_upto_here_1 (z);
5487 if (sgn < 0)
5488 return scm_from_double (atan2 (0.0, -1.0));
5489 else
5490 return scm_flo0;
5491 }
5492 else if (SCM_REALP (z))
5493 {
5494 if (SCM_REAL_VALUE (z) >= 0)
5495 return scm_flo0;
5496 else
5497 return scm_from_double (atan2 (0.0, -1.0));
5498 }
5499 else if (SCM_COMPLEXP (z))
5500 return scm_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
5501 else if (SCM_FRACTIONP (z))
5502 {
5503 if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
5504 return scm_flo0;
5505 else return scm_from_double (atan2 (0.0, -1.0));
5506 }
5507 else
5508 SCM_WTA_DISPATCH_1 (g_angle, z, SCM_ARG1, s_angle);
5509 }
5510
5511
5512 SCM_GPROC (s_exact_to_inexact, "exact->inexact", 1, 0, 0, scm_exact_to_inexact, g_exact_to_inexact);
5513 /* Convert the number @var{x} to its inexact representation.\n"
5514 */
5515 SCM
5516 scm_exact_to_inexact (SCM z)
5517 {
5518 if (SCM_I_INUMP (z))
5519 return scm_from_double ((double) SCM_I_INUM (z));
5520 else if (SCM_BIGP (z))
5521 return scm_from_double (scm_i_big2dbl (z));
5522 else if (SCM_FRACTIONP (z))
5523 return scm_from_double (scm_i_fraction2double (z));
5524 else if (SCM_INEXACTP (z))
5525 return z;
5526 else
5527 SCM_WTA_DISPATCH_1 (g_exact_to_inexact, z, 1, s_exact_to_inexact);
5528 }
5529
5530
5531 SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
5532 (SCM z),
5533 "Return an exact number that is numerically closest to @var{z}.")
5534 #define FUNC_NAME s_scm_inexact_to_exact
5535 {
5536 if (SCM_I_INUMP (z))
5537 return z;
5538 else if (SCM_BIGP (z))
5539 return z;
5540 else if (SCM_REALP (z))
5541 {
5542 if (xisinf (SCM_REAL_VALUE (z)) || xisnan (SCM_REAL_VALUE (z)))
5543 SCM_OUT_OF_RANGE (1, z);
5544 else
5545 {
5546 mpq_t frac;
5547 SCM q;
5548
5549 mpq_init (frac);
5550 mpq_set_d (frac, SCM_REAL_VALUE (z));
5551 q = scm_i_make_ratio (scm_i_mpz2num (mpq_numref (frac)),
5552 scm_i_mpz2num (mpq_denref (frac)));
5553
5554 /* When scm_i_make_ratio throws, we leak the memory allocated
5555 for frac...
5556 */
5557 mpq_clear (frac);
5558 return q;
5559 }
5560 }
5561 else if (SCM_FRACTIONP (z))
5562 return z;
5563 else
5564 SCM_WRONG_TYPE_ARG (1, z);
5565 }
5566 #undef FUNC_NAME
5567
5568 SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
5569 (SCM x, SCM err),
5570 "Return an exact number that is within @var{err} of @var{x}.")
5571 #define FUNC_NAME s_scm_rationalize
5572 {
5573 if (SCM_I_INUMP (x))
5574 return x;
5575 else if (SCM_BIGP (x))
5576 return x;
5577 else if ((SCM_REALP (x)) || SCM_FRACTIONP (x))
5578 {
5579 /* Use continued fractions to find closest ratio. All
5580 arithmetic is done with exact numbers.
5581 */
5582
5583 SCM ex = scm_inexact_to_exact (x);
5584 SCM int_part = scm_floor (ex);
5585 SCM tt = SCM_I_MAKINUM (1);
5586 SCM a1 = SCM_I_MAKINUM (0), a2 = SCM_I_MAKINUM (1), a = SCM_I_MAKINUM (0);
5587 SCM b1 = SCM_I_MAKINUM (1), b2 = SCM_I_MAKINUM (0), b = SCM_I_MAKINUM (0);
5588 SCM rx;
5589 int i = 0;
5590
5591 if (scm_is_true (scm_num_eq_p (ex, int_part)))
5592 return ex;
5593
5594 ex = scm_difference (ex, int_part); /* x = x-int_part */
5595 rx = scm_divide (ex, SCM_UNDEFINED); /* rx = 1/x */
5596
5597 /* We stop after a million iterations just to be absolutely sure
5598 that we don't go into an infinite loop. The process normally
5599 converges after less than a dozen iterations.
5600 */
5601
5602 err = scm_abs (err);
5603 while (++i < 1000000)
5604 {
5605 a = scm_sum (scm_product (a1, tt), a2); /* a = a1*tt + a2 */
5606 b = scm_sum (scm_product (b1, tt), b2); /* b = b1*tt + b2 */
5607 if (scm_is_false (scm_zero_p (b)) && /* b != 0 */
5608 scm_is_false
5609 (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))),
5610 err))) /* abs(x-a/b) <= err */
5611 {
5612 SCM res = scm_sum (int_part, scm_divide (a, b));
5613 if (scm_is_false (scm_exact_p (x))
5614 || scm_is_false (scm_exact_p (err)))
5615 return scm_exact_to_inexact (res);
5616 else
5617 return res;
5618 }
5619 rx = scm_divide (scm_difference (rx, tt), /* rx = 1/(rx - tt) */
5620 SCM_UNDEFINED);
5621 tt = scm_floor (rx); /* tt = floor (rx) */
5622 a2 = a1;
5623 b2 = b1;
5624 a1 = a;
5625 b1 = b;
5626 }
5627 scm_num_overflow (s_scm_rationalize);
5628 }
5629 else
5630 SCM_WRONG_TYPE_ARG (1, x);
5631 }
5632 #undef FUNC_NAME
5633
5634 /* conversion functions */
5635
5636 int
5637 scm_is_integer (SCM val)
5638 {
5639 return scm_is_true (scm_integer_p (val));
5640 }
5641
5642 int
5643 scm_is_signed_integer (SCM val, scm_t_intmax min, scm_t_intmax max)
5644 {
5645 if (SCM_I_INUMP (val))
5646 {
5647 scm_t_signed_bits n = SCM_I_INUM (val);
5648 return n >= min && n <= max;
5649 }
5650 else if (SCM_BIGP (val))
5651 {
5652 if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM)
5653 return 0;
5654 else if (min >= LONG_MIN && max <= LONG_MAX)
5655 {
5656 if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val)))
5657 {
5658 long n = mpz_get_si (SCM_I_BIG_MPZ (val));
5659 return n >= min && n <= max;
5660 }
5661 else
5662 return 0;
5663 }
5664 else
5665 {
5666 scm_t_intmax n;
5667 size_t count;
5668
5669 if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
5670 > CHAR_BIT*sizeof (scm_t_uintmax))
5671 return 0;
5672
5673 mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
5674 SCM_I_BIG_MPZ (val));
5675
5676 if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
5677 {
5678 if (n < 0)
5679 return 0;
5680 }
5681 else
5682 {
5683 n = -n;
5684 if (n >= 0)
5685 return 0;
5686 }
5687
5688 return n >= min && n <= max;
5689 }
5690 }
5691 else
5692 return 0;
5693 }
5694
5695 int
5696 scm_is_unsigned_integer (SCM val, scm_t_uintmax min, scm_t_uintmax max)
5697 {
5698 if (SCM_I_INUMP (val))
5699 {
5700 scm_t_signed_bits n = SCM_I_INUM (val);
5701 return n >= 0 && ((scm_t_uintmax)n) >= min && ((scm_t_uintmax)n) <= max;
5702 }
5703 else if (SCM_BIGP (val))
5704 {
5705 if (max <= SCM_MOST_POSITIVE_FIXNUM)
5706 return 0;
5707 else if (max <= ULONG_MAX)
5708 {
5709 if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
5710 {
5711 unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
5712 return n >= min && n <= max;
5713 }
5714 else
5715 return 0;
5716 }
5717 else
5718 {
5719 scm_t_uintmax n;
5720 size_t count;
5721
5722 if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
5723 return 0;
5724
5725 if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
5726 > CHAR_BIT*sizeof (scm_t_uintmax))
5727 return 0;
5728
5729 mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
5730 SCM_I_BIG_MPZ (val));
5731
5732 return n >= min && n <= max;
5733 }
5734 }
5735 else
5736 return 0;
5737 }
5738
5739 static void
5740 scm_i_range_error (SCM bad_val, SCM min, SCM max)
5741 {
5742 scm_error (scm_out_of_range_key,
5743 NULL,
5744 "Value out of range ~S to ~S: ~S",
5745 scm_list_3 (min, max, bad_val),
5746 scm_list_1 (bad_val));
5747 }
5748
5749 #define TYPE scm_t_intmax
5750 #define TYPE_MIN min
5751 #define TYPE_MAX max
5752 #define SIZEOF_TYPE 0
5753 #define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, scm_t_intmax min, scm_t_intmax max)
5754 #define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg)
5755 #include "libguile/conv-integer.i.c"
5756
5757 #define TYPE scm_t_uintmax
5758 #define TYPE_MIN min
5759 #define TYPE_MAX max
5760 #define SIZEOF_TYPE 0
5761 #define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, scm_t_uintmax min, scm_t_uintmax max)
5762 #define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg)
5763 #include "libguile/conv-uinteger.i.c"
5764
5765 #define TYPE scm_t_int8
5766 #define TYPE_MIN SCM_T_INT8_MIN
5767 #define TYPE_MAX SCM_T_INT8_MAX
5768 #define SIZEOF_TYPE 1
5769 #define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg)
5770 #define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg)
5771 #include "libguile/conv-integer.i.c"
5772
5773 #define TYPE scm_t_uint8
5774 #define TYPE_MIN 0
5775 #define TYPE_MAX SCM_T_UINT8_MAX
5776 #define SIZEOF_TYPE 1
5777 #define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg)
5778 #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg)
5779 #include "libguile/conv-uinteger.i.c"
5780
5781 #define TYPE scm_t_int16
5782 #define TYPE_MIN SCM_T_INT16_MIN
5783 #define TYPE_MAX SCM_T_INT16_MAX
5784 #define SIZEOF_TYPE 2
5785 #define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg)
5786 #define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg)
5787 #include "libguile/conv-integer.i.c"
5788
5789 #define TYPE scm_t_uint16
5790 #define TYPE_MIN 0
5791 #define TYPE_MAX SCM_T_UINT16_MAX
5792 #define SIZEOF_TYPE 2
5793 #define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg)
5794 #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg)
5795 #include "libguile/conv-uinteger.i.c"
5796
5797 #define TYPE scm_t_int32
5798 #define TYPE_MIN SCM_T_INT32_MIN
5799 #define TYPE_MAX SCM_T_INT32_MAX
5800 #define SIZEOF_TYPE 4
5801 #define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg)
5802 #define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg)
5803 #include "libguile/conv-integer.i.c"
5804
5805 #define TYPE scm_t_uint32
5806 #define TYPE_MIN 0
5807 #define TYPE_MAX SCM_T_UINT32_MAX
5808 #define SIZEOF_TYPE 4
5809 #define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg)
5810 #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg)
5811 #include "libguile/conv-uinteger.i.c"
5812
5813 #if SCM_HAVE_T_INT64
5814
5815 #define TYPE scm_t_int64
5816 #define TYPE_MIN SCM_T_INT64_MIN
5817 #define TYPE_MAX SCM_T_INT64_MAX
5818 #define SIZEOF_TYPE 8
5819 #define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg)
5820 #define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg)
5821 #include "libguile/conv-integer.i.c"
5822
5823 #define TYPE scm_t_uint64
5824 #define TYPE_MIN 0
5825 #define TYPE_MAX SCM_T_UINT64_MAX
5826 #define SIZEOF_TYPE 8
5827 #define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg)
5828 #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg)
5829 #include "libguile/conv-uinteger.i.c"
5830
5831 #endif
5832
5833 void
5834 scm_to_mpz (SCM val, mpz_t rop)
5835 {
5836 if (SCM_I_INUMP (val))
5837 mpz_set_si (rop, SCM_I_INUM (val));
5838 else if (SCM_BIGP (val))
5839 mpz_set (rop, SCM_I_BIG_MPZ (val));
5840 else
5841 scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
5842 }
5843
5844 SCM
5845 scm_from_mpz (mpz_t val)
5846 {
5847 return scm_i_mpz2num (val);
5848 }
5849
5850 int
5851 scm_is_real (SCM val)
5852 {
5853 return scm_is_true (scm_real_p (val));
5854 }
5855
5856 int
5857 scm_is_rational (SCM val)
5858 {
5859 return scm_is_true (scm_rational_p (val));
5860 }
5861
5862 double
5863 scm_to_double (SCM val)
5864 {
5865 if (SCM_I_INUMP (val))
5866 return SCM_I_INUM (val);
5867 else if (SCM_BIGP (val))
5868 return scm_i_big2dbl (val);
5869 else if (SCM_FRACTIONP (val))
5870 return scm_i_fraction2double (val);
5871 else if (SCM_REALP (val))
5872 return SCM_REAL_VALUE (val);
5873 else
5874 scm_wrong_type_arg_msg (NULL, 0, val, "real number");
5875 }
5876
5877 SCM
5878 scm_from_double (double val)
5879 {
5880 SCM z = scm_double_cell (scm_tc16_real, 0, 0, 0);
5881 SCM_REAL_VALUE (z) = val;
5882 return z;
5883 }
5884
5885 #if SCM_ENABLE_DISCOURAGED == 1
5886
5887 float
5888 scm_num2float (SCM num, unsigned long int pos, const char *s_caller)
5889 {
5890 if (SCM_BIGP (num))
5891 {
5892 float res = mpz_get_d (SCM_I_BIG_MPZ (num));
5893 if (!xisinf (res))
5894 return res;
5895 else
5896 scm_out_of_range (NULL, num);
5897 }
5898 else
5899 return scm_to_double (num);
5900 }
5901
5902 double
5903 scm_num2double (SCM num, unsigned long int pos, const char *s_caller)
5904 {
5905 if (SCM_BIGP (num))
5906 {
5907 double res = mpz_get_d (SCM_I_BIG_MPZ (num));
5908 if (!xisinf (res))
5909 return res;
5910 else
5911 scm_out_of_range (NULL, num);
5912 }
5913 else
5914 return scm_to_double (num);
5915 }
5916
5917 #endif
5918
5919 int
5920 scm_is_complex (SCM val)
5921 {
5922 return scm_is_true (scm_complex_p (val));
5923 }
5924
5925 double
5926 scm_c_real_part (SCM z)
5927 {
5928 if (SCM_COMPLEXP (z))
5929 return SCM_COMPLEX_REAL (z);
5930 else
5931 {
5932 /* Use the scm_real_part to get proper error checking and
5933 dispatching.
5934 */
5935 return scm_to_double (scm_real_part (z));
5936 }
5937 }
5938
5939 double
5940 scm_c_imag_part (SCM z)
5941 {
5942 if (SCM_COMPLEXP (z))
5943 return SCM_COMPLEX_IMAG (z);
5944 else
5945 {
5946 /* Use the scm_imag_part to get proper error checking and
5947 dispatching. The result will almost always be 0.0, but not
5948 always.
5949 */
5950 return scm_to_double (scm_imag_part (z));
5951 }
5952 }
5953
5954 double
5955 scm_c_magnitude (SCM z)
5956 {
5957 return scm_to_double (scm_magnitude (z));
5958 }
5959
5960 double
5961 scm_c_angle (SCM z)
5962 {
5963 return scm_to_double (scm_angle (z));
5964 }
5965
5966 int
5967 scm_is_number (SCM z)
5968 {
5969 return scm_is_true (scm_number_p (z));
5970 }
5971
5972 void
5973 scm_init_numbers ()
5974 {
5975 int i;
5976
5977 mpz_init_set_si (z_negative_one, -1);
5978
5979 /* It may be possible to tune the performance of some algorithms by using
5980 * the following constants to avoid the creation of bignums. Please, before
5981 * using these values, remember the two rules of program optimization:
5982 * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
5983 scm_c_define ("most-positive-fixnum",
5984 SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
5985 scm_c_define ("most-negative-fixnum",
5986 SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
5987
5988 scm_add_feature ("complex");
5989 scm_add_feature ("inexact");
5990 scm_flo0 = scm_from_double (0.0);
5991
5992 /* determine floating point precision */
5993 for (i=2; i <= SCM_MAX_DBL_RADIX; ++i)
5994 {
5995 init_dblprec(&scm_dblprec[i-2],i);
5996 init_fx_radix(fx_per_radix[i-2],i);
5997 }
5998 #ifdef DBL_DIG
5999 /* hard code precision for base 10 if the preprocessor tells us to... */
6000 scm_dblprec[10-2] = (DBL_DIG > 20) ? 20 : DBL_DIG;
6001 #endif
6002
6003 exactly_one_half = scm_permanent_object (scm_divide (SCM_I_MAKINUM (1),
6004 SCM_I_MAKINUM (2)));
6005 #include "libguile/numbers.x"
6006 }
6007
6008 /*
6009 Local Variables:
6010 c-file-style: "gnu"
6011 End:
6012 */