forked from cisco/ChezScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_3.ss
2976 lines (2741 loc) · 102 KB
/
5_3.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"5_3.ss"
;;; 5_3.ss
;;; Copyright 1984-2016 Cisco Systems, Inc.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Care must be take with floating point constants to permit cross
;;; compilation between machines with differing floating point styles.
;;; Negative zero, infinities, large or small numbers, non-binary
;;; fractions, and precise numbers are dangerous and should be calculated.
;;; positive zero, NAN, small integers, and binary fractions with only a few
;;; significant bits are safe on all current machines.
;;; examples:
;;; dangerous: -0.0, +inf.0, -inf.0, 1e100, 1e-100, 0.1
;;; safe: 0.0, +nan.0, 1.0, 2.0, 0.5
(eval-when (compile)
(define-constant max-float-exponent
(float-type-case
[(ieee) 1023]))
(define-constant min-float-exponent
(float-type-case
[(ieee) -1023]))
)
(let ()
; could use foreign-entry? primitive if foreign.ss were loaded first
(define op-if-entry?
(let ()
(define lookup
(foreign-procedure "(cs)lookup_foreign_entry" (string)
void*))
(lambda (op name)
(and (not (eqv? (lookup name) 0))
(op name)))))
(let ()
(define cflop1
(lambda (x)
(foreign-procedure x (double-float) double-float)))
(define cflop2
(lambda (x)
(foreign-procedure x (double-float double-float) double-float)))
(define schemeop1
(lambda (x)
(foreign-procedure x (scheme-object) scheme-object)))
(define schemeop2
(lambda (x)
(foreign-procedure x (scheme-object scheme-object) scheme-object)))
(let ()
(define biglength (schemeop1 "(cs)s_integer_length"))
(define bigodd? (schemeop1 "(cs)s_bigoddp"))
(define float (schemeop1 "(cs)s_float"))
(define big=
(foreign-procedure "(cs)s_big_eq" (scheme-object scheme-object)
boolean))
(define big<
(foreign-procedure "(cs)s_big_lt" (scheme-object scheme-object)
boolean))
(define integer-ash (schemeop2 "(cs)s_ash"))
(define integer+ (schemeop2 "(cs)add"))
(define integer* (schemeop2 "(cs)mul"))
(define integer- (schemeop2 "(cs)sub"))
(define integer/ (schemeop2 "(cs)s_div"))
(define intquotient (schemeop2 "(cs)ss_trunc"))
(define intquotient-remainder (schemeop2 "(cs)ss_trunc_rem"))
(define intremainder (schemeop2 "(cs)rem"))
(define $flsin (cflop1 "(cs)sin"))
(define $flcos (cflop1 "(cs)cos"))
(define $flasin (cflop1 "(cs)asin"))
(define $flacos (cflop1 "(cs)acos"))
(define $flfloor (cflop1 "(cs)floor"))
(define $flceiling (cflop1 "(cs)ceil"))
(let ()
(define omega
(float-type-case
[(ieee) (float #e1.7976931348623157e308)]))
(define $flexpt
(machine-case
[(i3nt ti3nt a6s2 ta6s2 i3s2 ti3s2 i3nb ti3nb a6nb ta6nb)
; pow(nan,+0.0) => nan instead of +1.0
(let ([cexpt (cflop2 "(cs)pow")])
(lambda (x y)
(cond
[(fl= y 0.0) 1.0]
[else (cexpt x y)])))]
[else (cflop2 "(cs)pow")]))
(define $fltan (cflop1 "(cs)tan"))
(define flcosh (cflop1 "(cs)cosh"))
(define fltanh
(machine-case
[(i3fb ti3fb)
; broken for -0.0, +/-inf
(let ([ctanh (cflop1 "(cs)tanh")])
(lambda (x)
(cond
[(fl= x 0.0) x]
[(infinity? x) (if (negated-flonum? x) -1.0 1.0)]
[else (ctanh x)])))]
[(i3nb ti3nb a6nb ta6nb)
; broken for -0.0
(let ([ctanh (cflop1 "(cs)tanh")])
(lambda (x)
(cond
[(fl= x 0.0) x]
[else (ctanh x)])))]
[else (cflop1 "(cs)tanh")]))
(define $flexp (cflop1 "(cs)exp"))
(define $fllog
(machine-case
[(a6s2 ta6s2 i3s2 ti3s2 i3ob ti3ob i3nb ti3nb a6nb ta6nb a6ob ta6ob)
; broken for -inf.0
(let ([clog (cflop1 "(cs)log")])
(lambda (x) (if (and (infinity? x) (negated-flonum? x)) +nan.0 (clog x))))]
[else (cflop1 "(cs)log")]))
(define $flsqrt (cflop1 "(cs)sqrt"))
(define flatan2
(machine-case
[(i3nt ti3nt)
; atan2(+inf.0,+inf.0) => pi/2 instead of pi/4
; atan2(-inf.0,-inf.0) => -pi/2 instead of -3pi/4
; atan2(+inf.0,-inf.0) => NAN instead of 3pi/4
; atan2(-inf.0,+inf.0) => NAN instead of -pi/4
; atan2(+0.0,-0.0) => +0.0 instead of +pi
; atan2(-0.0,-0.0) => -0.0 instead of -pi
; atan2(-0.0,-1.0) => pi instead of -pi
(let ([catan2 (cflop2 "(cs)atan2")])
(let ([pi (catan2 0.0 -1.0)])
(lambda (y x)
(cond
[(and (infinity? y) (infinity? x))
(let ([y (if (negated-flonum? y) -1.0 1.0)]
[x (if (negated-flonum? x) -1.0 1.0)])
(catan2 y x))]
[(and (fl= y 0.0) (not ($nan? x)))
(if (negated-flonum? y)
(if (negated-flonum? x) (fl- pi) (fl- 0.0))
(if (negated-flonum? x) pi 0.0))]
[else (catan2 y x)]))))]
[(i3ob ti3ob a6ob ta6ob a6s2 ta6s2 i3s2 ti3s2 i3nb ti3nb a6nb ta6nb)
; atan2(-0.0,+0.0) => +0.0 instead of -0.0
; atan2(+0.0,-0.0) => +0.0 instead of +pi
; atan2(-0.0,-0.0) => +0.0 instead of -pi
(let ([catan2 (cflop2 "(cs)atan2")])
(let ([pi (catan2 0.0 -1.0)])
(lambda (y x)
(cond
[(and (fl= y 0.0) (not ($nan? x)))
(if (negated-flonum? y)
(if (negated-flonum? x) (fl- pi) (fl- 0.0))
(if (negated-flonum? x) pi 0.0))]
[else (catan2 y x)]))))]
[else (cflop2 "(cs)atan2")]))
(define $flatan (cflop1 "(cs)atan"))
(define flsinh (cflop1 "(cs)sinh"))
(define flatanh
(or (op-if-entry? cflop1 "(cs)atanh")
; |x| <= 1
; principle expression:
; (log(1+x)-log(1-x))/2
; should use "log1p" but it doesn't exist on the 88k
(let ([f (lambda (x)
(fl* 0.5 (fl- ($fllog (fl+ 1.0 x)) ($fllog (fl- 1.0 x)))))])
(lambda (x)
(if (negated-flonum? x) (fl- (f (fl- x))) (f x))))))
(define fllog1+
(or (op-if-entry? cflop1 "(cs)log1p")
(lambda (x) ($fllog (fl+ 1.0 x)))))
(let ()
(define log2 ($fllog 2.0))
(define flhypot (cflop2 "(cs)hypot"))
(define flasinh
; scheme-coded version needs "log2"
(or (op-if-entry? cflop1 "(cs)asinh")
; prinicple expression:
; log(x + sqrt(xx + 1))
; avoids spurious overflows
; avoids underflow problems from negative x by using identity
; asinh(-x) = -asinh(x)
; should use "log1p" for small x but it doesn't exist on the 88k
(let ([f (lambda (x)
(if (fl= (fl+ x 1.0) x)
(fl+ ($fllog x) log2)
($fllog (fl+ x ($flsqrt (fl+ (fl* x x) 1.0))))))])
(lambda (x)
(if (negated-flonum? x) (fl- (f (fl- x))) (f x))))))
(define flacosh
; scheme-coded version needs "log2"
(or (op-if-entry? cflop1 "(cs)acosh")
; x >= 1
; prinicple expression:
; log(x + sqrt(xx - 1))
; avoids spurious overflows
(lambda (x)
(if (fl= (fl- x 1.0) x)
(fl+ ($fllog x) log2)
($fllog (fl+ x ($flsqrt (fl- (fl* x x) 1.0))))))))
(let ()
(define pi (flatan2 0.0 -1.0))
(define sqrt-omega ($flsqrt omega))
(define log-omega ($fllog omega))
(define acosh-omega (flacosh omega))
(let ()
(define-syntax define-trig-op
(syntax-rules ()
[(_ who flop cflop zero-value)
(set! who
(lambda (x)
(type-case x
[(flonum?) (flop x)]
[($inexactnum?) (cflop x)]
[(fixnum?) (if (fx= x 0) zero-value (who (fixnum->flonum x)))]
[(bignum? ratnum? $exactnum?) (who (inexact x))]
[else (nonnumber-error 'who x)])))]))
(define $flinteger-or-inf?
(lambda (x)
(fl= ($flfloor x) x)))
(define $flinteger?
(lambda (x)
(and ($flinteger-or-inf? x)
(not (exceptional-flonum? x)))))
(define nonnumber-error
(lambda (who what)
($oops who "~s is not a number" what)))
(define noncomplex-error
(lambda (who what)
($oops who "~s is not a complex number" what)))
(define nonreal-error
(lambda (who what)
($oops who "~s is not a real number" what)))
(define nonrational-error
(lambda (who what)
($oops who "~s is not a rational number" what)))
(define noninteger-error
(lambda (who what)
($oops who "~s is not an integer" what)))
(define nonexact-integer-error
(lambda (who what)
($oops who "~s is not an exact integer" what)))
(define noncflonum-error
(lambda (who what)
($oops who "~s is not a cflonum" what)))
(define domain-error
(lambda (who what)
($oops who "undefined for ~s" what)))
(define domain-error2
(lambda (who x y)
($oops who "undefined for values ~s and ~s" x y)))
; note: (cfl*i z) =/= (* +i z) if RP(z) == -0.0
(define cfl*i
(lambda (z)
(fl-make-rectangular (fl- (cfl-imag-part z)) (cfl-real-part z))))
; note: (cfl/i z) =/= (/ z +i) or (* -i z) if IP(z) == -0.0
(define cfl/i
(lambda (z)
(fl-make-rectangular (cfl-imag-part z) (fl- (cfl-real-part z)))))
; Some of the following is based on
; W. Kahan's "Branch Cuts for Complex Elementary Functions"
; in "The State of the Art of Numerical Analysis"
; (IMA/SIAM proceedings, 1986, pp 165-211)
; ed. by A. Iserles and M.J.D. Powell
; Kahan gives principal expressions and algorithms for several
; complex functions. The principal expressions are mathematically
; correct, but not necessarily good computationally. They
; do, however, make good test expressions for ordinary inputs.
; Steele's "Common Lisp: the Language" (second edition) was used
; to determine valid domains for some of the functions.
(define cflmagnitude
(lambda (z)
(flhypot (cfl-real-part z) (cfl-imag-part z))))
(define cflangle
(lambda (z)
(flatan2 (cfl-imag-part z) (cfl-real-part z))))
(define cfllog
; principal expression from Kahan:
; log(z) = log(|z|) + angle(z)i
; Kahan uses a different algorithm to calculate the real part.
(let ([f (lambda (x y)
; x >= y
(let ([r (fl/ y x)])
(fl+ ($fllog x) (fl* .5 (fllog1+ (fl* r r))))))]
[k (fl* .5 log2)])
(lambda (z)
(let ([x (cfl-real-part z)] [y (cfl-imag-part z)])
(fl-make-rectangular
(let ([x (flabs x)] [y (flabs y)])
(cond
[(fl> x y) (f x y)]
[(fl< x y) (f y x)]
[(fl= x y) (fl+ ($fllog x) k)]
[(infinity? x) x]
[(infinity? y) y]
[($nan? x) x]
[else y]))
(flatan2 y x))))))
(define cflsqrt
; principal expression from Kahan:
; sqrt(z) = expt(z,1/2)
; Kahan's algorithm except for the calculation of "a"
(let ([f (let ([k ($flsqrt (fl* .5 (fl+ ($flsqrt 2.0) 1.0)))])
(lambda (x y)
; sqrt(|x+yi| + |x|)/2
(cond
[(fl> x y)
(let ([r (fl/ y x)])
(fl* ($flsqrt x)
($flsqrt (fl* .5 (fl+ ($flsqrt (fl+ 1.0 (fl* r r)))
1.0)))))]
[(fl< x y)
(let ([r (fl/ x y)])
(fl* ($flsqrt y)
($flsqrt (fl* .5 (fl+ ($flsqrt (fl+ (fl* r r) 1.0))
r)))))]
[(fl= x y) (fl* ($flsqrt x) k)]
[(infinity? x) x]
[(infinity? y) y]
[($nan? x) x]
[else y])))])
(lambda (z)
(let ([x (cfl-real-part z)] [y (cfl-imag-part z)])
(let ([a (f (flabs x) (flabs y))])
(if (fl= a 0.0)
(fl-make-rectangular a y)
(let ([b (if (infinity? y) y (fl* (fl/ y a) .5))])
(if (fl< x 0.0)
(fl-make-rectangular
(flabs b)
(if (negated-flonum? y) (fl- a) a))
(fl-make-rectangular a b)))))))))
(define cflexp
; exp(a+bi) = exp(a)cos(b) + exp(a)sin(b)i
(lambda (z)
(let ([a (cfl-real-part z)] [b (cfl-imag-part z)])
(cond
; perhaps misguidedly treat x+0.0i the same as x
[(fl= b 0.0) (fl-make-rectangular ($flexp a) b)]
[(fl<= a log-omega)
(let ([e^a ($flexp a)])
(fl-make-rectangular (fl* e^a ($flcos b)) (fl* e^a ($flsin b))))]
[else (fl-make-rectangular
(let ([cosb ($flcos b)])
(if (fl< cosb 0.0)
(fl- ($flexp (fl+ a ($fllog (fl- cosb)))))
($flexp (fl+ a ($fllog cosb)))))
(let ([sinb ($flsin b)])
(if (fl< sinb 0.0)
(fl- ($flexp (fl+ a ($fllog (fl- sinb)))))
($flexp (fl+ a ($fllog sinb))))))]))))
(define cflslowsinh
; probably not the best way to handle this
(let ([f (lambda (z -z)
(cfl- (cflexp (cfl- z log2)) (cfl* .5 (cflexp -z))))])
(lambda (z)
(if (fl< (cfl-real-part z) 0.0)
(cfl- (f (cfl- z) z))
(f z (cfl- z))))))
(define cflslowcosh
; probably not the best way to handle this
(let ([f (lambda (z -z)
(cfl+ (cflexp (cfl- z log2)) (cfl* .5 (cflexp -z))))])
(lambda (z)
(if (fl< (cfl-real-part z) 0.0)
(f (cfl- z) z)
(f z (cfl- z))))))
(define cflsin
; sin(a+bi) = sin(a)cosh(b)+cos(a)sinh(b)i
(lambda (z)
(let ([a (cfl-real-part z)] [b (cfl-imag-part z)])
(if (fl<= (flabs b) acosh-omega)
(fl-make-rectangular (fl* ($flsin a) (flcosh b))
(fl* ($flcos a) (flsinh b)))
(cfl/i (cflslowsinh (cfl*i z)))))))
(define cflcos
; cos(a+bi) = cos(a)cosh(b)-sin(a)sinh(b)i
(lambda (z)
(let ([a (cfl-real-part z)] [b (cfl-imag-part z)])
(if (fl<= (flabs b) acosh-omega)
(fl-make-rectangular (fl* ($flcos a) (flcosh b))
(fl- (fl* ($flsin a) (flsinh b))))
(cflslowcosh (cfl*i z))))))
(define cfltan
; from Kahan
(lambda (z)
(cfl/i (cfltanh (cfl*i z)))))
(define cflacos
; from Kahan
; principal expression:
; 2log(sqrt((1+z)/2) + sqrt((1-z)/2)i)/i = pi/2 - asin(z)
; returns a+bi where
; a = 2atan(RP(sqrt(1-z))/RP(sqrt(1+z)))
; b = asinh(IP(conjugate(sqrt(1+z)))sqrt(1-z))
(lambda (z)
(let ([z- (cflsqrt (cfl- 1.0 z))]
[z+ (cflsqrt (cfl+ 1.0 z))])
(let ([a (cfl-real-part z-)] [b (cfl-imag-part z-)]
[c (cfl-real-part z+)] [d (cfl-imag-part z+)])
(fl-make-rectangular (fl* 2.0 ($flatan (fl/ a c)))
(flasinh (fl- (fl* b c) (fl* a d))))))))
(define cflasin
; from Kahan
; principal expression:
; asinh(iz)/i
; returns a+bi where
; a = atan(RP(z)/RP(sqrt(1-z)sqrt(1+z)))
; b = asinh(IP(conjugate(sqrt(1-z))sqrt(1+z)))
(lambda (z)
(let ([z- (cflsqrt (cfl- 1.0 z))]
[z+ (cflsqrt (cfl+ 1.0 z))])
(let ([a (cfl-real-part z-)] [b (cfl-imag-part z-)]
[c (cfl-real-part z+)] [d (cfl-imag-part z+)])
(fl-make-rectangular
($flatan (fl/ (cfl-real-part z) (fl- (fl* a c) (fl* b d))))
(flasinh (fl- (fl* a d) (fl* b c))))))))
(define cflasinh
; from Kahan
; principal expression:
; log(z + sqrt(1 + zz))
(lambda (z)
(cfl/i (cflasin (cfl*i z)))))
(define cflsinh
; sinh(a+bi) = sinh(a)cos(b)+cosh(a)sin(b)i
(lambda (z)
(let ([a (cfl-real-part z)] [b (cfl-imag-part z)])
(if (fl<= a acosh-omega)
(fl-make-rectangular (fl* (flsinh a) ($flcos b))
(fl* (flcosh a) ($flsin b)))
(cflslowsinh z)))))
(define cflcosh
; cosh(a+bi) = cosh(a)cos(b)+sinh(a)sin(b)i
(lambda (z)
(let ([a (cfl-real-part z)] [b (cfl-imag-part z)])
(if (fl<= a acosh-omega)
(fl-make-rectangular (fl* (flcosh a) ($flcos b))
(fl* (flsinh a) ($flsin b)))
(cflslowcosh z)))))
(define cfltanh
; from Kahan
(let ([theta (fl/ acosh-omega 4.0)])
(lambda (z)
(let ([x (cfl-real-part z)] [y (cfl-imag-part z)])
(let ([ax (flabs x)])
(if (fl> ax theta)
(fl-make-rectangular
(if (negated-flonum? x) -1.0 1.0)
(if (negated-flonum? y) (fl- 0.0) 0.0))
(let ([t ($fltan y)]
[s (flsinh x)])
(let ([beta (fl+ 1.0 (fl* t t))]
[ss (fl* s s)])
(let ([rho ($flsqrt (fl+ 1.0 ss))])
(if (infinity? t)
(fl-make-rectangular (fl/ rho s) (/ t))
(let ([k (/ (fl+ 1.0 (fl* beta ss)))])
(fl-make-rectangular (fl* beta rho s k)
(fl* t k)))))))))))))
(define cflacosh
; from Kahan
; principal expression:
; 2log(sqrt((z+1)/2) + sqrt((z-1)/2))
; returns a+bi where
; a = (asinh (real-part (* (conjugate (sqrt (- z 1))) (sqrt (+ z 1)))))
; b = (* 2 (atan (/ (imag-part (sqrt (- z 1))) (real-part (sqrt (+ z 1))))))
(lambda (z)
(let ([z- (cflsqrt (cfl- z 1.0))]
[z+ (cflsqrt (cfl+ z 1.0))])
(let ([a (cfl-real-part z-)] [b (cfl-imag-part z-)]
[c (cfl-real-part z+)] [d (cfl-imag-part z+)])
(fl-make-rectangular (flasinh (fl+ (fl* a c) (fl* b d)))
(fl* 2.0 ($flatan (fl/ b c))))))))
(define cflatanh
; principal expression from Kahan:
; (log(1+z) - log(1-z))/2
(let ([f (let ([theta (fl/ sqrt-omega 4.0)] [pi/2 (flatan2 1.0 0.0)])
(let ([rho (fl/ theta)] [-pi/2 (fl- pi/2)])
(lambda (x y)
; x is positive
(let ([ay (abs y)])
(cond
[(or (fl> x theta) (fl> ay theta))
; RP(1/z) +/- (pi/2)i
(fl-make-rectangular
(cond
[(fl> x ay) (fl/ (fl+ x (fl* (fl/ y x) y)))]
[(fl< x ay) (let ([r (fl/ y x)])
(fl/ r (fl+ (fl* x r) y)))]
[else (fl/ (fl+ x ay))])
(if (negated-flonum? y) pi/2 -pi/2))]
[(fl= x 1.0)
(let ([k (fl+ ay rho)])
(fl-make-rectangular
($fllog (fl/ ($flsqrt ($flsqrt (fl+ 4.0
(* y y))))
($flsqrt k)))
(fl/ (fl+ pi/2 ($flatan (fl/ k 2.0)))
(if (negated-flonum? y) 2.0 -2.0))))]
[else
(let ([1-x (fl- 1.0 x)]
[k (let ([k (fl+ ay rho)]) (fl* k k))])
(fl-make-rectangular
(fl/ (fllog1+ (fl/ (fl* 4.0 x)
(fl+ (fl* 1-x 1-x) k)))
4.0)
(fl/ (flatan2 (fl* 2.0 y)
(fl- (fl* 1-x (fl+ 1.0 x)) k))
-2.0)))])))))])
(lambda (z)
(let ([x (cfl-real-part z)] [y (cfl-imag-part z)])
(if (negated-flonum? x)
(cfl- (f (fl- x) y))
(f x (fl- y)))))))
(define cflatan
; from Kahan
; principal expression:
; arctanh(zi)/i
(lambda (z)
(cfl/i (cflatanh (cfl*i z)))))
(define exact-inexact+
(lambda (x y)
(cond
[(fixnum? x) (if (fx= x 0) y (fl+ (fixnum->flonum x) y))]
[(or (floatable? x) (fl= y 0.0)) (fl+ (inexact x) y)]
[(exceptional-flonum? y) y]
[else (inexact (+ x (exact y)))])))
(define exact-inexact-
(lambda (x y)
(cond
[(fixnum? x) (if (fx= x 0) (fl- y) (fl- (fixnum->flonum x) y))]
[(or (floatable? x) (fl= y 0.0)) (fl- (inexact x) y)]
[(exceptional-flonum? y) (fl- y)]
[else (inexact (- x (exact y)))])))
(define inexact-exact-
(lambda (x y)
(cond
[(fixnum? y) (fl- x (fixnum->flonum y))]
[(or (floatable? y) (fl= x 0.0)) (fl- x (inexact y))]
[(exceptional-flonum? x) x]
[else (inexact (- (exact x) y))])))
(define exact-inexact*
(lambda (x y)
(cond
[(fixnum? x) (if (fx= x 0) 0 (fl* (fixnum->flonum x) y))]
[(floatable? x) (fl* (inexact x) y)]
[(or (fl= y 0.0) (exceptional-flonum? y)) (if (< x 0) (fl- y) y)]
[else (inexact (* x (exact y)))])))
(define exact-inexact/
(lambda (x y)
(cond
[(fixnum? x) (fl/ (fixnum->flonum x) y)]
[(floatable? x) (fl/ (inexact x) y)]
[(or (fl= y 0.0) (exceptional-flonum? y))
(if (< x 0) (fl/ -1.0 y) (fl/ y))]
[else (inexact (/ x (exact y)))])))
(define inexact-exact/
(lambda (x y)
(cond
[(fixnum? y) (fl/ x (fixnum->flonum y))]
[(floatable? y) (fl/ x (inexact y))]
[(or (fl= x 0.0) (exceptional-flonum? x)) (if (< y 0) (fl- x) x)]
[else (inexact (/ (exact x) y))])))
(define floatable?
; x is "floatable" if it can be made inexact without overflow or underflow
(lambda (x)
(type-case x
[(fixnum?) #t]
[(bignum?) (fx<= (integer-length x) (constant max-float-exponent))]
[(ratnum?) (fx<= (constant min-float-exponent)
(fx- (integer-length (numerator x))
(integer-length (denominator x)))
(constant max-float-exponent))]
[($exactnum?) (and (floatable? (real-part x))
(floatable? (imag-part x)))]
[else #t])))
(define exact-inexact-compare?
; e is an exact number, i is a flonum
; Preserve transitivity by making i exact,
; unless i is +/-infinity or a NAN, in which case any normal flonum
; is a safe representation of e for comparative purposes.
(lambda (pred? e i)
(float-type-case
[(ieee)
(if (exceptional-flonum? i)
(pred? 0.0 i)
(pred? e (exact i)))]
[else (pred? e (exact i))])))
(define exact-sqrt
; x must be exact
; returns the exact square root if it exists, otherwise an approximation
(lambda (x)
(type-case x
[(fixnum? bignum?)
(if (< x 0) (make-rectangular 0 (isqrt (abs x))) (isqrt x))]
[(ratnum?)
(/ (exact-sqrt (numerator x)) (exact-sqrt (denominator x)))]
[else
(let ([rp (exact-sqrt (/ (+ (exact-sqrt (magnitude-squared x))
(real-part x))
2))])
(make-rectangular rp (/ (imag-part x) (* 2 rp))))])))
(define ($fldiv-and-mod x y)
(if (negated-flonum? y)
(let ([q ($flfloor (fl/ x (fl- y)))])
(values (fl- q) (fl+ x (fl* y q))))
(let ([q ($flfloor (fl/ x y))])
(values q (fl- x (fl* y q))))))
(define ($fldiv x y)
(if (negated-flonum? y)
(fl- ($flfloor (fl/ x (fl- y))))
($flfloor (fl/ x y))))
(define ($flmod x y)
(if (negated-flonum? y)
(fl+ x (fl* y ($flfloor (fl/ x (fl- y)))))
(fl- x (fl* y ($flfloor (fl/ x y))))))
(define ($fldiv0-and-mod0 x y)
; there doesn't seem to be an easy way to do this...
(let-values ([(d m) ($fldiv-and-mod x y)])
(if (fl> y 0.0)
(if (fl< m (fl/ y 2.0))
(values d m)
(values (fl+ d 1.0) (fl- m y)))
(if (fl< m (fl/ y -2.0))
(values d m)
(values (fl- d 1.0) (fl+ m y))))))
(define ($fldiv0 x y)
(let-values ([(d m) ($fldiv-and-mod x y)])
(if (fl> y 0.0)
(if (fl< m (fl/ y 2.0)) d (fl+ d 1.0))
(if (fl< m (fl/ y -2.0)) d (fl- d 1.0)))))
(define ($flmod0 x y)
(let ([m ($flmod x y)])
(if (fl> y 0.0)
(if (fl< m (fl/ y 2.0)) m (fl- m y))
(if (fl< m (fl/ y -2.0)) m (fl+ m y)))))
(define ($fxdiv-and-mod x y who) ; signal error on overflow if who != #f, otherwise return bignum
(if (fx< x 0)
(if (fx< y 0)
(if (fx> x y) ; |x| < |y| => q = 0, r = x != 0
(values 1 (fx- x y))
(if (and (fx= y -1) (fx= x (most-negative-fixnum)))
(if who
($impoops who "fixnum overflow with arguments ~s and ~s" x y)
(values (- (most-negative-fixnum)) 0))
(let* ([q (fxquotient x y)] [r (fx- x (fx* y q))])
(if (fx= r 0) (values q 0) (values (fx+ q 1) (fx- r y))))))
(if (fx> x (fx- y)) ; |x| < |y| => q = 0, r = x != 0
(values -1 (fx+ x y))
(let* ([q (fxquotient x y)] [r (fx- x (fx* y q))])
(if (fx= r 0) (values q 0) (values (fx- q 1) (fx+ r y))))))
(if (or (fx< x y) (fx> (fx- x) y)) ; |x| < |y| => q = 0, r = x
(values 0 x)
(let ([q (fxquotient x y)])
(values q (fx- x (fx* y q)))))))
(define ($fxdiv x y who) ; signal error on overflow if who != #f, otherwise return bignum
(if (fx< x 0)
(if (fx< y 0)
(if (fx> x y) ; |x| < |y| => q = 0, r = x != 0
1
(if (and (fx= y -1) (fx= x (most-negative-fixnum)))
(if who
($impoops who "fixnum overflow with arguments ~s and ~s" x y)
(- (most-negative-fixnum)))
(let ([q (fxquotient x y)])
(if (fx= x (fx* y q)) q (fx+ q 1)))))
(if (fx> x (fx- y)) ; |x| < |y| => q = 0, r = x != 0
-1
(let ([q (fxquotient x y)])
(if (fx= x (fx* y q)) q (fx- q 1)))))
(if (or (fx< x y) (fx> (fx- x) y)) ; |x| < |y| => q = 0, r = x
0
(fxquotient x y))))
(define ($fxmod x y) ; no overflow possible
(if (fx< x 0)
(if (fx< y 0)
(if (fx> x y) ; |x| < |y| => q = 0, r = x != 0
(fx- x y)
(if (and (fx= y -1) (fx= x (most-negative-fixnum)))
0
(let* ([q (fxquotient x y)] [r (fx- x (fx* y q))])
(if (fx= r 0) 0 (fx- r y)))))
(if (fx> x (fx- y)) ; |x| < |y| => q = 0, r = x != 0
(fx+ x y)
(let* ([q (fxquotient x y)] [r (fx- x (fx* y q))])
(if (fx= r 0) 0 (fx+ r y)))))
(if (or (fx< x y) (fx> (fx- x) y)) ; |x| < |y| => q = 0, r = x
x
(fx- x (fx* y (fxquotient x y))))))
(define ($fxdiv0-and-mod0 x y who)
(let-values ([(d m) ($fxdiv-and-mod x y who)])
(if (fx> y 0)
(if (fx< m (if (fx= y (most-positive-fixnum))
(ash (+ (most-positive-fixnum) 1) -1)
(fxsrl (fx+ y 1) 1)))
(values d m)
(values (fx+ d 1) (fx- m y)))
(if (fx< m (if (fx= y (most-negative-fixnum))
(ash (- 1 (most-negative-fixnum)) -1)
(fxsrl (fx- 1 y) 1)))
(values d m)
(values (fx- d 1) (fx+ m y))))))
(define ($fxdiv0 x y who)
(let-values ([(d m) ($fxdiv-and-mod x y who)])
(if (fx> y 0)
(if (fx< m (if (fx= y (most-positive-fixnum))
(ash (+ (most-positive-fixnum) 1) -1)
(fxsrl (fx+ y 1) 1)))
d
(fx+ d 1))
(if (fx< m (if (fx= y (most-negative-fixnum))
(ash (- 1 (most-negative-fixnum)) -1)
(fxsrl (fx- 1 y) 1)))
d
(fx- d 1)))))
(define ($fxmod0 x y)
(let ([m ($fxmod x y)])
(if (fx> y 0)
(if (fx< m (if (fx= y (most-positive-fixnum))
(ash (+ (most-positive-fixnum) 1) -1)
(fxsrl (fx+ y 1) 1)))
m
(fx- m y))
(if (fx< m (if (fx= y (most-negative-fixnum))
(ash (- 1 (most-negative-fixnum)) -1)
(fxsrl (fx- 1 y) 1)))
m
(fx+ m y)))))
(define ($exdiv-and-mod x y) ; like $fldiv-and-mod
(if (< y 0)
(let ([q (floor (/ x (- y)))])
(values (- q) (+ x (* y q))))
(let ([q (floor (/ x y))])
(values q (- x (* y q))))))
(define ($exdiv0-and-mod0 x y)
(let-values ([(d m) ($exdiv-and-mod x y)])
(if (> y 0)
(if (< m (/ y 2))
(values d m)
(values (+ d 1) (- m y)))
(if (< m (/ y -2))
(values d m)
(values (- d 1) (+ m y))))))
(define ($exdiv x y) ; like $fldiv
(if (< y 0)
(- (floor (/ x (- y))))
(floor (/ x y))))
(define ($exmod x y) ; like $flmod
(if (< y 0)
(+ x (* y (floor (/ x (- y)))))
(- x (* y (floor (/ x y))))))
(define $sll
(lambda (who x n)
(type-case n
[(fixnum?)
(unless (fx>= n 0) ($oops who "~s is not a nonnegative exact integer" n))
(type-case x
[(fixnum?)
(let ([max-fx-shift (- (constant fixnum-bits) 1)])
(if (fx> n max-fx-shift)
(integer-ash x n)
(let ([m (#3%fxsll x n)])
(if (fx= (fxsra m n) x)
m
(integer-ash x n)))))]
[(bignum?) (integer-ash x n)]
[else (nonexact-integer-error who x)])]
[(bignum?)
(unless ($bigpositive? n) ($oops who "~s is not a nonnegative exact integer" n))
(type-case x
[(fixnum? bignum?)
(let ([k (most-positive-fixnum)])
($sll who ($sll who x k) (- n k)))]
[else (nonexact-integer-error who x)])]
[else (nonexact-integer-error who n)])))
(define $sra
(lambda (who x n)
(type-case n
[(fixnum?)
(unless (fx>= n 0) ($oops who "~s is not a nonnegative exact integer" n))
(type-case x
[(fixnum?)
(let ([max-fx-shift (- (constant fixnum-bits) 1)])
(fxsra x (if (fx> n max-fx-shift) max-fx-shift n)))]
[(bignum?) (integer-ash x (- n))]
[else (nonexact-integer-error who x)])]
[(bignum?)
(unless ($bigpositive? n) ($oops who "~s is not a nonnegative exact integer" n))
(type-case x
[(fixnum? bignum?)
(let ([k (most-positive-fixnum)])
($sra who ($sra who x k) (- n k)))]
[else (nonexact-integer-error who x)])]
[else (nonexact-integer-error who n)])))
(set! integer?
(lambda (x)
(type-case x
[(fixnum? bignum?) #t]
[(flonum?) ($flinteger? x)]
[else #f])))
(set! integer-valued?
(lambda (x)
(type-case x
[(fixnum? bignum?) #t]
[(flonum?) ($flinteger? x)]
[($inexactnum?)
(and (fl= ($inexactnum-imag-part x) 0.0)
($flinteger? ($inexactnum-real-part x)))]
[else #f])))
(set! rational?
(lambda (x)
(type-case x
[(fixnum? bignum? ratnum?) #t]
[(flonum?) (not (exceptional-flonum? x))]
[else #f])))
(set! rational-valued?
(lambda (x)
(type-case x
[(fixnum? bignum? ratnum?) #t]
[(flonum?) (not (exceptional-flonum? x))]
[($inexactnum?) (fl= ($inexactnum-imag-part x) 0.0)]
[else #f])))
(set! real?
(lambda (x)
(type-case x
[(fixnum? flonum? bignum? ratnum?) #t]
[else #f])))
(set! real-valued?
(lambda (x)
(type-case x
[(fixnum? flonum? bignum? ratnum?) #t]
[($inexactnum?) (fl= ($inexactnum-imag-part x) 0.0)]
[else #f])))
(set! complex?
; same as number?
(lambda (x)
(type-case x
[(fixnum? cflonum? bignum? ratnum? $exactnum?) #t]
[else #f])))
(set! number?
; same as complex?
(lambda (x)
(type-case x
[(fixnum? cflonum? bignum? ratnum? $exactnum?) #t]
[else #f])))
(set! exact?
(lambda (x)
(type-case x
[(fixnum?) #t]
[(cflonum?) #f]
[(bignum? ratnum? $exactnum?) #t]
[else (nonnumber-error 'exact? x)])))
(set! inexact?
(lambda (x)
(type-case x
[(cflonum?) #t]
[(fixnum? bignum? ratnum? $exactnum?) #f]
[else (nonnumber-error 'inexact? x)])))
(set-who! numerator
(lambda (x)
(type-case x
[(ratnum?) ($ratio-numerator x)]
[(fixnum? bignum?) x]
[(flonum?)
(cond
[(exceptional-flonum? x) (nonrational-error who x)]
[($flinteger-or-inf? x) x]
[else (inexact (numerator (exact x)))])]
[else (nonrational-error who x)])))
(set-who! denominator
(lambda (x)
(type-case x
[(ratnum?) ($ratio-denominator x)]
[(fixnum? bignum?) 1]
[(flonum?)
(cond
[(exceptional-flonum? x) (nonrational-error who x)]
[($flinteger-or-inf? x) 1.0]
[else (inexact (denominator (exact x)))])]
[else (nonrational-error who x)])))
(set! real-part
(lambda (z)