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