* numbers.c (scm_istr2int): If the number is short (as most
[bpt/guile.git] / libguile / numbers.c
1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41 \f
42
43 #include <stdio.h>
44 #include <math.h>
45 #include "_scm.h"
46 #include "genio.h"
47 #include "unif.h"
48
49 #include "numbers.h"
50 \f
51 #define DIGITS '0':case '1':case '2':case '3':case '4':\
52 case '5':case '6':case '7':case '8':case '9'
53
54
55 /* IS_INF tests its floating point number for infiniteness
56 */
57 #ifndef IS_INF
58 # define IS_INF(x) ((x)==(x)/2)
59 #endif
60
61 /* MAXEXP is the maximum double precision expontent
62 * FLTMAX is less than or scm_equal the largest single precision float
63 */
64
65 #ifdef SCM_FLOATS
66 # ifdef STDC_HEADERS
67 # ifndef GO32
68 # include <float.h>
69 # endif /* ndef GO32 */
70 # endif /* def STDC_HEADERS */
71 # ifdef DBL_MAX_10_EXP
72 # define MAXEXP DBL_MAX_10_EXP
73 # else
74 # define MAXEXP 308 /* IEEE doubles */
75 # endif /* def DBL_MAX_10_EXP */
76 # ifdef FLT_MAX
77 # define FLTMAX FLT_MAX
78 # else
79 # define FLTMAX 1e+23
80 # endif /* def FLT_MAX */
81 #endif /* def SCM_FLOATS */
82
83 \f
84
85 SCM_PROC(s_exact_p, "exact?", 1, 0, 0, scm_exact_p);
86 #ifdef __STDC__
87 SCM
88 scm_exact_p(SCM x)
89 #else
90 SCM
91 scm_exact_p(x)
92 SCM x;
93 #endif
94 {
95 if SCM_INUMP(x) return SCM_BOOL_T;
96 #ifdef SCM_BIGDIG
97 if (SCM_NIMP(x) && SCM_BIGP(x)) return SCM_BOOL_T;
98 #endif
99 return SCM_BOOL_F;
100 }
101
102 SCM_PROC(s_odd_p, "odd?", 1, 0, 0, scm_odd_p);
103 #ifdef __STDC__
104 SCM
105 scm_odd_p(SCM n)
106 #else
107 SCM
108 scm_odd_p(n)
109 SCM n;
110 #endif
111 {
112 #ifdef SCM_BIGDIG
113 if SCM_NINUMP(n) {
114 SCM_ASSERT(SCM_NIMP(n) && SCM_BIGP(n), n, SCM_ARG1, s_odd_p);
115 return (1 & SCM_BDIGITS(n)[0]) ? SCM_BOOL_T : SCM_BOOL_F;
116 }
117 #else
118 SCM_ASSERT(SCM_INUMP(n), n, SCM_ARG1, s_odd_p);
119 #endif
120 return (4 & (int)n) ? SCM_BOOL_T : SCM_BOOL_F;
121 }
122
123 SCM_PROC(s_even_p, "even?", 1, 0, 0, scm_even_p);
124 #ifdef __STDC__
125 SCM
126 scm_even_p(SCM n)
127 #else
128 SCM
129 scm_even_p(n)
130 SCM n;
131 #endif
132 {
133 #ifdef SCM_BIGDIG
134 if SCM_NINUMP(n) {
135 SCM_ASSERT(SCM_NIMP(n) && SCM_BIGP(n), n, SCM_ARG1, s_even_p);
136 return (1 & SCM_BDIGITS(n)[0]) ? SCM_BOOL_F : SCM_BOOL_T;
137 }
138 #else
139 SCM_ASSERT(SCM_INUMP(n), n, SCM_ARG1, s_even_p);
140 #endif
141 return (4 & (int)n) ? SCM_BOOL_F : SCM_BOOL_T;
142 }
143
144 SCM_PROC(s_abs, "abs", 1, 0, 0, scm_abs);
145 #ifdef __STDC__
146 SCM
147 scm_abs(SCM x)
148 #else
149 SCM
150 scm_abs(x)
151 SCM x;
152 #endif
153 {
154 #ifdef SCM_BIGDIG
155 if SCM_NINUMP(x) {
156 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_abs);
157 if (SCM_TYP16(x)==scm_tc16_bigpos) return x;
158 return scm_copybig(x, 0);
159 }
160 #else
161 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_abs);
162 #endif
163 if (SCM_INUM(x) >= 0) return x;
164 x = -SCM_INUM(x);
165 if (!SCM_POSFIXABLE(x))
166 #ifdef SCM_BIGDIG
167 return scm_long2big(x);
168 #else
169 scm_num_overflow (s_abs);
170 #endif
171 return SCM_MAKINUM(x);
172 }
173
174 SCM_PROC(s_quotient, "quotient", 2, 0, 0, scm_quotient);
175 #ifdef __STDC__
176 SCM
177 scm_quotient(SCM x, SCM y)
178 #else
179 SCM
180 scm_quotient(x, y)
181 SCM x;
182 SCM y;
183 #endif
184 {
185 register long z;
186 #ifdef SCM_BIGDIG
187 if SCM_NINUMP(x) {
188 long w;
189 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_quotient);
190 if SCM_NINUMP(y) {
191 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
192 return scm_divbigbig(SCM_BDIGITS(x),
193 SCM_NUMDIGS(x),
194 SCM_BDIGITS(y),
195 SCM_NUMDIGS(y),
196 SCM_BIGSIGN(x) ^ SCM_BIGSIGN(y),
197 2);
198 }
199 z = SCM_INUM(y);
200 SCM_ASRTGO(z, ov);
201 if (1==z) return x;
202 if (z < 0) z = -z;
203 if (z < SCM_BIGRAD) {
204 w = scm_copybig(x, SCM_BIGSIGN(x) ? (y>0) : (y<0));
205 scm_divbigdig(SCM_BDIGITS(w), SCM_NUMDIGS(w), (SCM_BIGDIG)z);
206 return scm_normbig(w);
207 }
208 #ifndef SCM_DIGSTOOBIG
209 w = scm_pseudolong(z);
210 return scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), (SCM_BIGDIG *)&w, SCM_DIGSPERLONG,
211 SCM_BIGSIGN(x) ? (y>0) : (y<0), 2);
212 #else
213 { SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
214 scm_longdigs(z, zdigs);
215 return scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), zdigs, SCM_DIGSPERLONG,
216 SCM_BIGSIGN(x) ? (y>0) : (y<0), 2);
217 }
218 #endif
219 }
220 if SCM_NINUMP(y) {
221 # ifndef RECKLESS
222 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
223 bady: scm_wta(y, (char *)SCM_ARG2, s_quotient);
224 # endif
225 return SCM_INUM0;
226 }
227 #else
228 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_quotient);
229 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_quotient);
230 #endif
231 if ((z = SCM_INUM(y))==0)
232 ov: scm_num_overflow (s_quotient);
233 z = SCM_INUM(x)/z;
234 #ifdef BADIVSGNS
235 {
236 #if (__TURBOC__==1)
237 long t = ((y<0) ? -SCM_INUM(x) : SCM_INUM(x))%SCM_INUM(y);
238 #else
239 long t = SCM_INUM(x)%SCM_INUM(y);
240 #endif
241 if (t==0) ;
242 else if (t < 0)
243 if (x < 0) ;
244 else z--;
245 else if (x < 0) z++;
246 }
247 #endif
248 if (!SCM_FIXABLE(z))
249 #ifdef SCM_BIGDIG
250 return scm_long2big(z);
251 #else
252 scm_num_overflow (s_quotient);
253 #endif
254 return SCM_MAKINUM(z);
255 }
256
257 SCM_PROC(s_remainder, "remainder", 2, 0, 0, scm_remainder);
258 #ifdef __STDC__
259 SCM
260 scm_remainder(SCM x, SCM y)
261 #else
262 SCM
263 scm_remainder(x, y)
264 SCM x;
265 SCM y;
266 #endif
267 {
268 register long z;
269 #ifdef SCM_BIGDIG
270 if SCM_NINUMP(x) {
271 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_remainder);
272 if SCM_NINUMP(y) {
273 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
274 return scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BDIGITS(y), SCM_NUMDIGS(y),
275 SCM_BIGSIGN(x), 0);
276 }
277 if (!(z = SCM_INUM(y))) goto ov;
278 return scm_divbigint(x, z, SCM_BIGSIGN(x), 0);
279 }
280 if SCM_NINUMP(y) {
281 # ifndef RECKLESS
282 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
283 bady: scm_wta(y, (char *)SCM_ARG2, s_remainder);
284 # endif
285 return x;
286 }
287 #else
288 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_remainder);
289 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_remainder);
290 #endif
291 if (!(z = SCM_INUM(y)))
292 ov: scm_num_overflow (s_remainder);
293 #if (__TURBOC__==1)
294 if (z < 0) z = -z;
295 #endif
296 z = SCM_INUM(x)%z;
297 #ifdef BADIVSGNS
298 if (!z) ;
299 else if (z < 0)
300 if (x < 0) ;
301 else z += SCM_INUM(y);
302 else if (x < 0) z -= SCM_INUM(y);
303 #endif
304 return SCM_MAKINUM(z);
305 }
306
307 SCM_PROC(s_modulo, "modulo", 2, 0, 0, scm_modulo);
308 #ifdef __STDC__
309 SCM
310 scm_modulo(SCM x, SCM y)
311 #else
312 SCM
313 scm_modulo(x, y)
314 SCM x;
315 SCM y;
316 #endif
317 {
318 register long yy, z;
319 #ifdef SCM_BIGDIG
320 if SCM_NINUMP(x) {
321 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_modulo);
322 if SCM_NINUMP(y) {
323 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
324 return scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BDIGITS(y), SCM_NUMDIGS(y),
325 SCM_BIGSIGN(y), (SCM_BIGSIGN(x) ^ SCM_BIGSIGN(y)) ? 1 : 0);
326 }
327 if (!(z = SCM_INUM(y))) goto ov;
328 return scm_divbigint(x, z, y < 0, (SCM_BIGSIGN(x) ? (y > 0) : (y < 0)) ? 1 : 0);
329 }
330 if SCM_NINUMP(y) {
331 # ifndef RECKLESS
332 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
333 bady: scm_wta(y, (char *)SCM_ARG2, s_modulo);
334 # endif
335 return (SCM_BIGSIGN(y) ? (x>0) : (x<0)) ? scm_sum(x, y) : x;
336 }
337 #else
338 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_modulo);
339 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_modulo);
340 #endif
341 if (!(yy = SCM_INUM(y)))
342 ov: scm_num_overflow (s_modulo);
343 #if (__TURBOC__==1)
344 z = SCM_INUM(x);
345 z = ((yy<0) ? -z : z)%yy;
346 #else
347 z = SCM_INUM(x)%yy;
348 #endif
349 return SCM_MAKINUM(((yy<0) ? (z>0) : (z<0)) ? z+yy : z);
350 }
351
352 SCM_PROC1 (s_gcd, "gcd", scm_tc7_asubr, scm_gcd);
353 #ifdef __STDC__
354 SCM
355 scm_gcd(SCM x, SCM y)
356 #else
357 SCM
358 scm_gcd(x, y)
359 SCM x;
360 SCM y;
361 #endif
362 {
363 register long u, v, k, t;
364 if SCM_UNBNDP(y) return SCM_UNBNDP(x) ? SCM_INUM0 : x;
365 tailrec:
366 #ifdef SCM_BIGDIG
367 if SCM_NINUMP(x) {
368 big_gcd:
369 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_gcd);
370 if SCM_BIGSIGN(x) x = scm_copybig(x, 0);
371 newy:
372 if SCM_NINUMP(y) {
373 SCM_ASSERT(SCM_NIMP(y) && SCM_BIGP(y), y, SCM_ARG2, s_gcd);
374 if SCM_BIGSIGN(y) y = scm_copybig(y, 0);
375 switch (scm_bigcomp(x, y)) {
376 case -1:
377 swaprec: t = scm_remainder(x, y); x = y; y = t; goto tailrec;
378 case 0: return x;
379 case 1: y = scm_remainder(y, x); goto newy;
380 }
381 /* instead of the switch, we could just return scm_gcd(y, scm_modulo(x, y)); */
382 }
383 if (SCM_INUM0==y) return x; goto swaprec;
384 }
385 if SCM_NINUMP(y) { t=x; x=y; y=t; goto big_gcd;}
386 #else
387 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_gcd);
388 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_gcd);
389 #endif
390 u = SCM_INUM(x);
391 if (u<0) u = -u;
392 v = SCM_INUM(y);
393 if (v<0) v = -v;
394 else if (0==v) goto getout;
395 if (0==u) {u = v; goto getout;}
396 for (k = 1;!(1 & ((int)u|(int)v));k <<= 1, u >>= 1, v >>= 1);
397 if (1 & (int)u) t = -v;
398 else {
399 t = u;
400 b3:
401 t = SCM_SRS(t, 1);
402 }
403 if (!(1 & (int)t)) goto b3;
404 if (t>0) u = t;
405 else v = -t;
406 if ((t = u-v)) goto b3;
407 u = u*k;
408 getout:
409 if (!SCM_POSFIXABLE(u))
410 #ifdef SCM_BIGDIG
411 return scm_long2big(u);
412 #else
413 scm_num_overflow (s_gcd);
414 #endif
415 return SCM_MAKINUM(u);
416 }
417
418 SCM_PROC1 (s_lcm, "lcm", scm_tc7_asubr, scm_lcm);
419 #ifdef __STDC__
420 SCM
421 scm_lcm(SCM n1, SCM n2)
422 #else
423 SCM
424 scm_lcm(n1, n2)
425 SCM n1;
426 SCM n2;
427 #endif
428 {
429 SCM d;
430 if SCM_UNBNDP(n2) {
431 n2 = SCM_MAKINUM(1L);
432 if SCM_UNBNDP(n1) return n2;
433 }
434 d = scm_gcd(n1, n2);
435 if (SCM_INUM0==d) return d;
436 return scm_abs(scm_product(n1, scm_quotient(n2, d)));
437 }
438
439 #ifndef SCM_BIGDIG
440 # ifndef SCM_FLOATS
441 # define scm_long2num SCM_MAKINUM
442 # endif
443 #endif
444
445 #ifndef scm_long2num
446 SCM_PROC1 (s_logand, "logand", scm_tc7_asubr, scm_logand);
447 #ifdef __STDC__
448 SCM
449 scm_logand(SCM n1, SCM n2)
450 #else
451 SCM
452 scm_logand(n1, n2)
453 SCM n1;
454 SCM n2;
455 #endif
456 {
457 return scm_long2num(scm_num2long(n1, (char *)SCM_ARG1, s_logand)
458 & scm_num2long(n2, (char *)SCM_ARG2, s_logand));
459 }
460
461 SCM_PROC1 (s_logior, "logior", scm_tc7_asubr, scm_logior);
462 #ifdef __STDC__
463 SCM
464 scm_logior(SCM n1, SCM n2)
465 #else
466 SCM
467 scm_logior(n1, n2)
468 SCM n1;
469 SCM n2;
470 #endif
471 {
472 return scm_long2num(scm_num2long(n1, (char *)SCM_ARG1, s_logior)
473 | scm_num2long(n2, (char *)SCM_ARG2, s_logior));
474 }
475
476 SCM_PROC1 (s_logxor, "logxor", scm_tc7_asubr, scm_logxor);
477 #ifdef __STDC__
478 SCM
479 scm_logxor(SCM n1, SCM n2)
480 #else
481 SCM
482 scm_logxor(n1, n2)
483 SCM n1;
484 SCM n2;
485 #endif
486 {
487 return scm_long2num(scm_num2long(n1, (char *)SCM_ARG1, s_logxor)
488 ^ scm_num2long(n2, (char *)SCM_ARG2, s_logxor));
489 }
490
491 SCM_PROC(s_logtest, "logtest", 2, 0, 0, scm_logtest);
492 #ifdef __STDC__
493 SCM
494 scm_logtest(SCM n1, SCM n2)
495 #else
496 SCM
497 scm_logtest(n1, n2)
498 SCM n1;
499 SCM n2;
500 #endif
501 {
502 return ((scm_num2long (n1, (char *)SCM_ARG1, s_logtest)
503 & scm_num2long (n2, (char *)SCM_ARG2, s_logtest))
504 ? SCM_BOOL_T : SCM_BOOL_F);
505 }
506
507
508 SCM_PROC(s_logbit_p, "logbit?", 2, 0, 0, scm_logbit_p);
509 #ifdef __STDC__
510 SCM
511 scm_logbit_p(SCM n1, SCM n2)
512 #else
513 SCM
514 scm_logbit_p(n1, n2)
515 SCM n1;
516 SCM n2;
517 #endif
518 {
519 return (((1 << scm_num2long (n1, (char *)SCM_ARG1, s_logtest))
520 & scm_num2long (n2, (char *)SCM_ARG2, s_logtest))
521 ? SCM_BOOL_T : SCM_BOOL_F);
522 }
523
524 #else
525
526 SCM_PROC1 (s_logand, "logand", scm_tc7_asubr, scm_logand);
527 #ifdef __STDC__
528 SCM
529 scm_logand(SCM n1, SCM n2)
530 #else
531 SCM
532 scm_logand(n1, n2)
533 SCM n1;
534 SCM n2;
535 #endif
536 {
537 SCM_ASSERT(SCM_INUMP(n1), n1, SCM_ARG1, s_logand);
538 SCM_ASSERT(SCM_INUMP(n2), n2, SCM_ARG2, s_logand);
539 return SCM_MAKINUM(SCM_INUM(n1) & SCM_INUM(n2));
540 }
541
542 SCM_PROC1 (s_logior, "logior", scm_tc7_asubr, scm_logior);
543 #ifdef __STDC__
544 SCM
545 scm_logior(SCM n1, SCM n2)
546 #else
547 SCM
548 scm_logior(n1, n2)
549 SCM n1;
550 SCM n2;
551 #endif
552 {
553 SCM_ASSERT(SCM_INUMP(n1), n1, SCM_ARG1, s_logior);
554 SCM_ASSERT(SCM_INUMP(n2), n2, SCM_ARG2, s_logior);
555 return SCM_MAKINUM(SCM_INUM(n1) | SCM_INUM(n2));
556 }
557
558 SCM_PROC1 (s_logxor, "logxor", scm_tc7_asubr, scm_logxor);
559 #ifdef __STDC__
560 SCM
561 scm_logxor(SCM n1, SCM n2)
562 #else
563 SCM
564 scm_logxor(n1, n2)
565 SCM n1;
566 SCM n2;
567 #endif
568 {
569 SCM_ASSERT(SCM_INUMP(n1), n1, SCM_ARG1, s_logxor);
570 SCM_ASSERT(SCM_INUMP(n2), n2, SCM_ARG2, s_logxor);
571 return SCM_MAKINUM(SCM_INUM(n1) ^ SCM_INUM(n2));
572 }
573
574 SCM_PROC(s_logtest, "logtest", 2, 0, 0, scm_logtest);
575 #ifdef __STDC__
576 SCM
577 scm_logtest(SCM n1, SCM n2)
578 #else
579 SCM
580 scm_logtest(n1, n2)
581 SCM n1;
582 SCM n2;
583 #endif
584 {
585 SCM_ASSERT(SCM_INUMP(n1), n1, SCM_ARG1, s_logtest);
586 SCM_ASSERT(SCM_INUMP(n2), n2, SCM_ARG2, s_logtest);
587 return (SCM_INUM(n1) & SCM_INUM(n2)) ? SCM_BOOL_T : SCM_BOOL_F;
588 }
589
590 SCM_PROC(s_logbit_p, "logbit?", 2, 0, 0, scm_logbit_p);
591 #ifdef __STDC__
592 SCM
593 scm_logbit_p(SCM n1, SCM n2)
594 #else
595 SCM
596 scm_logbit_p(n1, n2)
597 SCM n1;
598 SCM n2;
599 #endif
600 {
601 SCM_ASSERT(SCM_INUMP(n1) && SCM_INUM(n1) >= 0, n1, SCM_ARG1, s_logbit_p);
602 SCM_ASSERT(SCM_INUMP(n2), n2, SCM_ARG2, s_logbit_p);
603 return ((1 << SCM_INUM(n1)) & SCM_INUM(n2)) ? SCM_BOOL_T : SCM_BOOL_F;
604 }
605 #endif
606
607 SCM_PROC(s_lognot, "lognot", 1, 0, 0, scm_lognot);
608 #ifdef __STDC__
609 SCM
610 scm_lognot(SCM n)
611 #else
612 SCM
613 scm_lognot(n)
614 SCM n;
615 #endif
616 {
617 SCM_ASSERT(SCM_INUMP(n), n, SCM_ARG1, s_lognot);
618 return scm_difference(SCM_MAKINUM(-1L), n);
619 }
620
621 SCM_PROC(s_integer_expt, "integer-expt", 2, 0, 0, scm_integer_expt);
622 #ifdef __STDC__
623 SCM
624 scm_integer_expt(SCM z1, SCM z2)
625 #else
626 SCM
627 scm_integer_expt(z1, z2)
628 SCM z1;
629 SCM z2;
630 #endif
631 {
632 SCM acc = SCM_MAKINUM(1L);
633 #ifdef SCM_BIGDIG
634 if (SCM_INUM0==z1 || acc==z1) return z1;
635 else if (SCM_MAKINUM(-1L)==z1) return SCM_BOOL_F==scm_even_p(z2)?z1:acc;
636 #endif
637 SCM_ASSERT(SCM_INUMP(z2), z2, SCM_ARG2, s_integer_expt);
638 z2 = SCM_INUM(z2);
639 if (z2 < 0) {
640 z2 = -z2;
641 z1 = scm_divide(z1, SCM_UNDEFINED);
642 }
643 while(1) {
644 if (0==z2) return acc;
645 if (1==z2) return scm_product(acc, z1);
646 if (z2 & 1) acc = scm_product(acc, z1);
647 z1 = scm_product(z1, z1);
648 z2 >>= 1;
649 }
650 }
651
652 SCM_PROC(s_ash, "ash", 2, 0, 0, scm_ash);
653 #ifdef __STDC__
654 SCM
655 scm_ash(SCM n, SCM cnt)
656 #else
657 SCM
658 scm_ash(n, cnt)
659 SCM n;
660 SCM cnt;
661 #endif
662 {
663 SCM res = SCM_INUM(n);
664 SCM_ASSERT(SCM_INUMP(cnt), cnt, SCM_ARG2, s_ash);
665 #ifdef SCM_BIGDIG
666 if(cnt < 0) {
667 res = scm_integer_expt(SCM_MAKINUM(2), SCM_MAKINUM(-SCM_INUM(cnt)));
668 if (SCM_NFALSEP(scm_negative_p(n)))
669 return scm_sum(SCM_MAKINUM(-1L), scm_quotient(scm_sum(SCM_MAKINUM(1L), n), res));
670 else return scm_quotient(n, res);
671 }
672 else return scm_product(n, scm_integer_expt(SCM_MAKINUM(2), cnt));
673 #else
674 SCM_ASSERT(SCM_INUMP(n), n, SCM_ARG1, s_ash);
675 cnt = SCM_INUM(cnt);
676 if (cnt < 0) return SCM_MAKINUM(SCM_SRS(res, -cnt));
677 res = SCM_MAKINUM(res<<cnt);
678 if (SCM_INUM(res)>>cnt != SCM_INUM(n))
679 scm_num_overflow (s_ash);
680 return res;
681 #endif
682 }
683
684 SCM_PROC(s_bit_extract, "bit-extract", 3, 0, 0, scm_bit_extract);
685 #ifdef __STDC__
686 SCM
687 scm_bit_extract(SCM n, SCM start, SCM end)
688 #else
689 SCM
690 scm_bit_extract(n, start, end)
691 SCM n;
692 SCM start;
693 SCM end;
694 #endif
695 {
696 SCM_ASSERT(SCM_INUMP(start), start, SCM_ARG2, s_bit_extract);
697 SCM_ASSERT(SCM_INUMP(end), end, SCM_ARG3, s_bit_extract);
698 start = SCM_INUM(start); end = SCM_INUM(end);
699 SCM_ASSERT(end >= start, SCM_MAKINUM(end), SCM_OUTOFRANGE, s_bit_extract);
700 #ifdef SCM_BIGDIG
701 if SCM_NINUMP(n)
702 return
703 scm_logand(scm_difference(scm_integer_expt(SCM_MAKINUM(2), SCM_MAKINUM(end - start)),
704 SCM_MAKINUM(1L)),
705 scm_ash(n, SCM_MAKINUM(-start)));
706 #else
707 SCM_ASSERT(SCM_INUMP(n), n, SCM_ARG1, s_bit_extract);
708 #endif
709 return SCM_MAKINUM((SCM_INUM(n)>>start) & ((1L<<(end-start))-1));
710 }
711
712 char scm_logtab[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
713 SCM_PROC(s_logcount, "logcount", 1, 0, 0, scm_logcount);
714 #ifdef __STDC__
715 SCM
716 scm_logcount (SCM n)
717 #else
718 SCM
719 scm_logcount(n)
720 SCM n;
721 #endif
722 {
723 register unsigned long c = 0;
724 register long nn;
725 #ifdef SCM_BIGDIG
726 if SCM_NINUMP(n) {
727 scm_sizet i; SCM_BIGDIG *ds, d;
728 SCM_ASSERT(SCM_NIMP(n) && SCM_BIGP(n), n, SCM_ARG1, s_logcount);
729 if SCM_BIGSIGN(n) return scm_logcount(scm_difference(SCM_MAKINUM(-1L), n));
730 ds = SCM_BDIGITS(n);
731 for(i = SCM_NUMDIGS(n); i--; )
732 for(d = ds[i]; d; d >>= 4) c += scm_logtab[15 & d];
733 return SCM_MAKINUM(c);
734 }
735 #else
736 SCM_ASSERT(SCM_INUMP(n), n, SCM_ARG1, s_logcount);
737 #endif
738 if ((nn = SCM_INUM(n)) < 0) nn = -1 - nn;
739 for(; nn; nn >>= 4) c += scm_logtab[15 & nn];
740 return SCM_MAKINUM(c);
741 }
742
743 char scm_ilentab[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4};
744 SCM_PROC(s_integer_length, "integer-length", 1, 0, 0, scm_integer_length);
745 #ifdef __STDC__
746 SCM
747 scm_integer_length(SCM n)
748 #else
749 SCM
750 scm_integer_length(n)
751 SCM n;
752 #endif
753 {
754 register unsigned long c = 0;
755 register long nn;
756 unsigned int l = 4;
757 #ifdef SCM_BIGDIG
758 if SCM_NINUMP(n) {
759 SCM_BIGDIG *ds, d;
760 SCM_ASSERT(SCM_NIMP(n) && SCM_BIGP(n), n, SCM_ARG1, s_integer_length);
761 if SCM_BIGSIGN(n) return scm_integer_length(scm_difference(SCM_MAKINUM(-1L), n));
762 ds = SCM_BDIGITS(n);
763 d = ds[c = SCM_NUMDIGS(n)-1];
764 for(c *= SCM_BITSPERDIG; d; d >>= 4) {c += 4; l = scm_ilentab[15 & d];}
765 return SCM_MAKINUM(c - 4 + l);
766 }
767 #else
768 SCM_ASSERT(SCM_INUMP(n), n, SCM_ARG1, s_integer_length);
769 #endif
770 if ((nn = SCM_INUM(n)) < 0) nn = -1 - nn;
771 for(;nn; nn >>= 4) {c += 4; l = scm_ilentab[15 & nn];}
772 return SCM_MAKINUM(c - 4 + l);
773 }
774
775
776 #ifdef SCM_BIGDIG
777 char s_bignum[] = "bignum";
778 #ifdef __STDC__
779 SCM
780 scm_mkbig(scm_sizet nlen, int sign)
781 #else
782 SCM
783 scm_mkbig(nlen, sign)
784 scm_sizet nlen;
785 int sign;
786 #endif
787 {
788 SCM v = nlen;
789 if (((v << 16) >> 16) != nlen)
790 scm_wta(SCM_MAKINUM(nlen), (char *)SCM_NALLOC, s_bignum);
791 SCM_NEWCELL(v);
792 SCM_DEFER_INTS;
793 SCM_SETCHARS(v, scm_must_malloc((long)(nlen*sizeof(SCM_BIGDIG)), s_bignum));
794 SCM_SETNUMDIGS(v, nlen, sign?scm_tc16_bigneg:scm_tc16_bigpos);
795 SCM_ALLOW_INTS;
796 return v;
797 }
798
799 #ifdef __STDC__
800 SCM
801 scm_big2inum(SCM b, scm_sizet l)
802 #else
803 SCM
804 scm_big2inum(b, l)
805 SCM b;
806 scm_sizet l;
807 #endif
808 {
809 unsigned long num = 0;
810 SCM_BIGDIG *tmp = SCM_BDIGITS(b);
811 while (l--) num = SCM_BIGUP(num) + tmp[l];
812 if (SCM_TYP16(b)==scm_tc16_bigpos) {
813 if SCM_POSFIXABLE(num) return SCM_MAKINUM(num);
814 }
815 else if SCM_UNEGFIXABLE(num) return SCM_MAKINUM(-num);
816 return b;
817 }
818
819
820 char s_adjbig[] = "scm_adjbig";
821 #ifdef __STDC__
822 SCM
823 scm_adjbig(SCM b, scm_sizet nlen)
824 #else
825 SCM
826 scm_adjbig(b, nlen)
827 SCM b;
828 scm_sizet nlen;
829 #endif
830 {
831 long nsiz = nlen;
832 if (((nsiz << 16) >> 16) != nlen) scm_wta(SCM_MAKINUM(nsiz), (char *)SCM_NALLOC, s_adjbig);
833 SCM_DEFER_INTS;
834 SCM_SETCHARS(b, (SCM_BIGDIG *)scm_must_realloc((char *)SCM_CHARS(b),
835 (long)(SCM_NUMDIGS(b)*sizeof(SCM_BIGDIG)),
836 (long)(nsiz*sizeof(SCM_BIGDIG)), s_adjbig));
837 SCM_SETNUMDIGS(b, nsiz, SCM_TYP16(b));
838 SCM_ALLOW_INTS;
839 return b;
840 }
841
842
843 #ifdef __STDC__
844 SCM
845 scm_normbig(SCM b)
846 #else
847 SCM
848 scm_normbig(b)
849 SCM b;
850 #endif
851 {
852 #ifndef _UNICOS
853 scm_sizet nlen = SCM_NUMDIGS(b);
854 #else
855 int nlen = SCM_NUMDIGS(b); /* unsigned nlen breaks on Cray when nlen => 0 */
856 #endif
857 SCM_BIGDIG *zds = SCM_BDIGITS(b);
858 while (nlen-- && !zds[nlen]); nlen++;
859 if (nlen * SCM_BITSPERDIG/SCM_CHAR_BIT <= sizeof(SCM))
860 if SCM_INUMP(b = scm_big2inum(b, (scm_sizet)nlen)) return b;
861 if (SCM_NUMDIGS(b)==nlen) return b;
862 return scm_adjbig(b, (scm_sizet)nlen);
863 }
864
865
866 #ifdef __STDC__
867 SCM
868 scm_copybig(SCM b, int sign)
869 #else
870 SCM
871 scm_copybig(b, sign)
872 SCM b;
873 int sign;
874 #endif
875 {
876 scm_sizet i = SCM_NUMDIGS(b);
877 SCM ans = scm_mkbig(i, sign);
878 SCM_BIGDIG *src = SCM_BDIGITS(b), *dst = SCM_BDIGITS(ans);
879 while (i--) dst[i] = src[i];
880 return ans;
881 }
882
883
884 #ifdef __STDC__
885 SCM
886 scm_long2big(long n)
887 #else
888 SCM
889 scm_long2big(n)
890 long n;
891 #endif
892 {
893 scm_sizet i = 0;
894 SCM_BIGDIG *digits;
895 SCM ans = scm_mkbig(SCM_DIGSPERLONG, n<0);
896 digits = SCM_BDIGITS(ans);
897 if (n < 0) n = -n;
898 while (i < SCM_DIGSPERLONG) {
899 digits[i++] = SCM_BIGLO(n);
900 n = SCM_BIGDN((unsigned long)n);
901 }
902 return ans;
903 }
904
905 #ifdef LONGLONGS
906 #ifdef __STDC__
907 SCM
908 scm_long_long2big(long_long n)
909 #else
910 SCM
911 scm_long_long2big(n)
912 long_long n;
913 #endif
914 {
915 scm_sizet i;
916 SCM_BIGDIG *digits;
917 SCM ans;
918 int n_digits;
919
920 {
921 long tn;
922 tn = (long) n;
923 if ((long long)tn == n)
924 return scm_long2big (tn);
925 }
926
927 {
928 long_long tn;
929
930 for (tn = n, n_digits = 0;
931 tn;
932 ++n_digits, tn = SCM_BIGDN ((ulong_long)tn))
933 ;
934 }
935
936 i = 0;
937 ans = scm_mkbig(n_digits, n<0);
938 digits = SCM_BDIGITS(ans);
939 if (n < 0)
940 n = -n;
941 while (i < n_digits) {
942 digits[i++] = SCM_BIGLO(n);
943 n = SCM_BIGDN((ulong_long)n);
944 }
945 return ans;
946 }
947 #endif
948
949 #ifdef __STDC__
950 SCM
951 scm_2ulong2big(unsigned long * np)
952 #else
953 SCM
954 scm_2ulong2big(np)
955 unsigned long * np;
956 #endif
957 {
958 unsigned long n;
959 scm_sizet i;
960 SCM_BIGDIG *digits;
961 SCM ans;
962
963 ans = scm_mkbig(2 * SCM_DIGSPERLONG, 0);
964 digits = SCM_BDIGITS(ans);
965
966 n = np[0];
967 for (i = 0; i < SCM_DIGSPERLONG; ++i)
968 {
969 digits[i] = SCM_BIGLO(n);
970 n = SCM_BIGDN((unsigned long)n);
971 }
972 n = np[1];
973 for (i = 0; i < SCM_DIGSPERLONG; ++i)
974 {
975 digits[i + SCM_DIGSPERLONG] = SCM_BIGLO(n);
976 n = SCM_BIGDN((unsigned long)n);
977 }
978 return ans;
979 }
980
981
982 #ifdef __STDC__
983 SCM
984 scm_ulong2big(unsigned long n)
985 #else
986 SCM
987 scm_ulong2big(n)
988 unsigned long n;
989 #endif
990 {
991 scm_sizet i = 0;
992 SCM_BIGDIG *digits;
993 SCM ans = scm_mkbig(SCM_DIGSPERLONG, 0);
994 digits = SCM_BDIGITS(ans);
995 while (i < SCM_DIGSPERLONG) {
996 digits[i++] = SCM_BIGLO(n);
997 n = SCM_BIGDN(n);
998 }
999 return ans;
1000 }
1001
1002
1003 #ifdef __STDC__
1004 int
1005 scm_bigcomp(SCM x, SCM y)
1006 #else
1007 int
1008 scm_bigcomp(x, y)
1009 SCM x;
1010 SCM y;
1011 #endif
1012 {
1013 int xsign = SCM_BIGSIGN(x);
1014 int ysign = SCM_BIGSIGN(y);
1015 scm_sizet xlen, ylen;
1016 if (ysign < xsign) return 1;
1017 if (ysign > xsign) return -1;
1018 if ((ylen = SCM_NUMDIGS(y)) > (xlen = SCM_NUMDIGS(x))) return (xsign) ? -1 : 1;
1019 if (ylen < xlen) return (xsign) ? 1 : -1;
1020 while(xlen-- && (SCM_BDIGITS(y)[xlen]==SCM_BDIGITS(x)[xlen]));
1021 if (-1==xlen) return 0;
1022 return (SCM_BDIGITS(y)[xlen] > SCM_BDIGITS(x)[xlen]) ?
1023 (xsign ? -1 : 1) : (xsign ? 1 : -1);
1024 }
1025
1026 #ifndef SCM_DIGSTOOBIG
1027
1028 #ifdef __STDC__
1029 long
1030 scm_pseudolong(long x)
1031 #else
1032 long
1033 scm_pseudolong(x)
1034 long x;
1035 #endif
1036 {
1037 union {
1038 long l;
1039 SCM_BIGDIG bd[SCM_DIGSPERLONG];
1040 } p;
1041 scm_sizet i = 0;
1042 if (x < 0) x = -x;
1043 while (i < SCM_DIGSPERLONG) {p.bd[i++] = SCM_BIGLO(x); x = SCM_BIGDN(x);}
1044 /* p.bd[0] = SCM_BIGLO(x); p.bd[1] = SCM_BIGDN(x); */
1045 return p.l;
1046 }
1047
1048 #else
1049
1050 #ifdef __STDC__
1051 void
1052 scm_longdigs(long x, SCM_BIGDIG digs[])
1053 #else
1054 void
1055 scm_longdigs(x, digs)
1056 long x;
1057 SCM_BIGDIG digs[];
1058 #endif
1059 {
1060 scm_sizet i = 0;
1061 if (x < 0) x = -x;
1062 while (i < SCM_DIGSPERLONG) {digs[i++] = SCM_BIGLO(x); x = SCM_BIGDN(x);}
1063 }
1064 #endif
1065
1066
1067 #ifdef __STDC__
1068 SCM
1069 scm_addbig(SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny)
1070 #else
1071 SCM
1072 scm_addbig(x, nx, xsgn, bigy, sgny)
1073 SCM_BIGDIG *x;
1074 scm_sizet nx;
1075 int xsgn;
1076 SCM bigy;
1077 int sgny;
1078 #endif
1079 {
1080 /* Assumes nx <= SCM_NUMDIGS(bigy) */
1081 /* Assumes xsgn and sgny scm_equal either 0 or 0x0100 */
1082 long num = 0;
1083 scm_sizet i = 0, ny = SCM_NUMDIGS(bigy);
1084 SCM z = scm_copybig(bigy, SCM_BIGSIGN(bigy) ^ sgny);
1085 SCM_BIGDIG *zds = SCM_BDIGITS(z);
1086 if (xsgn ^ SCM_BIGSIGN(z)) {
1087 do {
1088 num += (long) zds[i] - x[i];
1089 if (num < 0) {zds[i] = num + SCM_BIGRAD; num = -1;}
1090 else {zds[i] = SCM_BIGLO(num); num = 0;}
1091 } while (++i < nx);
1092 if (num && nx==ny) {
1093 num = 1; i = 0;
1094 SCM_CAR(z) ^= 0x0100;
1095 do {
1096 num += (SCM_BIGRAD-1) - zds[i];
1097 zds[i++] = SCM_BIGLO(num);
1098 num = SCM_BIGDN(num);
1099 } while (i < ny);
1100 }
1101 else while (i < ny) {
1102 num += zds[i];
1103 if (num < 0) {zds[i++] = num + SCM_BIGRAD; num = -1;}
1104 else {zds[i++] = SCM_BIGLO(num); num = 0;}
1105 }
1106 } else {
1107 do {
1108 num += (long) zds[i] + x[i];
1109 zds[i++] = SCM_BIGLO(num);
1110 num = SCM_BIGDN(num);
1111 } while (i < nx);
1112 if (!num) return z;
1113 while (i < ny) {
1114 num += zds[i];
1115 zds[i++] = SCM_BIGLO(num);
1116 num = SCM_BIGDN(num);
1117 if (!num) return z;
1118 }
1119 if (num) {z = scm_adjbig(z, ny+1); SCM_BDIGITS(z)[ny] = num; return z;}
1120 }
1121 return scm_normbig(z);
1122 }
1123
1124 #ifdef __STDC__
1125 SCM
1126 scm_mulbig(SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn)
1127 #else
1128 SCM
1129 scm_mulbig(x, nx, y, ny, sgn)
1130 SCM_BIGDIG *x;
1131 scm_sizet nx;
1132 SCM_BIGDIG *y;
1133 scm_sizet ny;
1134 int sgn;
1135 #endif
1136 {
1137 scm_sizet i = 0, j = nx + ny;
1138 unsigned long n = 0;
1139 SCM z = scm_mkbig(j, sgn);
1140 SCM_BIGDIG *zds = SCM_BDIGITS(z);
1141 while (j--) zds[j] = 0;
1142 do {
1143 j = 0;
1144 if (x[i]) {
1145 do {
1146 n += zds[i + j] + ((unsigned long) x[i] * y[j]);
1147 zds[i + j++] = SCM_BIGLO(n);
1148 n = SCM_BIGDN(n);
1149 } while (j < ny);
1150 if (n) {zds[i + j] = n; n = 0;}
1151 }
1152 } while (++i < nx);
1153 return scm_normbig(z);
1154 }
1155
1156 #ifdef __STDC__
1157 unsigned int
1158 scm_divbigdig(SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div)
1159 #else
1160 unsigned int
1161 scm_divbigdig(ds, h, div)
1162 SCM_BIGDIG *ds;
1163 scm_sizet h;
1164 SCM_BIGDIG div;
1165 #endif
1166 {
1167 register unsigned long t2 = 0;
1168 while(h--) {
1169 t2 = SCM_BIGUP(t2) + ds[h];
1170 ds[h] = t2 / div;
1171 t2 %= div;
1172 }
1173 return t2;
1174 }
1175
1176
1177 #ifdef __STDC__
1178 SCM
1179 scm_divbigint(SCM x, long z, int sgn, int mode)
1180 #else
1181 SCM
1182 scm_divbigint(x, z, sgn, mode)
1183 SCM x;
1184 long z;
1185 int sgn;
1186 int mode;
1187 #endif
1188 {
1189 if (z < 0) z = -z;
1190 if (z < SCM_BIGRAD) {
1191 register unsigned long t2 = 0;
1192 register SCM_BIGDIG *ds = SCM_BDIGITS(x);
1193 scm_sizet nd = SCM_NUMDIGS(x);
1194 while(nd--) t2 = (SCM_BIGUP(t2) + ds[nd]) % z;
1195 if (mode) t2 = z - t2;
1196 return SCM_MAKINUM(sgn ? -t2 : t2);
1197 }
1198 {
1199 #ifndef SCM_DIGSTOOBIG
1200 unsigned long t2 = scm_pseudolong(z);
1201 return scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), (SCM_BIGDIG *)&t2,
1202 SCM_DIGSPERLONG, sgn, mode);
1203 #else
1204 SCM_BIGDIG t2[SCM_DIGSPERLONG];
1205 scm_longdigs(z, t2);
1206 return scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), t2, SCM_DIGSPERLONG, sgn, mode);
1207 #endif
1208 }
1209 }
1210
1211 #ifdef __STDC__
1212 SCM
1213 scm_divbigbig(SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn, int modes)
1214 #else
1215 SCM
1216 scm_divbigbig(x, nx, y, ny, sgn, modes)
1217 SCM_BIGDIG *x;
1218 scm_sizet nx;
1219 SCM_BIGDIG *y;
1220 scm_sizet ny;
1221 int sgn;
1222 int modes;
1223 #endif
1224 {
1225 /* modes description
1226 0 remainder
1227 1 scm_modulo
1228 2 quotient
1229 3 quotient but returns 0 if division is not exact. */
1230 scm_sizet i = 0, j = 0;
1231 long num = 0;
1232 unsigned long t2 = 0;
1233 SCM z, newy;
1234 SCM_BIGDIG d = 0, qhat, *zds, *yds;
1235 /* algorithm requires nx >= ny */
1236 if (nx < ny)
1237 switch (modes) {
1238 case 0: /* remainder -- just return x */
1239 z = scm_mkbig(nx, sgn); zds = SCM_BDIGITS(z);
1240 do {zds[i] = x[i];} while (++i < nx);
1241 return z;
1242 case 1: /* scm_modulo -- return y-x */
1243 z = scm_mkbig(ny, sgn); zds = SCM_BDIGITS(z);
1244 do {
1245 num += (long) y[i] - x[i];
1246 if (num < 0) {zds[i] = num + SCM_BIGRAD; num = -1;}
1247 else {zds[i] = num; num = 0;}
1248 } while (++i < nx);
1249 while (i < ny) {
1250 num += y[i];
1251 if (num < 0) {zds[i++] = num + SCM_BIGRAD; num = -1;}
1252 else {zds[i++] = num; num = 0;}
1253 }
1254 goto doadj;
1255 case 2: return SCM_INUM0; /* quotient is zero */
1256 case 3: return 0; /* the division is not exact */
1257 }
1258
1259 z = scm_mkbig(nx==ny ? nx+2 : nx+1, sgn); zds = SCM_BDIGITS(z);
1260 if (nx==ny) zds[nx+1] = 0;
1261 while(!y[ny-1]) ny--; /* in case y came in as a psuedolong */
1262 if (y[ny-1] < (SCM_BIGRAD>>1)) { /* normalize operands */
1263 d = SCM_BIGRAD/(y[ny-1]+1);
1264 newy = scm_mkbig(ny, 0); yds = SCM_BDIGITS(newy);
1265 while(j < ny)
1266 {t2 += (unsigned long) y[j]*d; yds[j++] = SCM_BIGLO(t2); t2 = SCM_BIGDN(t2);}
1267 y = yds; j = 0; t2 = 0;
1268 while(j < nx)
1269 {t2 += (unsigned long) x[j]*d; zds[j++] = SCM_BIGLO(t2); t2 = SCM_BIGDN(t2);}
1270 zds[j] = t2;
1271 }
1272 else {zds[j = nx] = 0; while (j--) zds[j] = x[j];}
1273 j = nx==ny ? nx+1 : nx; /* dividend needs more digits than divisor */
1274 do { /* loop over digits of quotient */
1275 if (zds[j]==y[ny-1]) qhat = SCM_BIGRAD-1;
1276 else qhat = (SCM_BIGUP(zds[j]) + zds[j-1])/y[ny-1];
1277 if (!qhat) continue;
1278 i = 0; num = 0; t2 = 0;
1279 do { /* multiply and subtract */
1280 t2 += (unsigned long) y[i] * qhat;
1281 num += zds[j - ny + i] - SCM_BIGLO(t2);
1282 if (num < 0) {zds[j - ny + i] = num + SCM_BIGRAD; num = -1;}
1283 else {zds[j - ny + i] = num; num = 0;}
1284 t2 = SCM_BIGDN(t2);
1285 } while (++i < ny);
1286 num += zds[j - ny + i] - t2; /* borrow from high digit; don't update */
1287 while (num) { /* "add back" required */
1288 i = 0; num = 0; qhat--;
1289 do {
1290 num += (long) zds[j - ny + i] + y[i];
1291 zds[j - ny + i] = SCM_BIGLO(num);
1292 num = SCM_BIGDN(num);
1293 } while (++i < ny);
1294 num--;
1295 }
1296 if (modes & 2) zds[j] = qhat;
1297 } while (--j >= ny);
1298 switch (modes) {
1299 case 3: /* check that remainder==0 */
1300 for(j = ny;j && !zds[j-1];--j) ; if (j) return 0;
1301 case 2: /* move quotient down in z */
1302 j = (nx==ny ? nx+2 : nx+1) - ny;
1303 for (i = 0;i < j;i++) zds[i] = zds[i+ny];
1304 ny = i;
1305 break;
1306 case 1: /* subtract for scm_modulo */
1307 i = 0; num = 0; j = 0;
1308 do {num += y[i] - zds[i];
1309 j = j | zds[i];
1310 if (num < 0) {zds[i] = num + SCM_BIGRAD; num = -1;}
1311 else {zds[i] = num; num = 0;}
1312 } while (++i < ny);
1313 if (!j) return SCM_INUM0;
1314 case 0: /* just normalize remainder */
1315 if (d) scm_divbigdig(zds, ny, d);
1316 }
1317 doadj:
1318 for(j = ny;j && !zds[j-1];--j) ;
1319 if (j * SCM_BITSPERDIG <= sizeof(SCM)*SCM_CHAR_BIT)
1320 if SCM_INUMP(z = scm_big2inum(z, j)) return z;
1321 return scm_adjbig(z, j);
1322 }
1323 #endif
1324
1325
1326
1327 \f
1328
1329 /*** NUMBERS -> STRINGS ***/
1330 #ifdef SCM_FLOATS
1331 int scm_dblprec;
1332 static double fx[] = {0.0, 5e-1, 5e-2, 5e-3, 5e-4, 5e-5,
1333 5e-6, 5e-7, 5e-8, 5e-9, 5e-10,
1334 5e-11,5e-12,5e-13,5e-14,5e-15,
1335 5e-16,5e-17,5e-18,5e-19,5e-20};
1336
1337
1338
1339 #ifdef __STDC__
1340 static scm_sizet
1341 idbl2str(double f, char *a)
1342 #else
1343 static scm_sizet
1344 idbl2str(f, a)
1345 double f;
1346 char *a;
1347 #endif
1348 {
1349 int efmt, dpt, d, i, wp = scm_dblprec;
1350 scm_sizet ch = 0;
1351 int exp = 0;
1352
1353 if (f == 0.0) goto zero; /*{a[0]='0'; a[1]='.'; a[2]='0'; return 3;}*/
1354 if (f < 0.0) {f = -f;a[ch++]='-';}
1355 else if (f > 0.0) ;
1356 else goto funny;
1357 if (IS_INF(f))
1358 {
1359 if (ch == 0) a[ch++]='+';
1360 funny: a[ch++]='#'; a[ch++]='.'; a[ch++]='#'; return ch;
1361 }
1362 # ifdef DBL_MIN_10_EXP /* Prevent unnormalized values, as from
1363 make-uniform-vector, from causing infinite loops. */
1364 while (f < 1.0) {f *= 10.0; if (exp-- < DBL_MIN_10_EXP) goto funny;}
1365 while (f > 10.0) {f *= 0.10; if (exp++ > DBL_MAX_10_EXP) goto funny;}
1366 # else
1367 while (f < 1.0) {f *= 10.0; exp--;}
1368 while (f > 10.0) {f /= 10.0; exp++;}
1369 # endif
1370 if (f+fx[wp] >= 10.0) {f = 1.0; exp++;}
1371 zero:
1372 # ifdef ENGNOT
1373 dpt = (exp+9999)%3;
1374 exp -= dpt++;
1375 efmt = 1;
1376 # else
1377 efmt = (exp < -3) || (exp > wp+2);
1378 if (!efmt)
1379 if (exp < 0) {
1380 a[ch++] = '0';
1381 a[ch++] = '.';
1382 dpt = exp;
1383 while (++dpt) a[ch++] = '0';
1384 } else
1385 dpt = exp+1;
1386 else
1387 dpt = 1;
1388 # endif
1389
1390 do {
1391 d = f;
1392 f -= d;
1393 a[ch++] = d+'0';
1394 if (f < fx[wp]) break;
1395 if (f+fx[wp] >= 1.0) {
1396 a[ch-1]++;
1397 break;
1398 }
1399 f *= 10.0;
1400 if (!(--dpt)) a[ch++] = '.';
1401 } while (wp--);
1402
1403 if (dpt > 0)
1404 # ifndef ENGNOT
1405 if ((dpt > 4) && (exp > 6)) {
1406 d = (a[0]=='-'?2:1);
1407 for (i = ch++; i > d; i--)
1408 a[i] = a[i-1];
1409 a[d] = '.';
1410 efmt = 1;
1411 } else
1412 # endif
1413 {
1414 while (--dpt) a[ch++] = '0';
1415 a[ch++] = '.';
1416 }
1417 if (a[ch-1]=='.') a[ch++]='0'; /* trailing zero */
1418 if (efmt && exp) {
1419 a[ch++] = 'e';
1420 if (exp < 0) {
1421 exp = -exp;
1422 a[ch++] = '-';
1423 }
1424 for (i = 10; i <= exp; i *= 10);
1425 for (i /= 10; i; i /= 10) {
1426 a[ch++] = exp/i + '0';
1427 exp %= i;
1428 }
1429 }
1430 return ch;
1431 }
1432
1433 #ifdef __STDC__
1434 static scm_sizet
1435 iflo2str(SCM flt, char *str)
1436 #else
1437 static scm_sizet
1438 iflo2str(flt, str)
1439 SCM flt;
1440 char *str;
1441 #endif
1442 {
1443 scm_sizet i;
1444 # ifdef SCM_SINGLES
1445 if SCM_SINGP(flt) i = idbl2str(SCM_FLO(flt), str);
1446 else
1447 # endif
1448 i = idbl2str(SCM_REAL(flt), str);
1449 if SCM_CPLXP(flt) {
1450 if(0 <= SCM_IMAG(flt)) /* jeh */
1451 str[i++] = '+'; /* jeh */
1452 i += idbl2str(SCM_IMAG(flt), &str[i]);
1453 str[i++] = 'i';
1454 }
1455 return i;
1456 }
1457 #endif /* SCM_FLOATS */
1458
1459 #ifdef __STDC__
1460 scm_sizet
1461 scm_iint2str(long num, int rad, char *p)
1462 #else
1463 scm_sizet
1464 scm_iint2str(num, rad, p)
1465 long num;
1466 int rad;
1467 char *p;
1468 #endif
1469 {
1470 scm_sizet j;
1471 register int i = 1, d;
1472 register long n = num;
1473 if (n < 0) {n = -n; i++;}
1474 for (n /= rad;n > 0;n /= rad) i++;
1475 j = i;
1476 n = num;
1477 if (n < 0) {n = -n; *p++ = '-'; i--;}
1478 while (i--) {
1479 d = n % rad;
1480 n /= rad;
1481 p[i] = d + ((d < 10) ? '0' : 'a' - 10);
1482 }
1483 return j;
1484 }
1485
1486
1487 #ifdef SCM_BIGDIG
1488 #ifdef __STDC__
1489 static SCM
1490 big2str(SCM b, register unsigned int radix)
1491 #else
1492 static SCM
1493 big2str(b, radix)
1494 SCM b;
1495 register unsigned int radix;
1496 #endif
1497 {
1498 SCM t = scm_copybig(b, 0); /* sign of temp doesn't matter */
1499 register SCM_BIGDIG *ds = SCM_BDIGITS(t);
1500 scm_sizet i = SCM_NUMDIGS(t);
1501 scm_sizet j = radix==16 ? (SCM_BITSPERDIG*i)/4+2
1502 : radix >= 10 ? (SCM_BITSPERDIG*i*241L)/800+2
1503 : (SCM_BITSPERDIG*i)+2;
1504 scm_sizet k = 0;
1505 scm_sizet radct = 0;
1506 scm_sizet ch; /* jeh */
1507 SCM_BIGDIG radpow = 1, radmod = 0;
1508 SCM ss = scm_makstr((long)j, 0);
1509 char *s = SCM_CHARS(ss), c;
1510 while ((long) radpow * radix < SCM_BIGRAD) {
1511 radpow *= radix;
1512 radct++;
1513 }
1514 s[0] = scm_tc16_bigneg==SCM_TYP16(b) ? '-' : '+';
1515 while ((i || radmod) && j) {
1516 if (k == 0) {
1517 radmod = (SCM_BIGDIG)scm_divbigdig(ds, i, radpow);
1518 k = radct;
1519 if (!ds[i-1]) i--;
1520 }
1521 c = radmod % radix; radmod /= radix; k--;
1522 s[--j] = c < 10 ? c + '0' : c + 'a' - 10;
1523 }
1524 ch = s[0] == '-' ? 1 : 0; /* jeh */
1525 if (ch < j) { /* jeh */
1526 for(i = j;j < SCM_LENGTH(ss);j++) s[ch+j-i] = s[j]; /* jeh */
1527 scm_vector_set_length_x(ss, (SCM)SCM_MAKINUM(ch+SCM_LENGTH(ss)-i)); /* jeh */
1528 }
1529 return ss;
1530 }
1531 #endif
1532
1533
1534 SCM_PROC(s_number_to_string, "number->string", 1, 1, 0, scm_number_to_string);
1535 #ifdef __STDC__
1536 SCM
1537 scm_number_to_string(SCM x, SCM radix)
1538 #else
1539 SCM
1540 scm_number_to_string(x, radix)
1541 SCM x;
1542 SCM radix;
1543 #endif
1544 {
1545 if SCM_UNBNDP(radix) radix=SCM_MAKINUM(10L);
1546 else SCM_ASSERT(SCM_INUMP(radix), radix, SCM_ARG2, s_number_to_string);
1547 #ifdef SCM_FLOATS
1548 if SCM_NINUMP(x) {
1549 char num_buf[SCM_FLOBUFLEN];
1550 # ifdef SCM_BIGDIG
1551 SCM_ASRTGO(SCM_NIMP(x), badx);
1552 if SCM_BIGP(x) return big2str(x, (unsigned int)SCM_INUM(radix));
1553 # ifndef RECKLESS
1554 if (!(SCM_INEXP(x)))
1555 badx: scm_wta(x, (char *)SCM_ARG1, s_number_to_string);
1556 # endif
1557 # else
1558 SCM_ASSERT(SCM_NIMP(x) && SCM_INEXP(x), x, SCM_ARG1, s_number_to_string);
1559 # endif
1560 return scm_makfromstr(num_buf, iflo2str(x, num_buf), 0);
1561 }
1562 #else
1563 # ifdef SCM_BIGDIG
1564 if SCM_NINUMP(x) {
1565 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_number_to_string);
1566 return big2str(x, (unsigned int)SCM_INUM(radix));
1567 }
1568 # else
1569 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_number_to_string);
1570 # endif
1571 #endif
1572 {
1573 char num_buf[SCM_INTBUFLEN];
1574 return scm_makfromstr(num_buf,
1575 scm_iint2str(SCM_INUM(x), (int)SCM_INUM(radix), num_buf), 0);
1576 }
1577 }
1578
1579
1580 /* These print routines are stubbed here so that scm_repl.c doesn't need
1581 SCM_FLOATS or SCM_BIGDIGs conditionals */
1582 #ifdef __STDC__
1583 int
1584 scm_floprint(SCM sexp, SCM port, scm_print_state *pstate)
1585 #else
1586 int
1587 scm_floprint(sexp, port, pstate)
1588 SCM sexp;
1589 SCM port;
1590 scm_print_state *pstate;
1591 #endif
1592 {
1593 #ifdef SCM_FLOATS
1594 char num_buf[SCM_FLOBUFLEN];
1595 scm_gen_write (scm_regular_string, num_buf, iflo2str(sexp, num_buf), port);
1596 #else
1597 scm_ipruk("float", sexp, port);
1598 #endif
1599 return !0;
1600 }
1601
1602
1603 #ifdef __STDC__
1604 int
1605 scm_bigprint(SCM exp, SCM port, scm_print_state *pstate)
1606 #else
1607 int
1608 scm_bigprint(exp, port, pstate)
1609 SCM exp;
1610 SCM port;
1611 scm_print_state *pstate;
1612 #endif
1613 {
1614 #ifdef SCM_BIGDIG
1615 exp = big2str(exp, (unsigned int)10);
1616 scm_gen_write (scm_regular_string, SCM_CHARS(exp), (scm_sizet)SCM_LENGTH(exp), port);
1617 #else
1618 scm_ipruk("bignum", exp, port);
1619 #endif
1620 return !0;
1621 }
1622 /*** END nums->strs ***/
1623
1624 /*** STRINGS -> NUMBERS ***/
1625
1626 static SCM
1627 scm_small_istr2int(str, len, radix)
1628 char *str;
1629 long len;
1630 long radix;
1631 {
1632 register long n = 0, ln;
1633 register int c;
1634 register int i = 0;
1635 int lead_neg = 0;
1636 if (0 >= len) return SCM_BOOL_F; /* zero scm_length */
1637 switch (*str) { /* leading sign */
1638 case '-': lead_neg = 1;
1639 case '+': if (++i==len) return SCM_BOOL_F; /* bad if lone `+' or `-' */
1640 }
1641
1642 do {
1643 switch (c = str[i++]) {
1644 case DIGITS:
1645 c = c - '0';
1646 goto accumulate;
1647 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1648 c = c-'A'+10;
1649 goto accumulate;
1650 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1651 c = c-'a'+10;
1652 accumulate:
1653 if (c >= radix) return SCM_BOOL_F; /* bad digit for radix */
1654 ln = n;
1655 n = n * radix - c;
1656 /* Negation is a workaround for HP700 cc bug */
1657 if (n > ln || (-n > -SCM_MOST_NEGATIVE_FIXNUM)) goto ovfl;
1658 break;
1659 default:
1660 return SCM_BOOL_F; /* not a digit */
1661 }
1662 } while (i < len);
1663 if (!lead_neg) if ((n = -n) > SCM_MOST_POSITIVE_FIXNUM) goto ovfl;
1664 return SCM_MAKINUM(n);
1665 ovfl: /* overflow scheme integer */
1666 return SCM_BOOL_F;
1667 }
1668
1669
1670 #ifdef __STDC__
1671 SCM
1672 scm_istr2int(char *str, long len, long radix)
1673 #else
1674 SCM
1675 scm_istr2int(str, len, radix)
1676 char *str;
1677 long len;
1678 long radix;
1679 #endif
1680 {
1681 scm_sizet j;
1682 register scm_sizet k, blen = 1;
1683 scm_sizet i = 0;
1684 int c;
1685 SCM res;
1686 register SCM_BIGDIG *ds;
1687 register unsigned long t2;
1688
1689 if (0 >= len) return SCM_BOOL_F; /* zero scm_length */
1690
1691 /* Short numbers we parse directly into an int, to avoid the overhead
1692 of creating a bignum. */
1693 if (len < 6)
1694 return scm_small_istr2int (str, len, radix);
1695
1696 if (16==radix) j = 1+(4*len*sizeof(char))/(SCM_BITSPERDIG);
1697 else if (10 <= radix)
1698 j = 1+(84*len*sizeof(char))/(SCM_BITSPERDIG*25);
1699 else j = 1+(len*sizeof(char))/(SCM_BITSPERDIG);
1700 switch (str[0]) { /* leading sign */
1701 case '-':
1702 case '+': if (++i==len) return SCM_BOOL_F; /* bad if lone `+' or `-' */
1703 }
1704 res = scm_mkbig(j, '-'==str[0]);
1705 ds = SCM_BDIGITS(res);
1706 for (k = j;k--;) ds[k] = 0;
1707 do {
1708 switch (c = str[i++]) {
1709 case DIGITS:
1710 c = c - '0';
1711 goto accumulate;
1712 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1713 c = c-'A'+10;
1714 goto accumulate;
1715 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1716 c = c-'a'+10;
1717 accumulate:
1718 if (c >= radix) return SCM_BOOL_F; /* bad digit for radix */
1719 k = 0;
1720 t2 = c;
1721 moretodo:
1722 while(k < blen) {
1723 /* printf("k = %d, blen = %d, t2 = %ld, ds[k] = %d\n", k, blen, t2, ds[k]);*/
1724 t2 += ds[k]*radix;
1725 ds[k++] = SCM_BIGLO(t2);
1726 t2 = SCM_BIGDN(t2);
1727 }
1728 if (blen > j)
1729 scm_num_overflow ("bignum");
1730 if (t2) {blen++; goto moretodo;}
1731 break;
1732 default:
1733 return SCM_BOOL_F; /* not a digit */
1734 }
1735 } while (i < len);
1736 if (blen * SCM_BITSPERDIG/SCM_CHAR_BIT <= sizeof(SCM))
1737 if SCM_INUMP(res = scm_big2inum(res, blen)) return res;
1738 if (j==blen) return res;
1739 return scm_adjbig(res, blen);
1740 }
1741
1742 #ifdef SCM_FLOATS
1743 #ifdef __STDC__
1744 SCM
1745 scm_istr2flo(char *str, long len, long radix)
1746 #else
1747 SCM
1748 scm_istr2flo(str, len, radix)
1749 char *str;
1750 long len;
1751 long radix;
1752 #endif
1753 {
1754 register int c, i = 0;
1755 double lead_sgn;
1756 double res = 0.0, tmp = 0.0;
1757 int flg = 0;
1758 int point = 0;
1759 SCM second;
1760
1761 if (i >= len) return SCM_BOOL_F; /* zero scm_length */
1762
1763 switch (*str) { /* leading sign */
1764 case '-': lead_sgn = -1.0; i++; break;
1765 case '+': lead_sgn = 1.0; i++; break;
1766 default : lead_sgn = 0.0;
1767 }
1768 if (i==len) return SCM_BOOL_F; /* bad if lone `+' or `-' */
1769
1770 if (str[i]=='i' || str[i]=='I') { /* handle `+i' and `-i' */
1771 if (lead_sgn==0.0) return SCM_BOOL_F; /* must have leading sign */
1772 if (++i < len) return SCM_BOOL_F; /* `i' not last character */
1773 return scm_makdbl(0.0, lead_sgn);
1774 }
1775 do { /* check initial digits */
1776 switch (c = str[i]) {
1777 case DIGITS:
1778 c = c - '0';
1779 goto accum1;
1780 case 'D': case 'E': case 'F':
1781 if (radix==10) goto out1; /* must be exponent */
1782 case 'A': case 'B': case 'C':
1783 c = c-'A'+10;
1784 goto accum1;
1785 case 'd': case 'e': case 'f':
1786 if (radix==10) goto out1;
1787 case 'a': case 'b': case 'c':
1788 c = c-'a'+10;
1789 accum1:
1790 if (c >= radix) return SCM_BOOL_F; /* bad digit for radix */
1791 res = res * radix + c;
1792 flg = 1; /* res is valid */
1793 break;
1794 default:
1795 goto out1;
1796 }
1797 } while (++i < len);
1798 out1:
1799
1800 /* if true, then we did see a digit above, and res is valid */
1801 if (i==len) goto done;
1802
1803 /* By here, must have seen a digit,
1804 or must have next char be a `.' with radix==10 */
1805 if (!flg)
1806 if (!(str[i]=='.' && radix==10))
1807 return SCM_BOOL_F;
1808
1809 while (str[i]=='#') { /* optional sharps */
1810 res *= radix;
1811 if (++i==len) goto done;
1812 }
1813
1814 if (str[i]=='/') {
1815 while (++i < len) {
1816 switch (c = str[i]) {
1817 case DIGITS:
1818 c = c - '0';
1819 goto accum2;
1820 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1821 c = c-'A'+10;
1822 goto accum2;
1823 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1824 c = c-'a'+10;
1825 accum2:
1826 if (c >= radix) return SCM_BOOL_F;
1827 tmp = tmp * radix + c;
1828 break;
1829 default:
1830 goto out2;
1831 }
1832 }
1833 out2:
1834 if (tmp==0.0) return SCM_BOOL_F; /* `slash zero' not allowed */
1835 if (i < len)
1836 while (str[i]=='#') { /* optional sharps */
1837 tmp *= radix;
1838 if (++i==len) break;
1839 }
1840 res /= tmp;
1841 goto done;
1842 }
1843
1844 if (str[i]=='.') { /* decimal point notation */
1845 if (radix != 10) return SCM_BOOL_F; /* must be radix 10 */
1846 while (++i < len) {
1847 switch (c = str[i]) {
1848 case DIGITS:
1849 point--;
1850 res = res*10.0 + c-'0';
1851 flg = 1;
1852 break;
1853 default:
1854 goto out3;
1855 }
1856 }
1857 out3:
1858 if (!flg) return SCM_BOOL_F; /* no digits before or after decimal point */
1859 if (i==len) goto adjust;
1860 while (str[i]=='#') { /* ignore remaining sharps */
1861 if (++i==len) goto adjust;
1862 }
1863 }
1864
1865 switch (str[i]) { /* exponent */
1866 case 'd': case 'D':
1867 case 'e': case 'E':
1868 case 'f': case 'F':
1869 case 'l': case 'L':
1870 case 's': case 'S': {
1871 int expsgn = 1, expon = 0;
1872 if (radix != 10) return SCM_BOOL_F; /* only in radix 10 */
1873 if (++i==len) return SCM_BOOL_F; /* bad exponent */
1874 switch (str[i]) {
1875 case '-': expsgn=(-1);
1876 case '+': if (++i==len) return SCM_BOOL_F; /* bad exponent */
1877 }
1878 if (str[i] < '0' || str[i] > '9') return SCM_BOOL_F; /* bad exponent */
1879 do {
1880 switch (c = str[i]) {
1881 case DIGITS:
1882 expon = expon*10 + c-'0';
1883 if (expon > MAXEXP) return SCM_BOOL_F; /* exponent too large */
1884 break;
1885 default:
1886 goto out4;
1887 }
1888 } while (++i < len);
1889 out4:
1890 point += expsgn*expon;
1891 }
1892 }
1893
1894 adjust:
1895 if (point >= 0)
1896 while (point--) res *= 10.0;
1897 else
1898 # ifdef _UNICOS
1899 while (point++) res *= 0.1;
1900 # else
1901 while (point++) res /= 10.0;
1902 # endif
1903
1904 done:
1905 /* at this point, we have a legitimate floating point result */
1906 if (lead_sgn==-1.0) res = -res;
1907 if (i==len) return scm_makdbl(res, 0.0);
1908
1909 if (str[i]=='i' || str[i]=='I') { /* pure imaginary number */
1910 if (lead_sgn==0.0) return SCM_BOOL_F; /* must have leading sign */
1911 if (++i < len) return SCM_BOOL_F; /* `i' not last character */
1912 return scm_makdbl(0.0, res);
1913 }
1914
1915 switch (str[i++]) {
1916 case '-': lead_sgn = -1.0; break;
1917 case '+': lead_sgn = 1.0; break;
1918 case '@': { /* polar input for complex number */
1919 /* get a `real' for scm_angle */
1920 second = scm_istr2flo(&str[i], (long)(len-i), radix);
1921 if (!(SCM_INEXP(second))) return SCM_BOOL_F; /* not `real' */
1922 if (SCM_CPLXP(second)) return SCM_BOOL_F; /* not `real' */
1923 tmp = SCM_REALPART(second);
1924 return scm_makdbl(res*cos(tmp), res*sin(tmp));
1925 }
1926 default: return SCM_BOOL_F;
1927 }
1928
1929 /* at this point, last char must be `i' */
1930 if (str[len-1] != 'i' && str[len-1] != 'I') return SCM_BOOL_F;
1931 /* handles `x+i' and `x-i' */
1932 if (i==(len-1)) return scm_makdbl(res, lead_sgn);
1933 /* get a `ureal' for complex part */
1934 second = scm_istr2flo(&str[i], (long)((len-i)-1), radix);
1935 if (!(SCM_INEXP(second))) return SCM_BOOL_F; /* not `ureal' */
1936 if (SCM_CPLXP(second)) return SCM_BOOL_F; /* not `ureal' */
1937 tmp = SCM_REALPART(second);
1938 if (tmp < 0.0) return SCM_BOOL_F; /* not `ureal' */
1939 return scm_makdbl(res, (lead_sgn*tmp));
1940 }
1941 #endif /* SCM_FLOATS */
1942
1943
1944 #ifdef __STDC__
1945 SCM
1946 scm_istring2number(char *str, long len, long radix)
1947 #else
1948 SCM
1949 scm_istring2number(str, len, radix)
1950 char *str;
1951 long len;
1952 long radix;
1953 #endif
1954 {
1955 int i = 0;
1956 char ex = 0;
1957 char ex_p = 0, rx_p = 0; /* Only allow 1 exactness and 1 radix prefix */
1958 SCM res;
1959 if (len==1)
1960 if (*str=='+' || *str=='-') /* Catches lone `+' and `-' for speed */
1961 return SCM_BOOL_F;
1962
1963 while ((len-i) >= 2 && str[i]=='#' && ++i)
1964 switch (str[i++]) {
1965 case 'b': case 'B': if (rx_p++) return SCM_BOOL_F; radix = 2; break;
1966 case 'o': case 'O': if (rx_p++) return SCM_BOOL_F; radix = 8; break;
1967 case 'd': case 'D': if (rx_p++) return SCM_BOOL_F; radix = 10; break;
1968 case 'x': case 'X': if (rx_p++) return SCM_BOOL_F; radix = 16; break;
1969 case 'i': case 'I': if (ex_p++) return SCM_BOOL_F; ex = 2; break;
1970 case 'e': case 'E': if (ex_p++) return SCM_BOOL_F; ex = 1; break;
1971 default: return SCM_BOOL_F;
1972 }
1973
1974 switch (ex) {
1975 case 1:
1976 return scm_istr2int(&str[i], len-i, radix);
1977 case 0:
1978 res = scm_istr2int(&str[i], len-i, radix);
1979 if SCM_NFALSEP(res) return res;
1980 #ifdef SCM_FLOATS
1981 case 2: return scm_istr2flo(&str[i], len-i, radix);
1982 #endif
1983 }
1984 return SCM_BOOL_F;
1985 }
1986
1987
1988 SCM_PROC(s_string_to_number, "string->number", 1, 1, 0, scm_string_to_number);
1989 #ifdef __STDC__
1990 SCM
1991 scm_string_to_number(SCM str, SCM radix)
1992 #else
1993 SCM
1994 scm_string_to_number(str, radix)
1995 SCM str;
1996 SCM radix;
1997 #endif
1998 {
1999 SCM answer;
2000 if SCM_UNBNDP(radix) radix=SCM_MAKINUM(10L);
2001 else SCM_ASSERT(SCM_INUMP(radix), radix, SCM_ARG2, s_string_to_number);
2002 SCM_ASSERT(SCM_NIMP(str) && SCM_ROSTRINGP(str), str, SCM_ARG1, s_string_to_number);
2003 answer = scm_istring2number(SCM_ROCHARS(str), SCM_ROLENGTH(str), SCM_INUM(radix));
2004 return scm_return_first (answer, str);
2005 }
2006 /*** END strs->nums ***/
2007
2008 #ifdef SCM_FLOATS
2009 #ifdef __STDC__
2010 SCM
2011 scm_makdbl (double x, double y)
2012 #else
2013 SCM
2014 scm_makdbl (x, y)
2015 double x;
2016 double y;
2017 #endif
2018 {
2019 SCM z;
2020 if ((y==0.0) && (x==0.0)) return scm_flo0;
2021 SCM_NEWCELL(z);
2022 SCM_DEFER_INTS;
2023 if (y==0.0) {
2024 # ifdef SCM_SINGLES
2025 float fx = x;
2026 # ifndef SCM_SINGLESONLY
2027 if ((-FLTMAX < x) && (x < FLTMAX) && (fx==x))
2028 # endif
2029 {
2030 SCM_CAR(z) = scm_tc_flo;
2031 SCM_FLO(z) = x;
2032 SCM_ALLOW_INTS;
2033 return z;
2034 }
2035 # endif/* def SCM_SINGLES */
2036 SCM_CDR(z) = (SCM)scm_must_malloc(1L*sizeof(double), "real");
2037 SCM_CAR(z) = scm_tc_dblr;
2038 }
2039 else {
2040 SCM_CDR(z) = (SCM)scm_must_malloc(2L*sizeof(double), "complex");
2041 SCM_CAR(z) = scm_tc_dblc;
2042 SCM_IMAG(z) = y;
2043 }
2044 SCM_REAL(z) = x;
2045 SCM_ALLOW_INTS;
2046 return z;
2047 }
2048 #endif
2049
2050
2051 #ifdef __STDC__
2052 SCM
2053 scm_bigequal(SCM x, SCM y)
2054 #else
2055 SCM
2056 scm_bigequal(x, y)
2057 SCM x;
2058 SCM y;
2059 #endif
2060 {
2061 #ifdef SCM_BIGDIG
2062 if (0==scm_bigcomp(x, y)) return SCM_BOOL_T;
2063 #endif
2064 return SCM_BOOL_F;
2065 }
2066
2067
2068 #ifdef __STDC__
2069 SCM
2070 scm_floequal(SCM x, SCM y)
2071 #else
2072 SCM
2073 scm_floequal(x, y)
2074 SCM x;
2075 SCM y;
2076 #endif
2077 {
2078 #ifdef SCM_FLOATS
2079 if (SCM_REALPART(x) != SCM_REALPART(y)) return SCM_BOOL_F;
2080 if (!(SCM_CPLXP(x) && (SCM_IMAG(x) != SCM_IMAG(y)))) return SCM_BOOL_T;
2081 #endif
2082 return SCM_BOOL_F;
2083 }
2084
2085
2086
2087
2088 SCM_PROC(s_number_p, "number?", 1, 0, 0, scm_number_p);
2089 SCM_PROC(s_complex_p, "complex?", 1, 0, 0, scm_number_p);
2090 #ifdef __STDC__
2091 SCM
2092 scm_number_p(SCM x)
2093 #else
2094 SCM
2095 scm_number_p(x)
2096 SCM x;
2097 #endif
2098 {
2099 if SCM_INUMP(x) return SCM_BOOL_T;
2100 #ifdef SCM_FLOATS
2101 if (SCM_NIMP(x) && SCM_NUMP(x)) return SCM_BOOL_T;
2102 #else
2103 # ifdef SCM_BIGDIG
2104 if (SCM_NIMP(x) && SCM_NUMP(x)) return SCM_BOOL_T;
2105 # endif
2106 #endif
2107 return SCM_BOOL_F;
2108 }
2109
2110
2111
2112 #ifdef SCM_FLOATS
2113 SCM_PROC(s_real_p, "real?", 1, 0, 0, scm_real_p);
2114 SCM_PROC(s_rational_p, "rational?", 1, 0, 0, scm_real_p);
2115 #ifdef __STDC__
2116 SCM
2117 scm_real_p(SCM x)
2118 #else
2119 SCM
2120 scm_real_p(x)
2121 SCM x;
2122 #endif
2123 {
2124 if (SCM_INUMP(x))
2125 return SCM_BOOL_T;
2126 if (SCM_IMP(x))
2127 return SCM_BOOL_F;
2128 if (SCM_REALP(x))
2129 return SCM_BOOL_T;
2130 # ifdef SCM_BIGDIG
2131 if (SCM_BIGP(x))
2132 return SCM_BOOL_T;
2133 # endif
2134 return SCM_BOOL_F;
2135 }
2136
2137
2138
2139 SCM_PROC(s_int_p, "int?", 1, 0, 0, scm_int_p);
2140 #ifdef __STDC__
2141 SCM
2142 scm_int_p(SCM x)
2143 #else
2144 SCM
2145 scm_int_p(x)
2146 SCM x;
2147 #endif
2148 {
2149 double r;
2150 if SCM_INUMP(x) return SCM_BOOL_T;
2151 if SCM_IMP(x) return SCM_BOOL_F;
2152 # ifdef SCM_BIGDIG
2153 if SCM_BIGP(x) return SCM_BOOL_T;
2154 # endif
2155 if (!SCM_INEXP(x)) return SCM_BOOL_F;
2156 if SCM_CPLXP(x) return SCM_BOOL_F;
2157 r = SCM_REALPART(x);
2158 if (r==floor(r)) return SCM_BOOL_T;
2159 return SCM_BOOL_F;
2160 }
2161
2162
2163
2164 #endif /* SCM_FLOATS */
2165
2166 SCM_PROC(s_inexact_p, "inexact?", 1, 0, 0, scm_inexact_p);
2167 #ifdef __STDC__
2168 SCM
2169 scm_inexact_p(SCM x)
2170 #else
2171 SCM
2172 scm_inexact_p(x)
2173 SCM x;
2174 #endif
2175 {
2176 #ifdef SCM_FLOATS
2177 if (SCM_NIMP(x) && SCM_INEXP(x)) return SCM_BOOL_T;
2178 #endif
2179 return SCM_BOOL_F;
2180 }
2181
2182
2183
2184
2185 SCM_PROC1 (s_eq_p, "=?", scm_tc7_rpsubr, scm_num_eq_p);
2186 #ifdef __STDC__
2187 SCM
2188 scm_num_eq_p (SCM x, SCM y)
2189 #else
2190 SCM
2191 scm_num_eq_p (x, y)
2192 SCM x;
2193 SCM y;
2194 #endif
2195 {
2196 #ifdef SCM_FLOATS
2197 SCM t;
2198 if SCM_NINUMP(x) {
2199 # ifdef SCM_BIGDIG
2200 # ifndef RECKLESS
2201 if (!(SCM_NIMP(x)))
2202 badx: scm_wta(x, (char *)SCM_ARG1, s_eq_p);
2203 # endif
2204 if SCM_BIGP(x) {
2205 if SCM_INUMP(y) return SCM_BOOL_F;
2206 SCM_ASRTGO(SCM_NIMP(y), bady);
2207 if SCM_BIGP(y) return (0==scm_bigcomp(x, y)) ? SCM_BOOL_T : SCM_BOOL_F;
2208 SCM_ASRTGO(SCM_INEXP(y), bady);
2209 bigreal:
2210 return (SCM_REALP(y) && (scm_big2dbl(x)==SCM_REALPART(y))) ? SCM_BOOL_T : SCM_BOOL_F;
2211 }
2212 SCM_ASRTGO(SCM_INEXP(x), badx);
2213 # else
2214 SCM_ASSERT(SCM_NIMP(x) && SCM_INEXP(x), x, SCM_ARG1, s_eq_p);
2215 # endif
2216 if SCM_INUMP(y) {t = x; x = y; y = t; goto realint;}
2217 # ifdef SCM_BIGDIG
2218 SCM_ASRTGO(SCM_NIMP(y), bady);
2219 if SCM_BIGP(y) {t = x; x = y; y = t; goto bigreal;}
2220 SCM_ASRTGO(SCM_INEXP(y), bady);
2221 # else
2222 SCM_ASRTGO(SCM_NIMP(y) && SCM_INEXP(y), bady);
2223 # endif
2224 if (SCM_REALPART(x) != SCM_REALPART(y)) return SCM_BOOL_F;
2225 if SCM_CPLXP(x)
2226 return (SCM_CPLXP(y) && (SCM_IMAG(x)==SCM_IMAG(y))) ? SCM_BOOL_T : SCM_BOOL_F;
2227 return SCM_CPLXP(y) ? SCM_BOOL_F : SCM_BOOL_T;
2228 }
2229 if SCM_NINUMP(y) {
2230 # ifdef SCM_BIGDIG
2231 SCM_ASRTGO(SCM_NIMP(y), bady);
2232 if SCM_BIGP(y) return SCM_BOOL_F;
2233 # ifndef RECKLESS
2234 if (!(SCM_INEXP(y)))
2235 bady: scm_wta(y, (char *)SCM_ARG2, s_eq_p);
2236 # endif
2237 # else
2238 # ifndef RECKLESS
2239 if (!(SCM_NIMP(y) && SCM_INEXP(y)))
2240 bady: scm_wta(y, (char *)SCM_ARG2, s_eq_p);
2241 # endif
2242 # endif
2243 realint:
2244 return (SCM_REALP(y) && (((double)SCM_INUM(x))==SCM_REALPART(y))) ? SCM_BOOL_T : SCM_BOOL_F;
2245 }
2246 #else
2247 # ifdef SCM_BIGDIG
2248 if SCM_NINUMP(x) {
2249 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_eq_p);
2250 if SCM_INUMP(y) return SCM_BOOL_F;
2251 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
2252 return (0==scm_bigcomp(x, y)) ? SCM_BOOL_T : SCM_BOOL_F;
2253 }
2254 if SCM_NINUMP(y) {
2255 # ifndef RECKLESS
2256 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
2257 bady: scm_wta(y, (char *)SCM_ARG2, s_eq_p);
2258 # endif
2259 return SCM_BOOL_F;
2260 }
2261 # else
2262 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_eq_p);
2263 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_eq_p);
2264 # endif
2265 #endif
2266 return ((long)x==(long)y) ? SCM_BOOL_T : SCM_BOOL_F;
2267 }
2268
2269
2270
2271 SCM_PROC1 (s_less_p, "<?", scm_tc7_rpsubr, scm_less_p);
2272 #ifdef __STDC__
2273 SCM
2274 scm_less_p(SCM x, SCM y)
2275 #else
2276 SCM
2277 scm_less_p(x, y)
2278 SCM x;
2279 SCM y;
2280 #endif
2281 {
2282 #ifdef SCM_FLOATS
2283 if SCM_NINUMP(x) {
2284 # ifdef SCM_BIGDIG
2285 # ifndef RECKLESS
2286 if (!(SCM_NIMP(x)))
2287 badx: scm_wta(x, (char *)SCM_ARG1, s_less_p);
2288 # endif
2289 if SCM_BIGP(x) {
2290 if SCM_INUMP(y) return SCM_BIGSIGN(x) ? SCM_BOOL_T : SCM_BOOL_F;
2291 SCM_ASRTGO(SCM_NIMP(y), bady);
2292 if SCM_BIGP(y) return (1==scm_bigcomp(x, y)) ? SCM_BOOL_T : SCM_BOOL_F;
2293 SCM_ASRTGO(SCM_REALP(y), bady);
2294 return (scm_big2dbl(x) < SCM_REALPART(y)) ? SCM_BOOL_T : SCM_BOOL_F;
2295 }
2296 SCM_ASRTGO(SCM_REALP(x), badx);
2297 # else
2298 SCM_ASSERT(SCM_NIMP(x) && SCM_REALP(x), x, SCM_ARG1, s_less_p);
2299 # endif
2300 if (SCM_INUMP(y))
2301 return (SCM_REALPART(x) < ((double)SCM_INUM(y))) ? SCM_BOOL_T : SCM_BOOL_F;
2302 # ifdef SCM_BIGDIG
2303 SCM_ASRTGO(SCM_NIMP(y), bady);
2304 if SCM_BIGP(y) return (SCM_REALPART(x) < scm_big2dbl(y)) ? SCM_BOOL_T : SCM_BOOL_F;
2305 SCM_ASRTGO(SCM_REALP(y), bady);
2306 # else
2307 SCM_ASRTGO(SCM_NIMP(y) && SCM_REALP(y), bady);
2308 # endif
2309 return (SCM_REALPART(x) < SCM_REALPART(y)) ? SCM_BOOL_T : SCM_BOOL_F;
2310 }
2311 if SCM_NINUMP(y) {
2312 # ifdef SCM_BIGDIG
2313 SCM_ASRTGO(SCM_NIMP(y), bady);
2314 if SCM_BIGP(y) return SCM_BIGSIGN(y) ? SCM_BOOL_F : SCM_BOOL_T;
2315 # ifndef RECKLESS
2316 if (!(SCM_REALP(y)))
2317 bady: scm_wta(y, (char *)SCM_ARG2, s_less_p);
2318 # endif
2319 # else
2320 # ifndef RECKLESS
2321 if (!(SCM_NIMP(y) && SCM_REALP(y)))
2322 bady: scm_wta(y, (char *)SCM_ARG2, s_less_p);
2323 # endif
2324 # endif
2325 return (((double)SCM_INUM(x)) < SCM_REALPART(y)) ? SCM_BOOL_T : SCM_BOOL_F;
2326 }
2327 #else
2328 # ifdef SCM_BIGDIG
2329 if SCM_NINUMP(x) {
2330 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_less_p);
2331 if SCM_INUMP(y) return SCM_BIGSIGN(x) ? SCM_BOOL_T : SCM_BOOL_F;
2332 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
2333 return (1==scm_bigcomp(x, y)) ? SCM_BOOL_T : SCM_BOOL_F;
2334 }
2335 if SCM_NINUMP(y) {
2336 # ifndef RECKLESS
2337 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
2338 bady: scm_wta(y, (char *)SCM_ARG2, s_less_p);
2339 # endif
2340 return SCM_BIGSIGN(y) ? SCM_BOOL_F : SCM_BOOL_T;
2341 }
2342 # else
2343 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_less_p);
2344 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_less_p);
2345 # endif
2346 #endif
2347 return ((long)x < (long)y) ? SCM_BOOL_T : SCM_BOOL_F;
2348 }
2349
2350
2351 SCM_PROC1 (s_gr_p, ">?", scm_tc7_rpsubr, scm_gr_p);
2352 #ifdef __STDC__
2353 SCM
2354 scm_gr_p(SCM x, SCM y)
2355 #else
2356 SCM
2357 scm_gr_p(x, y)
2358 SCM x;
2359 SCM y;
2360 #endif
2361 {
2362 return scm_less_p(y, x);
2363 }
2364
2365
2366
2367 SCM_PROC1 (s_leq_p, "<=?", scm_tc7_rpsubr, scm_leq_p);
2368 #ifdef __STDC__
2369 SCM
2370 scm_leq_p(SCM x, SCM y)
2371 #else
2372 SCM
2373 scm_leq_p(x, y)
2374 SCM x;
2375 SCM y;
2376 #endif
2377 {
2378 return SCM_BOOL_NOT(scm_less_p(y, x));
2379 }
2380
2381
2382
2383 SCM_PROC1 (s_geq_p, ">=?", scm_tc7_rpsubr, scm_geq_p);
2384 #ifdef __STDC__
2385 SCM
2386 scm_geq_p(SCM x, SCM y)
2387 #else
2388 SCM
2389 scm_geq_p(x, y)
2390 SCM x;
2391 SCM y;
2392 #endif
2393 {
2394 return SCM_BOOL_NOT(scm_less_p(x, y));
2395 }
2396
2397
2398
2399 SCM_PROC(s_zero_p, "zero?", 1, 0, 0, scm_zero_p);
2400 #ifdef __STDC__
2401 SCM
2402 scm_zero_p(SCM z)
2403 #else
2404 SCM
2405 scm_zero_p(z)
2406 SCM z;
2407 #endif
2408 {
2409 #ifdef SCM_FLOATS
2410 if SCM_NINUMP(z) {
2411 # ifdef SCM_BIGDIG
2412 SCM_ASRTGO(SCM_NIMP(z), badz);
2413 if SCM_BIGP(z) return SCM_BOOL_F;
2414 # ifndef RECKLESS
2415 if (!(SCM_INEXP(z)))
2416 badz: scm_wta(z, (char *)SCM_ARG1, s_zero_p);
2417 # endif
2418 # else
2419 SCM_ASSERT(SCM_NIMP(z) && SCM_INEXP(z), z, SCM_ARG1, s_zero_p);
2420 # endif
2421 return (z==scm_flo0) ? SCM_BOOL_T : SCM_BOOL_F;
2422 }
2423 #else
2424 # ifdef SCM_BIGDIG
2425 if SCM_NINUMP(z) {
2426 SCM_ASSERT(SCM_NIMP(z) && SCM_BIGP(z), z, SCM_ARG1, s_zero_p);
2427 return SCM_BOOL_F;
2428 }
2429 # else
2430 SCM_ASSERT(SCM_INUMP(z), z, SCM_ARG1, s_zero_p);
2431 # endif
2432 #endif
2433 return (z==SCM_INUM0) ? SCM_BOOL_T: SCM_BOOL_F;
2434 }
2435
2436
2437
2438 SCM_PROC(s_positive_p, "positive?", 1, 0, 0, scm_positive_p);
2439 #ifdef __STDC__
2440 SCM
2441 scm_positive_p(SCM x)
2442 #else
2443 SCM
2444 scm_positive_p(x)
2445 SCM x;
2446 #endif
2447 {
2448 #ifdef SCM_FLOATS
2449 if SCM_NINUMP(x) {
2450 # ifdef SCM_BIGDIG
2451 SCM_ASRTGO(SCM_NIMP(x), badx);
2452 if SCM_BIGP(x) return SCM_TYP16(x)==scm_tc16_bigpos ? SCM_BOOL_T : SCM_BOOL_F;
2453 # ifndef RECKLESS
2454 if (!(SCM_REALP(x)))
2455 badx: scm_wta(x, (char *)SCM_ARG1, s_positive_p);
2456 # endif
2457 # else
2458 SCM_ASSERT(SCM_NIMP(x) && SCM_REALP(x), x, SCM_ARG1, s_positive_p);
2459 # endif
2460 return (SCM_REALPART(x) > 0.0) ? SCM_BOOL_T : SCM_BOOL_F;
2461 }
2462 #else
2463 # ifdef SCM_BIGDIG
2464 if SCM_NINUMP(x) {
2465 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_positive_p);
2466 return SCM_TYP16(x)==scm_tc16_bigpos ? SCM_BOOL_T : SCM_BOOL_F;
2467 }
2468 # else
2469 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_positive_p);
2470 # endif
2471 #endif
2472 return (x > SCM_INUM0) ? SCM_BOOL_T : SCM_BOOL_F;
2473 }
2474
2475
2476
2477 SCM_PROC(s_negative_p, "negative?", 1, 0, 0, scm_negative_p);
2478 #ifdef __STDC__
2479 SCM
2480 scm_negative_p(SCM x)
2481 #else
2482 SCM
2483 scm_negative_p(x)
2484 SCM x;
2485 #endif
2486 {
2487 #ifdef SCM_FLOATS
2488 if SCM_NINUMP(x) {
2489 # ifdef SCM_BIGDIG
2490 SCM_ASRTGO(SCM_NIMP(x), badx);
2491 if SCM_BIGP(x) return SCM_TYP16(x)==scm_tc16_bigpos ? SCM_BOOL_F : SCM_BOOL_T;
2492 # ifndef RECKLESS
2493 if (!(SCM_REALP(x)))
2494 badx: scm_wta(x, (char *)SCM_ARG1, s_negative_p);
2495 # endif
2496 # else
2497 SCM_ASSERT(SCM_NIMP(x) && SCM_REALP(x), x, SCM_ARG1, s_negative_p);
2498 # endif
2499 return (SCM_REALPART(x) < 0.0) ? SCM_BOOL_T : SCM_BOOL_F;
2500 }
2501 #else
2502 # ifdef SCM_BIGDIG
2503 if SCM_NINUMP(x) {
2504 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_negative_p);
2505 return (SCM_TYP16(x)==scm_tc16_bigneg) ? SCM_BOOL_T : SCM_BOOL_F;
2506 }
2507 # else
2508 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_negative_p);
2509 # endif
2510 #endif
2511 return (x < SCM_INUM0) ? SCM_BOOL_T : SCM_BOOL_F;
2512 }
2513
2514
2515 SCM_PROC1 (s_max, "max", scm_tc7_asubr, scm_max);
2516 #ifdef __STDC__
2517 SCM
2518 scm_max(SCM x, SCM y)
2519 #else
2520 SCM
2521 scm_max(x, y)
2522 SCM x;
2523 SCM y;
2524 #endif
2525 {
2526 #ifdef SCM_FLOATS
2527 double z;
2528 #endif
2529 if SCM_UNBNDP(y) {
2530 #ifndef RECKLESS
2531 if (!(SCM_NUMBERP(x)))
2532 badx: scm_wta(x, (char *)SCM_ARG1, s_max);
2533 #endif
2534 return x;
2535 }
2536 #ifdef SCM_FLOATS
2537 if SCM_NINUMP(x) {
2538 # ifdef SCM_BIGDIG
2539 SCM_ASRTGO(SCM_NIMP(x), badx);
2540 if SCM_BIGP(x) {
2541 if SCM_INUMP(y) return SCM_BIGSIGN(x) ? y : x;
2542 SCM_ASRTGO(SCM_NIMP(y), bady);
2543 if SCM_BIGP(y) return (1==scm_bigcomp(x, y)) ? y : x;
2544 SCM_ASRTGO(SCM_REALP(y), bady);
2545 z = scm_big2dbl(x);
2546 return (z < SCM_REALPART(y)) ? y : scm_makdbl(z, 0.0);
2547 }
2548 SCM_ASRTGO(SCM_REALP(x), badx);
2549 # else
2550 SCM_ASSERT(SCM_NIMP(x) && SCM_REALP(x), x, SCM_ARG1, s_max);
2551 # endif
2552 if (SCM_INUMP(y))
2553 return (SCM_REALPART(x) < (z = SCM_INUM(y))) ? scm_makdbl(z, 0.0) : x;
2554 # ifdef SCM_BIGDIG
2555 SCM_ASRTGO(SCM_NIMP(y), bady);
2556 if (SCM_BIGP(y))
2557 return (SCM_REALPART(x) < (z = scm_big2dbl(y))) ? scm_makdbl(z, 0.0) : x;
2558 SCM_ASRTGO(SCM_REALP(y), bady);
2559 # else
2560 SCM_ASRTGO(SCM_NIMP(y) && SCM_REALP(y), bady);
2561 # endif
2562 return (SCM_REALPART(x) < SCM_REALPART(y)) ? y : x;
2563 }
2564 if SCM_NINUMP(y) {
2565 # ifdef SCM_BIGDIG
2566 SCM_ASRTGO(SCM_NIMP(y), bady);
2567 if SCM_BIGP(y) return SCM_BIGSIGN(y) ? x : y;
2568 # ifndef RECKLESS
2569 if (!(SCM_REALP(y)))
2570 bady: scm_wta(y, (char *)SCM_ARG2, s_max);
2571 # endif
2572 # else
2573 # ifndef RECKLESS
2574 if (!(SCM_NIMP(y) && SCM_REALP(y)))
2575 bady: scm_wta(y, (char *)SCM_ARG2, s_max);
2576 # endif
2577 # endif
2578 return ((z = SCM_INUM(x)) < SCM_REALPART(y)) ? y : scm_makdbl(z, 0.0);
2579 }
2580 #else
2581 # ifdef SCM_BIGDIG
2582 if SCM_NINUMP(x) {
2583 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_max);
2584 if SCM_INUMP(y) return SCM_BIGSIGN(x) ? y : x;
2585 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
2586 return (1==scm_bigcomp(x, y)) ? y : x;
2587 }
2588 if SCM_NINUMP(y) {
2589 # ifndef RECKLESS
2590 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
2591 bady: scm_wta(y, (char *)SCM_ARG2, s_max);
2592 # endif
2593 return SCM_BIGSIGN(y) ? x : y;
2594 }
2595 # else
2596 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_max);
2597 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_max);
2598 # endif
2599 #endif
2600 return ((long)x < (long)y) ? y : x;
2601 }
2602
2603
2604
2605
2606 SCM_PROC1 (s_min, "min", scm_tc7_asubr, scm_min);
2607 #ifdef __STDC__
2608 SCM
2609 scm_min(SCM x, SCM y)
2610 #else
2611 SCM
2612 scm_min(x, y)
2613 SCM x;
2614 SCM y;
2615 #endif
2616 {
2617 #ifdef SCM_FLOATS
2618 double z;
2619 #endif
2620 if SCM_UNBNDP(y) {
2621 #ifndef RECKLESS
2622 if (!(SCM_NUMBERP(x)))
2623 badx:scm_wta(x, (char *)SCM_ARG1, s_min);
2624 #endif
2625 return x;
2626 }
2627 #ifdef SCM_FLOATS
2628 if SCM_NINUMP(x) {
2629 # ifdef SCM_BIGDIG
2630 SCM_ASRTGO(SCM_NIMP(x), badx);
2631 if SCM_BIGP(x) {
2632 if SCM_INUMP(y) return SCM_BIGSIGN(x) ? x : y;
2633 SCM_ASRTGO(SCM_NIMP(y), bady);
2634 if SCM_BIGP(y) return (-1==scm_bigcomp(x, y)) ? y : x;
2635 SCM_ASRTGO(SCM_REALP(y), bady);
2636 z = scm_big2dbl(x);
2637 return (z > SCM_REALPART(y)) ? y : scm_makdbl(z, 0.0);
2638 }
2639 SCM_ASRTGO(SCM_REALP(x), badx);
2640 # else
2641 SCM_ASSERT(SCM_NIMP(x) && SCM_REALP(x), x, SCM_ARG1, s_min);
2642 # endif
2643 if SCM_INUMP(y) return (SCM_REALPART(x) > (z = SCM_INUM(y))) ? scm_makdbl(z, 0.0) : x;
2644 # ifdef SCM_BIGDIG
2645 SCM_ASRTGO(SCM_NIMP(y), bady);
2646 if SCM_BIGP(y) return (SCM_REALPART(x) > (z = scm_big2dbl(y))) ? scm_makdbl(z, 0.0) : x;
2647 SCM_ASRTGO(SCM_REALP(y), bady);
2648 # else
2649 SCM_ASRTGO(SCM_NIMP(y) && SCM_REALP(y), bady);
2650 # endif
2651 return (SCM_REALPART(x) > SCM_REALPART(y)) ? y : x;
2652 }
2653 if SCM_NINUMP(y) {
2654 # ifdef SCM_BIGDIG
2655 SCM_ASRTGO(SCM_NIMP(y), bady);
2656 if SCM_BIGP(y) return SCM_BIGSIGN(y) ? y : x;
2657 # ifndef RECKLESS
2658 if (!(SCM_REALP(y)))
2659 bady: scm_wta(y, (char *)SCM_ARG2, s_min);
2660 # endif
2661 # else
2662 # ifndef RECKLESS
2663 if (!(SCM_NIMP(y) && SCM_REALP(y)))
2664 bady: scm_wta(y, (char *)SCM_ARG2, s_min);
2665 # endif
2666 # endif
2667 return ((z = SCM_INUM(x)) > SCM_REALPART(y)) ? y : scm_makdbl(z, 0.0);
2668 }
2669 #else
2670 # ifdef SCM_BIGDIG
2671 if SCM_NINUMP(x) {
2672 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_min);
2673 if SCM_INUMP(y) return SCM_BIGSIGN(x) ? x : y;
2674 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
2675 return (-1==scm_bigcomp(x, y)) ? y : x;
2676 }
2677 if SCM_NINUMP(y) {
2678 # ifndef RECKLESS
2679 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
2680 bady: scm_wta(y, (char *)SCM_ARG2, s_min);
2681 # endif
2682 return SCM_BIGSIGN(y) ? y : x;
2683 }
2684 # else
2685 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_min);
2686 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_min);
2687 # endif
2688 #endif
2689 return ((long)x > (long)y) ? y : x;
2690 }
2691
2692
2693
2694
2695 SCM_PROC1 (s_sum, "+", scm_tc7_asubr, scm_sum);
2696 #ifdef __STDC__
2697 SCM
2698 scm_sum(SCM x, SCM y)
2699 #else
2700 SCM
2701 scm_sum(x, y)
2702 SCM x;
2703 SCM y;
2704 #endif
2705 {
2706 if SCM_UNBNDP(y) {
2707 if SCM_UNBNDP(x) return SCM_INUM0;
2708 #ifndef RECKLESS
2709 if (!(SCM_NUMBERP(x)))
2710 badx: scm_wta(x, (char *)SCM_ARG1, s_sum);
2711 #endif
2712 return x;
2713 }
2714 #ifdef SCM_FLOATS
2715 if SCM_NINUMP(x) {
2716 SCM t;
2717 # ifdef SCM_BIGDIG
2718 SCM_ASRTGO(SCM_NIMP(x), badx);
2719 if SCM_BIGP(x) {
2720 if SCM_INUMP(y) {t = x; x = y; y = t; goto intbig;}
2721 SCM_ASRTGO(SCM_NIMP(y), bady);
2722 if SCM_BIGP(y) {
2723 if (SCM_NUMDIGS(x) > SCM_NUMDIGS(y)) {t = x; x = y; y = t;}
2724 return scm_addbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BIGSIGN(x), y, 0);
2725 }
2726 SCM_ASRTGO(SCM_INEXP(y), bady);
2727 bigreal: return scm_makdbl(scm_big2dbl(x)+SCM_REALPART(y), SCM_CPLXP(y)?SCM_IMAG(y):0.0);
2728 }
2729 SCM_ASRTGO(SCM_INEXP(x), badx);
2730 # else
2731 SCM_ASRTGO(SCM_NIMP(x) && SCM_INEXP(x), badx);
2732 # endif
2733 if SCM_INUMP(y) {t = x; x = y; y = t; goto intreal;}
2734 # ifdef SCM_BIGDIG
2735 SCM_ASRTGO(SCM_NIMP(y), bady);
2736 if SCM_BIGP(y) {t = x; x = y; y = t; goto bigreal;}
2737 # ifndef RECKLESS
2738 else if (!(SCM_INEXP(y)))
2739 bady: scm_wta(y, (char *)SCM_ARG2, s_sum);
2740 # endif
2741 # else
2742 # ifndef RECKLESS
2743 if (!(SCM_NIMP(y) && SCM_INEXP(y)))
2744 bady: scm_wta(y, (char *)SCM_ARG2, s_sum);
2745 # endif
2746 # endif
2747 { double i = 0.0;
2748 if SCM_CPLXP(x) i = SCM_IMAG(x);
2749 if SCM_CPLXP(y) i += SCM_IMAG(y);
2750 return scm_makdbl(SCM_REALPART(x)+SCM_REALPART(y), i); }
2751 }
2752 if SCM_NINUMP(y) {
2753 # ifdef SCM_BIGDIG
2754 SCM_ASRTGO(SCM_NIMP(y), bady);
2755 if SCM_BIGP(y)
2756 intbig: {
2757 # ifndef SCM_DIGSTOOBIG
2758 long z = scm_pseudolong(SCM_INUM(x));
2759 return scm_addbig((SCM_BIGDIG *)&z, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0);
2760 # else
2761 SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
2762 scm_longdigs(SCM_INUM(x), zdigs);
2763 return scm_addbig(zdigs, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0);
2764 # endif
2765 }
2766 SCM_ASRTGO(SCM_INEXP(y), bady);
2767 # else
2768 SCM_ASRTGO(SCM_NIMP(y) && SCM_INEXP(y), bady);
2769 # endif
2770 intreal: return scm_makdbl(SCM_INUM(x)+SCM_REALPART(y), SCM_CPLXP(y)?SCM_IMAG(y):0.0);
2771 }
2772 #else
2773 # ifdef SCM_BIGDIG
2774 if SCM_NINUMP(x) {
2775 SCM t;
2776 SCM_ASRTGO(SCM_NIMP(x) && SCM_BIGP(x), badx);
2777 if SCM_INUMP(y) {t = x; x = y; y = t; goto intbig;}
2778 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
2779 if (SCM_NUMDIGS(x) > SCM_NUMDIGS(y)) {t = x; x = y; y = t;}
2780 return scm_addbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BIGSIGN(x), y, 0);
2781 }
2782 if SCM_NINUMP(y) {
2783 # ifndef RECKLESS
2784 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
2785 bady: scm_wta(y, (char *)SCM_ARG2, s_sum);
2786 # endif
2787 intbig: {
2788 # ifndef SCM_DIGSTOOBIG
2789 long z = scm_pseudolong(SCM_INUM(x));
2790 return scm_addbig(&z, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0);
2791 # else
2792 SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
2793 scm_longdigs(SCM_INUM(x), zdigs);
2794 return scm_addbig(zdigs, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0);
2795 # endif
2796 }
2797 }
2798 # else
2799 SCM_ASRTGO(SCM_INUMP(x), badx);
2800 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_sum);
2801 # endif
2802 #endif
2803 x = SCM_INUM(x)+SCM_INUM(y);
2804 if SCM_FIXABLE(x) return SCM_MAKINUM(x);
2805 #ifdef SCM_BIGDIG
2806 return scm_long2big(x);
2807 #else
2808 # ifdef SCM_FLOATS
2809 return scm_makdbl((double)x, 0.0);
2810 # else
2811 scm_num_overflow (s_sum);
2812 return SCM_UNSPECIFIED;
2813 # endif
2814 #endif
2815 }
2816
2817
2818
2819
2820 SCM_PROC1 (s_difference, "-", scm_tc7_asubr, scm_difference);
2821 #ifdef __STDC__
2822 SCM
2823 scm_difference(SCM x, SCM y)
2824 #else
2825 SCM
2826 scm_difference(x, y)
2827 SCM x;
2828 SCM y;
2829 #endif
2830 {
2831 #ifdef SCM_FLOATS
2832 if SCM_NINUMP(x) {
2833 # ifndef RECKLESS
2834 if (!(SCM_NIMP(x)))
2835 badx: scm_wta(x, (char *)SCM_ARG1, s_difference);
2836 # endif
2837 if SCM_UNBNDP(y) {
2838 # ifdef SCM_BIGDIG
2839 if SCM_BIGP(x) {
2840 x = scm_copybig(x, !SCM_BIGSIGN(x));
2841 return SCM_NUMDIGS(x) * SCM_BITSPERDIG/SCM_CHAR_BIT <= sizeof(SCM) ?
2842 scm_big2inum(x, SCM_NUMDIGS(x)) : x;
2843 }
2844 # endif
2845 SCM_ASRTGO(SCM_INEXP(x), badx);
2846 return scm_makdbl(-SCM_REALPART(x), SCM_CPLXP(x)?-SCM_IMAG(x):0.0);
2847 }
2848 if SCM_INUMP(y) return scm_sum(x, SCM_MAKINUM(-SCM_INUM(y)));
2849 # ifdef SCM_BIGDIG
2850 SCM_ASRTGO(SCM_NIMP(y), bady);
2851 if SCM_BIGP(x) {
2852 if SCM_BIGP(y) return (SCM_NUMDIGS(x) < SCM_NUMDIGS(y)) ?
2853 scm_addbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BIGSIGN(x), y, 0x0100) :
2854 scm_addbig(SCM_BDIGITS(y), SCM_NUMDIGS(y), SCM_BIGSIGN(y) ^ 0x0100, x, 0);
2855 SCM_ASRTGO(SCM_INEXP(y), bady);
2856 return scm_makdbl(scm_big2dbl(x)-SCM_REALPART(y), SCM_CPLXP(y)?-SCM_IMAG(y):0.0);
2857 }
2858 SCM_ASRTGO(SCM_INEXP(x), badx);
2859 if SCM_BIGP(y) return scm_makdbl(SCM_REALPART(x)-scm_big2dbl(y), SCM_CPLXP(x)?SCM_IMAG(x):0.0);
2860 SCM_ASRTGO(SCM_INEXP(y), bady);
2861 # else
2862 SCM_ASRTGO(SCM_INEXP(x), badx);
2863 SCM_ASRTGO(SCM_NIMP(y) && SCM_INEXP(y), bady);
2864 # endif
2865 if SCM_CPLXP(x)
2866 if SCM_CPLXP(y)
2867 return scm_makdbl(SCM_REAL(x)-SCM_REAL(y), SCM_IMAG(x)-SCM_IMAG(y));
2868 else
2869 return scm_makdbl(SCM_REAL(x)-SCM_REALPART(y), SCM_IMAG(x));
2870 return scm_makdbl(SCM_REALPART(x)-SCM_REALPART(y), SCM_CPLXP(y)?-SCM_IMAG(y):0.0);
2871 }
2872 if SCM_UNBNDP(y) {x = -SCM_INUM(x); goto checkx;}
2873 if SCM_NINUMP(y) {
2874 # ifdef SCM_BIGDIG
2875 SCM_ASRTGO(SCM_NIMP(y), bady);
2876 if SCM_BIGP(y) {
2877 # ifndef SCM_DIGSTOOBIG
2878 long z = scm_pseudolong(SCM_INUM(x));
2879 return scm_addbig((SCM_BIGDIG *)&z, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0x0100);
2880 # else
2881 SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
2882 scm_longdigs(SCM_INUM(x), zdigs);
2883 return scm_addbig(zdigs, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0x0100);
2884 # endif
2885 }
2886 # ifndef RECKLESS
2887 if (!(SCM_INEXP(y)))
2888 bady: scm_wta(y, (char *)SCM_ARG2, s_difference);
2889 # endif
2890 # else
2891 # ifndef RECKLESS
2892 if (!(SCM_NIMP(y) && SCM_INEXP(y)))
2893 bady: scm_wta(y, (char *)SCM_ARG2, s_difference);
2894 # endif
2895 # endif
2896 return scm_makdbl(SCM_INUM(x)-SCM_REALPART(y), SCM_CPLXP(y)?-SCM_IMAG(y):0.0);
2897 }
2898 #else
2899 # ifdef SCM_BIGDIG
2900 if SCM_NINUMP(x) {
2901 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_difference);
2902 if SCM_UNBNDP(y) {
2903 x = scm_copybig(x, !SCM_BIGSIGN(x));
2904 return SCM_NUMDIGS(x) * SCM_BITSPERDIG/SCM_CHAR_BIT <= sizeof(SCM) ?
2905 scm_big2inum(x, SCM_NUMDIGS(x)) : x;
2906 }
2907 if SCM_INUMP(y) {
2908 # ifndef SCM_DIGSTOOBIG
2909 long z = scm_pseudolong(SCM_INUM(y));
2910 return scm_addbig(&z, SCM_DIGSPERLONG, (y < 0) ? 0 : 0x0100, x, 0);
2911 # else
2912 SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
2913 scm_longdigs(SCM_INUM(x), zdigs);
2914 return scm_addbig(zdigs, SCM_DIGSPERLONG, (y < 0) ? 0 : 0x0100, x, 0);
2915 # endif
2916 }
2917 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
2918 return (SCM_NUMDIGS(x) < SCM_NUMDIGS(y)) ?
2919 scm_addbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BIGSIGN(x), y, 0x0100) :
2920 scm_addbig(SCM_BDIGITS(y), SCM_NUMDIGS(y), SCM_BIGSIGN(y) ^ 0x0100, x, 0);
2921 }
2922 if SCM_UNBNDP(y) {x = -SCM_INUM(x); goto checkx;}
2923 if SCM_NINUMP(y) {
2924 # ifndef RECKLESS
2925 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
2926 bady: scm_wta(y, (char *)SCM_ARG2, s_difference);
2927 # endif
2928 {
2929 # ifndef SCM_DIGSTOOBIG
2930 long z = scm_pseudolong(SCM_INUM(x));
2931 return scm_addbig(&z, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0x0100);
2932 # else
2933 SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
2934 scm_longdigs(SCM_INUM(x), zdigs);
2935 return scm_addbig(zdigs, SCM_DIGSPERLONG, (x < 0) ? 0x0100 : 0, y, 0x0100);
2936 # endif
2937 }
2938 }
2939 # else
2940 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_difference);
2941 if SCM_UNBNDP(y) {x = -SCM_INUM(x); goto checkx;}
2942 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_difference);
2943 # endif
2944 #endif
2945 x = SCM_INUM(x)-SCM_INUM(y);
2946 checkx:
2947 if SCM_FIXABLE(x) return SCM_MAKINUM(x);
2948 #ifdef SCM_BIGDIG
2949 return scm_long2big(x);
2950 #else
2951 # ifdef SCM_FLOATS
2952 return scm_makdbl((double)x, 0.0);
2953 # else
2954 scm_num_overflow (s_difference);
2955 return SCM_UNSPECIFIED;
2956 # endif
2957 #endif
2958 }
2959
2960
2961
2962
2963 SCM_PROC1 (s_product, "*", scm_tc7_asubr, scm_product);
2964 #ifdef __STDC__
2965 SCM
2966 scm_product(SCM x, SCM y)
2967 #else
2968 SCM
2969 scm_product(x, y)
2970 SCM x;
2971 SCM y;
2972 #endif
2973 {
2974 if SCM_UNBNDP(y) {
2975 if SCM_UNBNDP(x) return SCM_MAKINUM(1L);
2976 #ifndef RECKLESS
2977 if (!(SCM_NUMBERP(x)))
2978 badx: scm_wta(x, (char *)SCM_ARG1, s_product);
2979 #endif
2980 return x;
2981 }
2982 #ifdef SCM_FLOATS
2983 if SCM_NINUMP(x) {
2984 SCM t;
2985 # ifdef SCM_BIGDIG
2986 SCM_ASRTGO(SCM_NIMP(x), badx);
2987 if SCM_BIGP(x) {
2988 if SCM_INUMP(y) {t = x; x = y; y = t; goto intbig;}
2989 SCM_ASRTGO(SCM_NIMP(y), bady);
2990 if SCM_BIGP(y) return scm_mulbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BDIGITS(y), SCM_NUMDIGS(y),
2991 SCM_BIGSIGN(x) ^ SCM_BIGSIGN(y));
2992 SCM_ASRTGO(SCM_INEXP(y), bady);
2993 bigreal: {
2994 double bg = scm_big2dbl(x);
2995 return scm_makdbl(bg*SCM_REALPART(y), SCM_CPLXP(y)?bg*SCM_IMAG(y):0.0); }
2996 }
2997 SCM_ASRTGO(SCM_INEXP(x), badx);
2998 # else
2999 SCM_ASRTGO(SCM_NIMP(x) && SCM_INEXP(x), badx);
3000 # endif
3001 if SCM_INUMP(y) {t = x; x = y; y = t; goto intreal;}
3002 # ifdef SCM_BIGDIG
3003 SCM_ASRTGO(SCM_NIMP(y), bady);
3004 if SCM_BIGP(y) {t = x; x = y; y = t; goto bigreal;}
3005 # ifndef RECKLESS
3006 else if (!(SCM_INEXP(y)))
3007 bady: scm_wta(y, (char *)SCM_ARG2, s_product);
3008 # endif
3009 # else
3010 # ifndef RECKLESS
3011 if (!(SCM_NIMP(y) && SCM_INEXP(y)))
3012 bady: scm_wta(y, (char *)SCM_ARG2, s_product);
3013 # endif
3014 # endif
3015 if SCM_CPLXP(x)
3016 if SCM_CPLXP(y)
3017 return scm_makdbl(SCM_REAL(x)*SCM_REAL(y)-SCM_IMAG(x)*SCM_IMAG(y),
3018 SCM_REAL(x)*SCM_IMAG(y)+SCM_IMAG(x)*SCM_REAL(y));
3019 else
3020 return scm_makdbl(SCM_REAL(x)*SCM_REALPART(y), SCM_IMAG(x)*SCM_REALPART(y));
3021 return scm_makdbl(SCM_REALPART(x)*SCM_REALPART(y),
3022 SCM_CPLXP(y)?SCM_REALPART(x)*SCM_IMAG(y):0.0);
3023 }
3024 if SCM_NINUMP(y) {
3025 # ifdef SCM_BIGDIG
3026 SCM_ASRTGO(SCM_NIMP(y), bady);
3027 if SCM_BIGP(y) {
3028 intbig: if (SCM_INUM0==x) return x; if (SCM_MAKINUM(1L)==x) return y;
3029 {
3030 # ifndef SCM_DIGSTOOBIG
3031 long z = scm_pseudolong(SCM_INUM(x));
3032 return scm_mulbig((SCM_BIGDIG *)&z, SCM_DIGSPERLONG, SCM_BDIGITS(y), SCM_NUMDIGS(y),
3033 SCM_BIGSIGN(y) ? (x>0) : (x<0));
3034 # else
3035 SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
3036 scm_longdigs(SCM_INUM(x), zdigs);
3037 return scm_mulbig(zdigs, SCM_DIGSPERLONG, SCM_BDIGITS(y), SCM_NUMDIGS(y),
3038 SCM_BIGSIGN(y) ? (x>0) : (x<0));
3039 # endif
3040 }
3041 }
3042 SCM_ASRTGO(SCM_INEXP(y), bady);
3043 # else
3044 SCM_ASRTGO(SCM_NIMP(y) && SCM_INEXP(y), bady);
3045 # endif
3046 intreal: return scm_makdbl(SCM_INUM(x)*SCM_REALPART(y), SCM_CPLXP(y)?SCM_INUM(x)*SCM_IMAG(y):0.0);
3047 }
3048 #else
3049 # ifdef SCM_BIGDIG
3050 if SCM_NINUMP(x) {
3051 SCM_ASRTGO(SCM_NIMP(x) && SCM_BIGP(x), badx);
3052 if SCM_INUMP(y) {SCM t = x; x = y; y = t; goto intbig;}
3053 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
3054 return scm_mulbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BDIGITS(y), SCM_NUMDIGS(y),
3055 SCM_BIGSIGN(x) ^ SCM_BIGSIGN(y));
3056 }
3057 if SCM_NINUMP(y) {
3058 # ifndef RECKLESS
3059 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
3060 bady: scm_wta(y, (char *)SCM_ARG2, s_product);
3061 # endif
3062 intbig: if (SCM_INUM0==x) return x; if (SCM_MAKINUM(1L)==x) return y;
3063 {
3064 # ifndef SCM_DIGSTOOBIG
3065 long z = scm_pseudolong(SCM_INUM(x));
3066 return scm_mulbig(&z, SCM_DIGSPERLONG, SCM_BDIGITS(y), SCM_NUMDIGS(y),
3067 SCM_BIGSIGN(y) ? (x>0) : (x<0));
3068 # else
3069 SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
3070 scm_longdigs(SCM_INUM(x), zdigs);
3071 return scm_mulbig(zdigs, SCM_DIGSPERLONG, SCM_BDIGITS(y), SCM_NUMDIGS(y),
3072 SCM_BIGSIGN(y) ? (x>0) : (x<0));
3073 # endif
3074 }
3075 }
3076 # else
3077 SCM_ASRTGO(SCM_INUMP(x), badx);
3078 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_product);
3079 # endif
3080 #endif
3081 {
3082 long i, j, k;
3083 i = SCM_INUM(x);
3084 if (0==i) return x;
3085 j = SCM_INUM(y);
3086 k = i * j;
3087 y = SCM_MAKINUM(k);
3088 if (k != SCM_INUM(y) || k/i != j)
3089 #ifdef SCM_BIGDIG
3090 { int sgn = (i < 0) ^ (j < 0);
3091 # ifndef SCM_DIGSTOOBIG
3092 i = scm_pseudolong(i);
3093 j = scm_pseudolong(j);
3094 return scm_mulbig((SCM_BIGDIG *)&i, SCM_DIGSPERLONG,
3095 (SCM_BIGDIG *)&j, SCM_DIGSPERLONG, sgn);
3096 # else /* SCM_DIGSTOOBIG */
3097 SCM_BIGDIG idigs[SCM_DIGSPERLONG];
3098 SCM_BIGDIG jdigs[SCM_DIGSPERLONG];
3099 scm_longdigs(i, idigs);
3100 scm_longdigs(j, jdigs);
3101 return scm_mulbig(idigs, SCM_DIGSPERLONG, jdigs, SCM_DIGSPERLONG, sgn);
3102 # endif
3103 }
3104 #else
3105 # ifdef SCM_FLOATS
3106 return scm_makdbl(((double)i)*((double)j), 0.0);
3107 # else
3108 scm_num_overflow (s_product);
3109 # endif
3110 #endif
3111 return y;
3112 }
3113 }
3114
3115
3116 #ifdef __STDC__
3117 double
3118 scm_num2dbl (SCM a, char * why)
3119 #else
3120 double
3121 scm_num2dbl (a, why)
3122 SCM a;
3123 char * why;
3124 #endif
3125 {
3126 if (SCM_INUMP (a))
3127 return (double) SCM_INUM (a);
3128 #ifdef SCM_FLOATS
3129 SCM_ASSERT (SCM_NIMP (a), a, "wrong type argument", why);
3130 if (SCM_REALP (a))
3131 return (SCM_REALPART (a));
3132 #endif
3133 #ifdef SCM_BIGDIG
3134 return scm_big2dbl (a);
3135 #endif
3136 SCM_ASSERT (0, a, "wrong type argument", why);
3137 return SCM_UNSPECIFIED;
3138 }
3139
3140
3141 SCM_PROC(s_fuck, "fuck", 1, 0, 0, scm_fuck);
3142 #ifdef __STDC__
3143 SCM
3144 scm_fuck (SCM a)
3145 #else
3146 SCM
3147 scm_fuck (a)
3148 SCM a;
3149 #endif
3150 {
3151 return scm_makdbl (scm_num2dbl (a, "just because"), 0.0);
3152 }
3153
3154 SCM_PROC1 (s_divide, "/", scm_tc7_asubr, scm_divide);
3155 #ifdef __STDC__
3156 SCM
3157 scm_divide(SCM x, SCM y)
3158 #else
3159 SCM
3160 scm_divide(x, y)
3161 SCM x;
3162 SCM y;
3163 #endif
3164 {
3165 #ifdef SCM_FLOATS
3166 double d, r, i, a;
3167 if SCM_NINUMP(x) {
3168 # ifndef RECKLESS
3169 if (!(SCM_NIMP(x)))
3170 badx: scm_wta(x, (char *)SCM_ARG1, s_divide);
3171 # endif
3172 if SCM_UNBNDP(y) {
3173 # ifdef SCM_BIGDIG
3174 if SCM_BIGP(x) return scm_makdbl(1.0/scm_big2dbl(x), 0.0);
3175 # endif
3176 SCM_ASRTGO(SCM_INEXP(x), badx);
3177 if SCM_REALP(x) return scm_makdbl(1.0/SCM_REALPART(x), 0.0);
3178 r = SCM_REAL(x); i = SCM_IMAG(x); d = r*r+i*i;
3179 return scm_makdbl(r/d, -i/d);
3180 }
3181 # ifdef SCM_BIGDIG
3182 if SCM_BIGP(x) {
3183 SCM z;
3184 if SCM_INUMP(y) {
3185 z = SCM_INUM(y);
3186 #ifndef RECKLESS
3187 if (!z)
3188 scm_num_overflow (s_divide);
3189 #endif
3190 if (1==z) return x;
3191 if (z < 0) z = -z;
3192 if (z < SCM_BIGRAD) {
3193 SCM w = scm_copybig(x, SCM_BIGSIGN(x) ? (y>0) : (y<0));
3194 return scm_divbigdig(SCM_BDIGITS(w), SCM_NUMDIGS(w), (SCM_BIGDIG)z) ?
3195 scm_makdbl(scm_big2dbl(x)/SCM_INUM(y), 0.0) : scm_normbig(w);
3196 }
3197 # ifndef SCM_DIGSTOOBIG
3198 z = scm_pseudolong(z);
3199 z = scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), (SCM_BIGDIG *)&z, SCM_DIGSPERLONG,
3200 SCM_BIGSIGN(x) ? (y>0) : (y<0), 3);
3201 # else
3202 { SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
3203 scm_longdigs(z, zdigs);
3204 z = scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), zdigs, SCM_DIGSPERLONG,
3205 SCM_BIGSIGN(x) ? (y>0) : (y<0), 3);}
3206 # endif
3207 return z ? z : scm_makdbl(scm_big2dbl(x)/SCM_INUM(y), 0.0);
3208 }
3209 SCM_ASRTGO(SCM_NIMP(y), bady);
3210 if SCM_BIGP(y) {
3211 z = scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BDIGITS(y), SCM_NUMDIGS(y),
3212 SCM_BIGSIGN(x) ^ SCM_BIGSIGN(y), 3);
3213 return z ? z : scm_makdbl(scm_big2dbl(x)/scm_big2dbl(y), 0.0);
3214 }
3215 SCM_ASRTGO(SCM_INEXP(y), bady);
3216 if SCM_REALP(y) return scm_makdbl(scm_big2dbl(x)/SCM_REALPART(y), 0.0);
3217 a = scm_big2dbl(x);
3218 goto complex_div;
3219 }
3220 # endif
3221 SCM_ASRTGO(SCM_INEXP(x), badx);
3222 if SCM_INUMP(y) {d = SCM_INUM(y); goto basic_div;}
3223 # ifdef SCM_BIGDIG
3224 SCM_ASRTGO(SCM_NIMP(y), bady);
3225 if SCM_BIGP(y) {d = scm_big2dbl(y); goto basic_div;}
3226 SCM_ASRTGO(SCM_INEXP(y), bady);
3227 # else
3228 SCM_ASRTGO(SCM_NIMP(y) && SCM_INEXP(y), bady);
3229 # endif
3230 if SCM_REALP(y) {
3231 d = SCM_REALPART(y);
3232 basic_div: return scm_makdbl(SCM_REALPART(x)/d, SCM_CPLXP(x)?SCM_IMAG(x)/d:0.0);
3233 }
3234 a = SCM_REALPART(x);
3235 if SCM_REALP(x) goto complex_div;
3236 r = SCM_REAL(y); i = SCM_IMAG(y); d = r*r+i*i;
3237 return scm_makdbl((a*r+SCM_IMAG(x)*i)/d, (SCM_IMAG(x)*r-a*i)/d);
3238 }
3239 if SCM_UNBNDP(y) {
3240 if ((SCM_MAKINUM(1L)==x) || (SCM_MAKINUM(-1L)==x)) return x;
3241 return scm_makdbl(1.0/((double)SCM_INUM(x)), 0.0);
3242 }
3243 if SCM_NINUMP(y) {
3244 # ifdef SCM_BIGDIG
3245 SCM_ASRTGO(SCM_NIMP(y), bady);
3246 if SCM_BIGP(y) return scm_makdbl(SCM_INUM(x)/scm_big2dbl(y), 0.0);
3247 # ifndef RECKLESS
3248 if (!(SCM_INEXP(y)))
3249 bady: scm_wta(y, (char *)SCM_ARG2, s_divide);
3250 # endif
3251 # else
3252 # ifndef RECKLESS
3253 if (!(SCM_NIMP(y) && SCM_INEXP(y)))
3254 bady: scm_wta(y, (char *)SCM_ARG2, s_divide);
3255 # endif
3256 # endif
3257 if (SCM_REALP(y))
3258 return scm_makdbl(SCM_INUM(x)/SCM_REALPART(y), 0.0);
3259 a = SCM_INUM(x);
3260 complex_div:
3261 r = SCM_REAL(y); i = SCM_IMAG(y); d = r*r+i*i;
3262 return scm_makdbl((a*r)/d, (-a*i)/d);
3263 }
3264 #else
3265 # ifdef SCM_BIGDIG
3266 if SCM_NINUMP(x) {
3267 SCM z;
3268 SCM_ASSERT(SCM_NIMP(x) && SCM_BIGP(x), x, SCM_ARG1, s_divide);
3269 if SCM_UNBNDP(y) goto ov;
3270 if SCM_INUMP(y) {
3271 z = SCM_INUM(y);
3272 if (!z) goto ov;
3273 if (1==z) return x;
3274 if (z < 0) z = -z;
3275 if (z < SCM_BIGRAD) {
3276 SCM w = scm_copybig(x, SCM_BIGSIGN(x) ? (y>0) : (y<0));
3277 if (scm_divbigdig(SCM_BDIGITS(w), SCM_NUMDIGS(w), (SCM_BIGDIG)z)) goto ov;
3278 return w;
3279 }
3280 # ifndef SCM_DIGSTOOBIG
3281 z = scm_pseudolong(z);
3282 z = scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), &z, SCM_DIGSPERLONG,
3283 SCM_BIGSIGN(x) ? (y>0) : (y<0), 3);
3284 # else
3285 { SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
3286 scm_longdigs(z, zdigs);
3287 z = scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), zdigs, SCM_DIGSPERLONG,
3288 SCM_BIGSIGN(x) ? (y>0) : (y<0), 3);}
3289 # endif
3290 } else {
3291 SCM_ASRTGO(SCM_NIMP(y) && SCM_BIGP(y), bady);
3292 z = scm_divbigbig(SCM_BDIGITS(x), SCM_NUMDIGS(x), SCM_BDIGITS(y), SCM_NUMDIGS(y),
3293 SCM_BIGSIGN(x) ^ SCM_BIGSIGN(y), 3);
3294 }
3295 if (!z) goto ov;
3296 return z;
3297 }
3298 if SCM_UNBNDP(y) {
3299 if ((SCM_MAKINUM(1L)==x) || (SCM_MAKINUM(-1L)==x)) return x;
3300 goto ov;
3301 }
3302 if SCM_NINUMP(y) {
3303 # ifndef RECKLESS
3304 if (!(SCM_NIMP(y) && SCM_BIGP(y)))
3305 bady: scm_wta(y, (char *)SCM_ARG2, s_divide);
3306 # endif
3307 goto ov;
3308 }
3309 # else
3310 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_divide);
3311 if SCM_UNBNDP(y) {
3312 if ((SCM_MAKINUM(1L)==x) || (SCM_MAKINUM(-1L)==x)) return x;
3313 goto ov;
3314 }
3315 SCM_ASSERT(SCM_INUMP(y), y, SCM_ARG2, s_divide);
3316 # endif
3317 #endif
3318 {
3319 long z = SCM_INUM(y);
3320 if ((0==z) || SCM_INUM(x)%z) goto ov;
3321 z = SCM_INUM(x)/z;
3322 if SCM_FIXABLE(z) return SCM_MAKINUM(z);
3323 #ifdef SCM_BIGDIG
3324 return scm_long2big(z);
3325 #endif
3326 #ifdef SCM_FLOATS
3327 ov: return scm_makdbl(((double)SCM_INUM(x))/((double)SCM_INUM(y)), 0.0);
3328 #else
3329 ov: scm_num_overflow (s_divide);
3330 return SCM_UNSPECIFIED;
3331 #endif
3332 }
3333 }
3334
3335
3336
3337
3338 #ifdef SCM_FLOATS
3339 SCM_PROC1 (s_asinh, "$asinh", scm_tc7_cxr, (SCM (*)()) scm_asinh);
3340 #ifdef __STDC__
3341 double
3342 scm_asinh(double x)
3343 #else
3344 double
3345 scm_asinh(x)
3346 double x;
3347 #endif
3348 {
3349 return log(x+sqrt(x*x+1));
3350 }
3351
3352
3353
3354
3355 SCM_PROC1 (s_acosh, "$acosh", scm_tc7_cxr, (SCM (*)()) scm_acosh);
3356 #ifdef __STDC__
3357 double
3358 scm_acosh(double x)
3359 #else
3360 double
3361 scm_acosh(x)
3362 double x;
3363 #endif
3364 {
3365 return log(x+sqrt(x*x-1));
3366 }
3367
3368
3369
3370
3371 SCM_PROC1 (s_atanh, "$atanh", scm_tc7_cxr, (SCM (*)()) scm_atanh);
3372 #ifdef __STDC__
3373 double
3374 scm_atanh(double x)
3375 #else
3376 double
3377 scm_atanh(x)
3378 double x;
3379 #endif
3380 {
3381 return 0.5*log((1+x)/(1-x));
3382 }
3383
3384
3385
3386
3387 SCM_PROC1 (s_truncate, "truncate", scm_tc7_cxr, (SCM (*)()) scm_truncate);
3388 #ifdef __STDC__
3389 double
3390 scm_truncate(double x)
3391 #else
3392 double
3393 scm_truncate(x)
3394 double x;
3395 #endif
3396 {
3397 if (x < 0.0) return -floor(-x);
3398 return floor(x);
3399 }
3400
3401
3402
3403 SCM_PROC1 (s_round, "round", scm_tc7_cxr, (SCM (*)()) scm_round);
3404 #ifdef __STDC__
3405 double
3406 scm_round(double x)
3407 #else
3408 double
3409 scm_round(x)
3410 double x;
3411 #endif
3412 {
3413 double plus_half = x + 0.5;
3414 double result = floor(plus_half);
3415 /* Adjust so that the scm_round is towards even. */
3416 return (plus_half == result && plus_half / 2 != floor(plus_half / 2))
3417 ? result - 1 : result;
3418 }
3419
3420
3421
3422 SCM_PROC1 (s_exact_to_inexact, "exact->inexact", scm_tc7_cxr, (SCM (*)()) scm_exact_to_inexact);
3423 #ifdef __STDC__
3424 double
3425 scm_exact_to_inexact(double z)
3426 #else
3427 double
3428 scm_exact_to_inexact(z)
3429 double z;
3430 #endif
3431 {
3432 return z;
3433 }
3434
3435
3436 SCM_PROC1 (s_i_floor, "floor", scm_tc7_cxr, (SCM (*)()) floor);
3437 SCM_PROC1 (s_i_ceil, "ceiling", scm_tc7_cxr, (SCM (*)()) ceil);
3438 SCM_PROC1 (s_i_sqrt, "$sqrt", scm_tc7_cxr, (SCM (*)())sqrt);
3439 SCM_PROC1 (s_i_abs, "$abs", scm_tc7_cxr, (SCM (*)())fabs);
3440 SCM_PROC1 (s_i_exp, "$exp", scm_tc7_cxr, (SCM (*)())exp);
3441 SCM_PROC1 (s_i_log, "$log", scm_tc7_cxr, (SCM (*)())log);
3442 SCM_PROC1 (s_i_sin, "$sin", scm_tc7_cxr, (SCM (*)())sin);
3443 SCM_PROC1 (s_i_cos, "$cos", scm_tc7_cxr, (SCM (*)())cos);
3444 SCM_PROC1 (s_i_tan, "$tan", scm_tc7_cxr, (SCM (*)())tan);
3445 SCM_PROC1 (s_i_asin, "$asin", scm_tc7_cxr, (SCM (*)())asin);
3446 SCM_PROC1 (s_i_acos, "$acos", scm_tc7_cxr, (SCM (*)())acos);
3447 SCM_PROC1 (s_i_atan, "$atan", scm_tc7_cxr, (SCM (*)())atan);
3448 SCM_PROC1 (s_i_sinh, "$sinh", scm_tc7_cxr, (SCM (*)())sinh);
3449 SCM_PROC1 (s_i_cosh, "$cosh", scm_tc7_cxr, (SCM (*)())cosh);
3450 SCM_PROC1 (s_i_tanh, "$tanh", scm_tc7_cxr, (SCM (*)())tanh);
3451
3452 struct dpair {double x, y;};
3453
3454 static void
3455 scm_two_doubles(z1, z2, sstring, xy)
3456 SCM z1, z2;
3457 char *sstring;
3458 struct dpair *xy;
3459 {
3460 if SCM_INUMP(z1) xy->x = SCM_INUM(z1);
3461 else {
3462 # ifdef SCM_BIGDIG
3463 SCM_ASRTGO(SCM_NIMP(z1), badz1);
3464 if SCM_BIGP(z1) xy->x = scm_big2dbl(z1);
3465 else {
3466 # ifndef RECKLESS
3467 if (!(SCM_REALP(z1)))
3468 badz1: scm_wta(z1, (char *)SCM_ARG1, sstring);
3469 # endif
3470 xy->x = SCM_REALPART(z1);}
3471 # else
3472 {SCM_ASSERT(SCM_NIMP(z1) && SCM_REALP(z1), z1, SCM_ARG1, sstring);
3473 xy->x = SCM_REALPART(z1);}
3474 # endif
3475 }
3476 if SCM_INUMP(z2) xy->y = SCM_INUM(z2);
3477 else {
3478 # ifdef SCM_BIGDIG
3479 SCM_ASRTGO(SCM_NIMP(z2), badz2);
3480 if SCM_BIGP(z2) xy->y = scm_big2dbl(z2);
3481 else {
3482 # ifndef RECKLESS
3483 if (!(SCM_REALP(z2)))
3484 badz2: scm_wta(z2, (char *)SCM_ARG2, sstring);
3485 # endif
3486 xy->y = SCM_REALPART(z2);}
3487 # else
3488 {SCM_ASSERT(SCM_NIMP(z2) && SCM_REALP(z2), z2, SCM_ARG2, sstring);
3489 xy->y = SCM_REALPART(z2);}
3490 # endif
3491 }
3492 }
3493
3494
3495
3496
3497 SCM_PROC(s_sys_expt, "$expt", 2, 0, 0, scm_sys_expt);
3498 #ifdef __STDC__
3499 SCM
3500 scm_sys_expt(SCM z1, SCM z2)
3501 #else
3502 SCM
3503 scm_sys_expt(z1, z2)
3504 SCM z1;
3505 SCM z2;
3506 #endif
3507 {
3508 struct dpair xy;
3509 scm_two_doubles(z1, z2, s_sys_expt, &xy);
3510 return scm_makdbl(pow(xy.x, xy.y), 0.0);
3511 }
3512
3513
3514
3515 SCM_PROC(s_sys_atan2, "$atan2", 2, 0, 0, scm_sys_atan2);
3516 #ifdef __STDC__
3517 SCM
3518 scm_sys_atan2(SCM z1, SCM z2)
3519 #else
3520 SCM
3521 scm_sys_atan2(z1, z2)
3522 SCM z1;
3523 SCM z2;
3524 #endif
3525 {
3526 struct dpair xy;
3527 scm_two_doubles(z1, z2, s_sys_atan2, &xy);
3528 return scm_makdbl(atan2(xy.x, xy.y), 0.0);
3529 }
3530
3531
3532
3533 SCM_PROC(s_make_rectangular, "make-rectangular", 2, 0, 0, scm_make_rectangular);
3534 #ifdef __STDC__
3535 SCM
3536 scm_make_rectangular(SCM z1, SCM z2)
3537 #else
3538 SCM
3539 scm_make_rectangular(z1, z2)
3540 SCM z1;
3541 SCM z2;
3542 #endif
3543 {
3544 struct dpair xy;
3545 scm_two_doubles(z1, z2, s_make_rectangular, &xy);
3546 return scm_makdbl(xy.x, xy.y);
3547 }
3548
3549
3550
3551 SCM_PROC(s_make_polar, "make-polar", 2, 0, 0, scm_make_polar);
3552 #ifdef __STDC__
3553 SCM
3554 scm_make_polar(SCM z1, SCM z2)
3555 #else
3556 SCM
3557 scm_make_polar(z1, z2)
3558 SCM z1;
3559 SCM z2;
3560 #endif
3561 {
3562 struct dpair xy;
3563 scm_two_doubles(z1, z2, s_make_polar, &xy);
3564 return scm_makdbl(xy.x*cos(xy.y), xy.x*sin(xy.y));
3565 }
3566
3567
3568
3569
3570 SCM_PROC(s_real_part, "real-part", 1, 0, 0, scm_real_part);
3571 #ifdef __STDC__
3572 SCM
3573 scm_real_part(SCM z)
3574 #else
3575 SCM
3576 scm_real_part(z)
3577 SCM z;
3578 #endif
3579 {
3580 if SCM_NINUMP(z) {
3581 # ifdef SCM_BIGDIG
3582 SCM_ASRTGO(SCM_NIMP(z), badz);
3583 if SCM_BIGP(z) return z;
3584 # ifndef RECKLESS
3585 if (!(SCM_INEXP(z)))
3586 badz: scm_wta(z, (char *)SCM_ARG1, s_real_part);
3587 # endif
3588 # else
3589 SCM_ASSERT(SCM_NIMP(z) && SCM_INEXP(z), z, SCM_ARG1, s_real_part);
3590 # endif
3591 if SCM_CPLXP(z) return scm_makdbl(SCM_REAL(z), 0.0);
3592 }
3593 return z;
3594 }
3595
3596
3597
3598 SCM_PROC(s_imag_part, "imag-part", 1, 0, 0, scm_imag_part);
3599 #ifdef __STDC__
3600 SCM
3601 scm_imag_part(SCM z)
3602 #else
3603 SCM
3604 scm_imag_part(z)
3605 SCM z;
3606 #endif
3607 {
3608 if SCM_INUMP(z) return SCM_INUM0;
3609 # ifdef SCM_BIGDIG
3610 SCM_ASRTGO(SCM_NIMP(z), badz);
3611 if SCM_BIGP(z) return SCM_INUM0;
3612 # ifndef RECKLESS
3613 if (!(SCM_INEXP(z)))
3614 badz: scm_wta(z, (char *)SCM_ARG1, s_imag_part);
3615 # endif
3616 # else
3617 SCM_ASSERT(SCM_NIMP(z) && SCM_INEXP(z), z, SCM_ARG1, s_imag_part);
3618 # endif
3619 if SCM_CPLXP(z) return scm_makdbl(SCM_IMAG(z), 0.0);
3620 return scm_flo0;
3621 }
3622
3623
3624
3625 SCM_PROC(s_magnitude, "magnitude", 1, 0, 0, scm_magnitude);
3626 #ifdef __STDC__
3627 SCM
3628 scm_magnitude(SCM z)
3629 #else
3630 SCM
3631 scm_magnitude(z)
3632 SCM z;
3633 #endif
3634 {
3635 if SCM_INUMP(z) return scm_abs(z);
3636 # ifdef SCM_BIGDIG
3637 SCM_ASRTGO(SCM_NIMP(z), badz);
3638 if SCM_BIGP(z) return scm_abs(z);
3639 # ifndef RECKLESS
3640 if (!(SCM_INEXP(z)))
3641 badz: scm_wta(z, (char *)SCM_ARG1, s_magnitude);
3642 # endif
3643 # else
3644 SCM_ASSERT(SCM_NIMP(z) && SCM_INEXP(z), z, SCM_ARG1, s_magnitude);
3645 # endif
3646 if SCM_CPLXP(z)
3647 {
3648 double i = SCM_IMAG(z), r = SCM_REAL(z);
3649 return scm_makdbl(sqrt(i*i+r*r), 0.0);
3650 }
3651 return scm_makdbl(fabs(SCM_REALPART(z)), 0.0);
3652 }
3653
3654
3655
3656
3657 SCM_PROC(s_angle, "angle", 1, 0, 0, scm_angle);
3658 #ifdef __STDC__
3659 SCM
3660 scm_angle(SCM z)
3661 #else
3662 SCM
3663 scm_angle(z)
3664 SCM z;
3665 #endif
3666 {
3667 double x, y = 0.0;
3668 if SCM_INUMP(z) {x = (z>=SCM_INUM0) ? 1.0 : -1.0; goto do_angle;}
3669 # ifdef SCM_BIGDIG
3670 SCM_ASRTGO(SCM_NIMP(z), badz);
3671 if SCM_BIGP(z) {x = (SCM_TYP16(z)==scm_tc16_bigpos) ? 1.0 : -1.0; goto do_angle;}
3672 # ifndef RECKLESS
3673 if (!(SCM_INEXP(z))) {
3674 badz: scm_wta(z, (char *)SCM_ARG1, s_angle);}
3675 # endif
3676 # else
3677 SCM_ASSERT(SCM_NIMP(z) && SCM_INEXP(z), z, SCM_ARG1, s_angle);
3678 # endif
3679 if (SCM_REALP(z))
3680 {
3681 x = SCM_REALPART(z);
3682 goto do_angle;
3683 }
3684 x = SCM_REAL(z); y = SCM_IMAG(z);
3685 do_angle:
3686 return scm_makdbl(atan2(y, x), 0.0);
3687 }
3688
3689
3690 SCM_PROC(s_inexact_to_exact, "inexact->exact", 1, 0, 0, scm_inexact_to_exact);
3691 #ifdef __STDC__
3692 SCM
3693 scm_inexact_to_exact(SCM z)
3694 #else
3695 SCM
3696 scm_inexact_to_exact(z)
3697 SCM z;
3698 #endif
3699 {
3700 if SCM_INUMP(z) return z;
3701 # ifdef SCM_BIGDIG
3702 SCM_ASRTGO(SCM_NIMP(z), badz);
3703 if SCM_BIGP(z) return z;
3704 # ifndef RECKLESS
3705 if (!(SCM_REALP(z)))
3706 badz: scm_wta(z, (char *)SCM_ARG1, s_inexact_to_exact);
3707 # endif
3708 # else
3709 SCM_ASSERT(SCM_NIMP(z) && SCM_REALP(z), z, SCM_ARG1, s_inexact_to_exact);
3710 # endif
3711 # ifdef SCM_BIGDIG
3712 {
3713 double u = floor(SCM_REALPART(z)+0.5);
3714 if ((u <= SCM_MOST_POSITIVE_FIXNUM) && (-u <= -SCM_MOST_NEGATIVE_FIXNUM)) {
3715 /* Negation is a workaround for HP700 cc bug */
3716 SCM ans = SCM_MAKINUM((long)u);
3717 if (SCM_INUM(ans)==(long)u) return ans;
3718 }
3719 SCM_ASRTGO(!IS_INF(u), badz); /* problem? */
3720 return scm_dbl2big(u);
3721 }
3722 # else
3723 return SCM_MAKINUM((long)floor(SCM_REALPART(z)+0.5));
3724 # endif
3725 }
3726
3727
3728
3729 #else /* ~SCM_FLOATS */
3730 SCM_PROC(s_trunc, "truncate", 1, 0, 0, scm_trunc);
3731 #ifdef __STDC__
3732 SCM
3733 scm_trunc(SCM x)
3734 #else
3735 SCM
3736 scm_trunc(x)
3737 SCM x;
3738 #endif
3739 {
3740 SCM_ASSERT(SCM_INUMP(x), x, SCM_ARG1, s_truncate);
3741 return x;
3742 }
3743
3744
3745
3746 #endif /* SCM_FLOATS */
3747
3748 #ifdef SCM_BIGDIG
3749 # ifdef SCM_FLOATS
3750 /* d must be integer */
3751 #ifdef __STDC__
3752 SCM
3753 scm_dbl2big(double d)
3754 #else
3755 SCM
3756 scm_dbl2big(d)
3757 double d;
3758 #endif
3759 {
3760 scm_sizet i = 0;
3761 long c;
3762 SCM_BIGDIG *digits;
3763 SCM ans;
3764 double u = (d < 0)?-d:d;
3765 while (0 != floor(u)) {u /= SCM_BIGRAD;i++;}
3766 ans = scm_mkbig(i, d < 0);
3767 digits = SCM_BDIGITS(ans);
3768 while (i--) {
3769 u *= SCM_BIGRAD;
3770 c = floor(u);
3771 u -= c;
3772 digits[i] = c;
3773 }
3774 #ifndef RECKLESS
3775 if (u != 0)
3776 scm_num_overflow ("dbl2big");
3777 #endif
3778 return ans;
3779 }
3780
3781
3782
3783 #ifdef __STDC__
3784 double
3785 scm_big2dbl(SCM b)
3786 #else
3787 double
3788 scm_big2dbl(b)
3789 SCM b;
3790 #endif
3791 {
3792 double ans = 0.0;
3793 scm_sizet i = SCM_NUMDIGS(b);
3794 SCM_BIGDIG *digits = SCM_BDIGITS(b);
3795 while (i--) ans = digits[i] + SCM_BIGRAD*ans;
3796 if (scm_tc16_bigneg==SCM_TYP16(b)) return -ans;
3797 return ans;
3798 }
3799 # endif
3800 #endif
3801
3802 #ifdef __STDC__
3803 SCM
3804 scm_long2num(long sl)
3805 #else
3806 SCM
3807 scm_long2num(sl)
3808 long sl;
3809 #endif
3810 {
3811 if (!SCM_FIXABLE(sl)) {
3812 #ifdef SCM_BIGDIG
3813 return scm_long2big(sl);
3814 #else
3815 # ifdef SCM_FLOATS
3816 return scm_makdbl((double) sl, 0.0);
3817 # else
3818 return SCM_BOOL_F;
3819 # endif
3820 #endif
3821 }
3822 return SCM_MAKINUM(sl);
3823 }
3824
3825
3826 #ifdef LONGLONGS
3827 #ifdef __STDC__
3828 SCM
3829 scm_long_long2num(long_long sl)
3830 #else
3831 SCM
3832 scm_long_long2num(sl)
3833 long_long sl;
3834 #endif
3835 {
3836 if (!SCM_FIXABLE(sl)) {
3837 #ifdef SCM_BIGDIG
3838 return scm_long_long2big(sl);
3839 #else
3840 # ifdef SCM_FLOATS
3841 return scm_makdbl((double) sl, 0.0);
3842 # else
3843 return SCM_BOOL_F;
3844 # endif
3845 #endif
3846 }
3847 return SCM_MAKINUM(sl);
3848 }
3849 #endif
3850
3851
3852 #ifdef __STDC__
3853 SCM
3854 scm_ulong2num(unsigned long sl)
3855 #else
3856 SCM
3857 scm_ulong2num(sl)
3858 unsigned long sl;
3859 #endif
3860 {
3861 if (!SCM_POSFIXABLE(sl)) {
3862 #ifdef SCM_BIGDIG
3863 return scm_ulong2big(sl);
3864 #else
3865 # ifdef SCM_FLOATS
3866 return scm_makdbl((double) sl, 0.0);
3867 # else
3868 return SCM_BOOL_F;
3869 # endif
3870 #endif
3871 }
3872 return SCM_MAKINUM(sl);
3873 }
3874
3875 #ifdef __STDC__
3876 long
3877 scm_num2long(SCM num, char *pos, char *s_caller)
3878 #else
3879 long
3880 scm_num2long(num, pos, s_caller)
3881 SCM num;
3882 char *pos;
3883 char *s_caller;
3884 #endif
3885 {
3886 long res;
3887 if (SCM_INUMP(num))
3888 {
3889 res = SCM_INUM(num);
3890 return res;
3891 }
3892 SCM_ASRTGO(SCM_NIMP(num), errout);
3893 #ifdef SCM_FLOATS
3894 if (SCM_REALP(num))
3895 {
3896 double u = SCM_REALPART(num);
3897 res = u;
3898 if ((double)res == u)
3899 {
3900 return res;
3901 }
3902 }
3903 #endif
3904 #ifdef SCM_BIGDIG
3905 if (SCM_BIGP(num)) {
3906 long oldres;
3907 scm_sizet l;
3908 res = 0;
3909 oldres = 0;
3910 for(l = SCM_NUMDIGS(num);l--;)
3911 {
3912 res = SCM_BIGUP(res) + SCM_BDIGITS(num)[l];
3913 if (res < oldres)
3914 goto errout;
3915 oldres = res;
3916 }
3917 if (SCM_TYP16 (num) == scm_tc16_bigpos)
3918 return res;
3919 else
3920 return -res;
3921 }
3922 #endif
3923 errout: scm_wta(num, pos, s_caller);
3924 return SCM_UNSPECIFIED;
3925 }
3926
3927
3928
3929
3930 #ifdef __STDC__
3931 long
3932 num2long(SCM num, char *pos, char *s_caller)
3933 #else
3934 long
3935 num2long(num, pos, s_caller)
3936 SCM num;
3937 char *pos;
3938 char *s_caller;
3939 #endif
3940 {
3941 long res;
3942 if SCM_INUMP(num) {
3943 res = SCM_INUM((long)num);
3944 return res;
3945 }
3946 SCM_ASRTGO(SCM_NIMP(num), errout);
3947 #ifdef SCM_FLOATS
3948 if SCM_REALP(num) {
3949 double u = SCM_REALPART(num);
3950 if (((SCM_MOST_NEGATIVE_FIXNUM * 4) <= u)
3951 && (u <= (SCM_MOST_POSITIVE_FIXNUM * 4 + 3))) {
3952 res = u;
3953 return res;
3954 }
3955 }
3956 #endif
3957 #ifdef SCM_BIGDIG
3958 if SCM_BIGP(num) {
3959 scm_sizet l = SCM_NUMDIGS(num);
3960 SCM_ASRTGO(SCM_DIGSPERLONG >= l, errout);
3961 res = 0;
3962 for(;l--;) res = SCM_BIGUP(res) + SCM_BDIGITS(num)[l];
3963 return res;
3964 }
3965 #endif
3966 errout: scm_wta(num, pos, s_caller);
3967 return SCM_UNSPECIFIED;
3968 }
3969
3970
3971 #ifdef LONGLONGS
3972 #ifdef __STDC__
3973 long_long
3974 scm_num2long_long(SCM num, char *pos, char *s_caller)
3975 #else
3976 long_long
3977 scm_num2long_long(num, pos, s_caller)
3978 SCM num;
3979 char *pos;
3980 char *s_caller;
3981 #endif
3982 {
3983 long_long res;
3984 if SCM_INUMP(num) {
3985 res = SCM_INUM((long_long)num);
3986 return res;
3987 }
3988 SCM_ASRTGO(SCM_NIMP(num), errout);
3989 #ifdef SCM_FLOATS
3990 if SCM_REALP(num) {
3991 double u = SCM_REALPART(num);
3992 if (((SCM_MOST_NEGATIVE_FIXNUM * 4) <= u)
3993 && (u <= (SCM_MOST_POSITIVE_FIXNUM * 4 + 3))) {
3994 res = u;
3995 return res;
3996 }
3997 }
3998 #endif
3999 #ifdef SCM_BIGDIG
4000 if SCM_BIGP(num) {
4001 scm_sizet l = SCM_NUMDIGS(num);
4002 SCM_ASRTGO(SCM_DIGSPERLONGLONG >= l, errout);
4003 res = 0;
4004 for(;l--;) res = SCM_LONGLONGBIGUP(res) + SCM_BDIGITS(num)[l];
4005 return res;
4006 }
4007 #endif
4008 errout: scm_wta(num, pos, s_caller);
4009 return SCM_UNSPECIFIED;
4010 }
4011 #endif
4012
4013
4014 #ifdef __STDC__
4015 unsigned long
4016 scm_num2ulong(SCM num, char *pos, char *s_caller)
4017 #else
4018 unsigned long
4019 scm_num2ulong(num, pos, s_caller)
4020 SCM num;
4021 char *pos;
4022 char *s_caller;
4023 #endif
4024 {
4025 unsigned long res;
4026 if (SCM_INUMP(num))
4027 {
4028 res = SCM_INUM((unsigned long)num);
4029 return res;
4030 }
4031 SCM_ASRTGO(SCM_NIMP(num), errout);
4032 #ifdef SCM_FLOATS
4033 if (SCM_REALP(num))
4034 {
4035 double u = SCM_REALPART(num);
4036 if ((0 <= u) && (u <= (unsigned long)~0L))
4037 {
4038 res = u;
4039 return res;
4040 }
4041 }
4042 #endif
4043 #ifdef SCM_BIGDIG
4044 if (SCM_BIGP(num)) {
4045 unsigned long oldres;
4046 scm_sizet l;
4047 res = 0;
4048 oldres = 0;
4049 for(l = SCM_NUMDIGS(num);l--;)
4050 {
4051 res = SCM_BIGUP(res) + SCM_BDIGITS(num)[l];
4052 if (res < oldres)
4053 goto errout;
4054 oldres = res;
4055 }
4056 return res;
4057 }
4058 #endif
4059 errout: scm_wta(num, pos, s_caller);
4060 return SCM_UNSPECIFIED;
4061 }
4062
4063
4064 #ifdef SCM_FLOATS
4065 # ifndef DBL_DIG
4066 static void add1(f, fsum)
4067 double f, *fsum;
4068 {
4069 *fsum = f + 1.0;
4070 }
4071 # endif
4072 #endif
4073
4074
4075 #ifdef __STDC__
4076 void
4077 scm_init_numbers (void)
4078 #else
4079 void
4080 scm_init_numbers ()
4081 #endif
4082 {
4083 #ifdef SCM_FLOATS
4084 SCM_NEWCELL(scm_flo0);
4085 # ifdef SCM_SINGLES
4086 SCM_CAR(scm_flo0) = scm_tc_flo;
4087 SCM_FLO(scm_flo0) = 0.0;
4088 # else
4089 SCM_CDR(scm_flo0) = (SCM)scm_must_malloc(1L*sizeof(double), "real");
4090 SCM_REAL(scm_flo0) = 0.0;
4091 SCM_CAR(scm_flo0) = scm_tc_dblr;
4092 # endif
4093 # ifdef DBL_DIG
4094 scm_dblprec = (DBL_DIG > 20) ? 20 : DBL_DIG;
4095 # else
4096 { /* determine floating point precision */
4097 double f = 0.1;
4098 double fsum = 1.0+f;
4099 while (fsum != 1.0) {
4100 f /= 10.0;
4101 if (++scm_dblprec > 20) break;
4102 add1(f, &fsum);
4103 }
4104 scm_dblprec = scm_dblprec-1;
4105 }
4106 # endif /* DBL_DIG */
4107 #endif
4108 #include "numbers.x"
4109 }
4110