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