Debian Codegraph @

scheme/ikarus.numerics.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 (library (ikarus flonums)
17 (export $flonum->exact flonum-parts
18 inexact->exact exact $flonum-rational? $flonum-integer? $flzero?
19 $flnegative? flpositive? flabs fixnum->flonum
20 flsin flcos fltan flasin flacos flatan fleven? flodd?
21 flfloor flceiling flnumerator fldenominator flexp fllog
22 flinteger? flonum-bytes flnan? flfinite? flinfinite?
23 flexpt $flround flround)
24 (import
25 (ikarus system $bytevectors)
26 (ikarus system $fx)
27 (only (ikarus system $flonums) $fl>= $flonum-sbe)
28 (ikarus system $bignums)
29 (except (ikarus system $flonums) $flonum-rational?
30 $flonum-integer? $flround)
31 (except (ikarus) inexact->exact exact flpositive? flabs fixnum->flonum
32 flsin flcos fltan flasin flacos flatan fleven? flodd?
33 flfloor flceiling flnumerator fldenominator flexp fllog
34 flexpt flinteger? flonum-parts flonum-bytes flnan? flfinite?
35 flinfinite? flround))
36
37 (define (flonum-bytes f)
38 (unless (flonum? f)
39 (die 'flonum-bytes "not a flonum" f))
40 (values
41 ($flonum-u8-ref f 0)
42 ($flonum-u8-ref f 1)
43 ($flonum-u8-ref f 2)
44 ($flonum-u8-ref f 3)
45 ($flonum-u8-ref f 4)
46 ($flonum-u8-ref f 5)
47 ($flonum-u8-ref f 6)
48 ($flonum-u8-ref f 7)))
49 (define (flonum-parts x)
50 (unless (flonum? x)
51 (die 'flonum-parts "not a flonum" x))
52 (let-values ([(b0 b1 b2 b3 b4 b5 b6 b7) (flonum-bytes x)])
53 (values
54 (zero? (fxlogand b0 128))
55 (+ (fxsll (fxlogand b0 127) 4)
56 (fxsra b1 4))
57 (+ (+ b7 (fxsll b6 8) (fxsll b5 16))
58 (* (+ b4
59 (fxsll b3 8)
60 (fxsll b2 16)
61 (fxsll (fxlogand b1 #b1111) 24))
62 (expt 2 24))))))
63 (define ($zero-m? f)
64 (and ($fxzero? ($flonum-u8-ref f 7))
65 ($fxzero? ($flonum-u8-ref f 6))
66 ($fxzero? ($flonum-u8-ref f 5))
67 ($fxzero? ($flonum-u8-ref f 4))
68 ($fxzero? ($flonum-u8-ref f 3))
69 ($fxzero? ($flonum-u8-ref f 2))
70 ($fxzero? ($fxlogand ($flonum-u8-ref f 1) #b1111))))
71
72 (define ($flonum-rational? x)
73 (let ([be ($fxlogand ($flonum-sbe x)
74 ($fxsub1 ($fxsll 1 11)))])
75 ($fx< be 2047)))
76
77 (define ($flonum-integer? x)
78 (let ([be ($fxlogand ($flonum-sbe x)
79 ($fxsub1 ($fxsll 1 11)))])
80 (cond
81 [($fx= be 2047) ;;; nans and infs
82 #f]
83 [($fx>= be 1075) ;;; magnitue large enough
84 #t]
85 [($fx= be 0) ;;; denormalized double, only +/-0.0 is integer
86 (and ($fx= ($flonum-u8-ref x 7) 0)
87 ($fx= ($flonum-u8-ref x 6) 0)
88 ($fx= ($flonum-u8-ref x 5) 0)
89 ($fx= ($flonum-u8-ref x 4) 0)
90 ($fx= ($flonum-u8-ref x 3) 0)
91 ($fx= ($flonum-u8-ref x 2) 0)
92 ($fx= ($flonum-u8-ref x 1) 0))]
93 [($fx< be ($fx+ 1075 -52)) ;;; too small to be an integer
94 #f]
95 [else ($fl= x ($$flround x))])))
96
97
98 (define ($$flround x)
99 (foreign-call "ikrt_fl_round" x ($make-flonum)))
100
101 (define ($flround x)
102 ;;; optimize for integer flonums case
103 (define (ratnum-round n nbe)
104 (let ([d (sll 1 nbe)])
105 (let ([q (sra n nbe)]
106 [r (bitwise-and n (sub1 d))])
107 (let ([r2 (+ r r)])
108 (cond
109 [(< r2 d) q]
110 [(> r2 d) (+ q 1)]
111 [else (if (even? q) q (+ q 1))])))))
112 (let ([sbe ($flonum-sbe x)])
113 (let ([be ($fxlogand sbe #x7FF)])
114 (cond
115 ;;; nans/infs/magnitude large enough to be an integer
116 [($fx>= be 1075) x]
117 [else
118 ;;; this really needs to get optimized.
119 (let-values ([(pos? be m) (flonum-parts x)])
120 (cond
121 [(= be 0) ;;; denormalized
122 (if pos? +0.0 -0.0)]
123 [else ; normalized flonum
124 (let ([r
125 (inexact
126 (ratnum-round (+ m (expt 2 52)) (- 1075 be)))])
127 (if pos? r ($fl* r -1.0)))]))]))))
128
129 (define (flround x)
130 (if (flonum? x)
131 ($flround x)
132 (die 'flround "not a flonum" x)))
133
134 (module ($flonum->exact)
135 (define ($flonum-signed-mantissa x)
136 (let ([b0 ($flonum-u8-ref x 0)])
137 (let ([m0 ($fx+ ($flonum-u8-ref x 7)
138 ($fx+ ($fxsll ($flonum-u8-ref x 6) 8)
139 ($fxsll ($flonum-u8-ref x 5) 16)))]
140 [m1 ($fx+ ($flonum-u8-ref x 4)
141 ($fx+ ($fxsll ($flonum-u8-ref x 3) 8)
142 ($fxsll ($flonum-u8-ref x 2) 16)))]
143 [m2 (let ([b1 ($flonum-u8-ref x 1)])
144 (if (and ($fx= ($fxlogand b0 #x7F) 0)
145 ($fx= ($fxsra b1 4) 0))
146 ($fxlogand b1 #xF)
147 ($fxlogor ($fxlogand b1 #xF) #x10)))])
148 (if ($fx= 0 ($fxlogand #x80 b0))
149 (+ (bitwise-arithmetic-shift-left ($fxlogor m1 ($fxsll m2 24)) 24) m0)
150 (+ (bitwise-arithmetic-shift-left
151 ($fx- 0 ($fxlogor m1 ($fxsll m2 24))) 24)
152 ($fx- 0 m0))))))
153 (define ($flonum->exact x)
154 (import (ikarus))
155 (let ([sbe ($flonum-sbe x)])
156 (let ([be ($fxlogand sbe #x7FF)])
157 (cond
158 [($fx= be 2047) #f] ;;; nans/infs
159 [($fx>= be 1075) ;;; magnitude large enough to be an integer
160 (bitwise-arithmetic-shift-left
161 ($flonum-signed-mantissa x)
162 (- be 1075))]
163 [else
164 ;;; this really needs to get optimized.
165 (let-values ([(pos? be m) (flonum-parts x)])
166 (cond
167 [(= be 0) ;;; denormalized
168 (if (= m 0)
169 0
170 (* (if pos? 1 -1)
171 (/ m (expt 2 1074))))]
172 [else ; normalized flonum
173 (/ (+ m (expt 2 52))
174 (bitwise-arithmetic-shift-left
175 (if pos? 1 -1)
176 (- 1075 be)))]))])))))
177
178 (define (flnumerator x)
179 (unless (flonum? x)
180 (die 'flnumerator "not a flonum" x))
181 (cond
182 [($flonum-integer? x) x]
183 [($flonum-rational? x)
184 (exact->inexact (numerator ($flonum->exact x)))]
185 [else x]))
186
187 (define (fldenominator x)
188 (unless (flonum? x)
189 (die 'fldenominator "not a flonum" x))
190 (cond
191 [($flonum-integer? x) 1.0]
192 [($flonum-rational? x)
193 (exact->inexact (denominator ($flonum->exact x)))]
194 [(flnan? x) x]
195 [else 1.0]))
196
197 (define (fleven? x)
198 ;;; FIXME: optimize
199 (unless (flonum? x)
200 (die 'fleven? "not a flonum" x))
201 (let ([v ($flonum->exact x)])
202 (cond
203 [(fixnum? v) ($fx= ($fxlogand v 1) 0)]
204 [(bignum? v)
205 (foreign-call "ikrt_even_bn" v)]
206 [else (die 'fleven? "not an integer flonum" x)])))
207
208 (define (flodd? x)
209 (unless (flonum? x)
210 (die 'flodd? "not a flonum" x))
211 ;;; FIXME: optimize
212 (let ([v ($flonum->exact x)])
213 (cond
214 [(fixnum? v) ($fx= ($fxlogand v 1) 1)]
215 [(bignum? v)
216 (not (foreign-call "ikrt_even_bn" v))]
217 [else (die 'flodd? "not an integer flonum" x)])))
218
219 (define (flinteger? x)
220 (if (flonum? x)
221 ($flonum-integer? x)
222 (die 'flinteger? "not a flonum" x)))
223
224 (define (flinfinite? x)
225 (if (flonum? x)
226 (let ([be (fxlogand ($flonum-sbe x) (sub1 (fxsll 1 11)))])
227 (and (fx= be 2047) ;;; nans and infs
228 ($zero-m? x)))
229 (die 'flinfinite? "not a flonum" x)))
230
231 (define (flnan? x)
232 (if (flonum? x)
233 (let ([be (fxlogand ($flonum-sbe x) (sub1 (fxsll 1 11)))])
234 (and (fx= be 2047) ;;; nans and infs
235 (not ($zero-m? x))))
236 (die 'flnan? "not a flonum" x)))
237
238 (define (flfinite? x)
239 (if (flonum? x)
240 (let ([be (fxlogand ($flonum-sbe x) (sub1 (fxsll 1 11)))])
241 (not (fx= be 2047)))
242 (die 'flfinite? "not a flonum" x)))
243
244 (define ($flzero? x)
245 (let ([be (fxlogand ($flonum-sbe x) (sub1 (fxsll 1 11)))])
246 (and
247 (fx= be 0) ;;; denormalized double, only +/-0.0 is integer
248 (and (fx= ($flonum-u8-ref x 7) 0)
249 (fx= ($flonum-u8-ref x 6) 0)
250 (fx= ($flonum-u8-ref x 5) 0)
251 (fx= ($flonum-u8-ref x 4) 0)
252 (fx= ($flonum-u8-ref x 3) 0)
253 (fx= ($flonum-u8-ref x 2) 0)
254 (fx= ($flonum-u8-ref x 1) 0)))))
255
256 (define ($flnegative? x)
257 (let ([b0 ($flonum-u8-ref x 0)])
258 (fx> b0 127)))
259
260 (define ($exact x who)
261 (import (ikarus system $compnums))
262 (cond
263 [(flonum? x)
264 (or ($flonum->exact x)
265 (die who "number has no real value" x))]
266 [(cflonum? x)
267 (make-rectangular
268 (or ($flonum->exact ($cflonum-real x))
269 (die who "number has no real value" x))
270 (or ($flonum->exact ($cflonum-imag x))
271 (die who "number has no real value" x)))]
272 [(or (fixnum? x) (ratnum? x) (bignum? x) (compnum? x)) x]
273 [else (die who "not a number" x)]))
274
275 (define (inexact->exact x) ($exact x 'inexact->exact))
276 (define (exact x) ($exact x 'exact))
277
278
279 (define (flpositive? x)
280 (if (flonum? x)
281 ($fl> x 0.0)
282 (die 'flpositive? "not a flonum" x)))
283
284 (define (flabs x)
285 (if (flonum? x)
286 (if ($fx> ($flonum-u8-ref x 0) 127)
287 ($fl* x -1.0)
288 x)
289 (die 'flabs "not a flonum" x)))
290
291 (define (fixnum->flonum x)
292 (if (fixnum? x)
293 ($fixnum->flonum x)
294 (die 'fixnum->flonum "not a fixnum")))
295
296 (define (flsin x)
297 (if (flonum? x)
298 (foreign-call "ikrt_fl_sin" x)
299 (die 'flsin "not a flonum" x)))
300
301 (define (flcos x)
302 (if (flonum? x)
303 (foreign-call "ikrt_fl_cos" x)
304 (die 'flcos "not a flonum" x)))
305
306 (define (fltan x)
307 (if (flonum? x)
308 (foreign-call "ikrt_fl_tan" x)
309 (die 'fltan "not a flonum" x)))
310
311 (define (flasin x)
312 (if (flonum? x)
313 (foreign-call "ikrt_fl_asin" x)
314 (die 'flasin "not a flonum" x)))
315
316 (define (flacos x)
317 (if (flonum? x)
318 (foreign-call "ikrt_fl_acos" x)
319 (die 'flacos "not a flonum" x)))
320
321 (define flatan
322 (case-lambda
323 [(x)
324 (if (flonum? x)
325 (foreign-call "ikrt_fl_atan" x)
326 (die 'flatan "not a flonum" x))]
327 [(x y)
328 (if (flonum? x)
329 (if (flonum? y)
330 (foreign-call "ikrt_atan2" x y)
331 (die 'flatan "not a flonum" y))
332 (die 'flatan "not a flonum" x))]))
333
334 (define (flfloor x)
335 (define (ratnum-floor x)
336 (let ([n (numerator x)] [d (denominator x)])
337 (let ([q (quotient n d)])
338 (if (>= n 0) q (- q 1)))))
339 (cond
340 [(flonum? x)
341 ;;; optimize for integer flonums case
342 (let ([e ($flonum->exact x)])
343 (cond
344 [(ratnum? e)
345 (exact->inexact (ratnum-floor e))]
346 [else x]))]
347 [else (die 'flfloor "not a flonum" x)]))
348
349 (define (flceiling x)
350 (cond
351 [(flonum? x)
352 ;;; optimize for integer flonums case
353 (let ([e ($flonum->exact x)])
354 (cond
355 [(ratnum? e)
356 (exact->inexact (ceiling e))]
357 [else x]))]
358 [else (die 'flceiling "not a flonum" x)]))
359
360 (define (flexp x)
361 (if (flonum? x)
362 (foreign-call "ikrt_fl_exp" x ($make-flonum))
363 (die 'flexp "not a flonum" x)))
364
365 (define fllog
366 (case-lambda
367 [(x)
368 (if (flonum? x)
369 (foreign-call "ikrt_fl_log" x)
370 (die 'fllog "not a flonum" x))]
371 [(x y)
372 (if (flonum? x)
373 (if (flonum? y)
374 (fl/ (foreign-call "ikrt_fl_log" x)
375 (foreign-call "ikrt_fl_log" y))
376 (die 'fllog "not a flonum" y))
377 (die 'fllog "not a flonum" x))]))
378
379 (define (flexpt x y)
380 (if (flonum? x)
381 (if (flonum? y)
382 (let ([y^ ($flonum->exact y)])
383 ;;; FIXME: performance bottleneck?
384 (cond
385 [(fixnum? y^) (inexact (expt x y^))]
386 [(bignum? y^) (inexact (expt x y^))]
387 [else
388 (foreign-call "ikrt_flfl_expt" x y ($make-flonum))]))
389 (die 'flexpt "not a flonum" y))
390 (die 'fllog "not a flonum" x)))
391 )
392
393
394 (library (ikarus generic-arithmetic)
395 (export + - * / zero? = < <= > >= add1 sub1 quotient remainder
396 modulo even? odd? bitwise-and bitwise-not bitwise-ior
397 bitwise-xor bitwise-if
398 bitwise-arithmetic-shift-right bitwise-arithmetic-shift-left
399 bitwise-arithmetic-shift
400 bitwise-length bitwise-copy-bit-field
401 bitwise-copy-bit bitwise-bit-field
402 positive? negative? expt gcd lcm numerator denominator
403 exact-integer-sqrt
404 quotient+remainder number->string min max
405 abs truncate fltruncate sra sll real->flonum
406 exact->inexact inexact floor ceiling round log fl=? fl<? fl<=? fl>?
407 fl>=? fl+ fl- fl* fl/ flsqrt flmin flzero? flnegative?
408 sin cos tan asin acos atan sqrt exp
409 sinh cosh tanh asinh acosh atanh
410 flmax random
411 error@add1 error@sub1)
412 (import
413 (ikarus system $fx)
414 (ikarus system $flonums)
415 (ikarus system $ratnums)
416 (ikarus system $bignums)
417 (ikarus system $compnums)
418 (ikarus system $chars)
419 (ikarus system $strings)
420 (only (ikarus flonums) $flonum->exact $flzero? $flnegative?
421 $flround)
422 (except (ikarus) + - * / zero? = < <= > >= add1 sub1 quotient
423 remainder modulo even? odd? quotient+remainder number->string
424 bitwise-arithmetic-shift-right bitwise-arithmetic-shift-left
425 bitwise-arithmetic-shift
426 bitwise-length bitwise-copy-bit-field
427 bitwise-copy-bit bitwise-bit-field
428 positive? negative? bitwise-and bitwise-not bitwise-ior
429 bitwise-xor bitwise-if
430 expt gcd lcm numerator denominator
431 exact->inexact inexact floor ceiling round log
432 exact-integer-sqrt min max abs real->flonum
433 fl=? fl<? fl<=? fl>? fl>=? fl+ fl- fl* fl/ flsqrt flmin
434 flzero? flnegative? sra sll exp
435 sin cos tan asin acos atan sqrt truncate fltruncate
436 sinh cosh tanh asinh acosh atanh
437 flmax random))
438
439 (define (bignum->flonum x)
440 (foreign-call "ikrt_bignum_to_flonum" x 0 ($make-flonum)))
441
442
443 ;;; (define (ratnum->flonum x)
444 ;;; (define (->flonum n d)
445 ;;; (let-values ([(q r) (quotient+remainder n d)])
446 ;;; (if (= r 0)
447 ;;; (inexact q)
448 ;;; (if (= q 0)
449 ;;; (/ (->flonum d n))
450 ;;; (+ q (->flonum r d))))))
451 ;;; (let ([n (numerator x)] [d (denominator x)])
452 ;;; (let ([b (bitwise-first-bit-set n)])
453 ;;; (if (eqv? b 0)
454 ;;; (let ([b (bitwise-first-bit-set d)])
455 ;;; (if (eqv? b 0)
456 ;;; (->flonum n d)
457 ;;; (/ (->flonum n (bitwise-arithmetic-shift-right d b))
458 ;;; (expt 2.0 b))))
459 ;;; (* (->flonum (bitwise-arithmetic-shift-right n b) d)
460 ;;; (expt 2.0 b))))))
461
462 ;;; (define (ratnum->flonum x)
463 ;;; (let f ([n ($ratnum-n x)] [d ($ratnum-d x)])
464 ;;; (let-values ([(q r) (quotient+remainder n d)])
465 ;;; (if (= q 0)
466 ;;; (/ 1.0 (f d n))
467 ;;; (if (= r 0)
468 ;;; (inexact q)
469 ;;; (+ q (f r d)))))))
470
471 ;;; (define (ratnum->flonum num)
472 ;;; (define (rat n m)
473 ;;; (let-values ([(q r) (quotient+remainder n m)])
474 ;;; (if (= r 0)
475 ;;; (inexact q)
476 ;;; (fl+ (inexact q) (fl/ 1.0 (rat m r))))))
477 ;;; (define (pos n d)
478 ;;; (cond
479 ;;; [(even? n)
480 ;;; (* (pos (sra n 1) d) 2.0)]
481 ;;; [(even? d)
482 ;;; (/ (pos n (sra d 1)) 2.0)]
483 ;;; [(> n d) (rat n d)]
484 ;;; [else
485 ;;; (/ (rat d n))]))
486 ;;; (let ([n ($ratnum-n num)] [d ($ratnum-d num)])
487 ;;; (if (> n 0)
488 ;;; (pos n d)
489 ;;; (- (pos (- n) d)))))
490
491 (define (ratnum->flonum num)
492 (define *precision* 53)
493 (define (long-div1 n d)
494 (let-values ([(q r) (quotient+remainder n d)])
495 (cond
496 [(< (* r 2) d) (inexact q)]
497 [else (inexact (+ q 1))]
498 ;[else (error #f "invalid" n d q r)]
499 )))
500 (define (long-div2 n d bits)
501 (let f ([bits bits] [ac (long-div1 n d)])
502 (cond
503 [(= bits 0) ac]
504 [else (f (- bits 1) (/ ac 2.0))])))
505 (define (pos n d)
506 (let ([nbits (bitwise-length n)]
507 [dbits (bitwise-length d)])
508 (let ([diff-bits (- nbits dbits)])
509 (if (>= diff-bits *precision*)
510 (long-div1 n d)
511 (let ([extra-bits (- *precision* diff-bits)])
512 (long-div2 (sll n extra-bits) d extra-bits))))))
513 (let ([n ($ratnum-n num)] [d ($ratnum-d num)])
514 (if (> n 0)
515 (pos n d)
516 (- (pos (- n) d)))))
517
518 (define (err who x)
519 (die who (if (number? x) "invalid argument" "not a number") x))
520
521
522 (define binary+
523 (lambda (x y)
524 (cond
525 [(fixnum? x)
526 (cond
527 [(fixnum? y)
528 (foreign-call "ikrt_fxfxplus" x y)]
529 [(bignum? y)
530 (foreign-call "ikrt_fxbnplus" x y)]
531 [(flonum? y)
532 ($fl+ ($fixnum->flonum x) y)]
533 [(ratnum? y)
534 ($make-ratnum
535 (+ (* x ($ratnum-d y)) ($ratnum-n y))
536 ($ratnum-d y))]
537 [(compnum? y)
538 ($make-compnum
539 (binary+ x ($compnum-real y))
540 ($compnum-imag y))]
541 [(cflonum? y)
542 ($make-cflonum
543 (binary+ x ($cflonum-real y))
544 ($fl+ 0.0 ($cflonum-imag y)))]
545 [else (err '+ y)])]
546 [(bignum? x)
547 (cond
548 [(fixnum? y)
549 (foreign-call "ikrt_fxbnplus" y x)]
550 [(bignum? y)
551 (foreign-call "ikrt_bnbnplus" x y)]
552 [(flonum? y)
553 ($fl+ (bignum->flonum x) y)]
554 [(ratnum? y)
555 ($make-ratnum
556 (+ (* x ($ratnum-d y)) ($ratnum-n y))
557 ($ratnum-d y))]
558 [(compnum? y)
559 ($make-compnum
560 (binary+ x ($compnum-real y))
561 ($compnum-imag y))]
562 [(cflonum? y)
563 ($make-cflonum
564 (binary+ x ($cflonum-real y))
565 ($cflonum-imag y))]
566 [else (err '+ y)])]
567 [(flonum? x)
568 (cond
569 [(fixnum? y)
570 ($fl+ x ($fixnum->flonum y))]
571 [(bignum? y)
572 ($fl+ x (bignum->flonum y))]
573 [(flonum? y)
574 ($fl+ x y)]
575 [(ratnum? y)
576 ($fl+ x (ratnum->flonum y))]
577 [(cflonum? y)
578 ($make-cflonum
579 ($fl+ x ($cflonum-real y))
580 ($cflonum-imag y))]
581 [(compnum? y)
582 ($make-cflonum
583 (binary+ x ($compnum-real y))
584 (inexact ($compnum-imag y)))]
585 [else (err '+ y)])]
586 [(ratnum? x)
587 (cond
588 [(or (fixnum? y) (bignum? y))
589 ($make-ratnum
590 (+ (* y ($ratnum-d x)) ($ratnum-n x))
591 ($ratnum-d x))]
592 [(flonum? y)
593 ($fl+ y (ratnum->flonum x))]
594 [(ratnum? y)
595 (let ([n0 ($ratnum-n x)] [n1 ($ratnum-n y)]
596 [d0 ($ratnum-d x)] [d1 ($ratnum-d y)])
597 ;;; FIXME: inefficient
598 (/ (+ (* n0 d1) (* n1 d0)) (* d0 d1)))]
599 [(compnum? y)
600 ($make-compnum
601 (binary+ x ($compnum-real y))
602 ($compnum-imag y))]
603 [(cflonum? y)
604 ($make-cflonum
605 (binary+ x ($cflonum-real y))
606 ($cflonum-imag y))]
607 [else (err '+ y)])]
608 [(compnum? x)
609 (cond
610 [(or (fixnum? y) (bignum? y) (ratnum? y))
611 ($make-compnum
612 (binary+ ($compnum-real x) y)
613 ($compnum-imag x))]
614 [(compnum? y)
615 ($make-rectangular
616 (binary+ ($compnum-real x) ($compnum-real y))
617 (binary+ ($compnum-imag x) ($compnum-imag y)))]
618 [(flonum? y)
619 ($make-cflonum
620 (binary+ y ($compnum-real x))
621 (inexact ($compnum-imag x)))]
622 [(cflonum? y)
623 ($make-cflonum
624 (binary+ ($compnum-real x) ($cflonum-real y))
625 (binary+ ($compnum-imag x) ($cflonum-imag y)))]
626 [else (err '+ y)])]
627 [(cflonum? x)
628 (cond
629 [(cflonum? y)
630 ($make-cflonum
631 (binary+ ($cflonum-real x) ($cflonum-real y))
632 (binary+ ($cflonum-imag x) ($cflonum-imag y)))]
633 [(flonum? y)
634 ($make-cflonum
635 ($fl+ ($cflonum-real x) y)
636 ($cflonum-imag x))]
637 [(or (fixnum? y) (bignum? y) (ratnum? y))
638 ($make-compnum
639 (binary+ ($compnum-real x) y)
640 ($compnum-imag x))]
641 [(compnum? y)
642 ($make-cflonum
643 (binary+ ($cflonum-real x) ($compnum-real y))
644 (binary+ ($cflonum-imag x) ($compnum-imag y)))]
645 [else (err '+ y)])]
646 [else (err '+ x)])))
647
648 (define binary-bitwise-and
649 (lambda (x y)
650 (cond
651 [(fixnum? x)
652 (cond
653 [(fixnum? y) ($fxlogand x y)]
654 [(bignum? y)
655 (foreign-call "ikrt_fxbnlogand" x y)]
656 [else
657 (die 'bitwise-and "not an exact integer" y)])]
658 [(bignum? x)
659 (cond
660 [(fixnum? y)
661 (foreign-call "ikrt_fxbnlogand" y x)]
662 [(bignum? y)
663 (foreign-call "ikrt_bnbnlogand" x y)]
664 [else
665 (die 'bitwise-and "not an exact integer" y)])]
666 [else (die 'bitwise-and "not an exact integer" x)])))
667
668 (define binary-bitwise-ior
669 (lambda (x y)
670 (cond
671 [(fixnum? x)
672 (cond
673 [(fixnum? y) ($fxlogor x y)]
674 [(bignum? y)
675 (foreign-call "ikrt_fxbnlogor" x y)]
676 [else
677 (die 'bitwise-ior "not an exact integer" y)])]
678 [(bignum? x)
679 (cond
680 [(fixnum? y)
681 (foreign-call "ikrt_fxbnlogor" y x)]
682 [(bignum? y)
683 (foreign-call "ikrt_bnbnlogor" x y)]
684 [else
685 (die 'bitwise-ior "not an exact integer" y)])]
686 [else (die 'bitwise-ior "not an exact integer" x)])))
687
688
689 (define binary-bitwise-xor
690 (lambda (x y)
691 (define (fxbn x y)
692 (let ([y0 (bitwise-and y (greatest-fixnum))]
693 [y1 (bitwise-arithmetic-shift-right y (- (fixnum-width) 1))])
694 (bitwise-ior
695 ($fxlogand ($fxlogxor x y0) (greatest-fixnum))
696 (bitwise-arithmetic-shift-left
697 (bitwise-arithmetic-shift-right
698 (if ($fx>= x 0) y (bitwise-not y))
699 (- (fixnum-width) 1))
700 (- (fixnum-width) 1)))))
701 (define (bnbn x y)
702 (let ([x0 (bitwise-and x (greatest-fixnum))]
703 [x1 (bitwise-arithmetic-shift-right x (- (fixnum-width) 1))]
704 [y0 (bitwise-and y (greatest-fixnum))]
705 [y1 (bitwise-arithmetic-shift-right y (- (fixnum-width) 1))])
706 (bitwise-ior
707 ($fxlogand ($fxlogxor x0 y0) (greatest-fixnum))
708 (bitwise-arithmetic-shift-left
709 (binary-bitwise-xor x1 y1)
710 (- (fixnum-width) 1)))))
711 (cond
712 [(fixnum? x)
713 (cond
714 [(fixnum? y) ($fxlogxor x y)]
715 [(bignum? y) (fxbn x y)]
716 [else
717 (die 'bitwise-xor "not an exact integer" y)])]
718 [(bignum? x)
719 (cond
720 [(fixnum? y) (fxbn y x)]
721 [(bignum? y) (bnbn x y)]
722 [else
723 (die 'bitwise-xor "not an exact integer" y)])]
724 [else (die 'bitwise-xor "not an exact integer" x)])))
725
726
727 (define binary-
728 (lambda (x y)
729 (cond
730 [(fixnum? x)
731 (cond
732 [(fixnum? y)
733 (foreign-call "ikrt_fxfxminus" x y)]
734 [(bignum? y)
735 (foreign-call "ikrt_fxbnminus" x y)]
736 [(flonum? y)
737 (if ($fx= x 0)
738 ($fl* y -1.0)
739 ($fl- ($fixnum->flonum x) y))]
740 [(ratnum? y)
741 (let ([n ($ratnum-n y)] [d ($ratnum-d y)])
742 (binary/ (binary- (binary* d x) n) d))]
743 [(compnum? y)
744 ($make-compnum
745 (binary- x ($compnum-real y))
746 (binary- 0 ($compnum-imag y)))]
747 [(cflonum? y)
748 ($make-cflonum
749 (binary- x ($cflonum-real y))
750 ($fl- 0.0 ($cflonum-imag y)))]
751 [else (err '- y)])]
752 [(bignum? x)
753 (cond
754 [(fixnum? y)
755 (foreign-call "ikrt_bnfxminus" x y)]
756 [(bignum? y)
757 (foreign-call "ikrt_bnbnminus" x y)]
758 [(flonum? y)
759 ($fl- (bignum->flonum x) y)]
760 [(ratnum? y)
761 (let ([n ($ratnum-n y)] [d ($ratnum-d y)])
762 (binary/ (binary- (binary* d x) n) d))]
763 [(compnum? y)
764 ($make-compnum
765 (binary- x ($compnum-real y))
766 (binary- 0 ($compnum-imag y)))]
767 [(cflonum? y)
768 ($make-cflonum
769 (binary- x ($cflonum-real y))
770 ($fl- 0.0 ($cflonum-imag y)))]
771 [else (err '- y)])]
772 [(flonum? x)
773 (cond
774 [(flonum? y)
775 ($fl- x y)]
776 [(cflonum? y)
777 ($make-cflonum
778 ($fl- x ($cflonum-real y))
779 ($fl- 0.0 ($cflonum-imag y)))]
780 [(fixnum? y)
781 ($fl- x ($fixnum->flonum y))]
782 [(bignum? y)
783 ($fl- x (bignum->flonum y))]
784 [(ratnum? y)
785 (let ([n ($ratnum-n y)] [d ($ratnum-d y)])
786 (binary/ (binary- (binary* d x) n) d))]
787 [(compnum? y)
788 ($make-cflonum
789 (binary- x ($compnum-real y))
790 (binary- 0.0 ($compnum-imag y)))]
791 [else (err '- y)])]
792 [(ratnum? x)
793 (let ([nx ($ratnum-n x)] [dx ($ratnum-d x)])
794 (cond
795 [(or (fixnum? y) (bignum? y) (flonum? y))
796 (binary/ (binary- nx (binary* dx y)) dx)]
797 [(ratnum? y)
798 (let ([ny ($ratnum-n y)] [dy ($ratnum-d y)])
799 (binary/ (binary- (binary* nx dy) (binary* ny dx))
800 (binary* dx dy)))]
801 [(compnum? y)
802 ($make-compnum
803 (binary- x ($compnum-real y))
804 (binary- 0 ($compnum-imag y)))]
805 [(cflonum? y)
806 ($make-cflonum
807 (binary- x ($cflonum-real y))
808 ($fl- 0.0 ($cflonum-imag y)))]
809 [else (err '- y)]))]
810 [(compnum? x)
811 (cond
812 [(or (fixnum? y) (bignum? y) (ratnum? y))
813 ($make-compnum
814 (binary- ($compnum-real x) y)
815 ($compnum-imag x))]
816 [(compnum? y)
817 ($make-rectangular
818 (binary- ($compnum-real x) ($compnum-real y))
819 (binary- ($compnum-imag x) ($compnum-imag y)))]
820 [(flonum? y)
821 ($make-cflonum
822 (binary- ($compnum-real x) y)
823 (binary- ($compnum-imag x) 0.0))]
824 [(cflonum? y)
825 ($make-cflonum
826 (binary- ($compnum-real x) ($cflonum-real y))
827 (binary- ($compnum-imag x) ($cflonum-imag y)))]
828 [else
829 (err '- y)])]
830 [(cflonum? x)
831 (cond
832 [(flonum? y)
833 ($make-cflonum
834 ($fl- ($cflonum-real x) y)
835 ($cflonum-imag x))]
836 [(cflonum? y)
837 ($make-cflonum
838 (binary- ($cflonum-real x) ($cflonum-real y))
839 (binary- ($cflonum-imag x) ($cflonum-imag y)))]
840 [(or (fixnum? y) (bignum? y) (ratnum? y))
841 ($make-cflonum
842 (binary- ($cflonum-real x) y)
843 ($cflonum-imag x))]
844 [(compnum? y)
845 ($make-cflonum
846 (binary- ($cflonum-real x) ($compnum-real y))
847 (binary- ($cflonum-imag x) ($compnum-imag y)))]
848 [else
849 (err '- y)])]
850 [else (err '- x)])))
851
852 (define binary*
853 (lambda (x y)
854 (cond
855 [(fixnum? x)
856 (cond
857 [(fixnum? y)
858 (foreign-call "ikrt_fxfxmult" x y)]
859 [(bignum? y)
860 (foreign-call "ikrt_fxbnmult" x y)]
861 [(flonum? y)
862 ($fl* ($fixnum->flonum x) y)]
863 [(ratnum? y)
864 (binary/ (binary* x ($ratnum-n y)) ($ratnum-d y))]
865 [(compnum? y)
866 ($make-rectangular
867 (binary* x ($compnum-real y))
868 (binary* x ($compnum-imag y)))]
869 [(cflonum? y)
870 ($make-cflonum
871 (binary* x ($cflonum-real y))
872 (binary* x ($cflonum-imag y)))]
873 [else (err '* y)])]
874 [(bignum? x)
875 (cond
876 [(fixnum? y)
877 (foreign-call "ikrt_fxbnmult" y x)]
878 [(bignum? y)
879 (foreign-call "ikrt_bnbnmult" x y)]
880 [(flonum? y)
881 ($fl* (bignum->flonum x) y)]
882 [(ratnum? y)
883 (binary/ (binary* x ($ratnum-n y)) ($ratnum-d y))]
884 [(compnum? y)
885 ($make-rectangular
886 (binary* x ($compnum-real y))
887 (binary* x ($compnum-imag y)))]
888 [(cflonum? y)
889 ($make-cflonum
890 (binary* x ($cflonum-real y))
891 (binary* x ($cflonum-imag y)))]
892 [else (err '* y)])]
893 [(flonum? x)
894 (cond
895 [(flonum? y)
896 ($fl* x y)]
897 [(cflonum? y)
898 ($make-cflonum
899 ($fl* x ($cflonum-real y))
900 ($fl* x ($cflonum-imag y)))]
901 [(fixnum? y)
902 ($fl* x ($fixnum->flonum y))]
903 [(bignum? y)
904 ($fl* x (bignum->flonum y))]
905 [(ratnum? y)
906 (binary/ (binary* x ($ratnum-n y)) ($ratnum-d y))]
907 [(compnum? y)
908 ($make-cflonum
909 (binary* x ($compnum-real y))
910 (binary* x ($compnum-imag y)))]
911 [else (err '* y)])]
912 [(ratnum? x)
913 (cond
914 [(ratnum? y)
915 (binary/ (binary* ($ratnum-n x) ($ratnum-n y))
916 (binary* ($ratnum-d x) ($ratnum-d y)))]
917 [(compnum? y)
918 ($make-rectangular
919 (binary* x ($compnum-real y))
920 (binary* x ($compnum-imag y)))]
921 [(cflonum? y)
922 ($make-cflonum
923 (binary* x ($cflonum-real y))
924 (binary* x ($cflonum-imag y)))]
925 [else (binary* y x)])]
926 [(compnum? x)
927 (cond
928 [(or (fixnum? y) (bignum? y) (ratnum? y))
929 ($make-rectangular
930 (binary* ($compnum-real x) y)
931 (binary* ($compnum-imag x) y))]
932 [(flonum? y)
933 ($make-cflonum
934 (binary* ($compnum-real x) y)
935 (binary* ($compnum-imag x) y))]
936 [(compnum? y)
937 (let ([r0 ($compnum-real x)]
938 [r1 ($compnum-real y)]
939 [i0 ($compnum-imag x)]
940 [i1 ($compnum-imag y)])
941 (make-rectangular
942 (- (* r0 r1) (* i0 i1))
943 (+ (* r0 i1) (* i0 r1))))]
944 [(cflonum? y)
945 (let ([r0 ($compnum-real x)]
946 [r1 ($cflonum-real y)]
947 [i0 ($compnum-imag x)]
948 [i1 ($cflonum-imag y)])
949 (make-rectangular
950 (- (* r0 r1) (* i0 i1))
951 (+ (* r0 i1) (* i0 r1))))]
952 [else (err '* y)])]
953 [(cflonum? x)
954 (cond
955 [(flonum? y)
956 ($make-cflonum
957 ($fl* ($cflonum-real x) y)
958 ($fl* ($cflonum-imag x) y))]
959 [(cflonum? y)
960 (let ([r0 ($cflonum-real x)]
961 [r1 ($cflonum-real y)]
962 [i0 ($cflonum-imag x)]
963 [i1 ($cflonum-imag y)])
964 ($make-cflonum
965 ($fl- ($fl* r0 r1) ($fl* i0 i1))
966 ($fl+ ($fl* r0 i1) ($fl* i0 r1))))]
967 [(or (fixnum? y) (bignum? y) (ratnum? y))
968 ($make-cflonum
969 (binary* ($compnum-real x) y)
970 (binary* ($compnum-imag x) y))]
971 [(compnum? y)
972 (let ([r0 ($compnum-real x)]
973 [r1 ($compnum-real y)]
974 [i0 ($compnum-imag x)]
975 [i1 ($compnum-imag y)])
976 (make-rectangular
977 (- (* r0 r1) (* i0 i1))
978 (+ (* r0 i1) (* i0 r1))))]
979 [else (err '* y)])]
980 [else (err '* x)])))
981
982 (define +
983 (case-lambda
984 [(x y) (binary+ x y)]
985 [(x y z) (binary+ (binary+ x y) z)]
986 [(a)
987 (cond
988 [(fixnum? a) a]
989 [(number? a) a]
990 [else (die '+ "not a number" a)])]
991 [() 0]
992 [(a b c d . e*)
993 (let f ([ac (binary+ (binary+ (binary+ a b) c) d)]
994 [e* e*])
995 (cond
996 [(null? e*) ac]
997 [else (f (binary+ ac (car e*)) (cdr e*))]))]))
998
999 (define bitwise-and
1000 (case-lambda
1001 [(x y) (binary-bitwise-and x y)]
1002 [(x y z) (binary-bitwise-and (binary-bitwise-and x y) z)]
1003 [(a)
1004 (cond
1005 [(fixnum? a) a]
1006 [(bignum? a) a]
1007 [else (die 'bitwise-and "not a number" a)])]
1008 [() -1]
1009 [(a b c d . e*)
1010 (let f ([ac (binary-bitwise-and a
1011 (binary-bitwise-and b
1012 (binary-bitwise-and c d)))]
1013 [e* e*])
1014 (cond
1015 [(null? e*) ac]
1016 [else (f (binary-bitwise-and ac (car e*)) (cdr e*))]))]))
1017
1018 (define bitwise-ior
1019 (case-lambda
1020 [(x y) (binary-bitwise-ior x y)]
1021 [(x y z) (binary-bitwise-ior (binary-bitwise-ior x y) z)]
1022 [(a)
1023 (cond
1024 [(fixnum? a) a]
1025 [(bignum? a) a]
1026 [else (die 'bitwise-ior "not a number" a)])]
1027 [() 0]
1028 [(a b c d . e*)
1029 (let f ([ac (binary-bitwise-ior a
1030 (binary-bitwise-ior b
1031 (binary-bitwise-ior c d)))]
1032 [e* e*])
1033 (cond
1034 [(null? e*) ac]
1035 [else (f (binary-bitwise-ior ac (car e*)) (cdr e*))]))]))
1036
1037 (define bitwise-xor
1038 (case-lambda
1039 [(x y) (binary-bitwise-xor x y)]
1040 [(x y z) (binary-bitwise-xor (binary-bitwise-xor x y) z)]
1041 [(a)
1042 (cond
1043 [(fixnum? a) a]
1044 [(bignum? a) a]
1045 [else (die 'bitwise-xor "not a number" a)])]
1046 [() 0]
1047 [(a b c d . e*)
1048 (let f ([ac (binary-bitwise-xor a
1049 (binary-bitwise-xor b
1050 (binary-bitwise-xor c d)))]
1051 [e* e*])
1052 (cond
1053 [(null? e*) ac]
1054 [else (f (binary-bitwise-xor ac (car e*)) (cdr e*))]))]))
1055
1056 (define (bitwise-not x)
1057 (cond
1058 [(fixnum? x) ($fxlognot x)]
1059 [(bignum? x) (foreign-call "ikrt_bnlognot" x)]
1060 [else (die 'bitwise-not "invalid argument" x)]))
1061
1062 (define (bitwise-if x y z)
1063 (define who 'bitwise-if)
1064 (define (err x) (die who "not an exact integer" x))
1065 (unless (or (fixnum? x) (bignum? x)) (err x))
1066 (unless (or (fixnum? y) (bignum? y)) (err y))
1067 (unless (or (fixnum? z) (bignum? z)) (err z))
1068 (bitwise-ior
1069 (bitwise-and x y)
1070 (bitwise-and (bitwise-not x) z)))
1071
1072 (define (bitwise-copy-bit-field x i j n)
1073 (define who 'bitwise-copy-bit-field)
1074 (define (err x) (die who "not an exact integer" x))
1075 (define (err2 x) (die who "index must be nonnegative" x))
1076 (define (err3 x y) (die who "indices must be in nondescending order" x y))
1077 (unless (or (fixnum? x) (bignum? x)) (err x))
1078 (unless (or (fixnum? i) (bignum? i)) (err i))
1079 (unless (or (fixnum? j) (bignum? j)) (err j))
1080 (unless (or (fixnum? n) (bignum? n)) (err n))
1081 (when (< i 0) (err2 i))
1082 (when (< j i) (err3 i j))
1083 (bitwise-if (sll (sub1 (sll 1 (- j i))) i) (sll n i) x))
1084
1085 (define -
1086 (case-lambda
1087 [(x y) (binary- x y)]
1088 [(x y z) (binary- (binary- x y) z)]
1089 [(a) (binary- 0 a)]
1090 [(a b c d . e*)
1091 (let f ([ac (binary- (binary- (binary- a b) c) d)]
1092 [e* e*])
1093 (cond
1094 [(null? e*) ac]
1095 [else (f (binary- ac (car e*)) (cdr e*))]))]))
1096
1097 (define *
1098 (case-lambda
1099 [(x y) (binary* x y)]
1100 [(x y z) (binary* (binary* x y) z)]
1101 [(a)
1102 (cond
1103 [(fixnum? a) a]
1104 [(number? a) a]
1105 [else (die '* "not a number" a)])]
1106 [() 1]
1107 [(a b c d . e*)
1108 (let f ([ac (binary* (binary* (binary* a b) c) d)]
1109 [e* e*])
1110 (cond
1111 [(null? e*) ac]
1112 [else (f (binary* ac (car e*)) (cdr e*))]))]))
1113
1114 (define (binary-gcd x y)
1115 (define (gcd x y)
1116 (cond
1117 [($fx= y 0) x]
1118 [else (gcd y (remainder x y))]))
1119 (let ([x (if (< x 0) (- x) x)]
1120 [y (if (< y 0) (- y) y)])
1121 (cond
1122 [(> x y) (gcd x y)]
1123 [(< x y) (gcd y x)]
1124 [else x])))
1125
1126 (define gcd
1127 (case-lambda
1128 [(x y)
1129 (cond
1130 [(or (fixnum? x) (bignum? x))
1131 (cond
1132 [(or (fixnum? y) (bignum? y))
1133 (binary-gcd x y)]
1134 [(number? y)
1135 (die 'gcd "not an exact integer" y)]
1136 [else
1137 (die 'gcd "not a number" y)])]
1138 [(number? x)
1139 (die 'gcd "not an exact integer" x)]
1140 [else
1141 (die 'gcd "not a number" x)])]
1142 [(x)
1143 (cond
1144 [(or (fixnum? x) (bignum? x)) x]
1145 [(number? x)
1146 (die 'gcd "not an exact integer" x)]
1147 [else
1148 (die 'gcd "not a number" x)])]
1149 [() 0]
1150 [(x y z . ls)
1151 (let f ([g (gcd (gcd x y) z)] [ls ls])
1152 (cond
1153 [(null? ls) g]
1154 [else (f (gcd g (car ls)) (cdr ls))]))]))
1155
1156
1157 (define lcm
1158 (case-lambda
1159 [(x y)
1160 (cond
1161 [(or (fixnum? x) (bignum? x))
1162 (cond
1163 [(or (fixnum? y) (bignum? y))
1164 (let ([x (if (< x 0) (- x) x)]
1165 [y (if (< y 0) (- y) y)])
1166 (let ([g (binary-gcd x y)])
1167 (binary* y (quotient x g))))]
1168 [(flonum? y)
1169 (let ([v ($flonum->exact y)])
1170 (cond
1171 [(or (fixnum? v) (bignum? v))
1172 (inexact (lcm x v))]
1173 [else (die 'lcm "not an integer" y)]))]
1174 [else
1175 (die 'lcm "not an integer" y)])]
1176 [(flonum? x)
1177 (let ([v ($flonum->exact x)])
1178 (cond
1179 [(or (fixnum? v) (bignum? v))
1180 (inexact (lcm v y))]
1181 [else (die 'lcm "not an integer" x)]))]
1182 [else
1183 (die 'lcm "not an integer" x)])]
1184 [(x)
1185 (cond
1186 [(or (fixnum? x) (bignum? x)) x]
1187 [(flonum? x)
1188 (let ([v ($flonum->exact x)])
1189 (cond
1190 [(or (fixnum? v) (bignum? v)) x]
1191 [else (die 'lcm "not an integer" x)]))]
1192 [else
1193 (die 'lcm "not an integer" x)])]
1194 [() 1]
1195 [(x y z . ls)
1196 ;;; FIXME: incorrect for multiple roundings
1197 (let f ([g (lcm (lcm x y) z)] [ls ls])
1198 (cond
1199 [(null? ls) g]
1200 [else (f (lcm g (car ls)) (cdr ls))]))]))
1201
1202
1203 (define binary/
1204 (lambda (x y)
1205 (define (x/compy x y)
1206 (let ([yr (real-part y)]
1207 [yi (imag-part y)])
1208 (let ([denom (+ (* yr yr) (* yi yi))])
1209 (make-rectangular
1210 (binary/ (* x yr) denom)
1211 (binary/ (* (- x) yi) denom)))))
1212 (define (compx/y x y)
1213 (let ([xr (real-part x)]
1214 [xi (imag-part x)])
1215 (make-rectangular
1216 (binary/ xr y)
1217 (binary/ xi y))))
1218 (define (compx/compy x y)
1219 (let ([xr (real-part x)]
1220 [xi (imag-part x)]
1221 [yr (real-part y)]
1222 [yi (imag-part y)])
1223 (let ([denom (+ (* yr yr) (* yi yi))])
1224 (make-rectangular
1225 (binary/ (+ (* xr yr) (* xi yi)) denom)
1226 (binary/ (- (* xi yr) (* xr yi)) denom)))))
1227 (cond
1228 [(flonum? x)
1229 (cond
1230 [(flonum? y) ($fl/ x y)]
1231 [(fixnum? y) ($fl/ x ($fixnum->flonum y))]
1232 [(bignum? y) ($fl/ x (bignum->flonum y))]
1233 [(ratnum? y) ($fl/ x (ratnum->flonum y))]
1234 [(or (cflonum? y) (compnum? y)) (x/compy x y)]
1235 [else (err '/ y)])]
1236 [(fixnum? x)
1237 (cond
1238 [(flonum? y) ($fl/ ($fixnum->flonum x) y)]
1239 [(fixnum? y)
1240 (cond
1241 [($fx= y 0) (die '/ "division by 0")]
1242 [($fx> y 0)
1243 (if ($fx= y 1)
1244 x
1245 (let ([g (binary-gcd x y)])
1246 (cond
1247 [($fx= g y) (fxquotient x g)]
1248 [($fx= g 1) ($make-ratnum x y)]
1249 [else
1250 ($make-ratnum (fxquotient x g) (fxquotient y g))])))]
1251 [else
1252 (if ($fx= y -1)
1253 (binary- 0 x)
1254 (let ([g (binary-gcd x y)])
1255 (cond
1256 [($fx= ($fx- 0 g) y) (binary- 0 (fxquotient x g))]
1257 [($fx= g 1) ($make-ratnum (binary- 0 x) (binary- 0 y))]
1258 [else
1259 ($make-ratnum
1260 (binary- 0 (fxquotient x g))
1261 (binary- 0 (fxquotient y g)))])))])]
1262 [(bignum? y)
1263 (if ($fx= x 0)
1264 0
1265 (let ([g (binary-gcd x y)])
1266 (cond
1267 [(= g y) (quotient x g)]
1268 [($bignum-positive? y)
1269 (if ($fx= g 1)
1270 ($make-ratnum x y)
1271 ($make-ratnum (fxquotient x g) (quotient y g)))]
1272 [else
1273 (if ($fx= g 1)
1274 ($make-ratnum (binary- 0 x) (binary- 0 y))
1275 ($make-ratnum
1276 (binary- 0 (fxquotient x g))
1277 (binary- 0 (quotient y g))))])))]
1278 [(ratnum? y)
1279 (/ (* x ($ratnum-d y)) ($ratnum-n y))]
1280 [(or (compnum? y) (cflonum? y)) (x/compy x y)]
1281 [else (err '/ y)])]
1282 [(bignum? x)
1283 (cond
1284 [(fixnum? y)
1285 (cond
1286 [($fx= y 0) (die '/ "division by 0")]
1287 [($fx> y 0)
1288 (if ($fx= y 1)
1289 x
1290 (let ([g (binary-gcd x y)])
1291 (cond
1292 [($fx= g 1) ($make-ratnum x y)]
1293 [($fx= g y) (quotient x g)]
1294 [else
1295 ($make-ratnum (quotient x g) (quotient y g))])))]
1296 [else
1297 (if ($fx= y -1)
1298 (- x)
1299 (let ([g (binary-gcd x y)])
1300 (cond
1301 [(= (- g) y) (- (quotient x g))]
1302 [else
1303 ($make-ratnum
1304 (- (quotient x g))
1305 (- (quotient y g)))])))])]
1306 [(bignum? y)
1307 (let ([g (binary-gcd x y)])
1308 (cond
1309 [($fx= g 1)
1310 (if ($bignum-positive? y)
1311 ($make-ratnum x y)
1312 ($make-ratnum
1313 (binary- 0 x)
1314 (binary- 0 y)))]
1315 [($bignum-positive? y)
1316 (if (= g y)
1317 (quotient x g)
1318 ($make-ratnum (quotient x g) (quotient y g)))]
1319 [else
1320 (let ([y (binary- 0 y)])
1321 (if (= g y)
1322 (binary- 0 (quotient x g))
1323 ($make-ratnum
1324 (binary- 0 (quotient x g))
1325 (quotient y g))))]))]
1326 [(flonum? y) ($fl/ (bignum->flonum x) y)]
1327 [(ratnum? y)
1328 (binary/ (binary* x ($ratnum-d y)) ($ratnum-n y))]
1329 [(or (compnum? y) (cflonum? y)) (x/compy x y)]
1330 [else (err '/ y)])]
1331 [(ratnum? x)
1332 (cond
1333 [(ratnum? y)
1334 (binary/
1335 (binary* ($ratnum-n x) ($ratnum-d y))
1336 (binary* ($ratnum-n y) ($ratnum-d x)))]
1337 [(or (compnum? y) (cflonum? y)) (x/compy x y)]
1338 [else (binary/ 1 (binary/ y x))])]
1339 [(or (compnum? x) (cflonum? x))
1340 (cond
1341 [(or (compnum? y) (cflonum? y)) (compx/compy x y)]
1342 [(or (fixnum? y) (bignum? y) (ratnum? y) (flonum? y)) (compx/y x y)]
1343 [else (err '/ y)])]
1344 [else (err '/ x)])))
1345
1346
1347 (define /
1348 (case-lambda
1349 [(x y) (binary/ x y)]
1350 [(x)
1351 (cond
1352 [(fixnum? x)
1353 (cond
1354 [($fxzero? x) (die '/ "division by 0")]
1355 [($fx> x 0)
1356 (if ($fx= x 1)
1357 1
1358 ($make-ratnum 1 x))]
1359 [else
1360 (if ($fx= x -1)
1361 -1
1362 ($make-ratnum -1 (- x)))])]
1363 [(bignum? x)
1364 (if ($bignum-positive? x)
1365 ($make-ratnum 1 x)
1366 ($make-ratnum -1 (- x)))]
1367 [(flonum? x) (foreign-call "ikrt_fl_invert" x)]
1368 [(ratnum? x)
1369 (let ([n ($ratnum-n x)] [d ($ratnum-d x)])
1370 (cond
1371 [($fx= n 1) d]
1372 [($fx= n -1) (- d)]
1373 [else
1374 (if (> 0 n)
1375 ($make-ratnum (- d) (- n))
1376 ($make-ratnum d n))]))]
1377 [(compnum? x) (binary/ 1 x)]
1378 [(cflonum? x) (binary/ 1 x)]
1379 [else (die '/ "not a number" x)])]
1380 [(x y z . ls)
1381 (let f ([a (binary/ x y)] [b z] [ls ls])
1382 (cond
1383 [(null? ls) (binary/ a b)]
1384 [else (f (binary/ a b) (car ls) (cdr ls))]))]))
1385
1386
1387 (define flmax
1388 (case-lambda
1389 [(x y)
1390 (if (flonum? x)
1391 (if (flonum? y)
1392 (if ($fl< x y)
1393 y
1394 x)
1395 (die 'flmax "not a flonum" y))
1396 (die 'flmax "not a flonum" x))]
1397 [(x y z . rest)
1398 (let f ([a (flmax x y)] [b z] [ls rest])
1399 (cond
1400 [(null? ls) (flmax a b)]
1401 [else
1402 (f (flmax a b) (car ls) (cdr ls))]))]
1403 [(x)
1404 (if (flonum? x)
1405 x
1406 (die 'flmax "not a number" x))]))
1407
1408 (define max
1409 (case-lambda
1410 [(x y)
1411 (cond
1412 [(fixnum? x)
1413 (cond
1414 [(fixnum? y)
1415 (if ($fx> x y) x y)]
1416 [(bignum? y)
1417 (if (positive-bignum? y) y x)]
1418 [(flonum? y)
1419 (let ([x ($fixnum->flonum x)])
1420 (if ($fl>= y x) y x))]
1421 [(ratnum? y) ;;; FIXME: optimize
1422 (if (>= x y) x y)]
1423 [else (die 'max "not a number" y)])]
1424 [(bignum? x)
1425 (cond
1426 [(fixnum? y)
1427 (if (positive-bignum? x) x y)]
1428 [(bignum? y)
1429 (if (bnbn> x y) x y)]
1430 [(flonum? y)
1431 (let ([x (bignum->flonum x)])
1432 (if ($fl>= y x) y x))]
1433 [(ratnum? y) ;;; FIXME: optimize
1434 (if (>= x y) x y)]
1435 [else (die 'max "not a number" y)])]
1436 [(flonum? x)
1437 (cond
1438 [(flonum? y)
1439 (if ($fl>= x y) x y)]
1440 [(fixnum? y)
1441 (let ([y ($fixnum->flonum y)])
1442 (if ($fl>= y x) y x))]
1443 [(bignum? y)
1444 (let ([y (bignum->flonum y)])
1445 (if ($fl>= y x) y x))]
1446 [(ratnum? y)
1447 ;;; FIXME: may be incorrect
1448 (let ([y (ratnum->flonum y)])
1449 (if ($fl>= y x) y x))]
1450 [else (die 'max "not a number" y)])]
1451 [(ratnum? x)
1452 (cond
1453 [(or (fixnum? y) (bignum? y) (ratnum? y))
1454 (if (>= x y) x y)]
1455 [(flonum? y)
1456 (let ([x (ratnum->flonum x)])
1457 (if ($fl>= x y) x y))]
1458 [else (die 'max "not a number" y)])]
1459 [else (die 'max "not a number" x)])]
1460 [(x y z . rest)
1461 (let f ([a (max x y)] [b z] [ls rest])
1462 (cond
1463 [(null? ls) (max a b)]
1464 [else
1465 (f (max a b) (car ls) (cdr ls))]))]
1466 [(x)
1467 (cond
1468 [(or (fixnum? x) (bignum? x) (ratnum? x) (flonum? x)) x]
1469 [else (die 'max "not a number" x)])]))
1470
1471 (define min
1472 (case-lambda
1473 [(x y)
1474 (cond
1475 [(fixnum? x)
1476 (cond
1477 [(fixnum? y)
1478 (if ($fx> x y) y x)]
1479 [(bignum? y)
1480 (if (positive-bignum? y) x y)]
1481 [(flonum? y)
1482 (let ([x ($fixnum->flonum x)])
1483 (if ($fl>= y x) x y))]
1484 [(ratnum? y) ;;; FIXME: optimize
1485 (if (>= x y) y x)]
1486 [else (die 'min "not a number" y)])]
1487 [(bignum? x)
1488 (cond
1489 [(fixnum? y)
1490 (if (positive-bignum? x) y x)]
1491 [(bignum? y)
1492 (if (bnbn> x y) y x)]
1493 [(flonum? y)
1494 (let ([x (bignum->flonum x)])
1495 (if ($fl>= y x) x y))]
1496 [(ratnum? y) ;;; FIXME: optimize
1497 (if (>= x y) y x)]
1498 [else (die 'min "not a number" y)])]
1499 [(flonum? x)
1500 (cond
1501 [(flonum? y)
1502 (if ($fl>= x y) y x)]
1503 [(fixnum? y)
1504 (let ([y ($fixnum->flonum y)])
1505 (if ($fl>= y x) x y))]
1506 [(bignum? y)
1507 (let ([y (bignum->flonum y)])
1508 (if ($fl>= y x) x y))]
1509 [(ratnum? y)
1510 ;;; FIXME: may be incorrect
1511 (let ([y (ratnum->flonum y)])
1512 (if ($fl>= y x) x y))]
1513 [else (die 'min "not a number" y)])]
1514 [(ratnum? x)
1515 (cond
1516 [(or (fixnum? y) (bignum? y) (ratnum? y))
1517 (if (>= x y) y x)]
1518 [(flonum? y)
1519 (let ([x (ratnum->flonum x)])
1520 (if ($fl>= x y) y x))]
1521 [else (die 'min "not a number" y)])]
1522 [else (die 'min "not a number" x)])]
1523 [(x y z . rest)
1524 (let f ([a (min x y)] [b z] [ls rest])
1525 (cond
1526 [(null? ls) (min a b)]
1527 [else
1528 (f (min a b) (car ls) (cdr ls))]))]
1529 [(x)
1530 (cond
1531 [(or (fixnum? x) (bignum? x) (ratnum? x) (flonum? x)) x]
1532 [else (die 'min "not a number" x)])]))
1533
1534 (define (abs x)
1535 (cond
1536 [(fixnum? x)
1537 (if ($fx< x 0) (- x) x)]
1538 [(bignum? x)
1539 (if ($bignum-positive? x) x (- x))]
1540 [(flonum? x)
1541 (if ($fx> ($flonum-u8-ref x 0) 127)
1542 ($fl* x -1.0)
1543 x)]
1544 [(ratnum? x)
1545 (let ([n ($ratnum-n x)])
1546 (if (< n 0)
1547 ($make-ratnum (- n) ($ratnum-d x))
1548 x))]
1549 [else (die 'abs "not a real number" x)]))
1550
1551 (define flmin
1552 (case-lambda
1553 [(x y)
1554 (if (flonum? x)
1555 (if (flonum? y)
1556 (if ($fl< x y) x y)
1557 (die 'flmin "not a flonum" y))
1558 (die 'flmin "not a flonum" x))]
1559 [(x y z . rest)
1560 (let f ([a (flmin x y)] [b z] [ls rest])
1561 (cond
1562 [(null? ls) (flmin a b)]
1563 [else
1564 (f (flmin a b) (car ls) (cdr ls))]))]
1565 [(x)
1566 (if (flonum? x)
1567 x
1568 (die 'flmin "not a flonum" x))]))
1569
1570 (define (->inexact x who)
1571 (cond
1572 [(fixnum? x) ($fixnum->flonum x)]
1573 [(bignum? x) (bignum->flonum x)]
1574 [(ratnum? x) (ratnum->flonum x)]
1575 [(flonum? x) x]
1576 [(compnum? x)
1577 (make-rectangular
1578 (->inexact (real-part x) who)
1579 (->inexact (imag-part x) who))]
1580 [(cflonum? x) x]
1581 [else
1582 (die who "not a number" x)]))
1583
1584 (define (exact->inexact x)
1585 (->inexact x 'exact->inexact))
1586
1587 (define (inexact x)
1588 (->inexact x 'inexact))
1589
1590 (define real->flonum
1591 (lambda (x)
1592 (cond
1593 [(fixnum? x) ($fixnum->flonum x)]
1594 [(bignum? x) (bignum->flonum x)]
1595 [(ratnum? x) (ratnum->flonum x)]
1596 [(flonum? x) x]
1597 [else
1598 (die 'real->flonum "not a real number" x)])))
1599
1600 (define positive-bignum?
1601 (lambda (x)
1602 (foreign-call "ikrt_positive_bn" x)))
1603
1604 (define even-bignum?
1605 (lambda (x)
1606 (foreign-call "ikrt_even_bn" x)))
1607
1608 (define ($fxeven? x)
1609 ($fxzero? ($fxlogand x 1)))
1610
1611 (define (even? x)
1612 (cond
1613 [(fixnum? x) ($fxeven? x)]
1614 [(bignum? x) (even-bignum? x)]
1615 [(flonum? x)
1616 (let ([v ($flonum->exact x)])
1617 (cond
1618 [(fixnum? v) ($fxeven? v)]
1619 [(bignum? v) (even-bignum? v)]
1620 [else (die 'even? "not an integer" x)]))]
1621 [else (die 'even? "not an integer" x)]))
1622
1623 (define (odd? x)
1624 (cond
1625 [(fixnum? x) (not ($fxeven? x))]
1626 [(bignum? x) (not (even-bignum? x))]
1627 [(flonum? x)
1628 (let ([v ($flonum->exact x)])
1629 (cond
1630 [(fixnum? v) (not ($fxeven? v))]
1631 [(bignum? v) (not (even-bignum? v))]
1632 [else (die 'odd? "not an integer" x)]))]
1633 [else (die 'odd? "not an integer" x)]))
1634
1635 (module (number->string)
1636 (module (bignum->string)
1637 (define (bignum->decimal-string x)
1638 (utf8->string (foreign-call "ikrt_bignum_to_bytevector" x)))
1639 (module (bignum->power-string)
1640 (define string-map "0123456789ABCDEF")
1641 (define (init-string x chars)
1642 (if ($bignum-positive? x)
1643 (make-string chars)
1644 (let ([s (make-string ($fxadd1 chars))])
1645 (string-set! s 0 #\-)
1646 s)))
1647 (define (bignum-bits x)
1648 (define (add-bits b n)
1649 (cond
1650 [($fxzero? b) n]
1651 [else (add-bits ($fxsra b 1) ($fx+ n 1))]))
1652 (let f ([i ($fxsub1 ($bignum-size x))])
1653 (let ([b ($bignum-byte-ref x i)])
1654 (cond
1655 [($fxzero? b) (f ($fxsub1 i))]
1656 [else (add-bits b ($fxsll i 3))]))))
1657 (define (bignum->power-string x mask shift)
1658 (let ([bits (bignum-bits x)])
1659 (let ([chars (fxquotient (fx+ bits (fx- shift 1)) shift)])
1660 (let* ([s (init-string x chars)]
1661 [n ($fx- (string-length s) 1)])
1662 (let f ([i 0] [j 0] [k 0] [b 0])
1663 (cond
1664 [($fx= i chars) s]
1665 [($fx< k 8)
1666 (f i ($fxadd1 j) ($fx+ k 8)
1667 ($fxlogor b
1668 ($fxsll ($bignum-byte-ref x j) k)))]
1669 [else
1670 (string-set! s ($fx- n i)
1671 (string-ref string-map
1672 ($fxlogand mask b)))
1673 (f ($fxadd1 i) j ($fx- k shift) ($fxsra b shift))])))))))
1674 (define (bignum->string x r)
1675 (case r
1676 [(10) (bignum->decimal-string x)]
1677 [(2) (bignum->power-string x 1 1)]
1678 [(8) (bignum->power-string x 7 3)]
1679 [(16) (bignum->power-string x 15 4)]
1680 [else (die 'number->string "BUG")])))
1681 (define ratnum->string
1682 (lambda (x r)
1683 (string-append
1684 ($number->string ($ratnum-n x) r)
1685 "/"
1686 ($number->string ($ratnum-d x) r))))
1687 (define (imag x r)
1688 (cond
1689 [(eqv? x 1) "+"]
1690 [(eqv? x -1) "-"]
1691 [(or (< x 0) (eqv? x -0.0))
1692 ($number->string x r)]
1693 [else (string-append "+" ($number->string x r))]))
1694 (define $number->string
1695 (lambda (x r)
1696 (import (ikarus system $compnums))
1697 (cond
1698 [(fixnum? x) (fixnum->string x r)]
1699 [(bignum? x) (bignum->string x r)]
1700 [(flonum? x)
1701 (unless (eqv? r 10)
1702 (die 'number->string
1703 "invalid radix for inexact number"
1704 r x))
1705 (flonum->string x)]
1706 [(ratnum? x) (ratnum->string x r)]
1707 [(compnum? x)
1708 (let ([xr ($compnum-real x)]
1709 [xi ($compnum-imag x)])
1710 (if (eqv? xr 0)
1711 (string-append (imag xi r) "i")
1712 (string-append
1713 ($number->string xr r)
1714 (imag xi r)
1715 "i")))]
1716 [(cflonum? x)
1717 (let ([xr ($cflonum-real x)]
1718 [xi ($cflonum-imag x)])
1719 (cond
1720 [(flnan? xi)
1721 (string-append ($number->string xr r) "+nan.0i")]
1722 [(flinfinite? xi)
1723 (string-append ($number->string xr r)
1724 (if ($fl> xi 0.0) "+inf.0i" "-inf.0i"))]
1725 [else
1726 (string-append
1727 ($number->string xr r) (imag xi r) "i")]))]
1728 [else (die 'number->string "not a number" x)])))
1729 (define do-warn
1730 (lambda ()
1731 (set! do-warn values)
1732 (raise-continuable
1733 (condition
1734 (make-warning)
1735 (make-who-condition 'number->string)
1736 (make-message-condition
1737 "precision argument is not supported")))))
1738 (define number->string
1739 (case-lambda
1740 [(x) ($number->string x 10)]
1741 [(x r)
1742 (unless (memv r '(2 8 10 16))
1743 (die 'number->string "invalid radix" r))
1744 ($number->string x r)]
1745 [(x r precision)
1746 ;(do-warn)
1747 (number->string x r)])))
1748
1749 (define modulo
1750 (lambda (n m)
1751 (cond
1752 [(fixnum? n)
1753 (cond
1754 [(fixnum? m)
1755 (if (eqv? m 0)
1756 (die 'modulo "division by zero" n m)
1757 ($fxmodulo n m))]
1758 [(bignum? m)
1759 (if ($fx< n 0)
1760 (if ($bignum-positive? m)
1761 (foreign-call "ikrt_fxbnplus" n m)
1762 n)
1763 (if ($bignum-positive? m)
1764 n
1765 (foreign-call "ikrt_fxbnplus" n m)))]
1766 [(flonum? m)
1767 (let ([v ($flonum->exact m)])
1768 (cond
1769 [(or (fixnum? v) (bignum? v))
1770 (inexact (modulo n v))]
1771 [else
1772 (die 'modulo "not an integer" m)]))]
1773 [(ratnum? m) (die 'modulo "not an integer" m)]
1774 [else (die 'modulo "not a number" m)])]
1775 [(bignum? n)
1776 (cond
1777 [(fixnum? m)
1778 (if (eqv? m 0)
1779 (die 'modulo "division by zero" n m)
1780 (foreign-call "ikrt_bnfx_modulo" n m))]
1781 [(bignum? m)
1782 (if ($bignum-positive? n)
1783 (if ($bignum-positive? m)
1784 (remainder n m)
1785 (+ m (remainder n m)))
1786 (if ($bignum-positive? m)
1787 (+ m (remainder n m))
1788 (remainder n m)))]
1789 [(flonum? m)
1790 (let ([v ($flonum->exact m)])
1791 (cond
1792 [(or (fixnum? v) (bignum? v))
1793 (inexact (modulo n v))]
1794 [else
1795 (die 'modulo "not an integer" m)]))]
1796 [(ratnum? m) (die 'modulo "not an integer" m)]
1797 [else (die 'modulo "not a number" m)])]
1798 [(flonum? n)
1799 (let ([v ($flonum->exact n)])
1800 (cond
1801 [(or (fixnum? v) (bignum? v))
1802 (inexact (modulo v m))]
1803 [else
1804 (die 'modulo "not an integer" n)]))]
1805 [(ratnum? n) (die 'modulo "not an integer" n)]
1806 [else (die 'modulo "not a number" n)])))
1807
1808 (define-syntax mk<
1809 (syntax-rules ()
1810 [(_ name fxfx< fxbn< bnfx< bnbn<
1811 fxfl< flfx< bnfl< flbn< flfl<
1812 fxrt< rtfx< bnrt< rtbn< flrt< rtfl< rtrt<)
1813 (let ()
1814 (define err
1815 (lambda (x) (die 'name "not a real number" x)))
1816 (define fxloopt
1817 (lambda (x y ls)
1818 (cond
1819 [(fixnum? y)
1820 (if (null? ls)
1821 (fxfx< x y)
1822 (if (fxfx< x y)
1823 (fxloopt y (car ls) (cdr ls))
1824 (loopf (car ls) (cdr ls))))]
1825 [(bignum? y)
1826 (if (null? ls)
1827 (fxbn< x y)
1828 (if (fxbn< x y)
1829 (bnloopt y (car ls) (cdr ls))
1830 (loopf (car ls) (cdr ls))))]
1831 [(flonum? y)
1832 (if (null? ls)
1833 (fxfl< x y)
1834 (if (fxfl< x y)
1835 (flloopt y (car ls) (cdr ls))
1836 (loopf (car ls) (cdr ls))))]
1837 [(ratnum? y)
1838 (if (null? ls)
1839 (fxrt< x y)
1840 (if (fxrt< x y)
1841 (rtloopt y (car ls) (cdr ls))
1842 (loopf (car ls) (cdr ls))))]
1843 [else (err y)])))
1844 (define bnloopt
1845 (lambda (x y ls)
1846 (cond
1847 [(fixnum? y)
1848 (if (null? ls)
1849 (bnfx< x y)
1850 (if (bnfx< x y)
1851 (fxloopt y (car ls) (cdr ls))
1852 (loopf (car ls) (cdr ls))))]
1853 [(bignum? y)
1854 (if (null? ls)
1855 (bnbn< x y)
1856 (if (bnbn< x y)
1857 (bnloopt y (car ls) (cdr ls))
1858 (loopf (car ls) (cdr ls))))]
1859 [(flonum? y)
1860 (if (null? ls)
1861 (bnfl< x y)
1862 (if (bnfl< x y)
1863 (flloopt y (car ls) (cdr ls))
1864 (loopf (car ls) (cdr ls))))]
1865 [(ratnum? y)
1866 (if (null? ls)
1867 (bnrt< x y)
1868 (if (bnrt< x y)
1869 (rtloopt y (car ls) (cdr ls))
1870 (loopf (car ls) (cdr ls))))]
1871 [else (err y)])))
1872 (define flloopt
1873 (lambda (x y ls)
1874 (cond
1875 [(fixnum? y)
1876 (if (null? ls)
1877 (flfx< x y)
1878 (if (flfx< x y)
1879 (fxloopt y (car ls) (cdr ls))
1880 (loopf (car ls) (cdr ls))))]
1881 [(bignum? y)
1882 (if (null? ls)
1883 (flbn< x y)
1884 (if (flbn< x y)
1885 (bnloopt y (car ls) (cdr ls))
1886 (loopf (car ls) (cdr ls))))]
1887 [(flonum? y)
1888 (if (null? ls)
1889 (flfl< x y)
1890 (if (flfl< x y)
1891 (flloopt y (car ls) (cdr ls))
1892 (loopf (car ls) (cdr ls))))]
1893 [(ratnum? y)
1894 (if (null? ls)
1895 (flrt< x y)
1896 (if (flrt< x y)
1897 (rtloopt y (car ls) (cdr ls))
1898 (loopf (car ls) (cdr ls))))]
1899 [else (err y)])))
1900 (define rtloopt
1901 (lambda (x y ls)
1902 (cond
1903 [(fixnum? y)
1904 (if (null? ls)
1905 (rtfx< x y)
1906 (if (rtfx< x y)
1907 (fxloopt y (car ls) (cdr ls))
1908 (loopf (car ls) (cdr ls))))]
1909 [(bignum? y)
1910 (if (null? ls)
1911 (rtbn< x y)
1912 (if (rtbn< x y)
1913 (bnloopt y (car ls) (cdr ls))
1914 (loopf (car ls) (cdr ls))))]
1915 [(flonum? y)
1916 (if (null? ls)
1917 (rtfl< x y)
1918 (if (rtfl< x y)
1919 (flloopt y (car ls) (cdr ls))
1920 (loopf (car ls) (cdr ls))))]
1921 [(ratnum? y)
1922 (if (null? ls)
1923 (rtrt< x y)
1924 (if (rtrt< x y)
1925 (rtloopt y (car ls) (cdr ls))
1926 (loopf (car ls) (cdr ls))))]
1927 [else (err y)])))
1928 (define loopf
1929 (lambda (x ls)
1930 (cond
1931 [(number? x)
1932 (if (null? ls)
1933 #f
1934 (loopf (car ls) (cdr ls)))]
1935 [else (err x)])))
1936 (define name
1937 (case-lambda
1938 [(x y)
1939 (cond
1940 [(fixnum? x)
1941 (cond
1942 [(fixnum? y) (fxfx< x y)]
1943 [(bignum? y) (fxbn< x y)]
1944 [(flonum? y) (fxfl< x y)]
1945 [(ratnum? y) (fxrt< x y)]
1946 [else (err y)])]
1947 [(bignum? x)
1948 (cond
1949 [(fixnum? y) (bnfx< x y)]
1950 [(bignum? y) (bnbn< x y)]
1951 [(flonum? y) (bnfl< x y)]
1952 [(ratnum? y) (bnrt< x y)]
1953 [else (err y)])]
1954 [(flonum? x)
1955 (cond
1956 [(fixnum? y) (flfx< x y)]
1957 [(bignum? y) (flbn< x y)]
1958 [(flonum? y) (flfl< x y)]
1959 [(ratnum? y) (flrt< x y)]
1960 [else (err y)])]
1961 [(ratnum? x)
1962 (cond
1963 [(fixnum? y) (rtfx< x y)]
1964 [(bignum? y) (rtbn< x y)]
1965 [(flonum? y) (rtfl< x y)]
1966 [(ratnum? y) (rtrt< x y)]
1967 [else (err y)])]
1968 [else (err x)])]
1969 [(x y z) (and (name x y) (name y z))]
1970 [(x) (if (number? x) #t (err x))]
1971 [(x y . ls)
1972 (cond
1973 [(fixnum? x) (fxloopt x y ls)]
1974 [(bignum? x) (bnloopt x y ls)]
1975 [(flonum? x) (flloopt x y ls)]
1976 [(ratnum? x) (rtloopt x y ls)]
1977 [else (err x)])]))
1978 name)]))
1979
1980
1981 (define-syntax false (syntax-rules () [(_ x y) #f]))
1982 (define-syntax bnbncmp
1983 (syntax-rules ()
1984 [(_ x y cmp)
1985 (cmp (foreign-call "ikrt_bnbncomp" x y) 0)]))
1986 (define-syntax bnbn= (syntax-rules () [(_ x y) (bnbncmp x y $fx=)]))
1987 (define-syntax bnbn< (syntax-rules () [(_ x y) (bnbncmp x y $fx<)]))
1988 (define-syntax bnbn> (syntax-rules () [(_ x y) (bnbncmp x y $fx>)]))
1989 (define-syntax bnbn<= (syntax-rules () [(_ x y) (bnbncmp x y $fx<=)]))
1990 (define-syntax bnbn>= (syntax-rules () [(_ x y) (bnbncmp x y $fx>=)]))
1991 (define-syntax fxbn< (syntax-rules () [(_ x y) (positive-bignum? y)]))
1992 (define-syntax bnfx< (syntax-rules () [(_ x y) (not (positive-bignum? x))]))
1993 (define-syntax fxbn> (syntax-rules () [(_ x y) (not (positive-bignum? y))]))
1994 (define-syntax bnfx> (syntax-rules () [(_ x y) (positive-bignum? x)]))
1995
1996 (define-syntax flcmp
1997 (syntax-rules ()
1998 [(_ flfl? flfx? fxfl? flbn? bnfl? fl?)
1999 (begin
2000 (define-syntax flfl?
2001 (syntax-rules () [(_ x y) (fl? x y)]))
2002 (define-syntax flfx?
2003 (syntax-rules () [(_ x y) (fl? x ($fixnum->flonum y))]))
2004 (define-syntax flbn?
2005 (syntax-rules () [(_ x y) (fl? x (bignum->flonum y))]))
2006 (define-syntax fxfl?
2007 (syntax-rules () [(_ x y) (fl? ($fixnum->flonum x) y)]))
2008 (define-syntax bnfl?
2009 (syntax-rules () [(_ x y) (fl? (bignum->flonum x) y)])))]))
2010
2011 ;;; #;
2012 ;;; (begin
2013 ;;; (define-syntax $fl=
2014 ;;; (syntax-rules () [(_ x y) (foreign-call "ikrt_fl_equal" x y)]))
2015 ;;; (define-syntax $fl<
2016 ;;; (syntax-rules () [(_ x y) (foreign-call "ikrt_fl_less" x y)]))
2017 ;;; (define-syntax $fl<=
2018 ;;; (syntax-rules () [(_ x y) (foreign-call "ikrt_fl_less_or_equal" x y)]))
2019 ;;; (define-syntax $fl>
2020 ;;; (syntax-rules () [(_ x y) (foreign-call "ikrt_fl_less" y x)]))
2021 ;;; (define-syntax $fl>=
2022 ;;; (syntax-rules () [(_ x y) (foreign-call "ikrt_fl_less_or_equal" y x)])))
2023
2024 (define-syntax define-flcmp
2025 (syntax-rules ()
2026 [(_ fl<? $fl<)
2027 (define fl<?
2028 (case-lambda
2029 [(x y)
2030 (if (flonum? x)
2031 (if (flonum? y)
2032 ($fl< x y)
2033 (die 'fl<? "not a flonum" y))
2034 (die 'fl<? "not a flonum" x))]
2035 [(x y z)
2036 (if (flonum? x)
2037 (if (flonum? y)
2038 (if (flonum? z)
2039 (and ($fl< x y) ($fl< y z))
2040 (die 'fl<? "not a flonum" z))
2041 (die 'fl<? "not a flonum" y))
2042 (die 'fl<? "not a flonum" x))]
2043 [(x)
2044 (or (flonum? x)
2045 (die 'fl<? "not a flonum" x))]
2046 [(x y . rest)
2047 (let ()
2048 (define (loopf a ls)
2049 (unless (flonum? a)
2050 (die 'fl<? "not a flonum" a))
2051 (if (null? ls)
2052 #f
2053 (loopf (car ls) (cdr ls))))
2054 (if (flonum? x)
2055 (if (flonum? y)
2056 (if ($fl< x y)
2057 (let f ([x y] [y (car rest)] [ls (cdr rest)])
2058 (if (flonum? y)
2059 (if (null? ls)
2060 ($fl< x y)
2061 (if ($fl< x y)
2062 (f y (car ls) (cdr ls))
2063 (loopf (car ls) (cdr ls))))
2064 (die 'fl<? "not a flonum" y)))
2065 (loopf (car rest) (cdr rest)))
2066 (die 'fl<? "not a flonum" y))
2067 (die 'fl<? "not a flonum" x)))]))]))
2068 (define-flcmp fl=? $fl=)
2069 (define-flcmp fl<? $fl<)
2070 (define-flcmp fl<=? $fl<=)
2071 (define-flcmp fl>? $fl>)
2072 (define-flcmp fl>=? $fl>=)
2073
2074 (define fl+
2075 (case-lambda
2076 [(x y)
2077 (if (flonum? x)
2078 (if (flonum? y)
2079 ($fl+ x y)
2080 (die 'fl+ "not a flonum" y))
2081 (die 'fl+ "not a flonum" x))]
2082 [(x y z)
2083 (fl+ (fl+ x y) z)]
2084 [(x y z q . rest)
2085 (let f ([ac (fl+ (fl+ (fl+ x y) z) q)] [rest rest])
2086 (if (null? rest)
2087 ac
2088 (f (fl+ ac (car rest)) (cdr rest))))]
2089 [(x)
2090 (if (flonum? x)
2091 x
2092 (die 'fl+ "not a flonum" x))]
2093 [() (exact->inexact 0)]))
2094
2095
2096 (define fl-
2097 (case-lambda
2098 [(x y)
2099 (if (flonum? x)
2100 (if (flonum? y)
2101 ($fl- x y)
2102 (die 'fl- "not a flonum" y))
2103 (die 'fl- "not a flonum" x))]
2104 [(x y z)
2105 (fl- (fl- x y) z)]
2106 [(x y z q . rest)
2107 (let f ([ac (fl- (fl- (fl- x y) z) q)] [rest rest])
2108 (if (null? rest)
2109 ac
2110 (f (fl- ac (car rest)) (cdr rest))))]
2111 [(x)
2112 (if (flonum? x)
2113 ($fl* -1.0 x)
2114 (die 'fl- "not a flonum" x))]))
2115
2116 (define fl*
2117 (case-lambda
2118 [(x y)
2119 (if (flonum? x)
2120 (if (flonum? y)
2121 ($fl* x y)
2122 (die 'fl* "not a flonum" y))
2123 (die 'fl* "not a flonum" x))]
2124 [(x y z)
2125 (fl* (fl* x y) z)]
2126 [(x y z q . rest)
2127 (let f ([ac (fl* (fl* (fl* x y) z) q)] [rest rest])
2128 (if (null? rest)
2129 ac
2130 (f (fl* ac (car rest)) (cdr rest))))]
2131 [(x)
2132 (if (flonum? x)
2133 x
2134 (die 'fl* "not a flonum" x))]
2135 [() 1.0]))
2136
2137 (define fl/
2138 (case-lambda
2139 [(x y)
2140 (if (flonum? x)
2141 (if (flonum? y)
2142 ($fl/ x y)
2143 (die 'fl/ "not a flonum" y))
2144 (die 'fl/ "not a flonum" x))]
2145 [(x y z)
2146 (fl/ (fl/ x y) z)]
2147 [(x y z q . rest)
2148 (let f ([ac (fl/ (fl/ (fl/ x y) z) q)] [rest rest])
2149 (if (null? rest)
2150 ac
2151 (f (fl/ ac (car rest)) (cdr rest))))]
2152 [(x)
2153 (if (flonum? x)
2154 ($fl/ 1.0 x)
2155 (die 'fl/ "not a flonum" x))]))
2156
2157 (flcmp flfl= flfx= fxfl= flbn= bnfl= $fl=)
2158 (flcmp flfl< flfx< fxfl< flbn< bnfl< $fl<)
2159 (flcmp flfl> flfx> fxfl> flbn> bnfl> $fl>)
2160 (flcmp flfl<= flfx<= fxfl<= flbn<= bnfl<= $fl<=)
2161 (flcmp flfl>= flfx>= fxfl>= flbn>= bnfl>= $fl>=)
2162
2163 (define-syntax cmp-ex/in
2164 (syntax-rules ()
2165 [(_ pred)
2166 (syntax-rules ()
2167 [(_ ex in)
2168 (let ([x ex] [y in])
2169 (if ($flonum-rational? y)
2170 (pred x (exact y))
2171 (pred (inexact x) y)))])]))
2172 (define-syntax cmp-in/ex
2173 (syntax-rules ()
2174 [(_ pred)
2175 (syntax-rules ()
2176 [(_ in ex)
2177 (let ([x in] [y ex])
2178 (if ($flonum-rational? x)
2179 (pred (exact x) y)
2180 (pred x (inexact y))))])]))
2181
2182 (define-syntax flrt= (cmp-in/ex =))
2183 (define-syntax rtfl= (cmp-ex/in =))
2184 (define-syntax flrt< (cmp-in/ex <))
2185 (define-syntax rtfl< (cmp-ex/in <))
2186 (define-syntax flrt<= (cmp-in/ex <=))
2187 (define-syntax rtfl<= (cmp-ex/in <=))
2188 (define-syntax flrt> (cmp-in/ex >))
2189 (define-syntax rtfl> (cmp-ex/in >))
2190 (define-syntax flrt>= (cmp-in/ex >=))
2191 (define-syntax rtfl>= (cmp-ex/in >=))
2192
2193 (define (exrt< x y) (< (* x ($ratnum-d y)) ($ratnum-n y)))
2194 (define (rtex< x y) (< ($ratnum-n x) (* y ($ratnum-d x))))
2195 (define (rtrt< x y) (< (* ($ratnum-n x) ($ratnum-d y)) (* ($ratnum-n y) ($ratnum-d x))))
2196 (define (rtrt<= x y) (<= (* ($ratnum-n x) ($ratnum-d y)) (* ($ratnum-n y) ($ratnum-d x))))
2197 (define (exrt> x y) (> (* x ($ratnum-d y)) ($ratnum-n y)))
2198 (define (rtex> x y) (> ($ratnum-n x) (* y ($ratnum-d x))))
2199 (define (rtrt> x y) (> (* ($ratnum-n x) ($ratnum-d y)) (* ($ratnum-n y) ($ratnum-d x))))
2200 (define (rtrt>= x y) (>= (* ($ratnum-n x) ($ratnum-d y)) (* ($ratnum-n y) ($ratnum-d x))))
2201 (define (rtrt= x y)
2202 (and (= ($ratnum-n x) ($ratnum-n y)) (= ($ratnum-d x) ($ratnum-d y))))
2203
2204
2205
2206 (define =
2207 (let ()
2208 (define err
2209 (lambda (x) (die '= "not a number" x)))
2210 (define (fx? x y)
2211 (cond
2212 [(fixnum? y) ($fx= x y)]
2213 [(flonum? y) (fxfl= x y)]
2214 [(or (bignum? y) (ratnum? y) (compnum? y)) #f]
2215 [(cflonum? y)
2216 (and (flfl= 0.0 ($cflonum-imag y)) (fxfl= x ($cflonum-real y)))]
2217 [else (err y)]))
2218 (define (bn? x y)
2219 (cond
2220 [(bignum? y) (bnbn= x y)]
2221 [(flonum? y) (bnfl= x y)]
2222 [(or (fixnum? y) (ratnum? y) (compnum? y)) #f]
2223 [(cflonum? y)
2224 (and (flfl= 0.0 ($cflonum-imag y)) (bnfl= x ($cflonum-real y)))]
2225 [else (err y)]))
2226 (define (fl? x y)
2227 (cond
2228 [(flonum? y) (flfl= x y)]
2229 [(fixnum? y) (flfx= x y)]
2230 [(bignum? y) (flbn= x y)]
2231 [(ratnum? y) (flrt= x y)]
2232 [(compnum? y) #f]
2233 [(cflonum? y)
2234 (and (flfl= 0.0 ($cflonum-imag y)) (flfl= x ($cflonum-real y)))]
2235 [else (err y)]))
2236 (define (rn? x y)
2237 (cond
2238 [(flonum? y) (rtfl= x y)]
2239 [(ratnum? y) (rtrt= x y)]
2240 [(or (fixnum? y) (bignum? y) (compnum? y)) #f]
2241 [(cflonum? y)
2242 (and (flfl= 0.0 ($cflonum-imag y)) (rtfl= x ($cflonum-real y)))]
2243 [else (err y)]))
2244 (define (cn? x y)
2245 (cond
2246 [(compnum? y) (cncn= x y)]
2247 [(cflonum? y) (cncf= x y)]
2248 [(or (fixnum? y) (bignum? y) (flonum? y) (ratnum? y)) #f]
2249 [else (err y)]))
2250 (define (cf? x y)
2251 (cond
2252 [(cflonum? y) (cfcf= x y)]
2253 [(compnum? y) (cncf= y x)]
2254 [(or (fixnum? y) (bignum? y) (flonum? y) (ratnum? y))
2255 (and (flfl= 0.0 ($cflonum-imag x)) (= ($cflonum-real x) y))]
2256 [else (err y)]))
2257 (define-syntax doloop
2258 (syntax-rules ()
2259 [(_ cmp x0 y0 ls0)
2260 (let loop ([x x0] [y y0] [ls ls0])
2261 (if (cmp x y)
2262 (if (null? ls) #t (loop x (car ls) (cdr ls)))
2263 (if (null? ls) #f (loopf (car ls) (cdr ls)))))]))
2264 (define loopf
2265 (lambda (x ls)
2266 (if (number? x)
2267 (if (null? ls)
2268 #f
2269 (loopf (car ls) (cdr ls)))
2270 (err x))))
2271 (define (cncn= x y)
2272 (and
2273 (= ($compnum-real x) ($compnum-real y))
2274 (= ($compnum-imag x) ($compnum-imag y))))
2275 (define (cncf= x y)
2276 (and
2277 (= ($compnum-real x) ($cflonum-real y))
2278 (= ($compnum-imag x) ($cflonum-imag y))))
2279 (define (cfcf= x y)
2280 (and
2281 (= ($cflonum-real x) ($cflonum-real y))
2282 (= ($cflonum-imag x) ($cflonum-imag y))))
2283 (define =
2284 (case-lambda
2285 [(x y)
2286 (cond
2287 [(fixnum? x) (fx? x y)]
2288 [(bignum? x) (bn? x y)]
2289 [(flonum? x) (fl? x y)]
2290 [(ratnum? x) (rn? x y)]
2291 [(compnum? x) (cn? x y)]
2292 [(cflonum? x) (cf? x y)]
2293 [else (err x)])]
2294 [(x y z) (if (= x y) (= y z) (if (number? z) #f (err z)))]
2295 [(x) (if (number? x) #t (err x))]
2296 [(x y . ls)
2297 (cond
2298 [(fixnum? x) (doloop fx? x y ls)]
2299 [(bignum? x) (doloop bn? x y ls)]
2300 [(flonum? x) (doloop fl? x y ls)]
2301 [(ratnum? x) (doloop rn? x y ls)]
2302 [(compnum? x) (doloop cn? x y ls)]
2303 [(cflonum? x) (doloop cf? x y ls)]
2304 [else (err x)])]))
2305 =))
2306
2307 ;(define =
2308 ; (mk< = $fx= false false bnbn= fxfl= flfx= bnfl= flbn= flfl=
2309 ; false false false false flrt= rtfl= rtrt=))
2310
2311 (define <
2312 (mk< < $fx< fxbn< bnfx< bnbn< fxfl< flfx< bnfl< flbn< flfl<
2313 exrt< rtex< exrt< rtex< flrt< rtfl< rtrt<))
2314 (define >
2315 (mk< > $fx> fxbn> bnfx> bnbn> fxfl> flfx> bnfl> flbn> flfl>
2316 exrt> rtex> exrt> rtex> flrt> rtfl> rtrt>))
2317 (define <=
2318 (mk< <= $fx<= fxbn< bnfx< bnbn<= fxfl<= flfx<= bnfl<= flbn<= flfl<=
2319 exrt< rtex< exrt< rtex< flrt<= rtfl<= rtrt<=))
2320 (define >=
2321 (mk< >= $fx>= fxbn> bnfx> bnbn>= fxfl>= flfx>= bnfl>= flbn>= flfl>=
2322 exrt> rtex> exrt> rtex> flrt>= rtfl>= rtrt>=))
2323
2324 (define error@add1
2325 (lambda (x)
2326 (import (ikarus))
2327 (cond
2328 [(fixnum? x) (+ (greatest-fixnum) 1)]
2329 [(number? x) (+ x 1)]
2330 [else (die 'add1 "not a number" x)])))
2331
2332 (define add1
2333 (lambda (x)
2334 (import (ikarus))
2335 (add1 x)))
2336
2337 (define error@sub1
2338 (lambda (x)
2339 (import (ikarus))
2340 (cond
2341 [(fixnum? x) (- (least-fixnum) 1)]
2342 [(number? x) (- x 1)]
2343 [else (die 'sub1 "not a number" x)])))
2344
2345 (define sub1
2346 (lambda (x)
2347 (import (ikarus))
2348 (sub1 x)))
2349
2350 (define zero?
2351 (lambda (x)
2352 (cond
2353 [(fixnum? x) (eq? x 0)]
2354 [(bignum? x) #f]
2355 [(ratnum? x) #f]
2356 [(flonum? x)
2357 (or ($fl= x 0.0) ($fl= x -0.0))]
2358 [(cflonum? x)
2359 (and ($fl= ($cflonum-real x) 0.0) ($fl= ($cflonum-imag x) 0.0))]
2360 [(compnum? x) #f]
2361 [else
2362 (die 'zero? "not a number" x)])))
2363
2364 (define (expt n m)
2365 ;;Return N raised to the power M. For non--zero N, this is:
2366 ;;
2367 ;; (expt N M) === (exp (log (expt N M)))
2368 ;; === (exp (* M (log N)))
2369 ;;
2370 ;;for N equal to zero:
2371 ;;
2372 ;; (expt 0.0 Z) = 1.0 if Z = 0.0
2373 ;; = 0.0 if (real-part Z) is positive
2374 ;;
2375 ;;for other cases in which the first argument is zero, either an
2376 ;;exception is raised with condition type
2377 ;;"&implementation-restriction", or an unspecified number object is
2378 ;;returned.
2379 ;;
2380 ;;For an exact real number object N and an exact integer object M,
2381 ;;(expt N M) must return an exact result. For all other values of N
2382 ;;and M, (expt N M) may return an inexact result, even when both N and
2383 ;;M are exact.
2384 ;;
2385 ;;Notice that this definition can lead to unintuitive results; from a
2386 ;;discussion with Kent Dybvig: It does seem like:
2387 ;;
2388 ;; (expt +inf.0+2.i 2)
2389 ;;
2390 ;;would be equivalent to:
2391 ;;
2392 ;; (* +inf.0+2.i +inf.0+2.i)
2393 ;;
2394 ;;which evaluates to +inf.0+inf.0i. Nevertheless, I'm not sure the
2395 ;;R6RS supports this interpretation. According to the description of
2396 ;;expt, when z1 is not zero,
2397 ;;
2398 ;; (expt z1 z2) => e^{z2 log z1}
2399 ;;
2400 ;;so,
2401 ;;
2402 ;; (expt +inf.0+2.i 2) => (exp (* 2 (log +inf.0+2.i)))
2403 ;;
2404 ;;Meanwhile, the Section 11.7.3 subsection on transcendental functions
2405 ;;defines log as follows:
2406 ;;
2407 ;; (log z) => log |z| + (angle z)i
2408 ;;
2409 ;;so,
2410 ;;
2411 ;; (log +inf.0+2.i) =>
2412 ;; (make-rectangular
2413 ;; (log (magnitude +inf.0+2.0i))
2414 ;; (angle +inf.0+2.0i))
2415 ;;
2416 ;;Since:
2417 ;;
2418 ;; (magnitude +inf.0+2.i) => +inf.0,
2419 ;; (log +inf.0) => +inf.0, and
2420 ;; (angle +inf.0+2.i) => 0.0,
2421 ;;
2422 ;;we have:
2423 ;;
2424 ;; (log +inf.0+2.i) => +inf.0+0.0i
2425 ;;
2426 ;;and finally:
2427 ;;
2428 ;; (expt +inf.0+2.i 2) => (exp (* 2 +inf.0+0.0i))
2429 ;; => (exp +inf.0+0.0i)
2430 ;; => (* (exp +inf.0) (exp 0.0i))
2431 ;; => (* +inf.0 1.0+0.0i)
2432 ;; => +inf.0+nan.0i
2433 ;;
2434 ;;because:
2435 ;;
2436 ;; (* +inf.0 0.0i) => +nan.0
2437 ;;
2438
2439 (define (%expt-fx n m)
2440 ;;Recursive function computing N^M when M is a fixnum and N is:
2441 ;;
2442 ;;* A real number, infinite included, NaN excluded.
2443 ;;
2444 ;;* A finite complex number.
2445 ;;
2446 ;;This function recurses a number of times equal to the bits in the
2447 ;;representation of M.
2448 ;;
2449 ;;Notes about used procedures:
2450 ;;
2451 ;;* ($fxlogand m 1) is 1 for even m and 0 for odd m, or in other
2452 ;;words: the return value is the rightmost bit of M.
2453 ;;
2454 ;;* $fxsra means "fixnum shift right arithmetic".
2455 ;;
2456 ;;* binary* is the multiplication with two arguments.
2457 ;;
2458 (cond (($fxzero? m)
2459 1)
2460 (($fxzero? ($fxlogand m 1)) ;the rightmost bit in M is zero
2461 (%expt-fx (binary* n n) ($fxsra m 1)))
2462 (else ;the rightmost bit in M is one
2463 (binary* n (%expt-fx (binary* n n) ($fxsra m 1))))))
2464 (unless (number? n)
2465 (die 'expt "not a numebr" n))
2466 (cond ((fixnum? m)
2467 (cond (($fxzero? m)
2468 (cond ((nan? n) +nan.0)
2469 ((exact? n) 1)
2470 (else 1.)))
2471 (($fx> m 0)
2472 (cond ((integer? n)
2473 (%expt-fx n m))
2474 ((ratnum? n)
2475 ($make-ratnum (%expt-fx ($ratnum-n n) m)
2476 (%expt-fx ($ratnum-d n) m)))
2477 ((real? n) ;this includes the real infinite +inf.0
2478 (if (nan? n)
2479 +nan.0
2480 (%expt-fx n m)))
2481 ;;In the following clauses N is a non-real.
2482 ((or (nan? (real-part n))
2483 (nan? (imag-part n)))
2484 +nan.0+nan.0i)
2485 ((infinite? n) ;this handles correctly some special cases
2486 (exp (* m (log n))))
2487 (else
2488 (%expt-fx n m))))
2489 (else ;M is negative
2490 (let ((v (expt n (- m))))
2491 (if (eq? v 0)
2492 0
2493 (/ 1 v))))))
2494 ((bignum? m)
2495 (cond ((eq? n 0) 0)
2496 ((eq? n 1) 1)
2497 ((eq? n -1) (if (even-bignum? m) 1 -1))
2498 ((nan? n) +nan.0)
2499 (else
2500 (die 'expt "result is too big to compute" n m))))
2501 ((flonum? m)
2502 (cond ((real? n)
2503 (cond ((nan? n)
2504 +nan.0)
2505 ((integer? m)
2506 ;;N^M when M is an integer always has a real
2507 ;;number as result.
2508 (flexpt (inexact n) m))
2509 ((negative? n)
2510 (exp (* m (log n))))
2511 (else
2512 (flexpt (inexact n) m))))
2513 ((or (nan? (real-part n))
2514 (nan? (imag-part n)))
2515 +nan.0+nan.0i)
2516 (else
2517 (exp (* m (log n))))))
2518 ((ratnum? m)
2519 ;;; (expt (expt n ($ratnum-n m))
2520 ;;; (inexact ($make-ratnum 1 ($ratnum-d m))))
2521 (expt n (inexact m)))
2522 ((or (compnum? m) (cflonum? m))
2523 (cond ((eq? n 0)
2524 0)
2525 ((or (nan? (real-part n))
2526 (nan? (imag-part n)))
2527 +nan.0+nan.0i)
2528 ((zero? n)
2529 (if (flonum? n) 0.0 0.0+0.0i))
2530 (else
2531 (exp (* m (log n))))))
2532 (else
2533 (die 'expt "not a number" m))))
2534
2535 (define quotient
2536 (lambda (x y)
2537 (let-values ([(q r) (quotient+remainder x y)])
2538 q)))
2539
2540 (define remainder
2541 (lambda (x y)
2542 (let-values ([(q r) (quotient+remainder x y)])
2543 r)))
2544
2545 (define quotient+remainder
2546 (lambda (x y)
2547 (cond
2548 [(eq? y 0)
2549 (die 'quotient+remainder
2550 "second argument must be non-zero")]
2551 [(fixnum? x)
2552 (cond
2553 [(fixnum? y)
2554 (if (eq? y -1)
2555 (values (- x) 0)
2556 (values (fxquotient x y) (fxremainder x y)))]
2557 [(bignum? y) (values 0 x)]
2558 [(flonum? y)
2559 (let ([v ($flonum->exact y)])
2560 (cond
2561 [(or (fixnum? v) (bignum? v))
2562 (let-values ([(q r) (quotient+remainder x v)])
2563 (values (inexact q) (inexact r)))]
2564 [else
2565 (die 'quotient+remainder "not an integer" y)]))]
2566 [else (die 'quotient+remainder "not an integer" y)])]
2567 [(bignum? x)
2568 (cond
2569 [(fixnum? y)
2570 (let ([p (foreign-call "ikrt_bnfxdivrem" x y)])
2571 (values (car p) (cdr p)))]
2572 [(bignum? y)
2573 (let ([p (foreign-call "ikrt_bnbndivrem" x y)])
2574 (values (car p) (cdr p)))]
2575 [(flonum? y)
2576 (let ([v ($flonum->exact y)])
2577 (cond
2578 [(or (fixnum? v) (bignum? v))
2579 (let-values ([(q r) (quotient+remainder x v)])
2580 (values (inexact q) (inexact r)))]
2581 [else
2582 (die 'quotient+remainder "not an integer" y)]))]
2583 [else (die 'quotient+remainder "not an integer" y)])]
2584 [(flonum? x)
2585 (let ([v ($flonum->exact x)])
2586 (cond
2587 [(or (fixnum? v) (bignum? v))
2588 (let-values ([(q r) (quotient+remainder v y)])
2589 (values (inexact q) (inexact r)))]
2590 [else (die 'quotient+remainder "not an integer" x)]))]
2591 [else (die 'quotient+remainder "not an integer" x)])))
2592
2593 (define positive?
2594 (lambda (x)
2595 (cond
2596 [(fixnum? x) ($fx> x 0)]
2597 [(flonum? x) ($fl> x 0.0)]
2598 [(bignum? x) (positive-bignum? x)]
2599 [(ratnum? x) (positive? ($ratnum-n x))]
2600 [else (die 'positive? "not a real number" x)])))
2601
2602 (define negative?
2603 (lambda (x)
2604 (cond
2605 [(fixnum? x) ($fx< x 0)]
2606 [(flonum? x) ($fl< x 0.0)]
2607 [(bignum? x) (not (positive-bignum? x))]
2608 [(ratnum? x) (negative? ($ratnum-n x))]
2609 [else (die 'negative? "not a real number" x)])))
2610
2611 (define sinh
2612 (lambda (x)
2613 (define who 'sinh)
2614 (cond
2615 [(flonum? x) (foreign-call "ikrt_fl_sinh" x)]
2616 [(or (fixnum? x) (bignum? x) (ratnum? x))
2617 (sinh (inexact x))]
2618 [(or (compnum? x) (cflonum? x))
2619 (let ([r (real-part x)] [i (imag-part x)])
2620 (make-rectangular
2621 (* (sinh r) (cos i))
2622 (* (cosh r) (sin i))))]
2623 [else (die who "not a number" x)])))
2624
2625 (define cosh
2626 (lambda (x)
2627 (define who 'cosh)
2628 (cond
2629 [(flonum? x) (foreign-call "ikrt_fl_cosh" x)]
2630 [(or (fixnum? x) (bignum? x) (ratnum? x))
2631 (cosh (inexact x))]
2632 [(or (compnum? x) (cflonum? x))
2633 (let ([r (real-part x)] [i (imag-part x)])
2634 (make-rectangular
2635 (* (cosh r) (cos i))
2636 (* (sinh r) (sin i))))]
2637 [else (die who "not a number" x)])))
2638
2639 (define tanh
2640 (lambda (x)
2641 (define who 'tanh)
2642 (cond
2643 [(flonum? x) (foreign-call "ikrt_fl_tanh" x)]
2644 [(or (fixnum? x) (bignum? x) (ratnum? x))
2645 (tanh (inexact x))]
2646 [(or (compnum? x) (cflonum? x))
2647 (let ([r (real-part x)] [i (imag-part x)])
2648 (let ([rr (* 2 r)] [ii (* 2 i)])
2649 (let ([cos2i (cos ii)] [cosh2r (cosh rr)])
2650 (make-rectangular
2651 (/ (tanh rr) (+ 1 (/ cos2i cosh2r)))
2652 (/ (sin ii) (+ cosh2r cos2i))))))]
2653 [else (die who "not a number" x)])))
2654
2655 (define asinh
2656 (lambda (x)
2657 (define who 'asinh)
2658 (cond
2659 [(flonum? x) (foreign-call "ikrt_fl_asinh" x)]
2660 [(or (fixnum? x) (bignum? x) (ratnum? x))
2661 (asinh (inexact x))]
2662 [(or (cflonum? x) (compnum? x))
2663 (let ([x (real-part x)] [y (imag-part x)])
2664 (cond
2665 [(= x 0)
2666 (let ([v (asin y)])
2667 (make-rectangular (imag-part v) (real-part v)))]
2668 [else
2669 (let* ([z^2 (+ (* x x) (* y y))]
2670 [z^2-1 (- z^2 1)]
2671 [z^2-1^2 (* z^2-1 z^2-1)]
2672 [y^2 (* y y)]
2673 [q (sqrt (+ z^2-1^2 (* 4 y^2)))])
2674 (define (sgn x) (if (< x 0) -1 1))
2675 (make-rectangular
2676 (* 0.5 (sgn x) (acosh (+ q z^2)))
2677 (* 0.5 (sgn y) (acos (- q z^2)))))]))]
2678 [else (die who "not a number" x)])))
2679
2680 (define acosh
2681 (lambda (x)
2682 (define who 'acosh)
2683 (cond
2684 [(flonum? x)
2685 (cond
2686 [($fl>= x 1.0) (foreign-call "ikrt_fl_acosh" x)]
2687 [($fl>= x -1.0)
2688 (make-rectangular 0 (atan (sqrt (- 1 (* x x))) x))]
2689 [($fl< x -1.0)
2690 (make-rectangular (acosh (- x)) PI)]
2691 [else +nan.0])]
2692 [(or (fixnum? x) (bignum? x) (ratnum? x))
2693 (acosh (inexact x))]
2694 [(or (cflonum? x) (compnum? x))
2695 (let ([x (real-part x)] [y (imag-part x)])
2696 (cond
2697 [(= x 0) (+ (asinh y) (make-rectangular 0 PI/2))]
2698 [else
2699 (let* ([z^2 (+ (* x x) (* y y))]
2700 [z^2-1 (- z^2 1)]
2701 [z^2-1^2 (* z^2-1 z^2-1)]
2702 [y^2 (* y y)]
2703 [q (sqrt (+ z^2-1^2 (* 4 y^2)))])
2704 (define (sgn x) (if (< x 0) -1 1))
2705 (+ (* 0.5 (sgn x) (acosh (+ q z^2)))
2706 (* 0.5i (sgn y)
2707 (- PI (* (sgn x) (acos (- q z^2)))))))]))]
2708 [else (die who "not a number" x)])))
2709
2710 (define atanh
2711 (lambda (x)
2712 (define who 'atanh)
2713 (cond
2714 [(flonum? x)
2715 (cond
2716 [(and (fl<=? x 1.0) (fl>=? x -1.0))
2717 (foreign-call "ikrt_fl_atanh" x)]
2718 [else
2719 (- (atanh (fl/ 1.0 x))
2720 (if (fl<? x 0.0) (* -i PI/2) (* +i PI/2)))])]
2721 [(or (fixnum? x) (bignum? x) (ratnum? x))
2722 (atanh (inexact x))]
2723 [(number? x) (error who "not implemented" x)]
2724 [else (die who "not a number" x)])))
2725
2726 (define sin
2727 (lambda (x)
2728 (cond
2729 [(flonum? x) (foreign-call "ikrt_fl_sin" x)]
2730 [(fixnum? x)
2731 (if (fx=? x 0)
2732 0
2733 (foreign-call "ikrt_fx_sin" x))]
2734 [(or (cflonum? x) (compnum? x))
2735 (let ([r (real-part x)] [i (imag-part x)])
2736 (make-rectangular
2737 (* (sin r) (cosh i))
2738 (* (cos r) (sinh i))))]
2739 [(number? x) (sin (inexact x))]
2740 [else (die 'sin "not a number" x)])))
2741
2742 (define cos
2743 (lambda (x)
2744 (cond
2745 [(flonum? x) (foreign-call "ikrt_fl_cos" x)]
2746 [(fixnum? x)
2747 (if (fx=? x 0)
2748 1
2749 (foreign-call "ikrt_fx_cos" x))]
2750 [(or (cflonum? x) (compnum? x))
2751 (let ([r (real-part x)] [i (imag-part x)])
2752 (make-rectangular
2753 (* (cos r) (cosh i))
2754 (* (sin r) (sinh i))))]
2755 [(number? x) (cos (inexact x))]
2756 [else (die 'cos "not a number" x)])))
2757
2758 (define tan
2759 (lambda (x)
2760 (cond
2761 [(flonum? x) (foreign-call "ikrt_fl_tan" x)]
2762 [(fixnum? x)
2763 (if (fx=? x 0)
2764 0
2765 (foreign-call "ikrt_fx_tan" x))]
2766 [(or (cflonum? x) (compnum? x))
2767 (let ([r (real-part x)] [i (imag-part x)])
2768 (make-rectangular
2769 (/ (sin (* 2 r))
2770 (+ (cos (* 2 r)) (cosh (* 2 i))))
2771 (/ (tanh (* 2 i))
2772 (+ 1 (/ (cos (* 2 r)) (cosh (* 2 i)))))))]
2773 [(number? x) (tan (inexact x))]
2774 [else (die 'tan "not a number" x)])))
2775
2776 (module (PI PI/2)
2777 (import (ikarus))
2778 (define PI (acos -1))
2779 (define PI/2 (/ PI 2)))
2780
2781 (define asin
2782 (lambda (x)
2783 (cond
2784 [(flonum? x)
2785 (cond
2786 [($fl> x 1.0)
2787 (make-rectangular PI/2 (acosh x))]
2788 [($fl< x -1.0)
2789 (make-rectangular (- PI/2) (- (acosh (- x))))]
2790 [else
2791 (foreign-call "ikrt_fl_asin" x)])]
2792 [(or (cflonum? x) (compnum? x))
2793 (let ([x (real-part x)] [y (imag-part x)])
2794 (cond
2795 [(= x 0) (make-rectangular 0 (asinh y))]
2796 [else
2797 (let* ([z^2 (+ (* x x) (* y y))]
2798 [z^2-1 (- z^2 1.0)]
2799 [z^2-1^2 (* z^2-1 z^2-1)]
2800 [y^2 (* y y)]
2801 [q (sqrt (+ z^2-1^2 (* 4.0 y^2)))])
2802 (define (sgn x) (if (< x 0) -1.0 1.0))
2803 (make-rectangular
2804 (* 0.5 (sgn x) (acos (- q z^2)))
2805 (* 0.5 (sgn y) (acosh (+ q z^2)))))]))]
2806 [(number? x) (asin (inexact x))]
2807 [else (die 'asin "not a number" x)])))
2808
2809 (define acos
2810 (lambda (x)
2811 (cond
2812 [(flonum? x)
2813 (cond
2814 [($fl> x 1.0)
2815 (make-rectangular 0 (acosh x))]
2816 [($fl< x -1.0)
2817 (make-rectangular PI (- (acosh (- x))))]
2818 [else
2819 (foreign-call "ikrt_fl_acos" x)])]
2820 [(or (cflonum? x) (compnum? x))
2821 (- PI/2 (asin x))]
2822 [(number? x) (acos (inexact x))]
2823 [else (die 'acos "not a number" x)])))
2824
2825 (define atan
2826 (case-lambda
2827 [(x)
2828 (cond
2829 [(flonum? x) (foreign-call "ikrt_fl_atan" x)]
2830 [(fixnum? x) (foreign-call "ikrt_fx_atan" x)]
2831 [(or (ratnum? x) (bignum? x) (compnum? x))
2832 (atan (inexact x))]
2833 ((cflonum? x)
2834 ;;Formula from Wikipedia section "Logarithmic forms":
2835 ;;
2836 ;; <http://en.wikipedia.org/wiki/Arc_tangent>
2837 ;;
2838 (if (let ((I (imag-part x)))
2839 (and (exact? I) (zero? I)))
2840 (flatan (real-part x))
2841 (* +0.5i
2842 (- (log (- 1 (* +1i x)))
2843 (log (+ 1 (* +1i x)))))))
2844 [else (die 'atan "not a number" x)])]
2845 [(y x)
2846 (unless (real? x) (die 'atan "not a real number" x))
2847 (unless (real? y) (die 'atan "not a real number" y))
2848 (foreign-call "ikrt_atan2" (inexact y) (inexact x))]))
2849
2850 (define sqrt
2851 (lambda (x)
2852 (cond
2853 [(flonum? x)
2854 (if ($fl< x 0.0)
2855 (make-rectangular 0.0
2856 (foreign-call "ikrt_fl_sqrt" ($fl- 0.0 x)))
2857 (foreign-call "ikrt_fl_sqrt" x))]
2858 [(fixnum? x)
2859 (cond
2860 [($fx< x 0)
2861 (make-rectangular 0 (sqrt (- x)))]
2862 [else
2863 (let-values ([(s r) (exact-integer-sqrt x)])
2864 (cond
2865 [(eq? r 0) s]
2866 [else (foreign-call "ikrt_fx_sqrt" x)]))])]
2867 [(bignum? x)
2868 (cond
2869 [($bignum-positive? x)
2870 (let-values ([(s r) (exact-integer-sqrt x)])
2871 (cond
2872 [(eq? r 0) s]
2873 [else
2874 (let ([v (sqrt (inexact x))])
2875 ;;; could the [dropped] residual ever affect the answer?
2876 (cond
2877 [(infinite? v)
2878 (if (bignum? s)
2879 (foreign-call "ikrt_bignum_to_flonum"
2880 s
2881 1 ;;; round up in case of a tie
2882 ($make-flonum))
2883 (inexact s))]
2884 [else v]))]))]
2885 [else
2886 (make-rectangular 0 (sqrt (- x)))])]
2887 [(ratnum? x)
2888 ;;; FIXME: incorrect as per bug 180170
2889 (/ (sqrt ($ratnum-n x)) (sqrt ($ratnum-d x)))]
2890 [(or (compnum? x) (cflonum? x))
2891 (let ([xr (real-part x)] [xi (imag-part x)])
2892 (let ([m (sqrt (+ (* xr xr) (* xi xi)))]
2893 [s (if (> xi 0) 1 -1)])
2894 (make-rectangular
2895 (sqrt (/ (+ m xr) 2))
2896 (* s (sqrt (/ (- m xr) 2))))))]
2897 [else (die 'sqrt "not a number" x)])))
2898
2899 (define flsqrt
2900 (lambda (x)
2901 (if (flonum? x)
2902 (foreign-call "ikrt_fl_sqrt" x)
2903 (die 'flsqrt "not a flonum" x))))
2904
2905 (define flzero?
2906 (lambda (x)
2907 (if (flonum? x)
2908 ($flzero? x)
2909 (die 'flzero? "not a flonum" x))))
2910
2911 (define flnegative?
2912 (lambda (x)
2913 (if (flonum? x)
2914 ($fl< x 0.0)
2915 (die 'flnegative? "not a flonum" x))))
2916
2917 (define exact-integer-sqrt
2918 (lambda (x)
2919 (define who 'exact-integer-sqrt)
2920 (cond
2921 [(fixnum? x)
2922 (cond
2923 [($fx= x 0) (values 0 0)]
2924 [($fx< x 0) (die who "invalid argument" x)]
2925 [else
2926 (let ([s (foreign-call "ikrt_exact_fixnum_sqrt" x)])
2927 (values s ($fx- x ($fx* s s))))])]
2928 [(bignum? x)
2929 (cond
2930 [($bignum-positive? x)
2931 (let ([r (foreign-call "ikrt_exact_bignum_sqrt" x)])
2932 (values (car r) (cdr r)))]
2933 [else (die who "invalid argument" x)])]
2934 [else (die who "invalid argument" x)])))
2935
2936
2937 (define numerator
2938 (lambda (x)
2939 (cond
2940 [(ratnum? x) ($ratnum-n x)]
2941 [(or (fixnum? x) (bignum? x)) x]
2942 [(flonum? x) (flnumerator x)]
2943 [else (die 'numerator "not an exact integer" x)])))
2944
2945 (define denominator
2946 (lambda (x)
2947 (cond
2948 [(ratnum? x) ($ratnum-d x)]
2949 [(or (fixnum? x) (bignum? x)) 1]
2950 [(flonum? x) (fldenominator x)]
2951 [else (die 'denominator "not an exact integer" x)])))
2952
2953
2954 (define (floor x)
2955 (define (ratnum-floor x)
2956 (let ([n (numerator x)] [d (denominator x)])
2957 (let ([q (quotient n d)])
2958 (if (>= n 0) q (- q 1)))))
2959 (cond
2960 [(flonum? x)
2961 ;;; optimize for integer flonums
2962 (let ([e ($flonum->exact x)])
2963 (cond
2964 [(ratnum? e)
2965 (exact->inexact (ratnum-floor e))]
2966 [else x]))]
2967 [(ratnum? x) (ratnum-floor x)]
2968 [(or (fixnum? x) (bignum? x)) x]
2969 [else (die 'floor "not a number" x)]))
2970
2971 (define (ceiling x)
2972 (define (ratnum-ceiling x)
2973 (let ([n (numerator x)] [d (denominator x)])
2974 (let ([q (quotient n d)])
2975 (if (< n 0) q (+ q 1)))))
2976 (cond
2977 [(flonum? x)
2978 ;;; optimize for integer flonums
2979 (let ([e ($flonum->exact x)])
2980 (cond
2981 [(ratnum? e) (exact->inexact (ratnum-ceiling e))]
2982 [else x]))]
2983 [(ratnum? x) (ratnum-ceiling x)]
2984 [(or (fixnum? x) (bignum? x)) x]
2985 [else (die 'ceiling "not a number" x)]))
2986
2987
2988 (define ($ratnum-round x)
2989 (let ([n ($ratnum-n x)] [d ($ratnum-d x)])
2990 (let-values ([(q r) (div-and-mod n d)])
2991 (let ([r2 (+ r r)])
2992 (cond
2993 [(< r2 d) q]
2994 [(> r2 d) (+ q 1)]
2995 [else (if (even? q) q (+ q 1))])))))
2996
2997 (define ($ratnum-truncate x)
2998 (let ([n ($ratnum-n x)] [d ($ratnum-d x)])
2999 (quotient n d)))
3000
3001
3002 (define (round x)
3003 (cond
3004 [(flonum? x) ($flround x)]
3005 [(ratnum? x) ($ratnum-round x)]
3006 [(or (fixnum? x) (bignum? x)) x]
3007 [else (die 'round "not a number" x)]))
3008
3009 (define (truncate x)
3010 ;;; FIXME: fltruncate should preserve the sign of -0.0.
3011 ;;;
3012 (cond
3013 [(flonum? x)
3014 (let ([e ($flonum->exact x)])
3015 (cond
3016 [(ratnum? e) (exact->inexact ($ratnum-truncate e))]
3017 [else x]))]
3018 [(ratnum? x) ($ratnum-truncate x)]
3019 [(or (fixnum? x) (bignum? x)) x]
3020 [else (die 'truncate "not a number" x)]))
3021
3022
3023 (define (fltruncate x)
3024 ;;; FIXME: fltruncate should preserve the sign of -0.0.
3025 (unless (flonum? x)
3026 (die 'fltruncate "not a flonum" x))
3027 (let ([v ($flonum->exact x)])
3028 (cond
3029 [(ratnum? v) (exact->inexact ($ratnum-truncate v))]
3030 [else x])))
3031
3032 (define log
3033 (case-lambda
3034 [(x)
3035 (cond
3036 [(fixnum? x)
3037 (cond
3038 [($fx= x 1) 0]
3039 [($fx= x 0) (die 'log "undefined around 0")]
3040 [($fx> x 0) (foreign-call "ikrt_fx_log" x)]
3041 [else (make-rectangular (log (- x)) (acos -1))])]
3042 [(flonum? x)
3043 (cond
3044 ((nan? x) +nan.0)
3045 [(fl>=? x 0.0) (foreign-call "ikrt_fl_log" x)]
3046 [else
3047 (make-rectangular
3048 (log (fl- 0.0 x))
3049 (acos -1))])]
3050 [(bignum? x)
3051 (if ($bignum-positive? x)
3052 (let ([v (log (inexact x))])
3053 (cond
3054 [(infinite? v)
3055 (let-values ([(s r) (exact-integer-sqrt x)])
3056 ;;; could the [dropped] residual ever affect the answer?
3057 (fl* 2.0 (log s)))]
3058 [else v]))
3059 (make-rectangular (log (- x)) (acos -1)))]
3060 [(ratnum? x)
3061 (- (log (numerator x)) (log (denominator x)))]
3062 [(or (compnum? x) (cflonum? x))
3063 (let ([xr (real-part x)] [xi (imag-part x)])
3064 (make-rectangular
3065 (/ (log (+ (* xr xr) (* xi xi))) 2)
3066 (atan xi xr)))]
3067 [else (die 'log "not a number" x)])]
3068 [(x y)
3069 (let ([ly (log y)])
3070 (if (eqv? ly 0)
3071 (die 'log "invalid arguments" x y)
3072 (/ (log x) ly)))]))
3073
3074 (define (random n)
3075 (if (fixnum? n)
3076 (if (fx> n 1)
3077 (foreign-call "ikrt_fxrandom" n)
3078 (if (fx= n 1)
3079 0
3080 (die 'random "incorrect argument" n)))
3081 (die 'random "not a fixnum" n)))
3082
3083
3084 (define (shift-right-arithmetic n m who)
3085 (cond
3086 [(fixnum? m)
3087 (cond
3088 [(fixnum? n)
3089 (cond
3090 [($fx>= m 0) ($fxsra n m)]
3091 [else (die who "offset must be non-negative" m)])]
3092 [(bignum? n)
3093 (cond
3094 [($fx> m 0)
3095 (foreign-call "ikrt_bignum_shift_right" n m)]
3096 [($fx= m 0) n]
3097 [else (die who "offset must be non-negative" m)])]
3098 [else (die who "not an exact integer" n)])]
3099 [(bignum? m)
3100 (cond
3101 [(fixnum? n) (if ($fx>= n 0) 0 -1)]
3102 [(bignum? n) (if ($bignum-positive? n) 0 -1)]
3103 [else (die who "not an exact integer" n)])]
3104 [else (die who "not an exact integer offset" m)]))
3105
3106 (define (sra n m)
3107 (shift-right-arithmetic n m 'sra))
3108
3109 (define (shift-left-logical n m who)
3110 (unless (fixnum? m)
3111 (die who "shift amount is not a fixnum"))
3112 (cond
3113 [(fixnum? n)
3114 (cond
3115 [($fx> m 0)
3116 (foreign-call "ikrt_fixnum_shift_left" n m)]
3117 [($fx= m 0) n]
3118 [else (die who "offset must be non-negative" m)])]
3119 [(bignum? n)
3120 (cond
3121 [($fx> m 0)
3122 (foreign-call "ikrt_bignum_shift_left" n m)]
3123 [($fx= m 0) n]
3124 [else (die who "offset must be non-negative" m)])]
3125 [else (die who "not an exact integer" n)]))
3126
3127 (define (sll n m)
3128 (shift-left-logical n m 'sll))
3129
3130 (define (bitwise-arithmetic-shift-right n m)
3131 (shift-right-arithmetic n m 'bitwise-arithmetic-shift-right))
3132 (define (bitwise-arithmetic-shift-left n m)
3133 (shift-left-logical n m 'bitwise-arithmetic-shift-left))
3134 (define (bitwise-arithmetic-shift n m)
3135 (define who 'bitwise-arithmetic-shift)
3136 (unless (fixnum? m)
3137 (die who "shift amount is not a fixnum"))
3138 (cond
3139 [(fixnum? n)
3140 (cond
3141 [($fx> m 0)
3142 (foreign-call "ikrt_fixnum_shift_left" n m)]
3143 [($fx= m 0) n]
3144 [else
3145 (let ([m^ (- m)])
3146 (unless (fixnum? m^)
3147 (die who "shift amount is too big" m))
3148 ($fxsra n m^))])]
3149 [(bignum? n)
3150 (cond
3151 [($fx> m 0)
3152 (foreign-call "ikrt_bignum_shift_left" n m)]
3153 [($fx= m 0) n]
3154 [else
3155 (let ([m^ (- m)])
3156 (unless (fixnum? m^)
3157 (die who "shift amount is too big" m))
3158 (foreign-call "ikrt_bignum_shift_right" n m^))])]
3159 [else (die who "not an exact integer" n)]))
3160
3161 (define (exp x)
3162 (cond
3163 [(flonum? x) (flexp x)]
3164 [(fixnum? x)
3165 (if ($fx= x 0) 1 (flexp (fixnum->flonum x)))]
3166 [(bignum? x) (flexp (bignum->flonum x))]
3167 [(ratnum? x) (flexp (ratnum->flonum x))]
3168 [(or (compnum? x) (cflonum? x))
3169 ;;In general:
3170 ;;
3171 ;; e^x = e^(xr + xi i)
3172 ;; = e^xr cos(xi) + e^xr sin(xi) i
3173 ;;
3174 ;;and:
3175 ;;
3176 ;; e^(xr+0.0i) = e^xr * e^(0.0 i) = e^xr * 1.0+0.0i
3177 ;;
3178 ;;so, in the special case xr=+inf.0, the imaginary part becomes:
3179 ;;
3180 ;; e^xr * sin(xi) * i = +inf.0 * 0.0 * i = +nan.0 * i
3181 ;;
3182 (let ([xr (real-part x)] [xi (imag-part x)])
3183 (let ([e^xr (exp xr)])
3184 (make-rectangular
3185 (* e^xr (cos xi))
3186 (* e^xr (sin xi)))))]
3187 [else (die 'exp "not a number" x)]))
3188
3189 (define (bitwise-length n)
3190 (cond
3191 [(fixnum? n) (fxlength n)]
3192 [(bignum? n) (foreign-call "ikrt_bignum_length" n)]
3193 [else (die 'bitwise-length "not an exact integer" n)]))
3194
3195 (define (bitwise-copy-bit n idx bit)
3196 (define who 'bitwise-copy-bit)
3197 (define (do-copy-bit n idx bit)
3198 (case bit
3199 [(0)
3200 (cond
3201 [(bitwise-bit-set? n idx)
3202 (bitwise-and n (bitwise-not (sll 1 idx)))]
3203 [else n])]
3204 [(1)
3205 (cond
3206 [(bitwise-bit-set? n idx) n]
3207 [(>= n 0) (+ n (sll 1 idx))]
3208 [else
3209 (bitwise-not
3210 (bitwise-and
3211 (bitwise-not n)
3212 (bitwise-not (sll 1 idx))))])]
3213 [else (die who "bit must be either 0 or 1" bit)]))
3214 (cond
3215 [(fixnum? idx)
3216 (cond
3217 [(fx< idx 0)
3218 (die who "negative bit index" idx)]
3219 [(or (fixnum? n) (bignum? n))
3220 (do-copy-bit n idx bit)]
3221 [else (die who "not an exact integer" n)])]
3222 [(bignum? idx)
3223 (unless (or (fixnum? n) (bignum? n))
3224 (die who "not an exact integer" n))
3225 (if ($bignum-positive? idx)
3226 (case bit
3227 [(0)
3228 (if (>= n 0)
3229 n
3230 (die who "unrepresentable result"))]
3231 [(1)
3232 (if (< n 0)
3233 n
3234 (die who "unrepresentable result"))]
3235 [else (die who "bit must be either 0 or 1" bit)])
3236 (die who "negative bit index" idx))]
3237 [else (die who "index is not an exact integer" idx)]))
3238
3239 (define (bitwise-bit-field n idx1 idx2)
3240 (define who 'bitwise-bit-field)
3241 (cond
3242 [(and (fixnum? idx1) (fx>= idx1 0))
3243 (cond
3244 [(and (fixnum? idx2) (fx>= idx2 0))
3245 (cond
3246 [(fx<= idx1 idx2)
3247 (cond
3248 [(or (fixnum? n) (bignum? n))
3249 (bitwise-and
3250 (sra n idx1)
3251 (- (sll 1 (- idx2 idx1)) 1))]
3252 [else (die who "not an exact integer" n)])]
3253 [else (die who "invalid order for indices" idx1 idx2)])]
3254 [else
3255 (if (not (fixnum? idx2))
3256 (die who "invalid index" idx2)
3257 (die who "negative index" idx2))])]
3258 [else
3259 (if (not (fixnum? idx1))
3260 (die who "invalid index" idx1)
3261 (die who "negative index" idx1))]))
3262
3263 )
3264
3265
3266
3267
3268 (library (ikarus flonum-conversion)
3269 (export string->flonum flonum->string)
3270 (import
3271 (rnrs bytevectors)
3272 (ikarus system $bytevectors)
3273 (ikarus system $flonums)
3274 (except (ikarus) flonum->string string->flonum ))
3275
3276 (module (flonum->string)
3277 (module (flonum->digits)
3278 (define flonum->digits
3279 (lambda (f e min-e p b B)
3280 ;;; flonum v = f * b^e
3281 ;;; p = precision (p >= 1)
3282 (let ([round? (even? f)])
3283 (if (>= e 0)
3284 (if (not (= f (expt b (- p 1))))
3285 (let ([be (expt b e)])
3286 (scale (* f be 2) 2 be be 0 B round? f e))
3287 (let* ([be (expt b e)] [be1 (* be b)])
3288 (scale (* f be1 2) (* b 2) be1 be 0 B round? f e)))
3289 (if (or (= e min-e) (not (= f (expt b (- p 1)))))
3290 (scale (* f 2) (* (expt b (- e)) 2) 1 1 0 B round? f e)
3291 (scale (* f b 2) (* (expt b (- 1 e)) 2) b 1 0 B round? f e))))))
3292 (define (len n)
3293 (let f ([n n] [i 0])
3294 (cond
3295 [(zero? n) i]
3296 [else (f (quotient n 2) (+ i 1))])))
3297 (define scale
3298 (lambda (r s m+ m- k B round? f e)
3299 (let ([est (inexact->exact
3300 (ceiling
3301 (- (* (+ e (len f) -1) (invlog2of B))
3302 1e-10)))])
3303 (if (>= est 0)
3304 (fixup r (* s (exptt B est)) m+ m- est B round?)
3305 (let ([scale (exptt B (- est))])
3306 (fixup (* r scale) s (* m+ scale) (* m- scale) est B round?))))))
3307 (define fixup
3308 (lambda (r s m+ m- k B round?)
3309 (if ((if round? >= >) (+ r m+) s) ; too low?
3310 (values (+ k 1) (generate r s m+ m- B round?))
3311 (values k (generate (* r B) s (* m+ B) (* m- B) B round?)))))
3312 (define (chr x)
3313 (vector-ref '#(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) x))
3314 (define generate
3315 (lambda (r s m+ m- B round?)
3316 (let-values ([(q r) (quotient+remainder r s)])
3317 (let ([tc1 ((if round? <= <) r m-)]
3318 [tc2 ((if round? >= >) (+ r m+) s)])
3319 (if (not tc1)
3320 (if (not tc2)
3321 (cons (chr q) (generate (* r B) s (* m+ B) (* m- B) B round?))
3322 (list (chr (+ q 1))))
3323 (if (not tc2)
3324 (list (chr q))
3325 (if (< (* r 2) s)
3326 (list (chr q))
3327 (list (chr (+ q 1))))))))))
3328 (define invlog2of
3329 (let ([table (make-vector 37)]
3330 [log2 (log 2)])
3331 (do ([B 2 (+ B 1)])
3332 ((= B 37))
3333 (vector-set! table B (/ log2 (log B))))
3334 (lambda (B)
3335 (if (<= 2 B 36)
3336 (vector-ref table B)
3337 (/ log2 (log B))))))
3338 (define exptt
3339 (let ([table (make-vector 326)])
3340 (do ([k 0 (+ k 1)] [v 1 (* v 10)])
3341 ((= k 326))
3342 (vector-set! table k v))
3343 (lambda (B k)
3344 (if (and (= B 10) (<= 0 k 325))
3345 (vector-ref table k)
3346 (expt B k))))))
3347 (define (format-flonum pos? expt digits)
3348 (define (next x)
3349 (if (null? x)
3350 (values #\0 '())
3351 (values (car x) (cdr x))))
3352 (define (format-flonum-no-expt expt d0 d*)
3353 (cond
3354 [(= expt 1)
3355 (cons d0 (if (null? d*) '(#\. #\0) (cons #\. d*)))]
3356 [else
3357 (cons d0
3358 (let-values ([(d0 d*) (next d*)])
3359 (format-flonum-no-expt (- expt 1) d0 d*)))]))
3360 (define (format-flonum-no-expt/neg expt d*)
3361 (cond
3362 [(= expt 0) d*]
3363 [else (cons #\0 (format-flonum-no-expt/neg (+ expt 1) d*))]))
3364 (define (sign pos? ls)
3365 (if pos?
3366 (list->string ls)
3367 (list->string (cons #\- ls))))
3368 (let ([d0 (car digits)] [d* (cdr digits)])
3369 (cond
3370 [(null? d*)
3371 (if (char=? d0 #\0)
3372 (if pos? "0.0" "-0.0")
3373 (if (= expt 1)
3374 (if pos?
3375 (string d0 #\. #\0)
3376 (string #\- d0 #\. #\0))
3377 (if (= expt 0)
3378 (if pos?
3379 (string #\0 #\. d0)
3380 (string #\- #\0 #\. d0))
3381 (string-append
3382 (if pos? "" "-")
3383 (string d0) "e" (fixnum->string (- expt 1))))))]
3384 [(and (null? d*) (char=? d0 #\0)) (if pos? "0.0" "-0.0")]
3385 [(<= 1 expt 9)
3386 (sign pos? (format-flonum-no-expt expt d0 d*))]
3387 [(<= -3 expt 0)
3388 (sign pos? (cons* #\0 #\. (format-flonum-no-expt/neg expt digits)))]
3389 [else
3390 (string-append
3391 (if pos? "" "-")
3392 (string d0) "." (list->string d*)
3393 "e" (fixnum->string (- expt 1)))])))
3394 (define (flo->string pos? m e p)
3395 (let-values ([(expt digits) (flonum->digits m e 10 p 2 10)])
3396 (format-flonum pos? expt digits)))
3397 (define (flonum->string x)
3398 (let-values ([(pos? be m) (flonum-parts x)])
3399 (cond
3400 [(<= 1 be 2046) ; normalized flonum
3401 (flo->string pos? (+ m (expt 2 52)) (- be 1075) 53)]
3402 [(= be 0)
3403 (flo->string pos? m -1074 52)]
3404 [(= be 2047)
3405 (if (= m 0)
3406 (if pos? "+inf.0" "-inf.0")
3407 ;;; Gee! nans have no sign!
3408 "+nan.0")]
3409 [else (die 'flonum->string "cannot happen")]))))
3410 ;;;
3411 (define (string->flonum x)
3412 (cond
3413 [(string? x)
3414 (foreign-call "ikrt_bytevector_to_flonum"
3415 (string->utf8 x))]
3416 [else
3417 (die 'string->flonum "not a string" x)])) )
3418
3419 (library (ikarus rationalize)
3420 (export rationalize)
3421 (import
3422 (except (ikarus) rationalize))
3423
3424 (define (rationalize x eps)
3425 (define who 'rationalize)
3426 (define (simplest x y)
3427 (cond
3428 [(< y x) (simplest y x)]
3429 [(= x y) x]
3430 [(> x 0)
3431 (let ([n (numerator x)] [d (denominator x)]
3432 [n^ (numerator y)] [d^ (denominator y)])
3433 (simplest^ n d n^ d^))]
3434 [(< y 0)
3435 (let ([n (numerator x)] [d (denominator x)]
3436 [n^ (numerator y)] [d^ (denominator y)])
3437 (- (simplest^ (- n^) d^ (- n) d)))]
3438 [else 0]))
3439 (define (simplest^ n d n^ d^)
3440 (let-values ([(q r) (div-and-mod n d)])
3441 (if (= r 0)
3442 q
3443 (let-values ([(q^ r^) (div-and-mod n^ d^)])
3444 (if (= q q^)
3445 (let ([v (simplest^ d^ r^ d r)])
3446 (let ([n^^ (numerator v)] [d^^ (denominator v)])
3447 (/ (+ (* q n^^) d^^) n^^)))
3448 (+ q 1))))))
3449 (define (go x eps)
3450 (simplest (- x eps) (+ x eps)))
3451 (cond
3452 [(flonum? x)
3453 (if (flfinite? x)
3454 (cond
3455 [(flonum? eps)
3456 (if (flfinite? eps) (go x eps) +0.0)]
3457 [(or (fixnum? eps) (bignum? eps) (ratnum? eps))
3458 (go x eps)]
3459 [else (die who "not a number" eps)])
3460 (cond
3461 [(flonum? eps)
3462 (if (flfinite? eps) x +nan.0)]
3463 [(or (fixnum? eps) (bignum? eps) (ratnum? eps))
3464 x]
3465 [else (die who "not a number" eps)]))]
3466 [(or (fixnum? x) (bignum? x) (ratnum? x))
3467 (cond
3468 [(flonum? eps)
3469 (if (flfinite? eps) (go x eps) +0.0)]
3470 [(or (fixnum? eps) (bignum? eps) (ratnum? eps))
3471 (go x eps)]
3472 [else (die who "not a number" eps)])]
3473 [else (die who "not a number" x)])))
3474
3475
3476 (library (ikarus r6rs-fu div/mod)
3477 (export div mod div-and-mod div0 mod0 div0-and-mod0)
3478 (import
3479 (except (ikarus)
3480 div mod div-and-mod div0 mod0 div0-and-mod0))
3481
3482 (define (div-and-mod* n m who)
3483 (import (ikarus system $fx)
3484 (only (ikarus system $flonums) $fl=)
3485 (ikarus flonums))
3486 (define (int-div-and-mod n m)
3487 (let ([d0 (quotient n m)])
3488 (let ([m0 (- n (* d0 m))])
3489 (if (>= m0 0)
3490 (values d0 m0)
3491 (if (>= m 0)
3492 (values (- d0 1) (+ m0 m))
3493 (values (+ d0 1) (- m0 m)))))))
3494 (define (rat-div-and-mod n m)
3495 (let ([x (/ n m)])
3496 (cond
3497 [(or (fixnum? x) (bignum? x))
3498 (values x 0)]
3499 [else
3500 (let ([n0 (numerator x)] [d0 (denominator x)])
3501 (let ([q (quotient n0 d0)])
3502 (let ([r (- n (* q m))])
3503 (if (>= r 0)
3504 (values q r)
3505 (if (> m 0)
3506 (values (- q 1) (+ r m))
3507 (values (+ q 1) (- r m)))))))])))
3508 (cond
3509 [(fixnum? m)
3510 (cond
3511 [($fx= m 0)
3512 (die who "division by 0")]
3513 [(or (fixnum? n) (bignum? n))
3514 (int-div-and-mod n m)]
3515 [(flonum? n)
3516 (fldiv-and-mod n (fixnum->flonum m))]
3517 [(ratnum? n)
3518 (rat-div-and-mod n m)]
3519 [else (die who "not a number" n)])]
3520 [(bignum? m)
3521 (cond
3522 [(or (fixnum? n) (bignum? n))
3523 (int-div-and-mod n m)]
3524 [(flonum? n)
3525 (let ([v ($flonum->exact n)])
3526 (unless v
3527 (die who "invalid argument" n))
3528 (let-values ([(a b) (div-and-mod* v m who)])
3529 (values (inexact a) (inexact b))))]
3530 [(ratnum? n)
3531 (rat-div-and-mod n m)]
3532 [else (die who "not a number" n)])]
3533 [(ratnum? m)
3534 (cond
3535 [(or (fixnum? n) (bignum? n) (ratnum? n))
3536 (rat-div-and-mod n m)]
3537 [(flonum? n)
3538 (let ([v ($flonum->exact n)])
3539 (unless v
3540 (die who "invalid argument" n))
3541 (let-values ([(a b) (div-and-mod* v m who)])
3542 (values (inexact a) (inexact b))))]
3543 [else (die who "not a number" n)])]
3544 [(flonum? m)
3545 (cond
3546 [($fl= m 0.0)
3547 (die who "division by 0.0")]
3548 [(flonum? n) (fldiv-and-mod n m)]
3549 [(fixnum? n)
3550 (fldiv-and-mod (fixnum->flonum n) m)]
3551 [(or (bignum? n) (ratnum? n))
3552 (let ([v ($flonum->exact m)])
3553 (unless v
3554 (die who "invalid argument" m))
3555 (let-values ([(a b) (div-and-mod* n v who)])
3556 (values (inexact a) (inexact b))))]
3557 [else (die who "not a number" n)])]
3558 [else (die who "not a number" m)]))
3559
3560 (define (div-and-mod n m)
3561 (div-and-mod* n m 'div-and-mod))
3562
3563 (define (div n m)
3564 (import (ikarus system $fx))
3565 (cond
3566 [(and (fixnum? n) (fixnum? m))
3567 (cond
3568 [(eq? m 0) (die 'div "division by 0")]
3569 [(eq? m -1) (- n)]
3570 [else
3571 (let ([d0 ($fxquotient n m)])
3572 (if ($fx>= n ($fx* d0 m))
3573 d0
3574 (if ($fx>= m 0)
3575 ($fx- d0 1)
3576 ($fx+ d0 1))))])]
3577 [else
3578 (let-values ([(a b) (div-and-mod* n m 'div)])
3579 a)]))
3580
3581 (define (mod n m)
3582 (import (ikarus system $fx))
3583 (cond
3584 [(and (fixnum? n) (fixnum? m))
3585 (cond
3586 [(eq? m 0) (die 'mod "division by 0")]
3587 [else
3588 (let ([d0 ($fxquotient n m)])
3589 (let ([m0 ($fx- n ($fx* d0 m))])
3590 (if ($fx>= m0 0)
3591 m0
3592 (if ($fx>= m 0)
3593 ($fx+ m0 m)
3594 ($fx- m0 m)))))])]
3595 [else
3596 (let-values ([(a b) (div-and-mod* n m 'mod)])
3597 b)]))
3598
3599 (define (div0-and-mod0 x y)
3600 (let-values ([(d m) (div-and-mod* x y 'div0-and-mod0)])
3601 (if (> y 0)
3602 (if (< m (/ y 2))
3603 (values d m)
3604 (values (+ d 1) (- m y)))
3605 (if (>= m (/ y -2))
3606 (values (- d 1) (+ m y))
3607 (values d m)))))
3608
3609 (define (div0 x y)
3610 (let-values ([(d m) (div-and-mod* x y 'div0)])
3611 (if (> y 0)
3612 (if (< m (/ y 2))
3613 d
3614 (+ d 1))
3615 (if (>= m (/ y -2))
3616 (- d 1)
3617 d))))
3618
3619 (define (mod0 x y)
3620 (let-values ([(d m) (div-and-mod* x y 'mod0)])
3621 (if (> y 0)
3622 (if (< m (/ y 2))
3623 m
3624 (- m y))
3625 (if (>= m (/ y -2))
3626 (+ m y)
3627 m))))
3628 )
3629
3630 (library (ikarus flonums div-and-mod)
3631 (export fldiv flmod fldiv-and-mod fldiv0 flmod0 fldiv0-and-mod0)
3632 (import
3633 (ikarus system $flonums)
3634 (ikarus system $fx)
3635 (except (ikarus)
3636 fldiv flmod fldiv-and-mod fldiv0 flmod0 fldiv0-and-mod0))
3637
3638 (define ($flmod n m)
3639 (let ([d0 (fltruncate ($fl/ n m))])
3640 (let ([m0 ($fl- n ($fl* d0 m))])
3641 (if ($fl>= m0 0.0)
3642 m0
3643 (if ($fl>= m 0.0)
3644 ($fl+ m0 m)
3645 ($fl- m0 m))))))
3646
3647 (define ($fldiv n m)
3648 (let ([d0 (fltruncate ($fl/ n m))])
3649 (if ($fl>= n ($fl* d0 m))
3650 d0
3651 (if ($fl>= m 0.0)
3652 ($fl- d0 1.0)
3653 ($fl+ d0 1.0)))))
3654
3655 (define ($fldiv-and-mod n m)
3656 (let ([d0 (fltruncate ($fl/ n m))])
3657 (let ([m0 ($fl- n ($fl* d0 m))])
3658 (if ($fl>= m0 0.0)
3659 (values d0 m0)
3660 (if ($fl>= m 0.0)
3661 (values ($fl- d0 1.0) ($fl+ m0 m))
3662 (values ($fl+ d0 1.0) ($fl- m0 m)))))))
3663
3664 (define (fldiv n m)
3665 (if (flonum? n)
3666 (if (flonum? m)
3667 ($fldiv n m)
3668 (die 'fldiv "not a flonum" m))
3669 (die 'fldiv "not a flonum" n)))
3670
3671 (define (flmod n m)
3672 (if (flonum? n)
3673 (if (flonum? m)
3674 ($flmod n m)
3675 (die 'flmod "not a flonum" m))
3676 (die 'flmod "not a flonum" n)))
3677
3678 (define (fldiv-and-mod n m)
3679 (if (flonum? n)
3680 (if (flonum? m)
3681 ($fldiv-and-mod n m)
3682 (die 'fldiv-and-mod "not a flonum" m))
3683 (die 'fldiv-and-mod "not a flonum" n)))
3684
3685 (define ($fldiv0-and-mod0 n m)
3686 (let ([d0 (fltruncate ($fl/ n m))])
3687 (let ([m0 ($fl- n ($fl* d0 m))])
3688 (if ($fl>= m 0.0)
3689 (if ($fl< m0 ($fl/ m 2.0))
3690 (if ($fl>= m0 ($fl/ m -2.0))
3691 (values d0 m0)
3692 (values ($fl- d0 1.0) ($fl+ m0 m)))
3693 (values ($fl+ d0 1.0) ($fl- m0 m)))
3694 (if ($fl< m0 ($fl/ m -2.0))
3695 (if ($fl>= m0 ($fl/ m 2.0))
3696 (values d0 m0)
3697 (values ($fl+ d0 1.0) ($fl- m0 m)))
3698 (values ($fl- d0 1.0) ($fl+ m0 m)))))))
3699
3700 (define ($fldiv0 n m)
3701 (let ([d0 (fltruncate ($fl/ n m))])
3702 (let ([m0 ($fl- n ($fl* d0 m))])
3703 (if ($fl>= m 0.0)
3704 (if ($fl< m0 ($fl/ m 2.0))
3705 (if ($fl>= m0 ($fl/ m -2.0))
3706 d0
3707 ($fl- d0 1.0))
3708 ($fl+ d0 1.0))
3709 (if ($fl< m0 ($fl/ m -2.0))
3710 (if ($fl>= m0 ($fl/ m 2.0))
3711 d0
3712 ($fl+ d0 1.0))
3713 ($fl- d0 1.0))))))
3714
3715 (define ($flmod0 n m)
3716 (let ([d0 (fltruncate ($fl/ n m))])
3717 (let ([m0 ($fl- n ($fl* d0 m))])
3718 (if ($fl>= m 0.0)
3719 (if ($fl< m0 ($fl/ m 2.0))
3720 (if ($fl>= m0 ($fl/ m -2.0))
3721 m0
3722 ($fl+ m0 m))
3723 ($fl- m0 m))
3724 (if ($fl< m0 ($fl/ m -2.0))
3725 (if ($fl>= m0 ($fl/ m 2.0))
3726 m0
3727 ($fl- m0 m))
3728 ($fl+ m0 m))))))
3729
3730 (define (fldiv0 n m)
3731 (if (flonum? n)
3732 (if (flonum? m)
3733 ($fldiv0 n m)
3734 (die 'fldiv0 "not a flonum" m))
3735 (die 'fldiv0 "not a flonum" n)))
3736
3737 (define (flmod0 n m)
3738 (if (flonum? n)
3739 (if (flonum? m)
3740 ($flmod0 n m)
3741 (die 'flmod0 "not a flonum" m))
3742 (die 'flmod0 "not a flonum" n)))
3743
3744 (define (fldiv0-and-mod0 n m)
3745 (if (flonum? n)
3746 (if (flonum? m)
3747 ($fldiv0-and-mod0 n m)
3748 (die 'fldiv0-and-mod0 "not a flonum" m))
3749 (die 'fldiv0-and-mod0 "not a flonum" n))))
3750
3751 (library (ikarus bitwise misc)
3752 (export fxfirst-bit-set bitwise-bit-set? bitwise-first-bit-set
3753 fxbit-count bitwise-bit-count
3754 fxlength
3755 fxbit-set?
3756 fxcopy-bit
3757 fxcopy-bit-field fxrotate-bit-field fxreverse-bit-field
3758 fxbit-field)
3759 (import
3760 (ikarus system $fx)
3761 (ikarus system $bignums)
3762 (ikarus system $flonums)
3763 (except (ikarus)
3764 fxfirst-bit-set bitwise-bit-set? bitwise-first-bit-set
3765 fxbit-count bitwise-bit-count
3766 fxlength
3767 fxbit-set?
3768 fxcopy-bit
3769 fxcopy-bit-field fxrotate-bit-field fxreverse-bit-field
3770 fxbit-field))
3771
3772 (module (bitwise-first-bit-set fxfirst-bit-set)
3773 (define (byte-first-bit-set x i)
3774 (import (ikarus system $bytevectors))
3775 (define-syntax make-first-bit-set-bytevector
3776 (lambda (x)
3777 (define (fst n)
3778 (cond
3779 [(odd? n) 0]
3780 [else (+ 1 (fst (bitwise-arithmetic-shift-right n 1)))]))
3781 (u8-list->bytevector
3782 (cons 0 #| not used |#
3783 (let f ([i 1])
3784 (cond
3785 [(= i 256) '()]
3786 [else (cons (fst i) (f (+ i 1)))]))))))
3787 (define bv (make-first-bit-set-bytevector))
3788 ($fx+ i ($bytevector-u8-ref bv x)))
3789 (define ($fxloop x i)
3790 (let ([y ($fxlogand x 255)])
3791 (if ($fx= y 0)
3792 ($fxloop ($fxsra x 8) ($fx+ i 8))
3793 (byte-first-bit-set y i))))
3794 (define ($bnloop x i idx)
3795 (let ([b ($bignum-byte-ref x idx)])
3796 (if ($fxzero? b)
3797 ($bnloop x ($fx+ i 8) ($fx+ idx 1))
3798 (byte-first-bit-set b i))))
3799 (define ($fxfirst-bit-set x)
3800 (if ($fx> x 0)
3801 ($fxloop x 0)
3802 (if ($fx= x 0)
3803 -1
3804 (if ($fx> x (least-fixnum))
3805 ($fxloop ($fx- 0 x) 0)
3806 ($bnloop (- x) 0 0)))))
3807 (define (fxfirst-bit-set x)
3808 (cond
3809 [(fixnum? x)
3810 ($fxfirst-bit-set x)]
3811 [else (die 'fxfirst-bit-set "not a fixnum" x)]))
3812 (define (bitwise-first-bit-set x)
3813 (cond
3814 [(fixnum? x)
3815 ($fxfirst-bit-set x)]
3816 [(bignum? x) ($bnloop x 0 0)]
3817 [else (die 'bitwise-first-bit-set "not an exact integer" x)])))
3818
3819 (module (fxbit-count bitwise-bit-count)
3820 (define (pos-fxbitcount n)
3821 ;;; nifty parrallel count from:
3822 ;;; http://infolab.stanford.edu/~manku/bitcount/bitcount.html
3823 (case (fixnum-width)
3824 [(30)
3825 (let ([m0 #x15555555]
3826 [m1 #x13333333]
3827 [m2 #x0f0f0f0f])
3828 (let* ([n ($fx+ ($fxlogand n m0) ($fxlogand ($fxsra n 1) m0))]
3829 [n ($fx+ ($fxlogand n m1) ($fxlogand ($fxsra n 2) m1))]
3830 [n ($fx+ ($fxlogand n m2) ($fxlogand ($fxsra n 4) m2))])
3831 ($fxmodulo n 255)))]
3832 [else
3833 (let ([m0 #x0555555555555555]
3834 [m1 #x0333333333333333]
3835 [m2 #x0f0f0f0f0f0f0f0f]
3836 [m3 #x00ff00ff00ff00ff])
3837 (let* ([n ($fx+ ($fxlogand n m0) ($fxlogand ($fxsra n 1) m0))]
3838 [n ($fx+ ($fxlogand n m1) ($fxlogand ($fxsra n 2) m1))]
3839 [n ($fx+ ($fxlogand n m2) ($fxlogand ($fxsra n 4) m2))]
3840 [n ($fx+ ($fxlogand n m3) ($fxlogand ($fxsra n 8) m3))])
3841 ($fxmodulo n 255)))]))
3842 (define ($fxbitcount n)
3843 (if ($fx< n 0)
3844 (fxlognot (pos-fxbitcount (fxlognot n)))
3845 (pos-fxbitcount n)))
3846 (define (bnbitcount n)
3847 (define (poscount x idx c)
3848 (let ([c (+ c
3849 ($fx+ (pos-fxbitcount
3850 ($fxlogor
3851 ($fxsll ($bignum-byte-ref x ($fx+ idx 3)) 8)
3852 ($bignum-byte-ref x ($fx+ idx 2))))
3853 (pos-fxbitcount
3854 ($fxlogor
3855 ($fxsll ($bignum-byte-ref x ($fxadd1 idx)) 8)
3856 ($bignum-byte-ref x idx)))))])
3857 (if ($fx= idx 0)
3858 c
3859 (poscount x ($fx- idx 4) c))))
3860 (if ($bignum-positive? n)
3861 (poscount n ($fx- ($bignum-size n) 4) 0)
3862 (let ([n (bitwise-not n)])
3863 (bitwise-not (poscount n ($fx- ($bignum-size n) 4) 0)))))
3864 (define (fxbit-count n)
3865 (cond
3866 [(fixnum? n) ($fxbitcount n)]
3867 [else (die 'fxbit-count "not a fixnum" n)]))
3868 (define (bitwise-bit-count n)
3869 (cond
3870 [(fixnum? n) ($fxbitcount n)]
3871 [(bignum? n) (bnbitcount n)]
3872 [else (die 'bitwise-bit-count "not an exact integer" n)])))
3873
3874 (define (fxlength x)
3875 (define (fxlength32 x)
3876 (let ([fl ($fixnum->flonum x)])
3877 (let ([sbe ($fxlogor
3878 ($fxsll ($flonum-u8-ref fl 0) 4)
3879 ($fxsra ($flonum-u8-ref fl 1) 4))])
3880 (cond
3881 [($fx= sbe 0) 0]
3882 [else ($fx- sbe 1022)]))))
3883 (define (fxlength64 x)
3884 (if ($fx> x #x7FFFFFFF)
3885 ($fx+ 31 (fxlength32 ($fxsra x 31)))
3886 (fxlength32 x)))
3887 (if (fixnum? x)
3888 (case (fixnum-width)
3889 [(30)
3890 (fxlength32 (if ($fx< x 0) ($fxlognot x) x))]
3891 [else
3892 (fxlength64 (if ($fx< x 0) ($fxlognot x) x))])
3893 (die 'fxlength "not a fixnum" x)))
3894
3895 (define (fxbit-set? x i)
3896 (define who 'fxbit-set?)
3897 (if (fixnum? x)
3898 (if (fixnum? i)
3899 (if (and ($fx<= 0 i) ($fx< i (fixnum-width)))
3900 (not ($fxzero? ($fxlogand ($fxsra x i) 1)))
3901 (die who "index out of range" i))
3902 (die who "index is not a fixnum" i))
3903 (die who "not a fixnum" x)))
3904
3905 (define (bitwise-bit-set? x i)
3906 (define who 'bitwise-bit-set?)
3907 (cond
3908 [(fixnum? i)
3909 (when ($fx< i 0)
3910 (die who "index must be non-negative" i))
3911 (cond
3912 [(fixnum? x)
3913 (if ($fx< i (fixnum-width))
3914 ($fx= ($fxlogand ($fxsra x i) 1) 1)
3915 ($fx< x 0))]
3916 [(bignum? x)
3917 (let ([n ($bignum-size x)])
3918 (let ([m ($fx* n 8)])
3919 (if ($fx< m i)
3920 (not ($bignum-positive? x))
3921 (if ($bignum-positive? x)
3922 (let ([b ($bignum-byte-ref x ($fxsra i 3))])
3923 ($fx= ($fxlogand ($fxsra b ($fxlogand i 7)) 1) 1))
3924 (= 1 (bitwise-and
3925 (bitwise-arithmetic-shift-right x i)
3926 1))))))]
3927 [else (die who "not an exact integer" x)])]
3928 [(bignum? i)
3929 (unless ($bignum-positive? i)
3930 (die who "index must be non-negative"))
3931 (cond
3932 [(fixnum? x) ($fx< x 0)]
3933 [(bignum? x)
3934 (= 1 (bitwise-and (bitwise-arithmetic-shift-right x i) 1))]
3935 [else (die who "not an exact integer" x)])]
3936 [else
3937 (die who "index is not an exact integer" i)]))
3938
3939
3940 (define (fxcopy-bit x i b)
3941 (define who 'fxcopy-bit)
3942 (if (fixnum? x)
3943 (if (fixnum? i)
3944 (if (and ($fx<= 0 i) ($fx< i (fixnum-width)))
3945 (case b
3946 [(0) ($fxlogand x ($fxlognot ($fxsll 1 i)))]
3947 [(1) ($fxlogor x ($fxsll 1 i))]
3948 [else (die who "invalid bit value" b)])
3949 (die who "index out of range" i))
3950 (die who "index is not a fixnum" i))
3951 (die who "not a fixnum" x)))
3952
3953 (define (fxcopy-bit-field x i j b)
3954 (define who 'fxcopy-bit-field)
3955 (if (fixnum? x)
3956 (if (fixnum? i)
3957 (if ($fx<= 0 i)
3958 (if (fixnum? j)
3959 (if ($fx< j (fixnum-width))
3960 (if ($fx<= i j)
3961 (if (fixnum? b)
3962 (let ([m
3963 ($fxlogxor
3964 ($fxsub1 ($fxsll 1 i))
3965 ($fxsub1 ($fxsll 1 j)))])
3966 ($fxlogor
3967 ($fxlogand m ($fxsll b i))
3968 ($fxlogand ($fxlognot m) x)))
3969 (die who "not a fixnum" b))
3970 (if ($fx<= 0 j)
3971 (die who "index out of range" j)
3972 (die who "indices not in order" i j)))
3973 (die who "index out of range" j))
3974 (die who "not a fixnum" j))
3975 (die who "index out of range" i))
3976 (die who "not a fixnum" i))
3977 (die who "not a fixnum" x)))
3978
3979 (define ($fxrotate-bit-field x i j c w)
3980 (let ([m ($fxsll ($fxsub1 ($fxsll 1 w)) i)])
3981 (let ([x0 ($fxlogand x m)])
3982 (let ([lt ($fxsll x0 c)] [rt ($fxsra x0 ($fx- w c))])
3983 (let ([x0 ($fxlogand ($fxlogor lt rt) m)])
3984 ($fxlogor x0 ($fxlogand x ($fxlognot m))))))))
3985
3986 (define (fxrotate-bit-field x i j c)
3987 (define who 'fxrotate-bit-field)
3988 (if (fixnum? x)
3989 (if (fixnum? i)
3990 (if ($fx>= i 0)
3991 (if (fixnum? j)
3992 (if ($fx< j (fixnum-width))
3993 (let ([w ($fx- j i)])
3994 (if ($fx>= w 0)
3995 (if (fixnum? c)
3996 (if (and ($fx>= c 0) ($fx< c w))
3997 ($fxrotate-bit-field x i j c w)
3998 (die who "count is invalid" c))
3999 (die who "count is not a fixnum" c))
4000 (die who "field width is negative" i j)))
4001 (die who "end index is out of range" j))
4002 (die who "end index is not a fixnum" j))
4003 (die who "start index is out of range" i))
4004 (die who "start index is not a fixnum" i))
4005 (die who "not a fixnum" x)))
4006
4007 (define (fxreverse-bit-field v start end)
4008
4009 (define (%fxreverse-bit-field30 v)
4010 (assert (= (fixnum-width) 30))
4011 (let* ( ;; Swap pairs of bits
4012 (tmp1 (fxarithmetic-shift-right (fxand v #b10000000000000000000000000000) 28))
4013 (v (fxior (fxarithmetic-shift-right (fxand v #b01010101010101010101010101010) 1)
4014 (fxarithmetic-shift-left (fxand v #b00101010101010101010101010101) 1)))
4015 ;; Swap 2-bit fields
4016 (v (fxior (fxarithmetic-shift-right (fxand v #b01100110011001100110011001100) 2)
4017 (fxarithmetic-shift-left (fxand v #b10011001100110011001100110011) 2)))
4018 ;; Swap 4-bit fields
4019 (tmp2 (fxarithmetic-shift-right (fxand v #b01111000000000000000000000000) 23))
4020 (v (fxior (fxarithmetic-shift-right (fxand v #b10000111100001111000011110000) 4)
4021 (fxarithmetic-shift-left (fxand v #b00000000011110000111100001111) 4)))
4022 ;; Swap bytes
4023 (tmp3 (fxarithmetic-shift-right (fxand v #b00000111111110000000000000000) 11))
4024 (v (fxior (fxarithmetic-shift-right (fxand v #b11111000000001111111100000000) 8)
4025 (fxarithmetic-shift-left (fxand v #b00000000000000000000011111111) 8))))
4026 ;; Put together the pieces
4027 (fxior (fxarithmetic-shift-left v 13)
4028 tmp1 tmp2 tmp3)))
4029
4030 (define who 'fxreverse-bit-field)
4031 (unless (fixnum? v)
4032 (assertion-violation who "expected fixnum as first argument" v))
4033 (unless (and (integer? start) (exact? start))
4034 (assertion-violation who "expected exact integer as second argument" start))
4035 (unless (and (integer? end) (exact? end))
4036 (assertion-violation who "expected exact integer as third argument" end))
4037 (unless (< -1 start (fixnum-width))
4038 (assertion-violation who
4039 (string-append "expected second argument between zero (included) and fixnum width "
4040 (number->string (fixnum-width)))
4041 start))
4042 (unless (< -1 end (fixnum-width))
4043 (assertion-violation who
4044 (string-append "expected third argument between zero (included) and fixnum width "
4045 (number->string (fixnum-width)))
4046 end))
4047 (unless (<= 0 start end)
4048 (assertion-violation who
4049 "expected second argument between zero (included) and third argument"
4050 start end))
4051
4052 ;;We have FIXNUM-WIDTH equal to 30.
4053 (fxior (fxarithmetic-shift-right
4054 (%fxreverse-bit-field30 (fxbit-field v start end))
4055 (fx- 29 end))
4056 (fxcopy-bit-field v start end 0)))
4057
4058
4059 (define (fxbit-field x i j)
4060 (define who 'fxbit-field)
4061 (if (fixnum? x)
4062 (if (fixnum? i)
4063 (if ($fx<= 0 i)
4064 (if (fixnum? j)
4065 (if ($fx< j (fixnum-width))
4066 (if ($fx<= i j)
4067 ($fxsra
4068 ($fxlogand x ($fxsub1 ($fxsll 1 j)))
4069 i)
4070 (if ($fx<= 0 j)
4071 (die who "index out of range" j)
4072 (die who "indices not in order" i j)))
4073 (die who "index out of range" j))
4074 (die who "not a fixnum" j))
4075 (die who "index out of range" i))
4076 (die who "not a fixnum" i))
4077 (die who "not a fixnum" x)))
4078
4079 )
4080
4081
4082 (library (ikarus complex-numbers)
4083 (export make-rectangular $make-rectangular make-polar
4084 real-part imag-part angle magnitude)
4085 (import
4086 (except (ikarus) make-rectangular make-polar
4087 real-part imag-part angle magnitude)
4088 (except (ikarus system $compnums) $make-rectangular))
4089
4090 (define ($make-rectangular r i)
4091 ;;; should be called with 2 exacts
4092 (if (eqv? i 0)
4093 r
4094 ($make-compnum r i)))
4095
4096 (define (make-rectangular r i)
4097 (define who 'make-rectangular)
4098 (define (err x)
4099 (die who "invalid argument" x))
4100 (cond
4101 [(flonum? i)
4102 (cond
4103 [(flonum? r) ($make-cflonum r i)]
4104 [(or (fixnum? r) (bignum? r) (ratnum? r))
4105 ($make-cflonum (inexact r) i)]
4106 [else (err r)])]
4107 [(eqv? i 0) (if (number? r) r (err r))]
4108 [(or (fixnum? i) (bignum? i) (ratnum? i))
4109 (cond
4110 [(or (fixnum? r) (bignum? r) (ratnum? r))
4111 ($make-rectangular r i)]
4112 [(flonum? r)
4113 ($make-cflonum r (inexact i))]
4114 [else (err r)])]
4115 [else (err i)]))
4116
4117 (define (make-polar mag angle)
4118 (define who 'make-polar)
4119 (unless (real? mag)
4120 (die who "not a real number" mag))
4121 (unless (real? angle)
4122 (die who "not a real number" angle))
4123 (make-rectangular
4124 (* mag (cos angle))
4125 (* mag (sin angle))))
4126
4127 (define magnitude
4128 (lambda (x)
4129 (cond
4130 [(or (fixnum? x) (bignum? x) (ratnum? x) (flonum? x))
4131 (abs x)]
4132 [(compnum? x)
4133 (let ([r ($compnum-real x)]
4134 [i ($compnum-imag x)])
4135 (sqrt (+ (* r r) (* i i))))]
4136 [(cflonum? x)
4137 (let ([r ($cflonum-real x)]
4138 [i ($cflonum-imag x)])
4139 (sqrt (+ (* r r) (* i i))))]
4140 [else
4141 (die 'magnitude "not a number" x)])))
4142
4143 (define angle
4144 (lambda (x)
4145 (import (ikarus system $bignums) (ikarus system $ratnums))
4146 (define PI (acos -1))
4147 (cond
4148 [(fixnum? x)
4149 (if (fx>? x 0)
4150 0
4151 (if (fx<? x 0)
4152 PI
4153 (die 'angle "undefined for 0")))]
4154 [(bignum? x)
4155 (if ($bignum-positive? x) 0 PI)]
4156 [(ratnum? x)
4157 (let ([n ($ratnum-n x)])
4158 (if (> n 0) 0 PI))]
4159 [(flonum? x)
4160 (atan 0.0 x)]
4161 [(compnum? x)
4162 (let ([r ($compnum-real x)]
4163 [i ($compnum-imag x)])
4164 (atan i r))]
4165 [(cflonum? x)
4166 (let ([r ($cflonum-real x)]
4167 [i ($cflonum-imag x)])
4168 (atan i r))]
4169 [else
4170 (die 'angle "not a number" x)])))
4171
4172 (define real-part
4173 (lambda (x)
4174 (cond
4175 [(fixnum? x) x]
4176 [(bignum? x) x]
4177 [(ratnum? x) x]
4178 [(flonum? x) x]
4179 [(compnum? x) ($compnum-real x)]
4180 [(cflonum? x) ($cflonum-real x)]
4181 [else
4182 (die 'real-part "not a number" x)])))
4183
4184 (define imag-part
4185 (lambda (x)
4186 (cond
4187 [(fixnum? x) 0]
4188 [(bignum? x) 0]
4189 [(ratnum? x) 0]
4190 [(flonum? x) 0]
4191 [(compnum? x) ($compnum-imag x)]
4192 [(cflonum? x) ($cflonum-imag x)]
4193 [else
4194 (die 'imag-part "not a number" x)])))
4195 )
4196
4197 (library (ikarus system flonums)
4198 (export $fixnum->flonum)
4199 (import (ikarus))
4200 (define $fixnum->flonum fixnum->flonum))