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