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