| 1 |
;; |
| 2 |
;; test numeric system implementation |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
|
| 7 |
(define (exp2 pow) |
| 8 |
(do ((i 0 (+ i 1)) |
| 9 |
(m 1 (+ m m))) |
| 10 |
((>= i pow) m))) |
| 11 |
|
| 12 |
(define (fermat n) ;Fermat's number |
| 13 |
(+ (expt 2 (expt 2 n)) 1)) |
| 14 |
|
| 15 |
(define Apply apply) ; avoid inline expansion |
| 16 |
|
| 17 |
(test-start "numbers") |
| 18 |
|
| 19 |
;;================================================================== |
| 20 |
;; Reader/writer |
| 21 |
;; |
| 22 |
|
| 23 |
;;------------------------------------------------------------------ |
| 24 |
(test-section "integer addition & reader") |
| 25 |
|
| 26 |
(define (i-tester x) |
| 27 |
(list x (+ x -1 x) (+ x x) (- x) (- (+ x -1 x)) (- 0 x x) (- 0 x x 1))) |
| 28 |
|
| 29 |
(test* "around 2^28" |
| 30 |
'(268435456 536870911 536870912 |
| 31 |
-268435456 -536870911 -536870912 -536870913) |
| 32 |
(i-tester (exp2 28))) |
| 33 |
|
| 34 |
(test* "around 2^31" |
| 35 |
'(2147483648 4294967295 4294967296 |
| 36 |
-2147483648 -4294967295 -4294967296 -4294967297) |
| 37 |
(i-tester (exp2 31))) |
| 38 |
|
| 39 |
(test* "around 2^60" |
| 40 |
'(1152921504606846976 2305843009213693951 2305843009213693952 |
| 41 |
-1152921504606846976 -2305843009213693951 -2305843009213693952 |
| 42 |
-2305843009213693953) |
| 43 |
(i-tester (exp2 60))) |
| 44 |
|
| 45 |
(test* "around 2^63" |
| 46 |
'(9223372036854775808 18446744073709551615 18446744073709551616 |
| 47 |
-9223372036854775808 -18446744073709551615 -18446744073709551616 |
| 48 |
-18446744073709551617) |
| 49 |
(i-tester (exp2 63))) |
| 50 |
|
| 51 |
(test* "around 2^127" |
| 52 |
'(170141183460469231731687303715884105728 |
| 53 |
340282366920938463463374607431768211455 |
| 54 |
340282366920938463463374607431768211456 |
| 55 |
-170141183460469231731687303715884105728 |
| 56 |
-340282366920938463463374607431768211455 |
| 57 |
-340282366920938463463374607431768211456 |
| 58 |
-340282366920938463463374607431768211457) |
| 59 |
(i-tester (exp2 127))) |
| 60 |
|
| 61 |
;; test for reader's overflow detection code |
| 62 |
(test* "peculiarity around 2^32" |
| 63 |
(* 477226729 10) 4772267290) |
| 64 |
|
| 65 |
(test* "radix" '(43605 342391 718048024785 |
| 66 |
123456789 123456789987654321 |
| 67 |
1193046 3735928559 3735928559 |
| 68 |
1049836114599 8455360875 |
| 69 |
1049836114599 1049836114599 -668 668 -668) |
| 70 |
(list #b1010101001010101 |
| 71 |
#o1234567 |
| 72 |
#o12345677654321 |
| 73 |
#d123456789 |
| 74 |
#d123456789987654321 |
| 75 |
#x123456 |
| 76 |
#xdeadbeef |
| 77 |
#xDeadBeef |
| 78 |
#36rdeadbeef |
| 79 |
#18RDeadBeef |
| 80 |
#36r#edeadbeef |
| 81 |
#e#36rdeadbeef |
| 82 |
#8r-1234 |
| 83 |
#8r#e+1234 |
| 84 |
#e#8r-1234)) |
| 85 |
|
| 86 |
(test* "exactness" #t (exact? #e10)) |
| 87 |
(test* "exactness" #t (exact? #e10.0)) |
| 88 |
(test* "exactness" #t (exact? #e10e10)) |
| 89 |
(test* "exactness" #t (exact? #e12.34)) |
| 90 |
(test* "inexactness" #f (exact? #i10)) |
| 91 |
(test* "inexactness" #f (exact? #i10.0)) |
| 92 |
(test* "inexactness" #f (exact? #i12.34)) |
| 93 |
|
| 94 |
(test* "exactness & radix" '(#t 3735928559 #t 3735928559) |
| 95 |
(list (exact? #e#xdeadbeef) |
| 96 |
#e#xdeadbeef |
| 97 |
(exact? #x#edeadbeef) |
| 98 |
#x#edeadbeef)) |
| 99 |
(test* "inexactness & radix" '(#f 3735928559.0 #f 3735928559.0) |
| 100 |
(list (exact? #i#xdeadbeef) |
| 101 |
#i#xdeadbeef |
| 102 |
(exact? #x#ideadbeef) |
| 103 |
#x#ideadbeef)) |
| 104 |
|
| 105 |
(test* "invalid exactness/radix spec" #f |
| 106 |
(or (string->number "#e") |
| 107 |
(string->number "#i") |
| 108 |
(string->number "#e#i3") |
| 109 |
(string->number "#i#e5") |
| 110 |
(string->number "#x#o13") |
| 111 |
(string->number "#e#b#i00101") |
| 112 |
(string->number "#123r15") |
| 113 |
(string->number "#x#12r15") |
| 114 |
(string->number "#12r#x15"))) |
| 115 |
|
| 116 |
(test* "radix prefix not allowed in strict r7rs" |
| 117 |
(test-error <read-error> #/Radix prefix isn't allowed/) |
| 118 |
(read-from-string "#!r7rs #12r123")) |
| 119 |
|
| 120 |
(test* "string->number exact argument" #t |
| 121 |
(eqv? (string->number "0.1234567890123" 10 'exact) |
| 122 |
1234567890123/10000000000000)) |
| 123 |
(test* "string->number exact argument (override)" #t |
| 124 |
(eqv? (string->number "#i0.1234567890123" 10 'exact) |
| 125 |
0.1234567890123)) |
| 126 |
(test* "string->number inexact argument" #t |
| 127 |
(eqv? (string->number "1/3" 10 'inexact) |
| 128 |
0.3333333333333333)) |
| 129 |
(test* "string->number inexact argument" #t |
| 130 |
(eqv? (string->number "#e1/3" 10 'inexact) |
| 131 |
1/3)) |
| 132 |
|
| 133 |
(define (radix-tester radix) |
| 134 |
(list |
| 135 |
(let loop ((digits 0) |
| 136 |
(input "1") |
| 137 |
(value 1)) |
| 138 |
(cond ((> digits 64) #t) |
| 139 |
((eqv? (string->number input radix) value) |
| 140 |
(loop (+ digits 1) (string-append input "0") (* value radix))) |
| 141 |
(else #f))) |
| 142 |
(let loop ((digits 0) |
| 143 |
(input (string (integer->digit (- radix 1) radix))) |
| 144 |
(value (- radix 1))) |
| 145 |
(cond ((> digits 64) #t) |
| 146 |
((eqv? (string->number input radix) value) |
| 147 |
(loop (+ digits 1) |
| 148 |
(string-append input (string (integer->digit (- radix 1) radix))) |
| 149 |
(+ (* value radix) (- radix 1)))) |
| 150 |
(else #f))))) |
| 151 |
|
| 152 |
(test* "base-2 reader" '(#t #t) (radix-tester 2)) |
| 153 |
(test* "base-3 reader" '(#t #t) (radix-tester 3)) |
| 154 |
(test* "base-4 reader" '(#t #t) (radix-tester 4)) |
| 155 |
(test* "base-5 reader" '(#t #t) (radix-tester 5)) |
| 156 |
(test* "base-6 reader" '(#t #t) (radix-tester 6)) |
| 157 |
(test* "base-7 reader" '(#t #t) (radix-tester 7)) |
| 158 |
(test* "base-8 reader" '(#t #t) (radix-tester 8)) |
| 159 |
(test* "base-9 reader" '(#t #t) (radix-tester 9)) |
| 160 |
(test* "base-10 reader" '(#t #t) (radix-tester 10)) |
| 161 |
(test* "base-11 reader" '(#t #t) (radix-tester 11)) |
| 162 |
(test* "base-12 reader" '(#t #t) (radix-tester 12)) |
| 163 |
(test* "base-13 reader" '(#t #t) (radix-tester 13)) |
| 164 |
(test* "base-14 reader" '(#t #t) (radix-tester 14)) |
| 165 |
(test* "base-15 reader" '(#t #t) (radix-tester 15)) |
| 166 |
(test* "base-16 reader" '(#t #t) (radix-tester 16)) |
| 167 |
(test* "base-17 reader" '(#t #t) (radix-tester 17)) |
| 168 |
(test* "base-18 reader" '(#t #t) (radix-tester 18)) |
| 169 |
(test* "base-19 reader" '(#t #t) (radix-tester 19)) |
| 170 |
(test* "base-20 reader" '(#t #t) (radix-tester 20)) |
| 171 |
(test* "base-21 reader" '(#t #t) (radix-tester 21)) |
| 172 |
(test* "base-22 reader" '(#t #t) (radix-tester 22)) |
| 173 |
(test* "base-23 reader" '(#t #t) (radix-tester 23)) |
| 174 |
(test* "base-24 reader" '(#t #t) (radix-tester 24)) |
| 175 |
(test* "base-25 reader" '(#t #t) (radix-tester 25)) |
| 176 |
(test* "base-26 reader" '(#t #t) (radix-tester 26)) |
| 177 |
(test* "base-27 reader" '(#t #t) (radix-tester 27)) |
| 178 |
(test* "base-28 reader" '(#t #t) (radix-tester 28)) |
| 179 |
(test* "base-29 reader" '(#t #t) (radix-tester 29)) |
| 180 |
(test* "base-30 reader" '(#t #t) (radix-tester 30)) |
| 181 |
(test* "base-31 reader" '(#t #t) (radix-tester 31)) |
| 182 |
(test* "base-32 reader" '(#t #t) (radix-tester 32)) |
| 183 |
(test* "base-33 reader" '(#t #t) (radix-tester 33)) |
| 184 |
(test* "base-34 reader" '(#t #t) (radix-tester 34)) |
| 185 |
(test* "base-35 reader" '(#t #t) (radix-tester 35)) |
| 186 |
(test* "base-36 reader" '(#t #t) (radix-tester 36)) |
| 187 |
|
| 188 |
;; SRFI-169 underscore in numbers |
| 189 |
(let ([data '(("0123" . 123) |
| 190 |
("0_1_2_3" . 123) |
| 191 |
("0_123" . 123) |
| 192 |
("01_23" . 123) |
| 193 |
("012_3" . 123) |
| 194 |
("+0123" . 123) |
| 195 |
("+0_123" . 123) |
| 196 |
("-0123" . -123) |
| 197 |
("-0_123" . -123) |
| 198 |
("_0123" . #f) |
| 199 |
("0123_" . #f) |
| 200 |
("0123__" . #f) |
| 201 |
("01__23" . #f) |
| 202 |
("0_1__2___3" . #f) |
| 203 |
("+_0123" . #f) |
| 204 |
("+0123_" . #f) |
| 205 |
("-_0123" . #f) |
| 206 |
("-0123_" . #f) |
| 207 |
|
| 208 |
("1_2_3/4_5_6_7" . 123/4567) |
| 209 |
("12_34/5_678" . 1234/5678) |
| 210 |
("1_2_3/_4_5_6_7" . #f) |
| 211 |
("_12_34/5_678" . #f) |
| 212 |
|
| 213 |
("0_1_23.4_5_6" . 123.456) |
| 214 |
("1_2_3.5e6" . 123.5e6) |
| 215 |
("1_2e1_2" . 12e12) |
| 216 |
("_0123.456" . #f) |
| 217 |
("0123_.456" . #f) |
| 218 |
("0123._456" . #f) |
| 219 |
("0123.456_" . #f) |
| 220 |
("123_.5e6" . #f) |
| 221 |
("123._5e6" . #f) |
| 222 |
("123.5_e6" . #f) |
| 223 |
("123.5e_6" . #f) |
| 224 |
("123.5e6_" . #f) |
| 225 |
("12_e12" . #f) |
| 226 |
("12e_12" . #f) |
| 227 |
("12e12_" . #f) |
| 228 |
("12e1__2" . #f) |
| 229 |
|
| 230 |
("-12_3.0_00_00-12_34.56_78i" . -123.0-1234.5678i) |
| 231 |
("-12_3.0_00_00@-12_34.56_78" . [email protected]) |
| 232 |
("-12_3.0_00_00-12_34.56_78_i" . #f) |
| 233 |
("-12_3.0_00_00-12_34.56_78i_" . #f) |
| 234 |
("-12_3.0_00_00_@-12_34.56_78" . #f) |
| 235 |
("-12_3.0_00_00@_-12_34.56_78" . #f) |
| 236 |
|
| 237 |
("#b10_10_10" . #b101010) |
| 238 |
("#o23_45_67" . #o234567) |
| 239 |
("#d45_67_89" . #d456789) |
| 240 |
("#xAB_CD_EF" . #xabcdef) |
| 241 |
("#x789_9B_C9_EF" . #x7899bc9ef) |
| 242 |
("#x-2_0" . #x-20) |
| 243 |
("#o+2_345_6" . #o+23456) |
| 244 |
("#x-_2" . #f) |
| 245 |
("_#x-_2" . #f) |
| 246 |
("#d_45_67_89" . #f) |
| 247 |
("#e_45/67_89" . #f) |
| 248 |
("#i#o_1234" . #f) |
| 249 |
("#i_#o_1234" . #f) |
| 250 |
("#e#x1234_" . #f))]) |
| 251 |
(dolist [d data] |
| 252 |
(test* (format "SRFI-169 ~s" d) (cdr d) |
| 253 |
(string->number (car d))))) |
| 254 |
|
| 255 |
(test* "SRFI-169 not allowed in r7rs strict mode" |
| 256 |
(test-error <read-error> #/bad numeric format/) |
| 257 |
(read-from-string "#!r7rs #x1234_5678")) |
| 258 |
|
| 259 |
;;------------------------------------------------------------------ |
| 260 |
(test-section "rational reader") |
| 261 |
|
| 262 |
(define (rational-test v) |
| 263 |
(if (number? v) (list v (exact? v)) v)) |
| 264 |
|
| 265 |
(test* "rational reader" '(1234 #t) (rational-test '1234/1)) |
| 266 |
(test* "rational reader" '(-1234 #t) (rational-test '-1234/1)) |
| 267 |
(test* "rational reader" '(1234 #t) (rational-test '+1234/1)) |
| 268 |
(test* "rational reader" '|1234/-1| (rational-test '1234/-1)) |
| 269 |
(test* "rational reader" '(1234 #t) (rational-test '2468/2)) |
| 270 |
(test* "rational reader" '(1/2 #t) (rational-test '1/2)) |
| 271 |
(test* "rational reader" '(-1/2 #t) (rational-test '-1/2)) |
| 272 |
(test* "rational reader" '(1/2 #t) (rational-test '+1/2)) |
| 273 |
(test* "rational reader" '(1/2 #t) (rational-test '751/1502)) |
| 274 |
|
| 275 |
(test* "rational reader" '(1 #t) |
| 276 |
(rational-test (string->number "3/03"))) |
| 277 |
(test* "rational reader" #f |
| 278 |
(rational-test (string->number "3/0"))) |
| 279 |
(test* "rational reader" #f |
| 280 |
(rational-test (string->number "-3/0"))) |
| 281 |
(test* "rational reader" #f |
| 282 |
(rational-test (string->number "0/0"))) |
| 283 |
(test* "rational reader" '(+inf.0 #f) |
| 284 |
(rational-test (string->number "#i3/0"))) |
| 285 |
(test* "rational reader" '(-inf.0 #f) |
| 286 |
(rational-test (string->number "#i-3/0"))) |
| 287 |
(test* "rational reader" '(+nan.0 #f) |
| 288 |
(rational-test (string->number "#i0/0"))) |
| 289 |
(test* "rational reader" #f |
| 290 |
(rational-test (string->number "3/3/4"))) |
| 291 |
(test* "rational reader" #f |
| 292 |
(rational-test (string->number "1/2."))) |
| 293 |
(test* "rational reader" #f |
| 294 |
(rational-test (string->number "1.3/2"))) |
| 295 |
|
| 296 |
(test* "rational reader" (test-error) |
| 297 |
(rational-test (read-from-string "#e3/0"))) |
| 298 |
(test* "rational reader" (test-error) |
| 299 |
(rational-test (read-from-string "#e-3/0"))) |
| 300 |
|
| 301 |
(test* "rational reader w/#e" '(1234 #t) |
| 302 |
(rational-test '#e1234/1)) |
| 303 |
(test* "rational reader w/#e" '(-1234 #t) |
| 304 |
(rational-test '#e-1234/1)) |
| 305 |
(test* "rational reader w/#e" '(32/7 #t) |
| 306 |
(rational-test '#e32/7)) |
| 307 |
(test* "rational reader w/#e" '(-32/7 #t) |
| 308 |
(rational-test '#e-32/7)) |
| 309 |
(test* "rational reader w/#i" '(1234.0 #f) |
| 310 |
(rational-test '#i1234/1)) |
| 311 |
(test* "rational reader w/#i" '(-1234.0 #f) |
| 312 |
(rational-test '#i-1234/1)) |
| 313 |
(test* "rational reader w/#i" '(-0.125 #f) |
| 314 |
(rational-test '#i-4/32)) |
| 315 |
|
| 316 |
(test* "rational reader w/radix" '(15 #t) |
| 317 |
(rational-test '#e#xff/11)) |
| 318 |
(test* "rational reader w/radix" '(56 #t) |
| 319 |
(rational-test '#o770/11)) |
| 320 |
(test* "rational reader w/radix" '(15.0 #f) |
| 321 |
(rational-test '#x#iff/11)) |
| 322 |
|
| 323 |
(test* "rational reader edge case" #t (symbol? (read-from-string "/1"))) |
| 324 |
(test* "rational reader edge case" #t (symbol? (read-from-string "-/1"))) |
| 325 |
(test* "rational reader edge case" #t (symbol? (read-from-string "+/1"))) |
| 326 |
|
| 327 |
;;------------------------------------------------------------------ |
| 328 |
(test-section "flonum reader") |
| 329 |
|
| 330 |
(define (flonum-test s) |
| 331 |
(let1 v (read-from-string s) |
| 332 |
(if (number? v) (list v (inexact? v)) v))) |
| 333 |
|
| 334 |
(test* "flonum reader" '(3.14 #t) (flonum-test "3.14")) |
| 335 |
(test* "flonum reader" '(0.14 #t) (flonum-test "0.14")) |
| 336 |
(test* "flonum reader" '(0.14 #t) (flonum-test ".14")) |
| 337 |
(test* "flonum reader" '(3.0 #t) (flonum-test "3.")) |
| 338 |
(test* "flonum reader" '(-3.14 #t) (flonum-test "-3.14")) |
| 339 |
(test* "flonum reader" '(-0.14 #t) (flonum-test "-0.14")) |
| 340 |
(test* "flonum reader" '(-0.14 #t) (flonum-test "-.14")) |
| 341 |
(test* "flonum reader" '(-3.0 #t) (flonum-test "-3.")) |
| 342 |
(test* "flonum reader" '(3.14 #t) (flonum-test "+3.14")) |
| 343 |
(test* "flonum reader" '(0.14 #t) (flonum-test "+0.14")) |
| 344 |
(test* "flonum reader" '(0.14 #t) (flonum-test "+.14")) |
| 345 |
(test* "flonum reader" '(3.0 #t) (flonum-test "+3.")) |
| 346 |
(test* "flonum reader" '(0.0 #t) (flonum-test ".0")) |
| 347 |
(test* "flonum reader" '(0.0 #t) (flonum-test "0.")) |
| 348 |
(test* "flonum reader" #f (string->number ".")) |
| 349 |
(test* "flonum reader" #f (string->number "-.")) |
| 350 |
(test* "flonum reader" #f (string->number "+.")) |
| 351 |
|
| 352 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "3.14e2")) |
| 353 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314e3")) |
| 354 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "314e0")) |
| 355 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "314e-0")) |
| 356 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "3140000e-4")) |
| 357 |
(test* "flonum reader (exp)" '(-314.0 #t) (flonum-test "-3.14e2")) |
| 358 |
(test* "flonum reader (exp)" '(-314.0 #t) (flonum-test "-.314e3")) |
| 359 |
(test* "flonum reader (exp)" '(-314.0 #t) (flonum-test "-314e0")) |
| 360 |
(test* "flonum reader (exp)" '(-314.0 #t) (flonum-test "-314.e-0")) |
| 361 |
(test* "flonum reader (exp)" '(-314.0 #t) (flonum-test "-3140000e-4")) |
| 362 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "+3.14e2")) |
| 363 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "+.314e3")) |
| 364 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "+314.e0")) |
| 365 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "+314e-0")) |
| 366 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test "+3140000.000e-4")) |
| 367 |
|
| 368 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314E3")) |
| 369 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314s3")) |
| 370 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314S3")) |
| 371 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314l3")) |
| 372 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314L3")) |
| 373 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314f3")) |
| 374 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314F3")) |
| 375 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314d3")) |
| 376 |
(test* "flonum reader (exp)" '(314.0 #t) (flonum-test ".314D3")) |
| 377 |
|
| 378 |
(test* "flonum reader (minimum denormalized number 5.0e-324)" #t |
| 379 |
(let1 x (expt 2.0 -1074) |
| 380 |
(= x (string->number (number->string x))))) |
| 381 |
(test* "flonum reader (minimum denormalized number -5.0e-324)" #t |
| 382 |
(let1 x (- (expt 2.0 -1074)) |
| 383 |
(= x (string->number (number->string x))))) |
| 384 |
|
| 385 |
(test* "flonum reader lots of digits" 1.0 |
| 386 |
(read-from-string |
| 387 |
"1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001")) |
| 388 |
(test* "flonum reader lots of digits" 1.0e308 |
| 389 |
(read-from-string |
| 390 |
"1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e308")) |
| 391 |
|
| 392 |
;; This hanged in 0.9.1. See Jens Thiele's message in gauche-devel |
| 393 |
;; in Feb. 2011. |
| 394 |
(test* "flonum reader (minimum normalized number)" #t |
| 395 |
(= (expt 2.0 (- 52 1074)) |
| 396 |
(string->number "2.2250738585072012e-308"))) |
| 397 |
|
| 398 |
;; Bugs reported |
| 399 |
(test* "flonum reader (zero with big exponent)" '(#t #t) |
| 400 |
(list (= 0.0 (string->number "0e324")) |
| 401 |
(= 0.0 (string->number "0e325")))) |
| 402 |
|
| 403 |
;; We used to allow 1#1 to be read as a symbol. As of 0.9.4, it is an error. |
| 404 |
(test* "padding" '(10.0 #t) (flonum-test "1#")) |
| 405 |
(test* "padding" '(10.0 #t) (flonum-test "1#.")) |
| 406 |
(test* "padding" '(10.0 #t) (flonum-test "1#.#")) |
| 407 |
(test* "padding" '(100.0 #t) (flonum-test "10#.#")) |
| 408 |
(test* "padding" '(100.0 #t) (flonum-test "1##.#")) |
| 409 |
(test* "padding" '(100.0 #t) (flonum-test "100.0#")) |
| 410 |
(test* "padding" '(1.0 #t) (flonum-test "1.#")) |
| 411 |
|
| 412 |
(test* "padding" (test-error) (flonum-test "1#1")) |
| 413 |
(test* "padding" (test-error) (flonum-test "1##1")) |
| 414 |
(test* "padding" (test-error) (flonum-test "1#.1")) |
| 415 |
(test* "padding" (test-error) (flonum-test "1.#1")) |
| 416 |
|
| 417 |
(test* "padding" (test-error) (flonum-test ".#")) |
| 418 |
(test* "padding" '(0.0 #t) (flonum-test "0.#")) |
| 419 |
(test* "padding" '(0.0 #t) (flonum-test ".0#")) |
| 420 |
(test* "padding" '(0.0 #t) (flonum-test "0#")) |
| 421 |
(test* "padding" '(0.0 #t) (flonum-test "0#.#")) |
| 422 |
(test* "padding" (test-error) (flonum-test "0#.0")) |
| 423 |
|
| 424 |
(test* "padding" '(1000.0 #t) (flonum-test "1#e2")) |
| 425 |
(test* "padding" '(1000.0 #t) (flonum-test "1##e1")) |
| 426 |
(test* "padding" '(1000.0 #t) (flonum-test "1#.##e2")) |
| 427 |
(test* "padding" '(0.0 #t) (flonum-test "0.#e2")) |
| 428 |
(test* "padding" '(0.0 #t) (flonum-test ".0#e2")) |
| 429 |
(test* "padding" (test-error) (flonum-test ".##e2")) |
| 430 |
|
| 431 |
(test* "padding (exactness)" '(100 #f) (flonum-test "#e1##")) |
| 432 |
(test* "padding (exactness)" '(120 #f) (flonum-test "#e12#")) |
| 433 |
(test* "padding (exactness)" '(120 #f) (flonum-test "#e12#.#")) |
| 434 |
(test* "padding (exactness)" '(100.0 #t) (flonum-test "#i1##")) |
| 435 |
(test* "padding (exactness)" '(120.0 #t) (flonum-test "#i12#")) |
| 436 |
(test* "padding (exactness)" '(120.0 #t) (flonum-test "#i12#.#")) |
| 437 |
|
| 438 |
(test* "exponent out-of-range 1" '(+inf.0 #t) (flonum-test "1e309")) |
| 439 |
(test* "exponent out-of-range 2" '(+inf.0 #t) (flonum-test "1e10000")) |
| 440 |
(test* "exponent out-of-range 3" '(+inf.0 #t) (flonum-test "1e1000000000000000000000000000000000000000000000000000000000000000")) |
| 441 |
(test* "exponent out-of-range 4" '(-inf.0 #t) (flonum-test "-1e309")) |
| 442 |
(test* "exponent out-of-range 5" '(-inf.0 #t) (flonum-test "-1e10000")) |
| 443 |
(test* "exponent out-of-range 6" '(-inf.0 #t) (flonum-test "-1e1000000000000000000000000000000000000000000000000000000000000000")) |
| 444 |
(test* "exponent out-of-range 7" '(0.0 #t) (flonum-test "1e-324")) |
| 445 |
(test* "exponent out-of-range 8" '(0.0 #t) (flonum-test "1e-1000")) |
| 446 |
(test* "exponent out-of-range 9" '(0.0 #t) (flonum-test "1e-1000000000000000000000000000000000000000000000000000000000000000000")) |
| 447 |
|
| 448 |
(test* "no integral part" 0.5 (read-from-string ".5")) |
| 449 |
(test* "no integral part" -0.5 (read-from-string "-.5")) |
| 450 |
(test* "no integral part" 0.5 (read-from-string "+.5")) |
| 451 |
|
| 452 |
;;------------------------------------------------------------------ |
| 453 |
(test-section "exact fractional number") |
| 454 |
|
| 455 |
(test* "exact fractonal number" 12345 |
| 456 |
(string->number "#e1.2345e4")) |
| 457 |
(test* "exact fractonal number" 123450000000000 |
| 458 |
(string->number "#e1.2345e14")) |
| 459 |
(test* "exact fractonal number" 12345/100 |
| 460 |
(string->number "#e1.2345e2")) |
| 461 |
(test* "exact fractonal number" 12345/1000000 |
| 462 |
(string->number "#e1.2345e-2")) |
| 463 |
(test* "exact fractonal number" -12345 |
| 464 |
(string->number "#e-1.2345e4")) |
| 465 |
(test* "exact fractonal number" -123450000000000 |
| 466 |
(string->number "#e-1.2345e14")) |
| 467 |
(test* "exact fractonal number" -12345/100 |
| 468 |
(string->number "#e-1.2345e2")) |
| 469 |
(test* "exact fractonal number" -12345/1000000 |
| 470 |
(string->number "#e-1.2345e-2")) |
| 471 |
|
| 472 |
(test* "exact fractonal number" (real-expt 10 296) |
| 473 |
(string->number "#e0.0001e300")) |
| 474 |
(test* "exact fractonal number" (- (real-expt 10 296)) |
| 475 |
(string->number "#e-0.0001e300")) |
| 476 |
|
| 477 |
(test* "exact fractonal number" (test-error) |
| 478 |
(read-from-string "#e1e330")) |
| 479 |
(test* "exact fractonal number" (test-error) |
| 480 |
(read-from-string "#e1e-330")) |
| 481 |
|
| 482 |
|
| 483 |
;;------------------------------------------------------------------ |
| 484 |
(test-section "complex reader") |
| 485 |
|
| 486 |
(define (decompose-complex z) |
| 487 |
(cond ((real? z) z) |
| 488 |
((complex? z) |
| 489 |
(list (real-part z) (imag-part z))) |
| 490 |
(else z))) |
| 491 |
|
| 492 |
(test* "complex reader" '(1.0 1.0) (decompose-complex '1+i)) |
| 493 |
(test* "complex reader" '(1.0 1.0) (decompose-complex '1+1i)) |
| 494 |
(test* "complex reader" '(1.0 -1.0) (decompose-complex '1-i)) |
| 495 |
(test* "complex reader" '(1.0 -1.0) (decompose-complex '1-1i)) |
| 496 |
(test* "complex reader" '(1.0 1.0) (decompose-complex '1.0+1i)) |
| 497 |
(test* "complex reader" '(1.0 1.0) (decompose-complex '1.0+1.0i)) |
| 498 |
(test* "complex reader" '(1e-5 1.0) (decompose-complex '1e-5+1i)) |
| 499 |
(test* "complex reader" '(1e+5 1.0) (decompose-complex '1e+5+1i)) |
| 500 |
(test* "complex reader" '(1.0 1e-5) (decompose-complex '1+1e-5i)) |
| 501 |
(test* "complex reader" '(1.0 1e+5) (decompose-complex '1+1e+5i)) |
| 502 |
(test* "complex reader" '(0.1 1e+4) (decompose-complex '0.1+0.1e+5i)) |
| 503 |
(test* "complex reader" '(0.0 1.0) (decompose-complex '+i)) |
| 504 |
(test* "complex reader" '(0.0 -1.0) (decompose-complex '-i)) |
| 505 |
(test* "complex reader" '(0.0 1.0) (decompose-complex '+1i)) |
| 506 |
(test* "complex reader" '(0.0 -1.0) (decompose-complex '-1i)) |
| 507 |
(test* "complex reader" '(0.0 1.0) (decompose-complex '+1.i)) |
| 508 |
(test* "complex reader" '(0.0 -1.0) (decompose-complex '-1.i)) |
| 509 |
(test* "complex reader" '(0.0 1.0) (decompose-complex '+1.0i)) |
| 510 |
(test* "complex reader" '(0.0 -1.0) (decompose-complex '-1.0i)) |
| 511 |
(test* "complex reader" 1.0 (decompose-complex '1+0.0i)) |
| 512 |
(test* "complex reader" 1.0 (decompose-complex '1+.0i)) |
| 513 |
(test* "complex reader" 1.0 (decompose-complex '1+0.i)) |
| 514 |
(test* "complex reader" 1.0 (decompose-complex '1+0.0e-43i)) |
| 515 |
(test* "complex reader" 100.0 (decompose-complex '1e2+0.0e-43i)) |
| 516 |
|
| 517 |
(test* "complex reader" 'i (decompose-complex 'i)) |
| 518 |
(test* "complex reader" #f (decompose-complex (string->number ".i"))) |
| 519 |
(test* "complex reader" #f (decompose-complex (string->number "+.i"))) |
| 520 |
(test* "complex reader" #f (decompose-complex (string->number "-.i"))) |
| 521 |
(test* "complex reader" '33i (decompose-complex '33i)) |
| 522 |
(test* "complex reader" 'i+1 (decompose-complex 'i+1)) |
| 523 |
(test* "complex reader" '|++i| (decompose-complex '++i)) |
| 524 |
(test* "complex reader" '|--i| (decompose-complex '--i)) |
| 525 |
|
| 526 |
(test* "complex reader" '(0.5 0.5) (decompose-complex 1/2+1/2i)) |
| 527 |
(test* "complex reader" '(0.0 0.5) (decompose-complex 0+1/2i)) |
| 528 |
(test* "complex reader" '(0.0 -0.5) (decompose-complex -1/2i)) |
| 529 |
(test* "complex reader" 1/2 (decompose-complex 1/2-0/2i)) |
| 530 |
(test* "complex reader" #f (decompose-complex (string->number "1/2-1/0i"))) |
| 531 |
|
| 532 |
(test* "complex reader (polar)" (make-polar 1.0 1.0) [email protected]) |
| 533 |
(test* "complex reader (polar)" (make-polar 1.0 -1.0) [email protected]) |
| 534 |
(test* "complex reader (polar)" (make-polar 1.0 1.0) 1.0@+1.0) |
| 535 |
(test* "complex reader (polar)" (make-polar -7.0 -3.0) [email protected]) |
| 536 |
(test* "complex reader (polar)" (make-polar 3.5 -3.0) 7/[email protected]) |
| 537 |
(test* "complex reader (polar)" #f (string->number "7/[email protected]")) |
| 538 |
|
| 539 |
;;------------------------------------------------------------------ |
| 540 |
(test-section "integer writer syntax") |
| 541 |
|
| 542 |
(define (i-tester2 x) |
| 543 |
(map number->string (i-tester x))) |
| 544 |
|
| 545 |
(test* "around 2^28" |
| 546 |
'("268435456" "536870911" "536870912" |
| 547 |
"-268435456" "-536870911" "-536870912" "-536870913") |
| 548 |
(i-tester2 (exp2 28))) |
| 549 |
|
| 550 |
(test* "around 2^31" |
| 551 |
'("2147483648" "4294967295" "4294967296" |
| 552 |
"-2147483648" "-4294967295" "-4294967296" "-4294967297") |
| 553 |
(i-tester2 (exp2 31))) |
| 554 |
|
| 555 |
(test* "around 2^60" |
| 556 |
'("1152921504606846976" "2305843009213693951" "2305843009213693952" |
| 557 |
"-1152921504606846976" "-2305843009213693951" "-2305843009213693952" |
| 558 |
"-2305843009213693953") |
| 559 |
(i-tester2 (exp2 60))) |
| 560 |
|
| 561 |
(test* "around 2^63" |
| 562 |
'("9223372036854775808" "18446744073709551615" "18446744073709551616" |
| 563 |
"-9223372036854775808" "-18446744073709551615" "-18446744073709551616" |
| 564 |
"-18446744073709551617") |
| 565 |
(i-tester2 (exp2 63))) |
| 566 |
|
| 567 |
(test* "around 2^127" |
| 568 |
'("170141183460469231731687303715884105728" |
| 569 |
"340282366920938463463374607431768211455" |
| 570 |
"340282366920938463463374607431768211456" |
| 571 |
"-170141183460469231731687303715884105728" |
| 572 |
"-340282366920938463463374607431768211455" |
| 573 |
"-340282366920938463463374607431768211456" |
| 574 |
"-340282366920938463463374607431768211457") |
| 575 |
(i-tester2 (exp2 127))) |
| 576 |
|
| 577 |
;;------------------------------------------------------------------ |
| 578 |
(test-section "number->string radix") |
| 579 |
|
| 580 |
(test* "number->string radix 1" |
| 581 |
'("100101" "1101" "211" "122" "101" "52" "45" "41" "37" "34" |
| 582 |
"31" "2b" "29" "27" "25" "23" "21" "1i" "1h" "1g" |
| 583 |
"1f" "1e" "1d" "1c" "1b" "1a" "19" "18" "17" "16" |
| 584 |
"15" "14" "13" "12" "11") |
| 585 |
(map (cut number->string <> <>) (make-list 35 37) (iota 35 2))) |
| 586 |
|
| 587 |
(test* "number->string radix error 1" (test-error) (number->string 42 0)) |
| 588 |
(test* "number->string radix error 2" (test-error) (number->string 42 1)) |
| 589 |
(test* "number->string radix error 3" (test-error) (number->string 42 37)) |
| 590 |
|
| 591 |
;;------------------------------------------------------------------ |
| 592 |
(test-section "number->string customization") |
| 593 |
|
| 594 |
(test* "number->string flags" |
| 595 |
'(("cafe" "CAFE" "CAFE" "+cafe" "#xcafe" "#x+CAFE") |
| 596 |
("cafebabedeadbeef" "CAFEBABEDEADBEEF" "CAFEBABEDEADBEEF" |
| 597 |
"+cafebabedeadbeef" "#xcafebabedeadbeef" "#x+CAFEBABEDEADBEEF") |
| 598 |
("0" "0" "0" "+0" "#x0" "#x+0") |
| 599 |
("-e" "-E" "-E" "-e" "#x-e" "#x-E") |
| 600 |
("a/b" "A/B" "A/B" "+a/b" "#xa/b" "#x+A/B") |
| 601 |
("1.0+1.0i" "1.0+1.0i" "1.0+1.0i" "+1.0+1.0i" "1.0+1.0i" "+1.0+1.0i")) |
| 602 |
(map (^n (map (cut number->string n 16 <>) |
| 603 |
'(#f #t (uppercase) (plus) (radix) (uppercase plus radix)))) |
| 604 |
'(#xcafe #xcafebabedeadbeef 0 -14 10/11 1+i))) |
| 605 |
|
| 606 |
;; Precision |
| 607 |
(dolist [n '((0.123456789 "0.12346" "0.1235" "0.123" "0.12" "0.1" "0.") |
| 608 |
(1.23456789 "1.23457" "1.2346" "1.235" "1.23" "1.2" "1.") |
| 609 |
(12.3456789 "12.34568" "12.3457" "12.346" "12.35" "12.3" "12.") |
| 610 |
(-123.456789 "-123.45679" "-123.4568" "-123.457" |
| 611 |
"-123.46" "-123.5" "-123.") |
| 612 |
(1.23456789e100 "1.23457e100" "1.2346e100" "1.235e100" |
| 613 |
"1.23e100" "1.2e100" "1.e100") |
| 614 |
(12.3456789e-101 "1.23457e-100" "1.2346e-100" "1.235e-100" |
| 615 |
"1.23e-100" "1.2e-100" "1.e-100") |
| 616 |
(1. "1.00000" "1.0000" "1.000" "1.00" "1.0" "1.") |
| 617 |
(1.1 "1.10000" "1.1000" "1.100" "1.10" "1.1" "1.") |
| 618 |
(1e100 "1.00000e100" "1.0000e100" "1.000e100" |
| 619 |
"1.00e100" "1.0e100" "1.e100"))] |
| 620 |
(define (runs flags) |
| 621 |
(map (cut number->string (car n) 10 flags <>) |
| 622 |
'(5 4 3 2 1 0))) |
| 623 |
(test* "number->string digits (effective)" (cdr n) (runs #f)) |
| 624 |
(test* "number->string digits (notational)" (cdr n) (runs '(notational)))) |
| 625 |
|
| 626 |
;; Difference between effective/notational rounding |
| 627 |
(dolist [data '((1.15 1 "1.1" "1.2") |
| 628 |
(4.15 1 "4.2" "4.2"))] |
| 629 |
(test* "effective / notational rounding" |
| 630 |
(cddr data) |
| 631 |
(list (number->string (car data) 10 #f (cadr data)) |
| 632 |
(number->string (car data) 10 '(notational) (cadr data))))) |
| 633 |
|
| 634 |
;; notational rounding carry propagation, round-to-even |
| 635 |
(dolist [data '((1.99999 (5 4 3 2 1) ("1.99999" "2.0000" "2.000" "2.00" "2.0")) |
| 636 |
(-9.999 (4 3 2 1) ("-9.9990" "-9.999" "-10.00" "-10.0")) |
| 637 |
(0.999 (4 3 2 1) ("0.9990" "0.999" "1.00" "1.0")) |
| 638 |
;(0.25 (1) ("0.2")) ; if we use banker's rounding |
| 639 |
(0.25 (1) ("0.3")) ; if we use commercial rounding |
| 640 |
(0.135 (2) ("0.14")))] |
| 641 |
(test* "notational rounding carry over" |
| 642 |
(caddr data) |
| 643 |
(map (cut number->string (car data) 10 '(notational) <>) |
| 644 |
(cadr data)))) |
| 645 |
|
| 646 |
;;================================================================== |
| 647 |
;; Conversions |
| 648 |
;; |
| 649 |
|
| 650 |
;; We first test expt, for we need to use it to test exact<->inexact |
| 651 |
;; conversion stuff. |
| 652 |
(test-section "expt") |
| 653 |
|
| 654 |
(test* "exact expt" 1 (expt 5 0)) |
| 655 |
(test* "exact expt" 9765625 (expt 5 10)) |
| 656 |
(test* "exact expt" 1220703125 (expt 5 13)) |
| 657 |
(test* "exact expt" 94039548065783000637498922977779654225493244541767001720700136502273380756378173828125 (expt 5 123)) |
| 658 |
(test* "exact expt" 1/94039548065783000637498922977779654225493244541767001720700136502273380756378173828125 (expt 5 -123)) |
| 659 |
(test* "exact expt" 1 (expt -5 0)) |
| 660 |
(test* "exact expt" 9765625 (expt -5 10)) |
| 661 |
(test* "exact expt" -1220703125 (expt -5 13)) |
| 662 |
(test* "exact expt" -94039548065783000637498922977779654225493244541767001720700136502273380756378173828125 (expt -5 123)) |
| 663 |
(test* "exact expt" -1/94039548065783000637498922977779654225493244541767001720700136502273380756378173828125 (expt -5 -123)) |
| 664 |
(test* "exact expt" 1 (expt 1 720000)) |
| 665 |
(test* "exact expt" 1 (expt -1 720000)) |
| 666 |
(test* "exact expt" -1 (expt -1 720001)) |
| 667 |
|
| 668 |
(test* "exact expt (ratinoal)" 8589934592/5559060566555523 |
| 669 |
(expt 2/3 33)) |
| 670 |
(test* "exact expt (rational)" -8589934592/5559060566555523 |
| 671 |
(expt -2/3 33)) |
| 672 |
(test* "exact expt (ratinoal)" 5559060566555523/8589934592 |
| 673 |
(expt 2/3 -33)) |
| 674 |
|
| 675 |
(test* "expt (0 raised to a complex power)" 0 (expt 0 5+.0000312i)) |
| 676 |
(test* "expt (0.0 raised to a complex power)" 0.0 (expt 0.0 5+.0000312i)) |
| 677 |
|
| 678 |
(test* "expt (coercion to inexact)" 1.4142135623730951 |
| 679 |
(expt 2 1/2) |
| 680 |
(lambda (x y) (approx=? x y))) ;; NB: pa$ will be tested later |
| 681 |
|
| 682 |
(let () |
| 683 |
(define (exact-expt-tester x y) |
| 684 |
(let1 x^y (expt x y) |
| 685 |
(test* "exact expt (non-integral power)" x (expt x^y (/ y))) |
| 686 |
(test* "exact expt (non-integral power)" (* x x) (expt x^y (/ 2 y))) |
| 687 |
(test* "exact expt (non-integral power, inexact fallback)" |
| 688 |
(expt (+ x^y 1.0) (/ y)) (expt (+ x^y 1) (/ y))) |
| 689 |
)) |
| 690 |
|
| 691 |
(exact-expt-tester 3 7) |
| 692 |
(exact-expt-tester 5 3) |
| 693 |
(exact-expt-tester 13 17) |
| 694 |
(exact-expt-tester 101 103) |
| 695 |
(exact-expt-tester 11/13 23) |
| 696 |
) |
| 697 |
|
| 698 |
;; expt-mod |
| 699 |
(define (test-expt-mod base mod) |
| 700 |
;; NB: we haven't tested iota. |
| 701 |
(let1 es (do ([e 1 (+ e 3)] [r '() (cons e r)]) |
| 702 |
[(> e 100) (reverse r)]) |
| 703 |
(test* (format "expt-mod(~a, e, ~a)" base mod) |
| 704 |
(map (^e (modulo (expt base e) mod)) es) |
| 705 |
(map (^e (expt-mod base e mod)) es)))) |
| 706 |
|
| 707 |
(test-expt-mod 35 41) |
| 708 |
(test-expt-mod 15841875 319999357) |
| 709 |
(test-expt-mod 915151975010144550184898988758 1775619891701751758948583493979350) |
| 710 |
(test-expt-mod -324574950475018750175057087501 100184859387038471089598349534598) |
| 711 |
(test-expt-mod 324574950475018750175057087501 -100184859387038471089598349534598) |
| 712 |
|
| 713 |
(test-section "exact<->inexact") |
| 714 |
|
| 715 |
(for-each |
| 716 |
(lambda (e&i) |
| 717 |
(let ((e (car e&i)) |
| 718 |
(i (cdr e&i))) |
| 719 |
(test* (format "exact->inexact ~s" i) i (exact->inexact e)) |
| 720 |
;; (- 0) is 0, but (- 0.0) is -0.0 |
| 721 |
(test* (format "exact->inexact ~s" (- i)) |
| 722 |
(if (= i 0.0) i (- i)) |
| 723 |
(exact->inexact (- e))) |
| 724 |
(test* (format "inexact->exact ~s" e) e (inexact->exact i)) |
| 725 |
(test* (format "inexact->exact ~s" (- e)) (- e) (inexact->exact (- i))) |
| 726 |
)) |
| 727 |
`((0 . 0.0) |
| 728 |
(1 . 1.0) |
| 729 |
(,(real-expt 2 52) . ,(real-expt 2.0 52)) |
| 730 |
(,(real-expt 2 53) . ,(real-expt 2.0 53)) |
| 731 |
(,(real-expt 2 54) . ,(real-expt 2.0 54)) |
| 732 |
|
| 733 |
(1/2 . 0.5) |
| 734 |
(3/4 . 0.75) |
| 735 |
|
| 736 |
(1/3 . 0.3333333333333333) |
| 737 |
)) |
| 738 |
|
| 739 |
;; Boundary conditions for exact->inexact |
| 740 |
;; If greatest-fixnum may be larger than the range the double can exactly |
| 741 |
;; represent, we'll lose precision. |
| 742 |
(test* "exact (greatest-fixnum)" |
| 743 |
(if (> (greatest-fixnum) (expt 2 53)) |
| 744 |
(+ (greatest-fixnum) 1) |
| 745 |
(greatest-fixnum)) |
| 746 |
(exact (inexact (greatest-fixnum)))) |
| 747 |
(test* "exact (least-fixnum)" (least-fixnum) (exact (inexact (least-fixnum)))) |
| 748 |
(test* "exact (64bit long max)" (expt 2 63) (exact (- (expt 2.0 63) 1))) |
| 749 |
(test* "exact (64bit long min)" (- (expt 2 63)) (exact (- (expt 2.0 63)))) |
| 750 |
|
| 751 |
;; Boundary conditions for inexact->exact |
| 752 |
;; Since inexact->exact returns a simplest rational within the flonum precision, |
| 753 |
;; the roundtrip of exact -> inexact -> exact isn't necessary kept, but |
| 754 |
;; inexact -> exact -> inexact is. |
| 755 |
(let ([one-plus-delta 1.0000000000000002] |
| 756 |
[one-minus-half-delta 0.9999999999999999]) |
| 757 |
(define (t what orig expect) |
| 758 |
(test* (format "inexact->exact->inexact roundtrip ~s" what) |
| 759 |
expect (exact->inexact (inexact->exact orig)))) |
| 760 |
(t "1+d" one-plus-delta one-plus-delta) |
| 761 |
(t "1-d" one-minus-half-delta one-minus-half-delta)) |
| 762 |
|
| 763 |
;; Rounding bignum to flonum, edge cases. |
| 764 |
;; Test patterns: |
| 765 |
;; |
| 766 |
;; <------53bits-------> |
| 767 |
;;a) 100000000...000000000100000....0000 round down (r0) |
| 768 |
;;b) 100000000...000000000100000....0001 round up (r1) |
| 769 |
;;c) 100000000...000000001100000....0000 round up (r2) |
| 770 |
;;d) 100000000...000000001011111....1111 round down (r1) |
| 771 |
;;e) 111111111...111111111100000....0000 round up, carry over (* r0 2) |
| 772 |
;;f) 101111111...111111111100000....0000 round up, no carry over (r3) |
| 773 |
;; <--32bits--> |
| 774 |
;;g) 100..0000111.....1111100000....0000 round up; boundary on ILP32 (r4) |
| 775 |
|
| 776 |
(let loop ((n 0) |
| 777 |
(a (+ (expt 2 53) 1)) |
| 778 |
(c (+ (expt 2 53) 3)) |
| 779 |
(e (- (expt 2 54) 1)) |
| 780 |
(f (+ (expt 2 53) (expt 2 52) -1)) |
| 781 |
(g (+ (expt 2 53) (expt 2 33) -1)) |
| 782 |
(r0 (expt 2.0 53)) |
| 783 |
(r1 (+ (expt 2.0 53) 2.0)) |
| 784 |
(r2 (+ (expt 2.0 53) 4.0)) |
| 785 |
(r3 (+ (expt 2.0 53) (expt 2.0 52))) |
| 786 |
(r4 (+ (expt 2.0 53) (expt 2.0 33)))) |
| 787 |
(when (< n 32) |
| 788 |
(test* (format "exact->inexact, pattern a: round down (~d)" n) |
| 789 |
r0 (exact->inexact a)) |
| 790 |
(test* (format "exact->inexact, pattern b: round up (~d)" n) |
| 791 |
r1 (exact->inexact (+ a 1))) |
| 792 |
(test* (format "exact->inexact, pattern c: round up (~d)" n) |
| 793 |
r2 (exact->inexact c)) |
| 794 |
(test* (format "exact->inexact, pattern d: round down (~d)" n) |
| 795 |
r1 (exact->inexact (- c 1))) |
| 796 |
(test* (format "exact->inexact, pattern e: round up (~d)" n) |
| 797 |
(* r0 2.0) (exact->inexact e)) |
| 798 |
(test* (format "exact->inexact, pattern f: round up (~d)" n) |
| 799 |
r3 (exact->inexact f)) |
| 800 |
(test* (format "exact->inexact, pattern g: round up (~d)" n) |
| 801 |
r4 (exact->inexact g)) |
| 802 |
(loop (+ n 1) (ash a 1) (ash c 1) (ash e 1) (ash f 1) (ash g 1) |
| 803 |
(* r0 2.0) (* r1 2.0) (* r2 2.0) (* r3 2.0) (* r4 2.0)))) |
| 804 |
|
| 805 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 1" |
| 806 |
(expt 8/9 342.0) |
| 807 |
(exact->inexact (expt 8/9 342)) |
| 808 |
(lambda (x y) (approx=? x y (* 100 (flonum-epsilon))))) |
| 809 |
|
| 810 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 2" |
| 811 |
(expt -8/9 343.0) |
| 812 |
(exact->inexact (expt -8/9 343)) |
| 813 |
(lambda (x y) (approx=? x y (* 100 (flonum-epsilon))))) |
| 814 |
|
| 815 |
;; The following few tests covers RATNUM paths in Scm_GetDouble |
| 816 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 3" |
| 817 |
1.0e-308 (exact->inexact (/ (expt 10 20) (expt 10 328)))) |
| 818 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 4" |
| 819 |
1.0e-310 (exact->inexact (/ (expt 10 20) (expt 10 330)))) |
| 820 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 5" |
| 821 |
1.0e308 (exact->inexact (/ (expt 10 328) (expt 10 20)))) |
| 822 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 6" |
| 823 |
+inf.0 (exact->inexact (/ (expt 10 329) (expt 10 20)))) |
| 824 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 7" |
| 825 |
-inf.0 (exact->inexact (/ (expt -10 329) (expt 10 20)))) |
| 826 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 8" |
| 827 |
-inf.0 (exact->inexact (/ (expt 10 329) (- (expt 10 20))))) |
| 828 |
(test* "expt (ratnum with large denom and numer) with inexact conversion 9" |
| 829 |
+inf.0 (exact->inexact (/ (expt -10 329) (- (expt 10 20))))) |
| 830 |
|
| 831 |
;; https://github.com/shirok/Gauche/issues/981 |
| 832 |
(test* "ratnum->flonum issue #981" |
| 833 |
1.8359300949164161 |
| 834 |
(inexact 1035859546035934049504829565510983225895287916488306950857981070740695366743867100031078278751721263935546679975493752204390207097630982126800619395521726105345471977984263375536153668125171048087514699084009194566637387873758291708865130695817935612736040318981321824597512811923654763186057026190157753057552061059245522812282412116919248265575561928861628323407993/564215134826848276252831832344252759525946974058862671355354703999819470970609520456416773316150761006247802274839317308656350897465480348466925072637254609949802609759223509184829190849169166300167112766883644251548735414869706971869759857165532089048602501963429772614519311164463558845963985632547565553450572603269820019252511251015890188236013933069476212768768)) |
| 835 |
|
| 836 |
;; denormalized range |
| 837 |
(let () |
| 838 |
(define data '(5.0e-324 ; minimum positive denormalized flonum |
| 839 |
-5.0e-324 |
| 840 |
1.0e-323 |
| 841 |
1.5e-323 |
| 842 |
2.0e-323 |
| 843 |
1.0e-322 |
| 844 |
1.04e-322 |
| 845 |
1.1e-322)) |
| 846 |
(dolist [d data] |
| 847 |
(test* #"inexact conversion in subnormal range ~d" d |
| 848 |
(inexact (exact d))))) |
| 849 |
;; close to inifinity (but not quite) |
| 850 |
(test* "ratnum -> flonum, close to infinity 1" 1.0e308 |
| 851 |
(inexact (/ (+ (expt 10 309) 1) 10))) |
| 852 |
(test* "ratnum -> flonum, close to infinity 2" 1.0e308 |
| 853 |
(inexact (/ (+ (expt 10 310) 1) 100))) |
| 854 |
|
| 855 |
;; Double-rounding issue in ratnum->flonum |
| 856 |
;; http://blog.practical-scheme.net/gauche/20200722-ratnum-flonum |
| 857 |
(test* "rat->flo" '() |
| 858 |
(rlet1 offending '() |
| 859 |
(dotimes [k 100] |
| 860 |
(let1 r (exact (+ 1 (* k 1/100 (exact (flonum-epsilon))))) |
| 861 |
(unless (eqv? (inexact r) |
| 862 |
(if (<= k 50) 1.0 (+ 1.0 (flonum-epsilon)))) |
| 863 |
(push! offending (vector k (inexact r) r)))) |
| 864 |
(let1 r (exact (- -1 (* k 1/100 (exact (flonum-epsilon))))) |
| 865 |
(unless (eqv? (inexact r) |
| 866 |
(if (<= k 50) -1.0 (- -1.0 (flonum-epsilon)))) |
| 867 |
(push! offending (vector k (inexact r) r))))))) |
| 868 |
|
| 869 |
;; this exhibits a bug fixed on 9/12/2013. |
| 870 |
(test* "real->rational" '(1/3 2/3) |
| 871 |
(list (real->rational 3/10 1/10 1/10) |
| 872 |
(real->rational 24/35 4/35 4/35))) |
| 873 |
|
| 874 |
(test* "rationalize (edge cases)" '(#t #t #t +inf.0 -inf.0 0.0) |
| 875 |
(list (nan? (rationalize +nan.0 0)) |
| 876 |
(nan? (rationalize 0 +nan.0)) |
| 877 |
(nan? (rationalize +inf.0 +inf.0)) |
| 878 |
(rationalize +inf.0 0) |
| 879 |
(rationalize -inf.0 0) |
| 880 |
(rationalize 1234 +inf.0))) |
| 881 |
(test* "rationalize (integers)" '(1 2 0 -1 -2 0) |
| 882 |
(list (rationalize 1 1/2) |
| 883 |
(rationalize 5 3) |
| 884 |
(rationalize 1 3) |
| 885 |
(rationalize -1 1/2) |
| 886 |
(rationalize -5 3) |
| 887 |
(rationalize -1 3))) |
| 888 |
(test* "rationalize (exactness)" '(#t #f #f #f) |
| 889 |
(list (exact? (rationalize 1/2 1/3)) |
| 890 |
(exact? (rationalize 0.5 1/3)) |
| 891 |
(exact? (rationalize 1/2 0.1)) |
| 892 |
(exact? (rationalize 0.5 0.1)))) |
| 893 |
|
| 894 |
;;================================================================== |
| 895 |
;; Predicates |
| 896 |
;; |
| 897 |
|
| 898 |
(test-section "predicates") |
| 899 |
|
| 900 |
(test* "integer?" #t (integer? 0)) |
| 901 |
(test* "integer?" #t (integer? 85736847562938475634534245)) |
| 902 |
(test* "integer?" #f (integer? 85736.534245)) |
| 903 |
(test* "integer?" #f (integer? 3.14)) |
| 904 |
(test* "integer?" #f (integer? 3+4i)) |
| 905 |
(test* "integer?" #t (integer? 3+0i)) |
| 906 |
(test* "integer?" #f (integer? #f)) |
| 907 |
(test* "integer?" #f (integer? +inf.0)) |
| 908 |
(test* "integer?" #f (integer? -inf.0)) |
| 909 |
(test* "integer?" #f (integer? +nan.0)) |
| 910 |
|
| 911 |
(test* "rational?" #t (rational? 0)) |
| 912 |
(test* "rational?" #t (rational? 85736847562938475634534245)) |
| 913 |
(test* "rational?" #t (rational? 1/2)) |
| 914 |
(test* "rational?" #t (rational? 85736.534245)) |
| 915 |
(test* "rational?" #t (rational? 3.14)) |
| 916 |
(test* "rational?" #f (rational? 3+4i)) |
| 917 |
(test* "rational?" #t (rational? 3+0i)) |
| 918 |
(test* "rational?" #f (rational? #f)) |
| 919 |
(test* "rational?" #f (rational? +inf.0)) |
| 920 |
(test* "rational?" #f (rational? -inf.0)) |
| 921 |
(test* "rational?" #f (rational? +nan.0)) |
| 922 |
|
| 923 |
(test* "real?" #t (real? 0)) |
| 924 |
(test* "real?" #t (real? 85736847562938475634534245)) |
| 925 |
(test* "real?" #t (real? 857368.4756293847)) |
| 926 |
(test* "real?" #t (real? 3+0i)) |
| 927 |
(test* "real?" #f (real? 3+4i)) |
| 928 |
(test* "real?" #f (real? +4.3i)) |
| 929 |
(test* "real?" #f (real? '())) |
| 930 |
(test* "real?" #t (real? +inf.0)) |
| 931 |
(test* "real?" #t (real? -inf.0)) |
| 932 |
(test* "real?" #t (real? +nan.0)) |
| 933 |
|
| 934 |
(test* "complex?" #t (complex? 0)) |
| 935 |
(test* "complex?" #t (complex? 85736847562938475634534245)) |
| 936 |
(test* "complex?" #t (complex? 857368.4756293847)) |
| 937 |
(test* "complex?" #t (complex? 3+0i)) |
| 938 |
(test* "complex?" #t (complex? 3+4i)) |
| 939 |
(test* "complex?" #t (complex? +4.3i)) |
| 940 |
(test* "complex?" #f (complex? '())) |
| 941 |
|
| 942 |
(test* "number?" #t (number? 0)) |
| 943 |
(test* "number?" #t (number? 85736847562938475634534245)) |
| 944 |
(test* "number?" #t (number? 857368.4756293847)) |
| 945 |
(test* "number?" #t (number? 3+0i)) |
| 946 |
(test* "number?" #t (number? 3+4i)) |
| 947 |
(test* "number?" #t (number? +4.3i)) |
| 948 |
(test* "number?" #f (number? '())) |
| 949 |
|
| 950 |
(test* "exact?" #t (exact? 1)) |
| 951 |
(test* "exact?" #t (exact? 4304953480349304983049304953804)) |
| 952 |
(test* "exact?" #t (exact? 430495348034930/4983049304953804)) |
| 953 |
(test* "exact?" #f (exact? 1.0)) |
| 954 |
(test* "exact?" #f (exact? 4304953480349304983.049304953804)) |
| 955 |
(test* "exact?" #f (exact? 1.0+0i)) |
| 956 |
(test* "exact?" #f (exact? 1.0+5i)) |
| 957 |
(test* "inexact?" #f (inexact? 1)) |
| 958 |
(test* "inexact?" #f (inexact? 4304953480349304983049304953804)) |
| 959 |
(test* "inexact?" #f (inexact? 430495348034930/4983049304953804)) |
| 960 |
(test* "inexact?" #t (inexact? 1.0)) |
| 961 |
(test* "inexact?" #t (inexact? 4304953480349304983.049304953804)) |
| 962 |
(test* "inexact?" #t (inexact? 1.0+0i)) |
| 963 |
(test* "inexact?" #t (inexact? 1.0+5i)) |
| 964 |
|
| 965 |
(test* "odd?" #t (odd? 1)) |
| 966 |
(test* "odd?" #f (odd? 2)) |
| 967 |
(test* "even?" #f (even? 1)) |
| 968 |
(test* "even?" #t (even? 2)) |
| 969 |
(test* "odd?" #t (odd? 1.0)) |
| 970 |
(test* "odd?" #f (odd? 2.0)) |
| 971 |
(test* "even?" #f (even? 1.0)) |
| 972 |
(test* "even?" #t (even? 2.0)) |
| 973 |
(test* "odd?" #t (odd? 10000000000000000000000000000000000001)) |
| 974 |
(test* "odd?" #f (odd? 10000000000000000000000000000000000002)) |
| 975 |
(test* "even?" #f (even? 10000000000000000000000000000000000001)) |
| 976 |
(test* "even?" #t (even? 10000000000000000000000000000000000002)) |
| 977 |
|
| 978 |
(test* "zero?" #t (zero? 0)) |
| 979 |
(test* "zero?" #t (zero? 0.0)) |
| 980 |
(test* "zero?" #t (zero? (- 10 10.0))) |
| 981 |
(test* "zero?" #t (zero? 0+0i)) |
| 982 |
(test* "zero?" #f (zero? 1.0)) |
| 983 |
(test* "zero?" #f (zero? +5i)) |
| 984 |
(test* "zero?" #f (zero? +nan.0)) |
| 985 |
(test* "positive?" #t (positive? 1)) |
| 986 |
(test* "positive?" #f (positive? -1)) |
| 987 |
(test* "positive?" #t (positive? 1/7)) |
| 988 |
(test* "positive?" #f (positive? -1/7)) |
| 989 |
(test* "positive?" #t (positive? 3.1416)) |
| 990 |
(test* "positive?" #f (positive? -3.1416)) |
| 991 |
(test* "positive?" #t (positive? 134539485343498539458394)) |
| 992 |
(test* "positive?" #f (positive? -134539485343498539458394)) |
| 993 |
(test* "positive?" #f (positive? +nan.0)) |
| 994 |
(test* "negative?" #f (negative? 1)) |
| 995 |
(test* "negative?" #t (negative? -1)) |
| 996 |
(test* "negative?" #f (negative? 1/7)) |
| 997 |
(test* "negative?" #t (negative? -1/7)) |
| 998 |
(test* "negative?" #f (negative? 3.1416)) |
| 999 |
(test* "negative?" #t (negative? -3.1416)) |
| 1000 |
(test* "negative?" #f (negative? 134539485343498539458394)) |
| 1001 |
(test* "negative?" #t (negative? -134539485343498539458394)) |
| 1002 |
(test* "negative?" #f (negative? +nan.0)) |
| 1003 |
|
| 1004 |
(let1 tester |
| 1005 |
(lambda (name proc result) |
| 1006 |
(test* name (test-error) (proc #t)) |
| 1007 |
(test* name result |
| 1008 |
(list (proc 1) (proc +inf.0) (proc -inf.0) (proc +nan.0)))) |
| 1009 |
(tester "finite?" finite? `(#t #f #f #f)) |
| 1010 |
(tester "infinite?" infinite? `(#f #t #t #f)) |
| 1011 |
(tester "nan?" nan? `(#f #f #f #t)) |
| 1012 |
) |
| 1013 |
|
| 1014 |
|
| 1015 |
(test* "eqv?" #t (eqv? 20 20)) |
| 1016 |
(test* "eqv?" #t (eqv? 20.0 20.00000)) |
| 1017 |
(test* "eqv?" #f (eqv? 4/5 0.8)) |
| 1018 |
(test* "eqv?" #t (eqv? (exact->inexact 4/5) 0.8)) |
| 1019 |
;(test* "eqv?" #f (eqv? 4/5 (inexact->exact 0.8))) |
| 1020 |
(test* "eqv?" #t (eqv? 20 (inexact->exact 20.0))) |
| 1021 |
(test* "eqv?" #f (eqv? 20 20.0)) |
| 1022 |
|
| 1023 |
(test* "eqv? with -0.0" #f (eqv? 0.0 -0.0)) ;R7RS requires this |
| 1024 |
|
| 1025 |
;; R7RS transitivity requirements |
| 1026 |
(test* "= inexact exact 0" #t (= 1 1.0)) |
| 1027 |
(test* "= inexact exact 1" #t (= 9007199254740992.0 9007199254740992)) |
| 1028 |
(test* "= inexact exact 2" #f (= 9007199254740992.0 9007199254740993)) |
| 1029 |
(test* "= inexact exact 3" #f (= 9007199254740993 9007199254740992.0)) |
| 1030 |
(test* "= inexact exact 4" #f (= 9007199254740992.0 18014398509481985/2)) |
| 1031 |
(test* "= inexact exact 5" #f (= 18014398509481985/2 9007199254740992.0)) |
| 1032 |
|
| 1033 |
|
| 1034 |
;; numeric comparison involving nan. we should test both |
| 1035 |
;; inlined case and applied case |
| 1036 |
(define-macro (test-nan-cmp op) |
| 1037 |
`(begin |
| 1038 |
(test* (format "NaN ~a (inlined)" ',op) '(#f #f #f) |
| 1039 |
(list (,op +nan.0 +nan.0) (,op +nan.0 0) (,op 0 +nan.0))) |
| 1040 |
(test* (format "NaN ~a (applied)" ',op) '(#f #f #f) |
| 1041 |
(list (Apply ,op '(+nan.0 +nan.0)) |
| 1042 |
(Apply ,op '(+nan.0 0)) |
| 1043 |
(Apply ,op '(0 +nan.0)))))) |
| 1044 |
(test-nan-cmp =) |
| 1045 |
(test-nan-cmp <) |
| 1046 |
(test-nan-cmp <=) |
| 1047 |
(test-nan-cmp >) |
| 1048 |
(test-nan-cmp >=) |
| 1049 |
|
| 1050 |
;; the following tests combine instructions for comparison. |
| 1051 |
(let ((zz #f)) |
| 1052 |
(set! zz 3.14) ;; prevent the compiler from optimizing constants |
| 1053 |
|
| 1054 |
(test* "NUMEQF" '(#t #t #f #f) |
| 1055 |
(list (= 3.14 zz) (= zz 3.14) (= 3.15 zz) (= zz 3.15))) |
| 1056 |
(test* "NLTF" '(#f #f #f #t #t #f) |
| 1057 |
(list (< 3.14 zz) (< zz 3.14) |
| 1058 |
(< 3.15 zz) (< zz 3.15) |
| 1059 |
(< 3.13 zz) (< zz 3.13))) |
| 1060 |
(test* "NLEF" '(#t #t #f #t #t #f) |
| 1061 |
(list (<= 3.14 zz) (<= zz 3.14) |
| 1062 |
(<= 3.15 zz) (<= zz 3.15) |
| 1063 |
(<= 3.13 zz) (<= zz 3.13))) |
| 1064 |
(test* "NGTF" '(#f #f #t #f #f #t) |
| 1065 |
(list (> 3.14 zz) (> zz 3.14) |
| 1066 |
(> 3.15 zz) (> zz 3.15) |
| 1067 |
(> 3.13 zz) (> zz 3.13))) |
| 1068 |
(test* "NGEF" '(#t #t #t #f #f #t) |
| 1069 |
(list (>= 3.14 zz) (>= zz 3.14) |
| 1070 |
(>= 3.15 zz) (>= zz 3.15) |
| 1071 |
(>= 3.13 zz) (>= zz 3.13))) |
| 1072 |
) |
| 1073 |
|
| 1074 |
;; Go through number comparison routines. |
| 1075 |
;; assumes a >= b, a > 0, b > 0 |
| 1076 |
;; we use apply to prevent inlining. |
| 1077 |
(define (numcmp-test msg eq a b) |
| 1078 |
(let ((pp (list a b)) |
| 1079 |
(pm (list a (- b))) |
| 1080 |
(mp (list (- a) b)) |
| 1081 |
(mm (list (- a) (- b)))) |
| 1082 |
(define (test4 op opname rev results) |
| 1083 |
(for-each (lambda (result comb args) |
| 1084 |
(test* #"~msg ~(if rev 'rev \"\") ~opname(~comb)" result |
| 1085 |
(Apply op (if rev (reverse args) args)))) |
| 1086 |
results '(++ +- -+ --) (list pp pm mp mm))) |
| 1087 |
(test4 = '= #f (list eq #f #f eq)) |
| 1088 |
(test4 = '= #t (list eq #f #f eq)) |
| 1089 |
(test4 >= '>= #f (list #t #t #f eq)) |
| 1090 |
(test4 >= '>= #t (list eq #f #t #t)) |
| 1091 |
(test4 > '> #f (list (not eq) #t #f #f)) |
| 1092 |
(test4 > '> #t (list #f #f #t (not eq))) |
| 1093 |
(test4 <= '<= #f (list eq #f #t #t)) |
| 1094 |
(test4 <= '<= #t (list #t #t #f eq)) |
| 1095 |
(test4 < '< #f (list #f #f #t (not eq))) |
| 1096 |
(test4 < '< #t (list (not eq) #t #f #f)) |
| 1097 |
)) |
| 1098 |
|
| 1099 |
(numcmp-test "fixnum vs fixnum eq" #t 156 156) |
| 1100 |
(numcmp-test "fixnum vs fixnum ne" #f 878252 73224) |
| 1101 |
(numcmp-test "bignum vs fixnum ne" #f (expt 3 50) 9982425) |
| 1102 |
(numcmp-test "bignum vs bignum eq" #t (expt 3 50) (expt 3 50)) |
| 1103 |
(numcmp-test "bignum vs bignum ne" #f (expt 3 50) (expt 3 49)) |
| 1104 |
(numcmp-test "flonum vs fixnum eq" #t 314.0 314) |
| 1105 |
(numcmp-test "flonum vs fixnum ne" #f 3140.0 314) |
| 1106 |
(numcmp-test "flonum vs bignum eq" #t (expt 2.0 64) (expt 2 64)) |
| 1107 |
(numcmp-test "flonum vs bignum ne" #f (expt 2.0 64) (expt 2 63)) |
| 1108 |
(numcmp-test "flonum (inf) vs bignum ne" #f +inf.0 (expt 2 64)) |
| 1109 |
(numcmp-test "ratnum vs fixnum ne" #f 13/2 6) |
| 1110 |
(numcmp-test "ratnum vs ratnum eq" #t 3/5 3/5) |
| 1111 |
(numcmp-test "ratnum vs ratnum 1 ne" #f 3/5 4/7) |
| 1112 |
(numcmp-test "ratnum vs ratnum 2 ne" #f 4/5 3/7) |
| 1113 |
(numcmp-test "ratnum vs ratnum 3 ne" #f 4/7 2/5) |
| 1114 |
(numcmp-test "ratnum vs ratnum 4 ne" #f 4/7 3/7) |
| 1115 |
(numcmp-test "ratnum vs flonum eq" #t 3/8 0.375) |
| 1116 |
(numcmp-test "ratnum vs flonum ne" #f 8/9 0.6) |
| 1117 |
(numcmp-test "ratnum vs bignum ne" #f (/ (+ (expt 2 64) 1) 2) (expt 2 63)) |
| 1118 |
|
| 1119 |
(test* "numcmp -inf vs bignum" #t (Apply > (list (expt 2 64) -inf.0))) |
| 1120 |
|
| 1121 |
;; This tests variable number of arguments. The current stub code accepts |
| 1122 |
;; up to 4 args in stack and the rest by list, so we want to test the |
| 1123 |
;; boundary case. |
| 1124 |
(define (numcmp-multiarg-test lis eq lt le gt ge) |
| 1125 |
(test* #"=~lis" eq (Apply = lis)) |
| 1126 |
(test* #"<~lis" lt (Apply < lis)) |
| 1127 |
(test* #"<=~lis" le (Apply <= lis)) |
| 1128 |
(test* #">~lis" gt (Apply > lis)) |
| 1129 |
(test* #">=~lis" ge (Apply >= lis))) |
| 1130 |
|
| 1131 |
;; = < <= > >= |
| 1132 |
(numcmp-multiarg-test '(1 2 3 4) #f #t #t #f #f) |
| 1133 |
(numcmp-multiarg-test '(1 2 3 3) #f #f #t #f #f) |
| 1134 |
(numcmp-multiarg-test '(1 2 3 2) #f #f #f #f #f) |
| 1135 |
(numcmp-multiarg-test '(1 2 3 4 5) #f #t #t #f #f) |
| 1136 |
(numcmp-multiarg-test '(1 2 3 4 4) #f #f #t #f #f) |
| 1137 |
(numcmp-multiarg-test '(1 2 3 4 3) #f #f #f #f #f) |
| 1138 |
(numcmp-multiarg-test '(1 2 3 4 5 6) #f #t #t #f #f) |
| 1139 |
(numcmp-multiarg-test '(1 2 3 4 5 5) #f #f #t #f #f) |
| 1140 |
(numcmp-multiarg-test '(1 2 3 4 5 4) #f #f #f #f #f) |
| 1141 |
(numcmp-multiarg-test '(4 3 2 1) #f #f #f #t #t) |
| 1142 |
(numcmp-multiarg-test '(4 3 2 2) #f #f #f #f #t) |
| 1143 |
(numcmp-multiarg-test '(4 3 2 3) #f #f #f #f #f) |
| 1144 |
(numcmp-multiarg-test '(5 4 3 2 1) #f #f #f #t #t) |
| 1145 |
(numcmp-multiarg-test '(5 4 3 2 2) #f #f #f #f #t) |
| 1146 |
(numcmp-multiarg-test '(5 4 3 2 3) #f #f #f #f #f) |
| 1147 |
(numcmp-multiarg-test '(6 5 4 3 2 1) #f #f #f #t #t) |
| 1148 |
(numcmp-multiarg-test '(6 5 4 3 2 2) #f #f #f #f #t) |
| 1149 |
(numcmp-multiarg-test '(6 5 4 3 2 3) #f #f #f #f #f) |
| 1150 |
(numcmp-multiarg-test '(1 1 1 1 1) #t #f #t #f #t) |
| 1151 |
(numcmp-multiarg-test '(1 1 1 1 2) #f #f #t #f #f) |
| 1152 |
(numcmp-multiarg-test '(1 1 1 1 0) #f #f #f #f #t) |
| 1153 |
(numcmp-multiarg-test '(1 1 1 1 1 1) #t #f #t #f #t) |
| 1154 |
(numcmp-multiarg-test '(1 1 1 1 1 2) #f #f #t #f #f) |
| 1155 |
(numcmp-multiarg-test '(1 1 1 1 1 0) #f #f #f #f #t) |
| 1156 |
|
| 1157 |
(define (numrange3-test x y z left-inclusive right-inclusive) |
| 1158 |
(test* #"(<=:< ~x ~y ~z)" left-inclusive (<=:< x y z)) |
| 1159 |
(test* #"(<:<= ~x ~y ~z)" right-inclusive (<:<= x y z))) |
| 1160 |
|
| 1161 |
(numrange3-test 1 2 3 #t #t) |
| 1162 |
(numrange3-test 1 1 3 #t #f) |
| 1163 |
(numrange3-test 1 3 3 #f #t) |
| 1164 |
(numrange3-test 1 0 3 #f #f) |
| 1165 |
(numrange3-test 1 4 3 #f #f) |
| 1166 |
(numrange3-test 1 1 1 #f #f) |
| 1167 |
|
| 1168 |
(numrange3-test +nan.0 2 3 #f #f) |
| 1169 |
(numrange3-test 1 +nan.0 3 #f #f) |
| 1170 |
(numrange3-test 1 2 +nan.0 #f #f) |
| 1171 |
(numrange3-test -inf.0 1 +inf.0 #t #t) |
| 1172 |
(numrange3-test -inf.0 -inf.0 +inf.0 #t #f) |
| 1173 |
(numrange3-test -inf.0 +inf.0 +inf.0 #f #t) |
| 1174 |
(numrange3-test -inf.0 +nan.0 +inf.0 #f #f) |
| 1175 |
|
| 1176 |
|
| 1177 |
;; This is from the bug report from Bill Schottsteadt. Before 0.8.10 |
| 1178 |
;; this yielded #t because of the precision loss in fixnum vs ratnum |
| 1179 |
;; comparison. |
| 1180 |
|
| 1181 |
(test* "fixnum/ratnum comparison" #f |
| 1182 |
(= -98781233389595723930250385525631360344437602649022271391716773162526352115087074898920261954897888235939429993829738630297052776667061779065100945771127020439712527398509771853491319737304616607041615012797134365574007368603232768089410097730646360760856052946465578073788924743642391638455649511108051053789425902013657106523269224045822294981391380222050223141347787674321888089837786284947870569165079491411110074602544203383038299901291952931113248943344436935596614205784436844912243069019367149526328612664067719765890897558075277707055756274228634652905751880612235340874976952880431555921814590049070979276358637989837532124647692152520447680373275200239544449293834424643702763974403094033892112967196087310232853165951285609426599617479356206218697586025251765476179158153123631158173662488102357611674821528467825910806391548770908013608889792001203039243914696463472490444573930050190716726220002151679336252008777326482398042427845860796285369622627679324605214987983884122808994422164327311297556122943400093231935477754959547620500784989043704825777186301417894825200797719289692636286337716705491307686644214213732116277102140558505945554566856673724837541141206267647285222293953181717113434757149921850120377706206012113994795124049471433490016083401216757825264766474891405185591236321448744678896448941259668731597494947127423662646933419809756274038044752395708014998820826196523041220918922611359697502638594907608648168849193813197790291360087857093790119162389573209640804111261616771827989939551840471235079945175327536638365874717775169210186608268924244639016270610098894971732892267642318266405837012482726627199088381027028630711279130575230815976484191675172279903609489448225149181063260231957171204855841611039996959582465138269247794842445177715476581512709861409446684911276158067098438009067149531119008707418601627426255891/2063950098473886055933596136103014753954685977787179797499441692283103642150668140884348149132839387663291870239435604463778573480782766958396423322880804442523056530013282118705429274303746421980903580754656364533869319744640130831962767797772323836293079599182477171562218297208495122660799328579852852969560730744211066545295945803939271680397511478811389399527913043145952054883289558914237172406636283114284363301999238526952309439259354223729114988806937903509692118585280437646676248013406270664905997291670857985754768850507766359973207600149782819306010561088246502918148146264806947375101624011387317921439210509902170092173796154464078297852707797984007992277904626058467143192149921546030028316990855470478894515952884526783686210401408859364838148201339959570732480920969000913791571631154267939054105878236201498477027265774680071188764947522112650857013491135901945605796776829525789886482760578142306057177990048751864852763036720112071475134369179525117161001517868525821398753039187062869247457336940152614866298628205010037695017885878296140891234142925514925051385440766473260338168038302226808098439763889250948602137806546736025439919604390464712793474019469457135856879584745805794574609707742445431851999335443724488636749987837445626810087003490329257105472274738811579817454656532496370562155449815456374456838912258383282154811001588175608617475540639254689723629881619252699580383612847920348111900440075645703960104081690968807839189109040568288972353424306876947127635585164905071821419089229871978994388197349499565628906992171901547121903117815637249359328193980583892566359962066242217169190169986105579733710057404319381685578470983838597020624234209884597110721892707818651210378187525863009879314177842634871978427592746452643603586344401223449546482306838947819060455178762434166799996220143825677025686435609179225302671777326568324855229172912876656233006785717920665743720753617646617017219230313226844735567400507490772935145894670445831971526014183234960075574401616682479457962912905141754252265169682318523572680657053374002911007741991220001444440319448034755483178790032581428679303588017268970 0)) |
| 1183 |
|
| 1184 |
(let () |
| 1185 |
(define (test-minmax mi ma data) |
| 1186 |
(test* (format "min, max ~s" data) |
| 1187 |
(list mi ma) |
| 1188 |
(list (Apply min data) (Apply max data)))) |
| 1189 |
(test-minmax 0 10 '(3 10 2 0 5)) |
| 1190 |
(test-minmax -1/3 99/5 '(2 6 99/5 0 -1/6 -1/3)) |
| 1191 |
(test-minmax -10.0 10.0 '(3 10 2.0 -10 5)) |
| 1192 |
(test-minmax -inf.0 +inf.0 '(5 -inf.0 2 +inf.0 1)) |
| 1193 |
(test-minmax +nan.0 +nan.0 '(5 -inf.0 +nan.0 +inf.0 1)) |
| 1194 |
(test-minmax +nan.0 +nan.0 '(+nan.0 -inf.0 3 +inf.0 1)) |
| 1195 |
) |
| 1196 |
|
| 1197 |
;;================================================================== |
| 1198 |
;; Fixnum stuff |
| 1199 |
;; |
| 1200 |
|
| 1201 |
(test* "fixnum? fixnum" #t (fixnum? 0)) |
| 1202 |
(test* "fixnum? ratnum" #f (fixnum? 1/2)) |
| 1203 |
(test* "fixnum? bignum" #f (fixnum? (expt 2 256))) |
| 1204 |
(test* "fixnum? flonum" #f (fixnum? 3.14)) |
| 1205 |
(test* "fixnum? compnum" #f (fixnum? 1+3i)) |
| 1206 |
|
| 1207 |
(test* "fixnum? greatest" #t (fixnum? (greatest-fixnum))) |
| 1208 |
(test* "fixnum? greatest+1" #f (fixnum? (+ (greatest-fixnum) 1))) |
| 1209 |
(test* "fixnum? least" #t (fixnum? (least-fixnum))) |
| 1210 |
(test* "fixnum? least-1" #f (fixnum? (- (least-fixnum) 1))) |
| 1211 |
|
| 1212 |
(test* "greatest fixnum & width" (greatest-fixnum) |
| 1213 |
(- (ash 1 (- (fixnum-width) 1)) 1)) |
| 1214 |
(test* "least fixnum & width" (least-fixnum) |
| 1215 |
(- (ash 1 (- (fixnum-width) 1)))) |
| 1216 |
|
| 1217 |
;;================================================================== |
| 1218 |
;; Arithmetics |
| 1219 |
;; |
| 1220 |
|
| 1221 |
;;------------------------------------------------------------------ |
| 1222 |
(test-section "integer addition") |
| 1223 |
|
| 1224 |
(define x #xffffffff00000000ffffffff00000000) |
| 1225 |
(define xx (- x)) |
| 1226 |
(define y #x00000002000000000000000200000000) |
| 1227 |
(define yy (- y)) |
| 1228 |
(define z #x00000000000000010000000000000001) |
| 1229 |
(test* "bignum + bignum" #x100000001000000010000000100000000 |
| 1230 |
(+ x y)) |
| 1231 |
(test* "bignum + -bignum" #xfffffffd00000000fffffffd00000000 |
| 1232 |
(+ x yy)) |
| 1233 |
(test* "bignum - bignum" #xfffffffefffffffffffffffeffffffff |
| 1234 |
(- x z)) |
| 1235 |
(test* "bignum - bignum" x |
| 1236 |
(- (+ x y) y)) |
| 1237 |
(test* "-bignum + bignum" #x-fffffffd00000000fffffffd00000000 |
| 1238 |
(+ xx y)) |
| 1239 |
(test* "-bignum + -bignum" #x-100000001000000010000000100000000 |
| 1240 |
(+ xx yy)) |
| 1241 |
(test* "-bignum - bignum" #x-100000001000000010000000100000000 |
| 1242 |
(- xx y)) |
| 1243 |
(test* "-bignum - -bignum" #x-fffffffd00000000fffffffd00000000 |
| 1244 |
(- xx yy)) |
| 1245 |
|
| 1246 |
;; This test a possible shortcut in Scm_Add etc. We use apply |
| 1247 |
;; to avoid operators from being inlined. |
| 1248 |
(test* "0 + bignum" (list x x) |
| 1249 |
(list (Apply + (list 0 x)) (Apply + (list x 0)))) |
| 1250 |
(test* "0 - bignum" (list (- x) x) |
| 1251 |
(list (Apply - (list 0 x)) (Apply - (list x 0)))) |
| 1252 |
(test* "0 * bignum" (list 0 0) |
| 1253 |
(list (Apply * (list 0 x)) (Apply * (list x 0)))) |
| 1254 |
(test* "1 * bignum" (list x x) |
| 1255 |
(list (Apply * (list 1 x)) (Apply * (list x 1)))) |
| 1256 |
(test* "bignum / 1" x |
| 1257 |
(Apply / (list x 1))) |
| 1258 |
|
| 1259 |
;;------------------------------------------------------------------ |
| 1260 |
(test-section "small immediate integer constants") |
| 1261 |
|
| 1262 |
;; pushing small literal integer on the stack may be done |
| 1263 |
;; by combined instruction PUSHI. These test if it works. |
| 1264 |
|
| 1265 |
(define (foo a b c d e) (list a b c d e)) |
| 1266 |
|
| 1267 |
;; 2^19-1 |
| 1268 |
(test* "PUSHI" '(0 524287 524288 -524287 -524288) |
| 1269 |
(foo 0 524287 524288 -524287 -524288)) |
| 1270 |
;; 2^51-1 |
| 1271 |
(test* "PUSHI" '(0 2251799813685247 2251799813685248 |
| 1272 |
-2251799813685247 -2251799813685248 ) |
| 1273 |
(foo 0 2251799813685247 2251799813685248 |
| 1274 |
-2251799813685247 -2251799813685248)) |
| 1275 |
|
| 1276 |
;;------------------------------------------------------------------ |
| 1277 |
(test-section "small immediate integer additions") |
| 1278 |
|
| 1279 |
;; small literal integer x (-2^19 <= x < 2^19 on 32bit architecture) |
| 1280 |
;; in binary addition/subtraction is compiled in special instructuions, |
| 1281 |
;; NUMADDI and NUMSUBI. |
| 1282 |
|
| 1283 |
(define x 2) |
| 1284 |
(test* "NUMADDI" 5 (+ 3 x)) |
| 1285 |
(test* "NUMADDI" 5 (+ x 3)) |
| 1286 |
(test* "NUMADDI" 1 (+ -1 x)) |
| 1287 |
(test* "NUMADDI" 1 (+ x -1)) |
| 1288 |
(test* "NUMSUBI" 1 (- 3 x)) |
| 1289 |
(test* "NUMSUBI" -1 (- x 3)) |
| 1290 |
(test* "NUMSUBI" -5 (- -3 x)) |
| 1291 |
(test* "NUMSUBI" 5 (- x -3)) |
| 1292 |
(define x 2.0) |
| 1293 |
(test* "NUMADDI" 5.0 (+ 3 x)) |
| 1294 |
(test* "NUMADDI" 5.0 (+ x 3)) |
| 1295 |
(test* "NUMADDI" 1.0 (+ -1 x)) |
| 1296 |
(test* "NUMADDI" 1.0 (+ x -1)) |
| 1297 |
(test* "NUMSUBI" 1.0 (- 3 x)) |
| 1298 |
(test* "NUMSUBI" -1.0 (- x 3)) |
| 1299 |
(test* "NUMSUBI" -5.0 (- -3 x)) |
| 1300 |
(test* "NUMSUBI" 5.0 (- x -3)) |
| 1301 |
(define x #x100000000) |
| 1302 |
(test* "NUMADDI" #x100000003 (+ 3 x)) |
| 1303 |
(test* "NUMADDI" #x100000003 (+ x 3)) |
| 1304 |
(test* "NUMADDI" #xffffffff (+ -1 x)) |
| 1305 |
(test* "NUMADDI" #xffffffff (+ x -1)) |
| 1306 |
(test* "NUMSUBI" #x-fffffffd (- 3 x)) |
| 1307 |
(test* "NUMSUBI" #xfffffffd (- x 3)) |
| 1308 |
(test* "NUMSUBI" #x-100000003 (- -3 x)) |
| 1309 |
(test* "NUMSUBI" #x100000003 (- x -3)) |
| 1310 |
(define x 33/7) |
| 1311 |
(test* "NUMADDI" 54/7 (+ 3 x)) |
| 1312 |
(test* "NUMADDI" 54/7 (+ x 3)) |
| 1313 |
(test* "NUMADDI" 26/7 (+ -1 x)) |
| 1314 |
(test* "NUMADDI" 26/7 (+ x -1)) |
| 1315 |
(test* "NUMADDI" -12/7 (- 3 x)) |
| 1316 |
(test* "NUMADDI" 12/7 (- x 3)) |
| 1317 |
(test* "NUMADDI" -54/7 (- -3 x)) |
| 1318 |
(test* "NUMADDI" 54/7 (- x -3)) |
| 1319 |
|
| 1320 |
(test* "NUMADDI" 30 (+ 10 (if #t 20 25))) |
| 1321 |
(test* "NUMADDI" 30 (+ (if #t 20 25) 10)) |
| 1322 |
(test* "NUMADDI" 35 (+ 10 (if #f 20 25))) |
| 1323 |
(test* "NUMADDI" 35 (+ (if #f 20 25) 10)) |
| 1324 |
(test* "NUMADDI" 30 (let ((x #t)) (+ 10 (if x 20 25)))) |
| 1325 |
(test* "NUMADDI" 30 (let ((x #t)) (+ (if x 20 25) 10))) |
| 1326 |
(test* "NUMADDI" 35 (let ((x #f)) (+ 10 (if x 20 25)))) |
| 1327 |
(test* "NUMADDI" 35 (let ((x #f)) (+ (if x 20 25) 10))) |
| 1328 |
(test* "NUMADDI" 21 (+ 10 (do ((x 0 (+ x 1))) ((> x 10) x)))) |
| 1329 |
(test* "NUMADDI" 21 (+ (do ((x 0 (+ x 1))) ((> x 10) x)) 10)) |
| 1330 |
(test* "NUMSUBI" -10 (- 10 (if #t 20 25))) |
| 1331 |
(test* "NUMSUBI" 10 (- (if #t 20 25) 10)) |
| 1332 |
(test* "NUMSUBI" -15 (- 10 (if #f 20 25))) |
| 1333 |
(test* "NUMSUBI" 15 (- (if #f 20 25) 10)) |
| 1334 |
(test* "NUMSUBI" -10 (let ((x #t)) (- 10 (if x 20 25)))) |
| 1335 |
(test* "NUMSUBI" 10 (let ((x #t)) (- (if x 20 25) 10))) |
| 1336 |
(test* "NUMSUBI" -15 (let ((x #f)) (- 10 (if x 20 25)))) |
| 1337 |
(test* "NUMSUBI" 15 (let ((x #f)) (- (if x 20 25) 10))) |
| 1338 |
(test* "NUMSUBI" -1 (- 10 (do ((x 0 (+ x 1))) ((> x 10) x)))) |
| 1339 |
(test* "NUMSUBI" 1 (- (do ((x 0 (+ x 1))) ((> x 10) x)) 10)) |
| 1340 |
|
| 1341 |
;;------------------------------------------------------------------ |
| 1342 |
(test-section "immediate flonum integer arith") |
| 1343 |
|
| 1344 |
;; tests special instructions for immediate flonum integer arithmetic |
| 1345 |
|
| 1346 |
|
| 1347 |
(define x 2.0) |
| 1348 |
(test* "NUMADDF" 5.0 (+ 3 x)) |
| 1349 |
(test* "NUMADDF" 5.0 (+ x 3)) |
| 1350 |
(test* "NUMADDF" 1.0 (+ -1 x)) |
| 1351 |
(test* "NUMADDF" 1.0 (+ x -1)) |
| 1352 |
(test* "NUMADDF" 2.0+1.0i (+ +i x)) |
| 1353 |
(test* "NUMADDF" 2.0+1.0i (+ x +i)) |
| 1354 |
|
| 1355 |
(test* "NUMSUBF" 1.0 (- 3 x)) |
| 1356 |
(test* "NUMSUBF" -1.0 (- x 3)) |
| 1357 |
(test* "NUMSUBF" -5.0 (- -3 x)) |
| 1358 |
(test* "NUMSUBF" 5.0 (- x -3)) |
| 1359 |
(test* "NUMSUBF" -2.0+1.0i (- +i x)) |
| 1360 |
(test* "NUMSUBF" 2.0-1.0i (- x +i)) |
| 1361 |
|
| 1362 |
(test* "NUMMULF" 4.0 (* x 2)) |
| 1363 |
(test* "NUMMULF" 4.0 (* 2 x)) |
| 1364 |
(test* "NUMMULF" 3.0 (* x 1.5)) |
| 1365 |
(test* "NUMMULF" 3.0 (* 1.5 x)) |
| 1366 |
(test* "NUMMULF" 0+2.0i (* x +i)) |
| 1367 |
(test* "NUMMULF" 0+2.0i (* +i x)) |
| 1368 |
|
| 1369 |
(test* "NUMDIVF" 0.5 (/ x 4)) |
| 1370 |
(test* "NUMDIVF" 2.0 (/ 4 x)) |
| 1371 |
(test* "NUMDIVF" 0.5 (/ x 4.0)) |
| 1372 |
(test* "NUMDIVF" 2.0 (/ 4.0 x)) |
| 1373 |
(test* "NUMDIVF" 0.0-0.5i (/ x +4i)) |
| 1374 |
(test* "NUMDIVF" 0.0+2.0i (/ +4i x)) |
| 1375 |
|
| 1376 |
;;------------------------------------------------------------------ |
| 1377 |
(test-section "rational number addition") |
| 1378 |
|
| 1379 |
(test* "ratnum +" 482/247 (+ 11/13 21/19)) |
| 1380 |
(test* "ratnum -" -64/247 (- 11/13 21/19)) |
| 1381 |
|
| 1382 |
;; tests possible shortcut in Scm_Add etc. |
| 1383 |
(test* "ratnum + 0" (list 11/13 11/13) |
| 1384 |
(list (Apply + '(0 11/13)) (Apply + '(11/13 0)))) |
| 1385 |
(test* "ratnum - 0" (list -11/13 11/13) |
| 1386 |
(list (Apply - '(0 11/13)) (Apply - '(11/13 0)))) |
| 1387 |
(test* "ratnum * 0" (list 0 0) |
| 1388 |
(list (Apply * '(0 11/13)) (Apply * '(11/13 0)))) |
| 1389 |
(test* "ratnum * 1" (list 11/13 11/13) |
| 1390 |
(list (Apply * '(1 11/13)) (Apply * '(11/13 1)))) |
| 1391 |
(test* "ratnum / 1" 11/13 |
| 1392 |
(Apply / '(11/13 1))) |
| 1393 |
|
| 1394 |
;;------------------------------------------------------------------ |
| 1395 |
(test-section "promotions in addition") |
| 1396 |
|
| 1397 |
(define-syntax +-tester |
| 1398 |
(syntax-rules () |
| 1399 |
((_ (+ . args)) |
| 1400 |
(let ((inline (+ . args)) |
| 1401 |
(other (Apply + 'args))) |
| 1402 |
(and (= inline other) |
| 1403 |
(list inline (exact? inline))))))) |
| 1404 |
|
| 1405 |
(test* "+" '(0 #t) (+-tester (+))) |
| 1406 |
(test* "+" '(1 #t) (+-tester (+ 1))) |
| 1407 |
(test* "+" '(3 #t) (+-tester (+ 1 2))) |
| 1408 |
(test* "+" '(6 #t) (+-tester (+ 1 2 3))) |
| 1409 |
(test* "+" '(1 #t) (+-tester (+ 1/6 1/3 1/2))) |
| 1410 |
(test* "+" '(1.0 #f) (+-tester (+ 1.0))) |
| 1411 |
(test* "+" '(3.0 #f) (+-tester (+ 1.0 2))) |
| 1412 |
(test* "+" '(3.0 #f) (+-tester (+ 1 2.0))) |
| 1413 |
(test* "+" '(6.0 #f) (+-tester (+ 1 2 3.0))) |
| 1414 |
(test* "+" '(1.0 #f) (+-tester (+ 1/6 1/3 0.5))) |
| 1415 |
(test* "+" '(1+i #f) (+-tester (+ 1 +i))) |
| 1416 |
(test* "+" '(3+i #f) (+-tester (+ 1 2 +i))) |
| 1417 |
(test* "+" '(3+i #f) (+-tester (+ +i 1 2))) |
| 1418 |
(test* "+" '(3+i #f) (+-tester (+ 1.0 2 +i))) |
| 1419 |
(test* "+" '(3+i #f) (+-tester (+ +i 1.0 2))) |
| 1420 |
(test* "+" '(4294967298.0 #f) (+-tester (+ 4294967297 1.0))) |
| 1421 |
(test* "+" '(4294967299.0 #f) (+-tester (+ 4294967297 1 1.0))) |
| 1422 |
(test* "+" '(4294967298.0-i #f) (+-tester (+ 4294967297 1.0 -i))) |
| 1423 |
(test* "+" '(4294967298.0-i #f) (+-tester (+ -i 4294967297 1.0))) |
| 1424 |
(test* "+" '(4294967298.0-i #f) (+-tester (+ 1.0 4294967297 -i))) |
| 1425 |
|
| 1426 |
;;------------------------------------------------------------------ |
| 1427 |
(test-section "integer multiplication") |
| 1428 |
|
| 1429 |
(define (m-result x) (list x (- x) (- x) x x (- x) (- x) x)) |
| 1430 |
(define (m-tester x y) |
| 1431 |
(list (* x y) (* (- x) y) (* x (- y)) (* (- x) (- y)) |
| 1432 |
(Apply * (list x y)) (Apply * (list (- x) y)) |
| 1433 |
(Apply * (list x (- y))) (Apply * (list (- x) (- y))))) |
| 1434 |
|
| 1435 |
(test* "fix*fix->big[1]" (m-result 727836879) |
| 1436 |
(m-tester 41943 17353)) |
| 1437 |
(test* "fix*fix->big[1]" (m-result 3663846879) |
| 1438 |
(m-tester 41943 87353)) |
| 1439 |
(test* "fix*fix->big[2]" (m-result 4294967296) |
| 1440 |
(m-tester 65536 65536)) |
| 1441 |
(test* "fix*fix->big[2]" (m-result 366384949959) |
| 1442 |
(m-tester 4194303 87353)) |
| 1443 |
(test* "fix*big[1]->big[1]" (m-result 3378812463) |
| 1444 |
(m-tester 3 1126270821)) |
| 1445 |
(test* "fix*big[1]->big[2]" (m-result 368276265762816) |
| 1446 |
(m-tester 85746 4294967296)) |
| 1447 |
(test* "big[1]*fix->big[1]" (m-result 3378812463) |
| 1448 |
(m-tester 1126270821 3)) |
| 1449 |
(test* "big[1]*fix->big[2]" (m-result 368276265762816) |
| 1450 |
(m-tester 4294967296 85746)) |
| 1451 |
(test* "big[2]*fix->big[2]" (m-result 12312849128741) |
| 1452 |
(m-tester 535341266467 23)) |
| 1453 |
(test* "big[1]*big[1]->big[2]" (m-result 1345585795375391817) |
| 1454 |
(m-tester 1194726677 1126270821)) |
| 1455 |
|
| 1456 |
;; Large number multiplication test using Fermat's number |
| 1457 |
;; The decomposition of Fermat's number is taken from |
| 1458 |
;; http://www.dd.iij4u.or.jp/~okuyamak/Information/Fermat.html |
| 1459 |
(test* "fermat(7)" (fermat 7) |
| 1460 |
(* 59649589127497217 5704689200685129054721)) |
| 1461 |
(test* "fermat(8)" (fermat 8) |
| 1462 |
(* 1238926361552897 |
| 1463 |
93461639715357977769163558199606896584051237541638188580280321)) |
| 1464 |
(test* "fermat(9)" (fermat 9) |
| 1465 |
(* 2424833 |
| 1466 |
7455602825647884208337395736200454918783366342657 |
| 1467 |
741640062627530801524787141901937474059940781097519023905821316144415759504705008092818711693940737)) |
| 1468 |
(test* "fermat(10)" (fermat 10) |
| 1469 |
(* 45592577 |
| 1470 |
6487031809 |
| 1471 |
4659775785220018543264560743076778192897 |
| 1472 |
130439874405488189727484768796509903946608530841611892186895295776832416251471863574140227977573104895898783928842923844831149032913798729088601617946094119449010595906710130531906171018354491609619193912488538116080712299672322806217820753127014424577 |
| 1473 |
)) |
| 1474 |
(test* "fermat(11)" (fermat 11) |
| 1475 |
(* 319489 |
| 1476 |
974849 |
| 1477 |
167988556341760475137 |
| 1478 |
3560841906445833920513 |
| 1479 |
173462447179147555430258970864309778377421844723664084649347019061363579192879108857591038330408837177983810868451546421940712978306134189864280826014542758708589243873685563973118948869399158545506611147420216132557017260564139394366945793220968665108959685482705388072645828554151936401912464931182546092879815733057795573358504982279280090942872567591518912118622751714319229788100979251036035496917279912663527358783236647193154777091427745377038294584918917590325110939381322486044298573971650711059244462177542540706913047034664643603491382441723306598834177 |
| 1480 |
)) |
| 1481 |
|
| 1482 |
;;------------------------------------------------------------------ |
| 1483 |
(test-section "multiplication short cuts") |
| 1484 |
|
| 1485 |
;; these test shortcut in Scm_Mul |
| 1486 |
;; note the difference of 0 and 0.0 |
| 1487 |
(let1 big 100000000000000000000 |
| 1488 |
(test* "bignum * 0" 0 (Apply * `(,big 0)) eqv?) |
| 1489 |
(test* "0 * bignum" 0 (Apply * `(0 ,big)) eqv?) |
| 1490 |
(test* "bignum * 1" big (Apply * `(,big 1)) eqv?) |
| 1491 |
(test* "1 * bignum" big (Apply * `(1 ,big)) eqv?) |
| 1492 |
|
| 1493 |
(test* "bignum * 0.0" 0.0 (Apply * `(,big 0.0)) eqv?) |
| 1494 |
(test* "0.0 * bignum" 0.0 (Apply * `(0.0 ,big)) eqv?) |
| 1495 |
(test* "bignum * 1.0" 1.0e20 (Apply * `(,big 1.0)) eqv?) |
| 1496 |
(test* "1.0 * bignum" 1.0e20 (Apply * `(1.0 ,big)) eqv?) |
| 1497 |
|
| 1498 |
;; 2^n shortcut |
| 1499 |
(dotimes [n 64] |
| 1500 |
(test* (format "bignum * 2^~a" n) |
| 1501 |
(ash big n) |
| 1502 |
(Apply * `(,big ,(expt 2 n))) |
| 1503 |
eqv?) |
| 1504 |
(test* (format "2^~a & bignum" n) |
| 1505 |
(ash big n) |
| 1506 |
(Apply * `(,(expt 2 n) ,big)) |
| 1507 |
eqv?)) |
| 1508 |
) |
| 1509 |
|
| 1510 |
(test* "ratnum * 0" 0 (Apply * '(1/2 0)) eqv?) |
| 1511 |
(test* "0 * ratnum" 0 (Apply * '(0 1/2)) eqv?) |
| 1512 |
(test* "ratnum * 1" 1/2 (Apply * '(1/2 1)) eqv?) |
| 1513 |
(test* "1 * ratnum" 1/2 (Apply * '(1 1/2)) eqv?) |
| 1514 |
|
| 1515 |
(test* "ratnum * 0.0" 0.0 (Apply * '(1/2 0.0)) eqv?) |
| 1516 |
(test* "0.0 * ratnum" 0.0 (Apply * '(0.0 1/2)) eqv?) |
| 1517 |
(test* "ratnum * 1.0" 0.5 (Apply * '(1/2 1.0)) eqv?) |
| 1518 |
(test* "1.0 * ratnum" 0.5 (Apply * '(1.0 1/2)) eqv?) |
| 1519 |
|
| 1520 |
(test* "flonum * 0" 0 (Apply * '(3.0 0)) eqv?) |
| 1521 |
(test* "0 * flonum" 0 (Apply * '(0 3.0)) eqv?) |
| 1522 |
(test* "flonum * 1" 3.0 (Apply * '(3.0 1)) eqv?) |
| 1523 |
(test* "1 * flonum" 3.0 (Apply * '(1 3.0)) eqv?) |
| 1524 |
|
| 1525 |
(test* "flonum * 0.0" 0.0 (Apply * '(3.0 0.0)) eqv?) |
| 1526 |
(test* "0.0 * flonum" 0.0 (Apply * '(0.0 3.0)) eqv?) |
| 1527 |
(test* "flonum * 1.0" 3.0 (Apply * '(3.0 1.0)) eqv?) |
| 1528 |
(test* "1.0 * flonum" 3.0 (Apply * '(1.0 3.0)) eqv?) |
| 1529 |
|
| 1530 |
(test* "compnum * 0" 0 (* 0 +i) eqv?) |
| 1531 |
(test* "0 * compnum" 0 (* +i 0) eqv?) |
| 1532 |
(test* "compnum * 1" +i (* 1 +i) eqv?) |
| 1533 |
(test* "1 * compnum" +i (* +i 1) eqv?) |
| 1534 |
|
| 1535 |
(test* "compnum * 0.0" 0.0 (* 0.0 +i) eqv?) |
| 1536 |
(test* "0.0 * compnum" 0.0 (* +i 0.0) eqv?) |
| 1537 |
(test* "compnum * 1.0" +i (* 1.0 +i) eqv?) |
| 1538 |
(test* "1.0 * compnum" +i (* +i 1.0) eqv?) |
| 1539 |
|
| 1540 |
;;------------------------------------------------------------------ |
| 1541 |
(test-section "division") |
| 1542 |
|
| 1543 |
(test* "exact division" 3/20 (/ 3 4 5)) |
| 1544 |
(test* "exact division" 1/2 (/ 9223372036854775808 18446744073709551616)) |
| 1545 |
(test* "exact division" 4692297364841/7 |
| 1546 |
(/ 28153784189046 42)) |
| 1547 |
(test* "exact division" 7/4692297364841 |
| 1548 |
(/ 42 28153784189046)) |
| 1549 |
(test* "exact division" -7/4692297364841 |
| 1550 |
(/ 42 -28153784189046)) |
| 1551 |
(test* "exact division" 7/4692297364841 |
| 1552 |
(/ -42 -28153784189046)) |
| 1553 |
(test* "exact reciprocal" 1/3 (/ 3)) |
| 1554 |
(test* "exact reciprocal" -1/3 (/ -3)) |
| 1555 |
(test* "exact reciprocal" 5/6 (/ 6/5)) |
| 1556 |
(test* "exact reciprocal" -5/6 (/ -6/5)) |
| 1557 |
(test* "exact reciprocal" 7/4692297364841 (/ 4692297364841/7)) |
| 1558 |
|
| 1559 |
;; avoid inlining |
| 1560 |
(define (divide . args) |
| 1561 |
(Apply / args)) |
| 1562 |
|
| 1563 |
(define (divide. . args) |
| 1564 |
(Apply /. args)) |
| 1565 |
|
| 1566 |
(test* "division by zero" (test-error) (divide 0)) |
| 1567 |
(test* "division by zero" (test-error) (divide 0 0)) |
| 1568 |
(test* "division by zero" (test-error) (divide 3 0)) |
| 1569 |
(test* "division by zero" (test-error) (divide 1/2 0)) |
| 1570 |
(test* "division by zero" +inf.0 (divide 0.0)) |
| 1571 |
(test* "division by zero" #t (nan? (divide 0.0 0))) |
| 1572 |
(test* "division by zero" #t (nan? (divide 0 0.0))) |
| 1573 |
(test* "division by zero" #t (nan? (divide 0.0 0.0))) |
| 1574 |
(test* "division by zero" +inf.0 (divide 0.5 0)) |
| 1575 |
|
| 1576 |
(test* "division by zero" +inf.0 (divide. 0)) |
| 1577 |
(test* "division by zero" #t (nan? (divide. 0 0))) |
| 1578 |
(test* "division by zero" +inf.0 (divide. 3 0)) |
| 1579 |
(test* "division by zero" +inf.0 (divide. 1/2 0)) |
| 1580 |
(test* "division by zero" #t (nan? (divide. 0.0 0))) |
| 1581 |
(test* "division by zero" #t (nan? (divide. 0 0.0))) |
| 1582 |
(test* "division by zero" #t (nan? (divide. 0.0 0.0))) |
| 1583 |
(test* "division by zero" #t (nan? (divide. +nan.0 0))) |
| 1584 |
(test* "division by zero" #t (nan? (divide. +nan.0 0.0))) |
| 1585 |
(test* "division by zero" +inf.0 (divide. 0.5 0)) |
| 1586 |
|
| 1587 |
(test* "division by zero" +inf.0+inf.0i (/ 1+2i 0.0)) |
| 1588 |
(test* "division by zero" +inf.0-inf.0i (/ 1-2i 0.0)) |
| 1589 |
(test* "division by zero" -inf.0+inf.0i (/ -1+2i 0.0)) |
| 1590 |
(test* "division by zero" -inf.0-inf.0i (/ -1-2i 0.0)) |
| 1591 |
|
| 1592 |
(test* "division by zero" #t |
| 1593 |
(let ((r (/ 0+1i 0))) |
| 1594 |
(and (nan? (real-part r)) |
| 1595 |
(= (imag-part r) +inf.0)))) |
| 1596 |
|
| 1597 |
(test* "division by zero" #t |
| 1598 |
(let ((r (/ 0+1i 0.0))) |
| 1599 |
(and (nan? (real-part r)) |
| 1600 |
(= (imag-part r) +inf.0)))) |
| 1601 |
|
| 1602 |
;; See if we don't fold exact divide-by-zero case. If compile blindly |
| 1603 |
;; fold constant division, the following causes compile-time error |
| 1604 |
;; rather than runtime error. |
| 1605 |
(let () |
| 1606 |
(define (recip x) (or x (/ 0))) |
| 1607 |
(define (two x) (or x (/ 2 0))) |
| 1608 |
(define (three x) (or x (/ 2 4 0))) |
| 1609 |
(define (four x) (or x (/ 2 0 4 3))) |
| 1610 |
|
| 1611 |
(define (tests exp arg) |
| 1612 |
(test* "div-by-zero constant folding 1" exp (recip arg)) |
| 1613 |
(test* "div-by-zero constant folding 2" exp (two arg)) |
| 1614 |
(test* "div-by-zero constant folding 3" exp (three arg)) |
| 1615 |
(test* "div-by-zero constant folding 4" exp (four arg))) |
| 1616 |
|
| 1617 |
(tests 10 10) |
| 1618 |
(tests (test-error) #f) |
| 1619 |
) |
| 1620 |
|
| 1621 |
(define (d-result x exact?) (list x (- x) (- x) x exact?)) |
| 1622 |
(define (d-tester x y) |
| 1623 |
(list (/ x y) (/ (- x) y) (/ x (- y)) (/ (- x) (- y)) |
| 1624 |
(exact? (/ x y)))) |
| 1625 |
|
| 1626 |
;; inexact division |
| 1627 |
(test* "exact/inexact -> inexact" (d-result 3.25 #f) |
| 1628 |
(d-tester 13 4.0)) |
| 1629 |
(test* "exact/inexact -> inexact" (d-result 1.625 #f) |
| 1630 |
(d-tester 13/2 4.0)) |
| 1631 |
(test* "inexact/exact -> inexact" (d-result 3.25 #f) |
| 1632 |
(d-tester 13.0 4)) |
| 1633 |
(test* "inexact/exact -> inexact" (d-result 9.75 #f) |
| 1634 |
(d-tester 13.0 4/3)) |
| 1635 |
(test* "inexact/inexact -> inexact" (d-result 3.25 #f) |
| 1636 |
(d-tester 13.0 4.0)) |
| 1637 |
|
| 1638 |
;; complex division |
| 1639 |
(test* "complex division" 0.0 |
| 1640 |
(let ((a 3) |
| 1641 |
(b 4+3i) |
| 1642 |
(c 7.3)) |
| 1643 |
(- (/ a b c) |
| 1644 |
(/ (/ a b) c)))) |
| 1645 |
|
| 1646 |
;;------------------------------------------------------------------ |
| 1647 |
(test-section "quotient") |
| 1648 |
|
| 1649 |
(define (q-result x exact?) (list x (- x) (- x) x exact?)) |
| 1650 |
(define (q-tester x y) |
| 1651 |
(list (quotient x y) (quotient (- x) y) |
| 1652 |
(quotient x (- y)) (quotient (- x) (- y)) |
| 1653 |
(exact? (quotient x y)))) |
| 1654 |
|
| 1655 |
;; these uses BignumDivSI -> bignum_sdiv |
| 1656 |
(test* "big[1]/fix->fix" (q-result 17353 #t) |
| 1657 |
(q-tester 727836879 41943)) |
| 1658 |
(test* "big[1]/fix->fix" (q-result 136582 #t) |
| 1659 |
(q-tester 3735928559 27353)) |
| 1660 |
(test* "big[2]/fix->big[1]" (q-result 535341266467 #t) |
| 1661 |
(q-tester 12312849128741 23)) |
| 1662 |
(test* "big[2]/fix->big[2]" (q-result 12312849128741 #t) |
| 1663 |
(q-tester 12312849128741 1)) |
| 1664 |
|
| 1665 |
;; these uses BignumDivSI -> bignum_gdiv |
| 1666 |
(test* "big[1]/fix->fix" (q-result 41943 #t) |
| 1667 |
(q-tester 3663846879 87353)) |
| 1668 |
(test* "big[2]/fix->fix" (q-result 19088743 #t) |
| 1669 |
(q-tester 705986470884353 36984440)) |
| 1670 |
(test* "big[2]/fix->fix" (q-result 92894912 #t) |
| 1671 |
(q-tester 12312849128741 132546)) |
| 1672 |
(test* "big[2]/fix->big[1]" (q-result 2582762030 #t) |
| 1673 |
(q-tester 425897458766735 164900)) |
| 1674 |
|
| 1675 |
;; these uses BignumDivRem |
| 1676 |
(test* "big[1]/big[1]->fix" (q-result 2 #t) |
| 1677 |
(q-tester 4020957098 1952679221)) |
| 1678 |
(test* "big[1]/big[1] -> fix" (q-result 0 #t) |
| 1679 |
(q-tester 1952679221 4020957098)) |
| 1680 |
;; this tests loop in estimation phase |
| 1681 |
(test* "big[3]/big[2] -> big[1]" (q-result #xffff0001 #t) |
| 1682 |
(q-tester #x10000000000000000 #x10000ffff)) |
| 1683 |
;; this test goes through a rare case handling code ("add back") in |
| 1684 |
;; the algorithm. |
| 1685 |
(test* "big[3]/big[2] -> fix" (q-result #xeffe #t) |
| 1686 |
(q-tester #x7800000000000000 #x80008889ffff)) |
| 1687 |
|
| 1688 |
;; inexact quotient |
| 1689 |
(test* "exact/inexact -> inexact" (q-result 3.0 #f) |
| 1690 |
(q-tester 13 4.0)) |
| 1691 |
(test* "inexact/exact -> inexact" (q-result 3.0 #f) |
| 1692 |
(q-tester 13.0 4)) |
| 1693 |
(test* "inexact/inexact -> inexact" (q-result 3.0 #f) |
| 1694 |
(q-tester 13.0 4.0)) |
| 1695 |
(test* "exact/inexact -> inexact" (q-result 17353.0 #f) |
| 1696 |
(q-tester 727836879 41943.0)) |
| 1697 |
(test* "inexact/exact -> inexact" (q-result 17353.0 #f) |
| 1698 |
(q-tester 727836879.0 41943)) |
| 1699 |
(test* "inexact/inexact -> inexact" (q-result 17353.0 #f) |
| 1700 |
(q-tester 727836879.0 41943.0)) |
| 1701 |
|
| 1702 |
;; Test by fermat numbers |
| 1703 |
(test* "fermat(7)" 59649589127497217 |
| 1704 |
(quotient (fermat 7) 5704689200685129054721)) |
| 1705 |
(test* "fermat(8)" 1238926361552897 |
| 1706 |
(quotient (fermat 8) 93461639715357977769163558199606896584051237541638188580280321)) |
| 1707 |
(test* "fermat(9)" 2424833 |
| 1708 |
(quotient (quotient (fermat 9) 7455602825647884208337395736200454918783366342657) |
| 1709 |
741640062627530801524787141901937474059940781097519023905821316144415759504705008092818711693940737)) |
| 1710 |
(test* "fermat(10)" 4659775785220018543264560743076778192897 |
| 1711 |
(quotient (quotient (quotient (fermat 10) |
| 1712 |
130439874405488189727484768796509903946608530841611892186895295776832416251471863574140227977573104895898783928842923844831149032913798729088601617946094119449010595906710130531906171018354491609619193912488538116080712299672322806217820753127014424577) |
| 1713 |
6487031809) |
| 1714 |
45592577)) |
| 1715 |
(test* "fermat(11)" 3560841906445833920513 |
| 1716 |
(quotient (quotient (quotient (quotient (fermat 11) |
| 1717 |
167988556341760475137) |
| 1718 |
173462447179147555430258970864309778377421844723664084649347019061363579192879108857591038330408837177983810868451546421940712978306134189864280826014542758708589243873685563973118948869399158545506611147420216132557017260564139394366945793220968665108959685482705388072645828554151936401912464931182546092879815733057795573358504982279280090942872567591518912118622751714319229788100979251036035496917279912663527358783236647193154777091427745377038294584918917590325110939381322486044298573971650711059244462177542540706913047034664643603491382441723306598834177 |
| 1719 |
) |
| 1720 |
974849) |
| 1721 |
319489)) |
| 1722 |
|
| 1723 |
;;------------------------------------------------------------------ |
| 1724 |
(test-section "modulo and remainder") |
| 1725 |
|
| 1726 |
;; invariance: |
| 1727 |
;; suppose (modulo x y) = m |
| 1728 |
;; (remainder x y) = r |
| 1729 |
;; (quotient x y) = q |
| 1730 |
;; then |
| 1731 |
;; (sign r) = (sign x) |
| 1732 |
;; (sign m) = (sign y) |
| 1733 |
;; (+ (* q y) r) = x |
| 1734 |
;; (+ (* q y) m) = x if (sign (* x y)) >= 0, |
| 1735 |
;; (+ x y) if (sign (* x y)) < 0 |
| 1736 |
;; (exact? m) = (and (exact? x) (exact? y)) |
| 1737 |
;; (exact? r) = (and (exact? x) (exact? y)) |
| 1738 |
|
| 1739 |
(define (mr-tester msg x y) |
| 1740 |
(define (sign a) (cond [(< a 0) -1] [(= a 0) 0] [else 1])) |
| 1741 |
(define (mr-tester-1 x y) |
| 1742 |
;; We don't use quotient&remainder, for we want to test remainder-only path. |
| 1743 |
(let1 adjust-exactness (if (and (exact? x) (exact? y)) exact inexact) |
| 1744 |
(test* (format "modulo and remainder (~a) ~s ~s" msg x y) |
| 1745 |
`(:rem (,(sign x) |
| 1746 |
,(adjust-exactness x) |
| 1747 |
,(and (exact? x) (exact? y))) |
| 1748 |
:mod (,(sign y) |
| 1749 |
,(adjust-exactness (if (>= (sign (* x y)) 0) x (+ x y))) |
| 1750 |
,(and (exact? x) (exact? y)))) |
| 1751 |
(let ([q (quotient x y)] |
| 1752 |
[m (modulo x y)] |
| 1753 |
[r (remainder x y)] |
| 1754 |
[e? (and (exact? x) (exact? y))]) |
| 1755 |
`(:rem (,(sign r) ,(+ (* q y) r) ,(exact? r)) |
| 1756 |
:mod (,(sign m) ,(+ (* q y) m) ,(exact? m))))))) |
| 1757 |
(mr-tester-1 x y) |
| 1758 |
(mr-tester-1 (- x) y) |
| 1759 |
(mr-tester-1 x (- y)) |
| 1760 |
(mr-tester-1 (- x) (- y))) |
| 1761 |
|
| 1762 |
(mr-tester "fix op fix -> fix" 13 4) |
| 1763 |
(mr-tester "fix op fix -> fix" 1234 87935) |
| 1764 |
(mr-tester "fix op big -> fix" 12345 3735928559) ;32bit |
| 1765 |
(mr-tester "fix op big -> fix" 8478574387345 #x7fffffffffffffff) ;64bit |
| 1766 |
|
| 1767 |
;; These go through Scm_BignumRemSI |
| 1768 |
(mr-tester "big op fix -> fix" #x7f245637 41943) ;32bit |
| 1769 |
(mr-tester "big op fix -> fix" #x7f787486ff73cacb 41943) ;64bit |
| 1770 |
(mr-tester "big2 op big1 -> big1" #x9aa9bbcb #x50053343) ; 32bit |
| 1771 |
(mr-tester "big2 op big1 -> big1" #x9aa9bbcb10013303 #x50053343cafebabe) ; 64bit |
| 1772 |
|
| 1773 |
;; These go through BignumDivRem |
| 1774 |
(mr-tester "big op big -> big" |
| 1775 |
#x78ab76d8aa7787a78963556174babdccade44e54e543232ab |
| 1776 |
#xabcbdbdbcbdabefbebfbebbbababba) |
| 1777 |
|
| 1778 |
;; this tests loop in estimation phase (32bit) |
| 1779 |
(mr-tester "big[3] rem big[2] -> big[1]" #x10000000000000000 #x10000ffff) |
| 1780 |
;; this tests "add back" code (32bit) |
| 1781 |
(mr-tester "big[3] rem big[2] -> big[2]" #x7800000000000000 #x80008889ffff) |
| 1782 |
|
| 1783 |
;; Inexact |
| 1784 |
(mr-tester "exact rem inexact -> inexact" 13 4.0) |
| 1785 |
(mr-tester "inexact rem exact -> inexact" 13.0 4) |
| 1786 |
(mr-tester "inexact rem inexact -> inexact" 13.0 4.0) |
| 1787 |
(mr-tester "exact rem inexact -> inexact" 3735928559 27353.0) |
| 1788 |
(mr-tester "inexact rem exact -> inexact" 3735928559.0 27353) |
| 1789 |
(mr-tester "inexact rem inexact -> inexact" 3735928559.0 27353.0) |
| 1790 |
|
| 1791 |
;; test by mersenne prime? - code by 'hipster' |
| 1792 |
|
| 1793 |
(define (mersenne-prime? p) |
| 1794 |
(let ((m (- (expt 2 p) 1))) |
| 1795 |
(do ((i 3 (+ i 1)) |
| 1796 |
(s 4 (modulo (- (* s s) 2) m))) |
| 1797 |
((= i (+ p 1)) (= s 0))))) |
| 1798 |
|
| 1799 |
(test* "mersenne prime" |
| 1800 |
'(#t #t #t #t #t #t #t #t #t #t #t #t #t #t) |
| 1801 |
(map mersenne-prime? '(3 5 7 13 17 19 31 61 89 107 127 521 607 1279))) |
| 1802 |
|
| 1803 |
;;------------------------------------------------------------------ |
| 1804 |
(test-section "quotient&remainder") |
| 1805 |
|
| 1806 |
(let () |
| 1807 |
(define (check x y) |
| 1808 |
(test* (format "quotient&remainder ~s ~s" x y) |
| 1809 |
(list (quotient x y) (remainder x y)) |
| 1810 |
(receive (q r) (quotient&remainder x y) (list q r)))) |
| 1811 |
(define (do-quadrants p) |
| 1812 |
(lambda (x y) (p x y) (p (- x) y) (p x (- y)) (p (- x) (- y)))) |
| 1813 |
(define do-exactness |
| 1814 |
(let1 p (do-quadrants check) |
| 1815 |
(lambda (x y) (p x y) (p (inexact x) y) (p x (inexact y))))) |
| 1816 |
|
| 1817 |
(do-exactness 3 2) |
| 1818 |
(do-exactness 7 3) |
| 1819 |
(do-exactness 7 9) |
| 1820 |
) |
| 1821 |
|
| 1822 |
;;------------------------------------------------------------------ |
| 1823 |
(test-section "div and mod") |
| 1824 |
|
| 1825 |
(let () |
| 1826 |
(define (do-quadrants proc) |
| 1827 |
(lambda (x y =) |
| 1828 |
(proc x y =) |
| 1829 |
(proc (- x) y =) |
| 1830 |
(proc x (- y) =) |
| 1831 |
(proc (- x) (- y) =))) |
| 1832 |
|
| 1833 |
(define (test-div x y =) |
| 1834 |
(test* (format "~a div ~a" x y) '(#t #t) |
| 1835 |
(receive (d m) (div-and-mod x y) |
| 1836 |
(let1 z (+ (* d y) m) |
| 1837 |
(list (or (= x z) z) |
| 1838 |
(or (and (<= 0 m) (< m (abs y))) m)))))) |
| 1839 |
|
| 1840 |
(define (test-div0 x y =) |
| 1841 |
(test* (format "~a div0 ~a" x y) '(#t #t) |
| 1842 |
(receive (d m) (div0-and-mod0 x y) |
| 1843 |
(let1 z (+ (* d y) m) |
| 1844 |
(list (or (= x z) z) |
| 1845 |
(or (and (<= (- (abs y)) (* m 2)) |
| 1846 |
(< (* m 2) (abs y))) |
| 1847 |
m)))))) |
| 1848 |
|
| 1849 |
((do-quadrants test-div) 3 2 =) |
| 1850 |
((do-quadrants test-div) 3.0 2 (lambda (a b) (approx=? a b))) |
| 1851 |
((do-quadrants test-div) 123 10 =) |
| 1852 |
((do-quadrants test-div) 123.0 10.0 (lambda (a b) (approx=? a b))) |
| 1853 |
((do-quadrants test-div) 123/7 10/7 =) |
| 1854 |
((do-quadrants test-div) 123/7 5 =) |
| 1855 |
((do-quadrants test-div) 123 5/7 =) |
| 1856 |
((do-quadrants test-div) 130.75 10.5 =) |
| 1857 |
|
| 1858 |
((do-quadrants test-div0) 123 10 =) |
| 1859 |
((do-quadrants test-div0) 129 10 =) |
| 1860 |
((do-quadrants test-div0) 123.0 10.0 (lambda (a b) (approx=? a b))) |
| 1861 |
((do-quadrants test-div0) 129.0 10.0 (lambda (a b) (approx=? a b))) |
| 1862 |
((do-quadrants test-div0) 123/7 10/7 =) |
| 1863 |
((do-quadrants test-div0) 129/7 10/7 =) |
| 1864 |
((do-quadrants test-div0) 121/7 5 =) |
| 1865 |
((do-quadrants test-div0) 124/7 5 =) |
| 1866 |
((do-quadrants test-div0) 121 5/7 =) |
| 1867 |
((do-quadrants test-div0) 124 5/7 =) |
| 1868 |
((do-quadrants test-div0) 130.75 10.5 =) |
| 1869 |
((do-quadrants test-div0) 129.75 10.5 =) |
| 1870 |
) |
| 1871 |
|
| 1872 |
|
| 1873 |
;;------------------------------------------------------------------ |
| 1874 |
(test-section "absolute values") |
| 1875 |
|
| 1876 |
(test* "abs (minimum negative of 30-bit wide fixnum)" (expt 2 29) (abs (- (expt 2 29)))) |
| 1877 |
(test* "abs (minimum negative of 62-bit wide fixnum)" (expt 2 61) (abs (- (expt 2 61)))) |
| 1878 |
|
| 1879 |
(test* "abs (negative zero)" (/ (abs -0.0)) +inf.0) |
| 1880 |
|
| 1881 |
(test* "magnitude (exact real)" 1 (magnitude 1)) |
| 1882 |
|
| 1883 |
|
| 1884 |
;;------------------------------------------------------------------ |
| 1885 |
(test-section "rounding") |
| 1886 |
|
| 1887 |
(define (round-tester value exactness cei flo tru rou) |
| 1888 |
(test* (string-append "rounding " (number->string value)) |
| 1889 |
(list exactness cei flo tru rou) |
| 1890 |
(let ((c (ceiling value)) |
| 1891 |
(f (floor value)) |
| 1892 |
(t (truncate value)) |
| 1893 |
(r (round value))) |
| 1894 |
(list (and (exact? c) (exact? f) (exact? t) (exact? r)) |
| 1895 |
c f t r)))) |
| 1896 |
|
| 1897 |
(round-tester 0 #t 0 0 0 0) |
| 1898 |
(round-tester 3 #t 3 3 3 3) |
| 1899 |
(round-tester -3 #t -3 -3 -3 -3) |
| 1900 |
(round-tester (expt 2 99) #t (expt 2 99) (expt 2 99) (expt 2 99) (expt 2 99)) |
| 1901 |
(round-tester (- (expt 2 99)) #t |
| 1902 |
(- (expt 2 99)) (- (expt 2 99)) (- (expt 2 99)) (- (expt 2 99))) |
| 1903 |
|
| 1904 |
(round-tester 9/4 #t 3 2 2 2) |
| 1905 |
(round-tester -9/4 #t -2 -3 -2 -2) |
| 1906 |
(round-tester 34985495387484938453495/17 #t |
| 1907 |
2057970316910878732559 |
| 1908 |
2057970316910878732558 |
| 1909 |
2057970316910878732558 |
| 1910 |
2057970316910878732559) |
| 1911 |
(round-tester -34985495387484938453495/17 #t |
| 1912 |
-2057970316910878732558 |
| 1913 |
-2057970316910878732559 |
| 1914 |
-2057970316910878732558 |
| 1915 |
-2057970316910878732559) |
| 1916 |
|
| 1917 |
(round-tester 35565/2 #t 17783 17782 17782 17782) |
| 1918 |
(round-tester -35565/2 #t -17782 -17783 -17782 -17782) |
| 1919 |
(round-tester 35567/2 #t 17784 17783 17783 17784) |
| 1920 |
(round-tester -35567/2 #t -17783 -17784 -17783 -17784) |
| 1921 |
|
| 1922 |
;; Ensure that rounding never yields -0.0. |
| 1923 |
;; It is not specified in R7RS, but having them return -0.0 complicates |
| 1924 |
;; integer division operations; notably, comparing division result to |
| 1925 |
;; 0.0 with equal? fails if division returns -0.0, which is mostly unexpected. |
| 1926 |
(test* "ceiling to 0.0 (not -0.0)" 0.0 (ceiling -0.5)) |
| 1927 |
(test* "truncate to 0.0 (not -0.0)" 0.0 (truncate -0.5)) |
| 1928 |
(test* "round to 0.0 (not -0.0)" 0.0 (round -0.5)) |
| 1929 |
|
| 1930 |
(test* "round->exact" 3 (round->exact 3.4) =) |
| 1931 |
(test* "round->exact" 4 (round->exact 3.5) =) |
| 1932 |
(test* "round->exact" |
| 1933 |
(if (> (greatest-fixnum) (expt 2 53)) ; same as "exact (greatest-fixnum)" |
| 1934 |
(+ (greatest-fixnum) 1) |
| 1935 |
(greatest-fixnum)) |
| 1936 |
(round->exact (inexact (greatest-fixnum)))) |
| 1937 |
(test* "round->exact" (least-fixnum) (round->exact (inexact (least-fixnum)))) |
| 1938 |
(test* "round->exact" (expt 2 63) (round->exact (- (expt 2.0 63) 1))) |
| 1939 |
(test* "round->exact" (- (expt 2 63)) (round->exact (- (expt 2.0 63)))) |
| 1940 |
(test* "floor->exact" 3 (floor->exact 3.4) =) |
| 1941 |
(test* "floor->exact" -4 (floor->exact -3.5) =) |
| 1942 |
(test* "floor->exact" |
| 1943 |
(if (> (greatest-fixnum) (expt 2 53)) ; same as "exact (greatest-fixnum)" |
| 1944 |
(+ (greatest-fixnum) 1) |
| 1945 |
(greatest-fixnum)) |
| 1946 |
(floor->exact (inexact (greatest-fixnum)))) |
| 1947 |
(test* "floor->exact" (least-fixnum) (floor->exact (inexact (least-fixnum)))) |
| 1948 |
(test* "floor->exact" (expt 2 63) (floor->exact (- (expt 2.0 63) 1))) |
| 1949 |
(test* "floor->exact" (- (expt 2 63)) (floor->exact (- (expt 2.0 63)))) |
| 1950 |
(test* "ceiling->exact" 4 (ceiling->exact 3.4) =) |
| 1951 |
(test* "ceiling->exact" -3 (ceiling->exact -3.5) =) |
| 1952 |
(test* "ceiling->exact" |
| 1953 |
(if (> (greatest-fixnum) (expt 2 53)) ; same as "exact (greatest-fixnum)" |
| 1954 |
(+ (greatest-fixnum) 1) |
| 1955 |
(greatest-fixnum)) |
| 1956 |
(ceiling->exact (inexact (greatest-fixnum)))) |
| 1957 |
(test* "ceiling->exact" (least-fixnum) (ceiling->exact (inexact (least-fixnum)))) |
| 1958 |
(test* "ceiling->exact" (expt 2 63) (ceiling->exact (- (expt 2.0 63) 1))) |
| 1959 |
(test* "ceiling->exact" (- (expt 2 63)) (ceiling->exact (- (expt 2.0 63)))) |
| 1960 |
(test* "truncate->exact" 3 (truncate->exact 3.4) =) |
| 1961 |
(test* "truncate->exact" -3 (truncate->exact -3.5) =) |
| 1962 |
(test* "truncate->exact" |
| 1963 |
(if (> (greatest-fixnum) (expt 2 53)) ; same as "exact (greatest-fixnum)" |
| 1964 |
(+ (greatest-fixnum) 1) |
| 1965 |
(greatest-fixnum)) |
| 1966 |
(truncate->exact (inexact (greatest-fixnum)))) |
| 1967 |
(test* "truncate->exact" (least-fixnum) (truncate->exact (inexact (least-fixnum)))) |
| 1968 |
(test* "truncate->exact" (expt 2 63) (truncate->exact (- (expt 2.0 63) 1))) |
| 1969 |
(test* "truncate->exact" (- (expt 2 63)) (truncate->exact (- (expt 2.0 63)))) |
| 1970 |
|
| 1971 |
;;------------------------------------------------------------------ |
| 1972 |
(test-section "clamping") |
| 1973 |
|
| 1974 |
(test* "clamp (1)" 1 (clamp 1) eqv?) |
| 1975 |
(test* "clamp (1 #f)" 1 (clamp 1 #f) eqv?) |
| 1976 |
(test* "clamp (1 #f #f)" 1 (clamp 1 #f #f) eqv?) |
| 1977 |
(test* "clamp (1.0)" 1.0 (clamp 1.0) eqv?) |
| 1978 |
(test* "clamp (1.0 #f)" 1.0 (clamp 1.0 #f) eqv?) |
| 1979 |
(test* "clamp (1.0 #f #f)" 1.0 (clamp 1.0 #f #f) eqv?) |
| 1980 |
|
| 1981 |
(test* "clamp (1 0)" 1 (clamp 1 0) eqv?) |
| 1982 |
(test* "clamp (1 0 #f)" 1 (clamp 1 0 #f) eqv?) |
| 1983 |
(test* "clamp (1 0 2)" 1 (clamp 1 0 2) eqv?) |
| 1984 |
(test* "clamp (1 5/4)" 5/4 (clamp 1 5/4) eqv?) |
| 1985 |
(test* "clamp (1 5/4 #f)" 5/4 (clamp 1 5/4 #f) eqv?) |
| 1986 |
(test* "clamp (1 #f 5/4)" 1 (clamp 1 #f 5/4) eqv?) |
| 1987 |
(test* "clamp (1 0 3/4)" 3/4 (clamp 1 0 3/4) eqv?) |
| 1988 |
(test* "clamp (1 #f 3/4)" 3/4 (clamp 1 #f 3/4) eqv?) |
| 1989 |
|
| 1990 |
(test* "clamp (1.0 0)" 1.0 (clamp 1.0 0) eqv?) |
| 1991 |
(test* "clamp (1.0 0 #f)" 1.0 (clamp 1.0 0 #f) eqv?) |
| 1992 |
(test* "clamp (1.0 0 2)" 1.0 (clamp 1.0 0 2) eqv?) |
| 1993 |
(test* "clamp (1.0 5/4)" 1.25 (clamp 1.0 5/4) eqv?) |
| 1994 |
(test* "clamp (1.0 5/4 #f)" 1.25 (clamp 1.0 5/4 #f) eqv?) |
| 1995 |
(test* "clamp (1.0 #f 5/4)" 1.0 (clamp 1.0 #f 5/4) eqv?) |
| 1996 |
(test* "clamp (1.0 0 3/4)" 0.75 (clamp 1.0 0 3/4) eqv?) |
| 1997 |
(test* "clamp (1.0 #f 3/4)" 0.75 (clamp 1.0 #f 3/4) eqv?) |
| 1998 |
|
| 1999 |
(test* "clamp (1 0.0)" 1.0 (clamp 1 0.0) eqv?) |
| 2000 |
(test* "clamp (1 0.0 #f)" 1.0 (clamp 1 0.0 #f) eqv?) |
| 2001 |
(test* "clamp (1 0.0 2)" 1.0 (clamp 1 0.0 2) eqv?) |
| 2002 |
(test* "clamp (1 0 2.0)" 1.0 (clamp 1 0 2.0) eqv?) |
| 2003 |
(test* "clamp (1 1.25)" 1.25 (clamp 1 1.25) eqv?) |
| 2004 |
(test* "clamp (1 #f 1.25)" 1.0 (clamp 1 #f 1.25) eqv?) |
| 2005 |
(test* "clamp (1 1.25 #f)" 1.25 (clamp 1 1.25 #f) eqv?) |
| 2006 |
(test* "clamp (1 0.0 3/4)" 0.75 (clamp 1 0.0 3/4) eqv?) |
| 2007 |
(test* "clamp (1 0 0.75)" 0.75 (clamp 1 0 0.75) eqv?) |
| 2008 |
|
| 2009 |
(test* "clamp (1 -inf.0 +inf.0)" 1.0 (clamp 1 -inf.0 +inf.0) eqv?) |
| 2010 |
|
| 2011 |
;;------------------------------------------------------------------ |
| 2012 |
(test-section "logical operations") |
| 2013 |
|
| 2014 |
;; covers |
| 2015 |
(define bitwise-tester-x 0) |
| 2016 |
(define bitwise-tester-y 0) |
| 2017 |
|
| 2018 |
(define-macro (ash-tester msg expect x y) |
| 2019 |
`(begin |
| 2020 |
(set! bitwise-tester-x ,x) |
| 2021 |
(set! bitwise-tester-y ,y) |
| 2022 |
(test* ,(format "ash (~a) compile-time constant, inlined, generic1, generic2" msg) |
| 2023 |
(list ,expect ,expect ,expect ,expect) |
| 2024 |
(list (ash ,x ,y) |
| 2025 |
(ash bitwise-tester-x ,y) |
| 2026 |
(ash ,x bitwise-tester-y) |
| 2027 |
(ash bitwise-tester-x bitwise-tester-y))))) |
| 2028 |
|
| 2029 |
(ash-tester "fixnum" #x408000 #x81 15) |
| 2030 |
(ash-tester "fixnum" #x81 #x408000 -15) |
| 2031 |
(ash-tester "fixnum" #x01 #x408000 -22) |
| 2032 |
(ash-tester "fixnum" 0 #x408000 -23) |
| 2033 |
(ash-tester "fixnum" 0 #x408000 -24) |
| 2034 |
(ash-tester "fixnum" 0 #x408000 -100) |
| 2035 |
(ash-tester "fixnum" #x81 #x81 0) |
| 2036 |
(ash-tester "neg. fixnum" #x-408000 #x-81 15) |
| 2037 |
(ash-tester "neg. fixnum" #x-81 #x-408000 -15) |
| 2038 |
(ash-tester "fixnum" -2 #x-408000 -22) |
| 2039 |
(ash-tester "fixnum" -1 #x-408000 -23) |
| 2040 |
(ash-tester "fixnum" -1 #x-408000 -24) |
| 2041 |
(ash-tester "fixnum" -1 #x-408000 -100) |
| 2042 |
(ash-tester "fixnum" #x-408000 #x-408000 0) |
| 2043 |
|
| 2044 |
(ash-tester "fixnum->bignum" #x81000000 #x81 24) |
| 2045 |
(ash-tester "fixnum->bignum" #x4080000000 #x81 31) |
| 2046 |
(ash-tester "fixnum->bignum" #x8100000000 #x81 32) |
| 2047 |
(ash-tester "fixnum->bignum" #x8100000000000000 #x81 56) |
| 2048 |
(ash-tester "fixnum->bignum" #x408000000000000000 #x81 63) |
| 2049 |
(ash-tester "fixnum->bignum" #x810000000000000000 #x81 64) |
| 2050 |
(ash-tester "neg.fixnum->bignum" #x-81000000 #x-81 24) |
| 2051 |
(ash-tester "neg.fixnum->bignum" #x-4080000000 #x-81 31) |
| 2052 |
(ash-tester "neg.fixnum->bignum" #x-8100000000 #x-81 32) |
| 2053 |
(ash-tester "neg.fixnum->bignum" #x-8100000000000000 #x-81 56) |
| 2054 |
(ash-tester "neg.fixnum->bignum" #x-408000000000000000 #x-81 63) |
| 2055 |
(ash-tester "neg.fixnum->bignum" #x-810000000000000000 #x-81 64) |
| 2056 |
|
| 2057 |
(ash-tester "bignum->fixnum" #x81 #x81000000 -24) |
| 2058 |
(ash-tester "bignum->fixnum" #x40 #x81000000 -25) |
| 2059 |
(ash-tester "bignum->fixnum" 1 #x81000000 -31) |
| 2060 |
(ash-tester "bignum->fixnum" 0 #x81000000 -32) |
| 2061 |
(ash-tester "bignum->fixnum" 0 #x81000000 -100) |
| 2062 |
(ash-tester "bignum->fixnum" #x81 #x4080000000 -31) |
| 2063 |
(ash-tester "bignum->fixnum" #x81 #x8100000000 -32) |
| 2064 |
(ash-tester "bignum->fixnum" #x40 #x8100000000 -33) |
| 2065 |
(ash-tester "bignum->fixnum" 1 #x8100000000 -39) |
| 2066 |
(ash-tester "bignum->fixnum" 0 #x8100000000 -40) |
| 2067 |
(ash-tester "bignum->fixnum" 0 #x8100000000 -100) |
| 2068 |
(ash-tester "bignum->fixnum" #x81 #x8100000000000000 -56) |
| 2069 |
(ash-tester "bignum->fixnum" #x81 #x408000000000000000 -63) |
| 2070 |
(ash-tester "bignum->fixnum" #x40 #x408000000000000000 -64) |
| 2071 |
(ash-tester "bignum->fixnum" #x20 #x408000000000000000 -65) |
| 2072 |
(ash-tester "bignum->fixnum" 1 #x408000000000000000 -70) |
| 2073 |
(ash-tester "bignum->fixnum" 0 #x408000000000000000 -71) |
| 2074 |
(ash-tester "bignum->fixnum" 0 #x408000000000000000 -100) |
| 2075 |
|
| 2076 |
(ash-tester "neg.bignum->fixnum" #x-81 #x-81000000 -24) |
| 2077 |
(ash-tester "neg.bignum->fixnum" #x-41 #x-81000000 -25) |
| 2078 |
(ash-tester "neg.bignum->fixnum" #x-21 #x-81000000 -26) |
| 2079 |
(ash-tester "neg.bignum->fixnum" -2 #x-81000000 -31) |
| 2080 |
(ash-tester "neg.bignum->fixnum" -1 #x-81000000 -32) |
| 2081 |
(ash-tester "neg.bignum->fixnum" -1 #x-81000000 -33) |
| 2082 |
(ash-tester "neg.bignum->fixnum" -1 #x-81000000 -100) |
| 2083 |
(ash-tester "neg.bignum->fixnum" #x-81 #x-4080000000 -31) |
| 2084 |
(ash-tester "neg.bignum->fixnum" #x-41 #x-4080000000 -32) |
| 2085 |
(ash-tester "neg.bignum->fixnum" #x-21 #x-4080000000 -33) |
| 2086 |
(ash-tester "neg.bignum->fixnum" -2 #x-4080000000 -38) |
| 2087 |
(ash-tester "neg.bignum->fixnum" -1 #x-4080000000 -39) |
| 2088 |
(ash-tester "neg.bignum->fixnum" -1 #x-4080000000 -100) |
| 2089 |
(ash-tester "neg.bignum->fixnum" #x-81 #x-408000000000000000 -63) |
| 2090 |
(ash-tester "neg.bignum->fixnum" #x-41 #x-408000000000000000 -64) |
| 2091 |
(ash-tester "neg.bignum->fixnum" #x-21 #x-408000000000000000 -65) |
| 2092 |
(ash-tester "neg.bignum->fixnum" -2 #x-408000000000000000 -70) |
| 2093 |
(ash-tester "neg.bignum->fixnum" -1 #x-408000000000000000 -71) |
| 2094 |
(ash-tester "neg.bignum->fixnum" -1 #x-408000000000000000 -72) |
| 2095 |
|
| 2096 |
(ash-tester "bignum->bignum" #x12345678123456780 #x1234567812345678 4) |
| 2097 |
(ash-tester "bignum->bignum" #x1234567812345678000000000000000 #x1234567812345678 60) |
| 2098 |
(ash-tester "bignum->bignum" #x12345678123456780000000000000000 #x1234567812345678 64) |
| 2099 |
(ash-tester "bignum->bignum" #x123456781234567 #x1234567812345678 -4) |
| 2100 |
(ash-tester "bignum->bignum" #x12345678 #x1234567812345678 -32) |
| 2101 |
(ash-tester "neg.bignum->bignum" #x-123456781234568 #x-1234567812345678 -4) |
| 2102 |
(ash-tester "bignum->bignum" #x-12345679 #x-1234567812345678 -32) |
| 2103 |
|
| 2104 |
(test* "lognot (fixnum)" -1 (lognot 0)) |
| 2105 |
(test* "lognot (fixnum)" 0 (lognot -1)) |
| 2106 |
(test* "lognot (fixnum)" -65536 (lognot 65535)) |
| 2107 |
(test* "lognot (fixnum)" 65535 (lognot -65536)) |
| 2108 |
(test* "lognot (bignum)" #x-1000000000000000001 |
| 2109 |
(lognot #x1000000000000000000)) |
| 2110 |
(test* "lognot (bignum)" #x1000000000000000000 |
| 2111 |
(lognot #x-1000000000000000001)) |
| 2112 |
|
| 2113 |
(define-macro (logop-tester op msg expect x y) |
| 2114 |
`(begin |
| 2115 |
(set! bitwise-tester-x ,x) |
| 2116 |
(set! bitwise-tester-y ,y) |
| 2117 |
(test* (format "~a (~a)" ',op ,msg) |
| 2118 |
(list ,expect ,expect ,expect ,expect ,expect ,expect) |
| 2119 |
(list (,op ,x ,y) |
| 2120 |
(,op bitwise-tester-x ,y) |
| 2121 |
(,op ,x bitwise-tester-y) |
| 2122 |
(,op bitwise-tester-x bitwise-tester-y) |
| 2123 |
(,op bitwise-tester-x ,y bitwise-tester-x) |
| 2124 |
(,op ,x bitwise-tester-x ,y bitwise-tester-y))))) |
| 2125 |
|
| 2126 |
(logop-tester logand "+fix & 0" 0 #x123456 0) |
| 2127 |
(logop-tester logand "+big & 0" 0 #x1234567812345678 0) |
| 2128 |
(logop-tester logand "+fix & -1" #x123456 #x123456 -1) |
| 2129 |
(logop-tester logand "+big & -1" #x1234567812345678 #x1234567812345678 -1) |
| 2130 |
(logop-tester logand "+fix & +fix" #x2244 #xaa55 #x6666) |
| 2131 |
(logop-tester logand "+fix & +big" #x2244 #xaa55 #x6666666666) |
| 2132 |
(logop-tester logand "+big & +fix" #x4422 #xaa55aa55aa #x6666) |
| 2133 |
(logop-tester logand "+big & +big" #x2244224422 #xaa55aa55aa #x6666666666) |
| 2134 |
(logop-tester logand "+big & +big" #x103454301aaccaa #x123456789abcdef #xfedcba987654321fedcba987654321fedcba) |
| 2135 |
(logop-tester logand "+big & +big" #x400000 #xaa55ea55aa #x55aa55aa55) |
| 2136 |
(logop-tester logand "+fix & -fix" #x8810 #xaa55 #x-6666) |
| 2137 |
(logop-tester logand "+fix & -big" #x8810 #xaa55 #x-6666666666) |
| 2138 |
(logop-tester logand "+big & -fix" #xaa55aa118a #xaa55aa55aa #x-6666) |
| 2139 |
(logop-tester logand "+big & -big" #x881188118a #xaa55aa55aa #x-6666666666) |
| 2140 |
(logop-tester logand "+big & -big" #x20002488010146 #x123456789abcdef #x-fedcba987654321fedcba987654321fedcba) |
| 2141 |
(logop-tester logand "-fix & +fix" #x4422 #x-aa55 #x6666) |
| 2142 |
(logop-tester logand "-fix & +big" #x6666664422 #x-aa55 #x6666666666) |
| 2143 |
(logop-tester logand "-big & +fix" #x2246 #x-aa55aa55aa #x6666) |
| 2144 |
(logop-tester logand "-big & +big" #x4422442246 #x-aa55aa55aa #x6666666666) |
| 2145 |
(logop-tester logand "-big & +big" #xfedcba987654321fedcba884200020541010 #x-123456789abcdef #xfedcba987654321fedcba987654321fedcba) |
| 2146 |
(logop-tester logand "-fix & -fix" #x-ee76 #x-aa55 #x-6666) |
| 2147 |
(logop-tester logand "-fix & -big" #x-666666ee76 #x-aa55 #x-6666666666) |
| 2148 |
(logop-tester logand "-big & -fix" #x-aa55aa77ee #x-aa55aa55aa #x-6666) |
| 2149 |
(logop-tester logand "-big & -big" #x-ee77ee77ee #x-aa55aa55aa #x-6666666666) |
| 2150 |
(logop-tester logand "-big & -big" #x-fedcba987654321fedcba9a76567a9ffde00 #x-123456789abcdef #x-fedcba987654321fedcba987654321fedcba) |
| 2151 |
|
| 2152 |
(logop-tester logior "+fix | 0" #x123456 #x123456 0) |
| 2153 |
(logop-tester logior "+big | 0" #x1234567812345678 #x1234567812345678 0) |
| 2154 |
(logop-tester logior "+fix | -1" -1 #x123456 -1) |
| 2155 |
(logop-tester logior "+big | -1" -1 #x1234567812345678 -1) |
| 2156 |
(logop-tester logior "+fix | +fix" #xee77 #xaa55 #x6666) |
| 2157 |
(logop-tester logior "+fix | +big" #x666666ee77 #xaa55 #x6666666666) |
| 2158 |
(logop-tester logior "+big | +fix" #xaa55aa77ee #xaa55aa55aa #x6666) |
| 2159 |
(logop-tester logior "+big | +big" #xee77ee77ee #xaa55aa55aa #x6666666666) |
| 2160 |
(logop-tester logior "+big | +big" #xfedcba987654321fedcba9a76567a9ffddff #x123456789abcdef #xfedcba987654321fedcba987654321fedcba) |
| 2161 |
(logop-tester logior "+fix | -fix" #x-4421 #xaa55 #x-6666) |
| 2162 |
(logop-tester logior "+fix | -big" #x-6666664421 #xaa55 #x-6666666666) |
| 2163 |
(logop-tester logior "+big | -fix" #x-2246 #xaa55aa55aa #x-6666) |
| 2164 |
(logop-tester logior "+big | -big" #x-4422442246 #xaa55aa55aa #x-6666666666) |
| 2165 |
(logop-tester logior "+big | -big" #x-fedcba987654321fedcba884200020541011 #x123456789abcdef #x-fedcba987654321fedcba987654321fedcba) |
| 2166 |
(logop-tester logior "-fix | +fix" #x-8811 #x-aa55 #x6666) |
| 2167 |
(logop-tester logior "-fix | +big" #x-8811 #x-aa55 #x6666666666) |
| 2168 |
(logop-tester logior "-big | +fix" #x-aa55aa118a #x-aa55aa55aa #x6666) |
| 2169 |
(logop-tester logior "-big | +big" #x-881188118a #x-aa55aa55aa #x6666666666) |
| 2170 |
(logop-tester logior "-big | +big" #x-20002488010145 #x-123456789abcdef #xfedcba987654321fedcba987654321fedcba) |
| 2171 |
(logop-tester logior "-fix | -fix" #x-2245 #x-aa55 #x-6666) |
| 2172 |
(logop-tester logior "-fix | -big" #x-2245 #x-aa55 #x-6666666666) |
| 2173 |
(logop-tester logior "-big | -fix" #x-4422 #x-aa55aa55aa #x-6666) |
| 2174 |
(logop-tester logior "-big | -big" #x-2244224422 #x-aa55aa55aa #x-6666666666) |
| 2175 |
(logop-tester logior "-big | -big" #x-103454301aacca9 #x-123456789abcdef #x-fedcba987654321fedcba987654321fedcba) |
| 2176 |
|
| 2177 |
;; regression test for incorrect check till 0.9.1 |
| 2178 |
(test* "lognot (error)" (test-error) (lognot 1/2)) |
| 2179 |
(test* "logand (error)" (test-error) (logand 3 1/2)) |
| 2180 |
(test* "logior (error)" (test-error) (logior 3 1/2)) |
| 2181 |
(test* "logxor (error)" (test-error) (logxor 3 1/2)) |
| 2182 |
|
| 2183 |
;; zero and one-argument bitops a la SRFI-60 |
| 2184 |
(test* "logand (0arg)" -1 (logand)) |
| 2185 |
(test* "logand (1arg)" 1 (logand 1)) |
| 2186 |
(test* "logand (1arg)" (test-error) (logand 3.14)) |
| 2187 |
(test* "logior (0arg)" 0 (logior)) |
| 2188 |
(test* "logior (1arg)" 1 (logior 1)) |
| 2189 |
(test* "logior (1arg)" (test-error) (logior 3.14)) |
| 2190 |
(test* "logxor (0arg)" 0 (logxor)) |
| 2191 |
(test* "logxor (1arg)" 1 (logxor 1)) |
| 2192 |
(test* "logxor (1arg)" (test-error) (logxor 3.14)) |
| 2193 |
;; check optimizer |
| 2194 |
;; http://chaton.practical-scheme.net/gauche/a/2021/04/04#entry-6069b57b-bbbff |
| 2195 |
(test* "logior (optimizer)" 1 (logior (if #f 0 1))) |
| 2196 |
|
| 2197 |
|
| 2198 |
(test* "logtest" #t |
| 2199 |
(logtest #xfeedbabe #x10000000)) |
| 2200 |
(test* "logtest" #f |
| 2201 |
(logtest #xfeedbabe #x01100101)) |
| 2202 |
|
| 2203 |
(let loop ((a 1) ; 1, 10, 100, ... |
| 2204 |
(b 1) ; 1, 11, 111, ... |
| 2205 |
(c 2) ; 10, 101, 1001, ... |
| 2206 |
(n 1)) ; counter |
| 2207 |
(when (< n 69) |
| 2208 |
(test* (format "logcount (positive, 100...) ~a" n) 1 (logcount a)) |
| 2209 |
(test* (format "logcount (positive, 111...) ~a" n) n (logcount b)) |
| 2210 |
(test* (format "logcount (negative, 100...) ~a" n) (- n 1) (logcount (- a))) |
| 2211 |
(test* (format "logcount (negative, 100..1) ~a" n) 1 (logcount (- c))) |
| 2212 |
(loop (+ b 1) (+ b b 1) (+ b b 3) (+ n 1)))) |
| 2213 |
|
| 2214 |
(test* "logbit?" '(#f #t #t #f #t #f #f) |
| 2215 |
(map (^i (logbit? i #b10110)) '(0 1 2 3 4 5 6))) |
| 2216 |
(test* "logbit?" '(#f #t #f #t #f #t #t) |
| 2217 |
(map (^i (logbit? i #b-10110)) '(0 1 2 3 4 5 6))) |
| 2218 |
|
| 2219 |
(test* "logset+clear" #b01100101 |
| 2220 |
(logset+clear #b11001100 #b01100001 #b10001000)) |
| 2221 |
|
| 2222 |
(test* "copy-bit" #b11010110 |
| 2223 |
(copy-bit 4 #b11000110 #t)) |
| 2224 |
(test* "copy-bit" #b11000110 |
| 2225 |
(copy-bit 4 #b11000110 #f)) |
| 2226 |
(test* "copy-bit" #b10000110 |
| 2227 |
(copy-bit 6 #b11000110 #f)) |
| 2228 |
|
| 2229 |
(test* "bit-field" #b1010 |
| 2230 |
(bit-field #b1101101010 0 4)) |
| 2231 |
(test* "bit-field" #b10110 |
| 2232 |
(bit-field #b1101101010 4 9)) |
| 2233 |
|
| 2234 |
(test* "copy-bit-field" #b1101100000 |
| 2235 |
(copy-bit-field #b1101101010 0 0 4)) |
| 2236 |
(test* "copy-bit-field" #b1101101111 |
| 2237 |
(copy-bit-field #b1101101010 -1 0 4)) |
| 2238 |
(test* "copy-bit-field" #b1111111111101010 |
| 2239 |
(copy-bit-field #b1101101010 -1 5 16)) |
| 2240 |
|
| 2241 |
(test* "integer-length" 8 (integer-length #b10101010)) |
| 2242 |
(test* "integer-length" 4 (integer-length #b1111)) |
| 2243 |
(test* "integer-length" 0 (integer-length 0)) |
| 2244 |
(test* "integer-length" 0 (integer-length -1)) |
| 2245 |
(test* "integer-length" 1 (integer-length 1)) |
| 2246 |
(test* "integer-length" 1 (integer-length -2)) |
| 2247 |
(test* "integer-length" 29 (integer-length (- (expt 2 29) 1))) |
| 2248 |
(test* "integer-length" 30 (integer-length (expt 2 29))) |
| 2249 |
(test* "integer-length" 61 (integer-length (- (expt 2 61) 1))) |
| 2250 |
(test* "integer-length" 62 (integer-length (expt 2 61))) |
| 2251 |
(test* "integer-length" 29 (integer-length (- (expt 2 29)))) |
| 2252 |
(test* "integer-length" 30 (integer-length (- (- (expt 2 29)) 1))) |
| 2253 |
(test* "integer-length" 61 (integer-length (- (expt 2 61)))) |
| 2254 |
(test* "integer-length" 62 (integer-length (- (- (expt 2 61)) 1))) |
| 2255 |
(test* "integer-length" 1025 (integer-length (expt 2 1024))) |
| 2256 |
|
| 2257 |
(let1 2s-exponent-factor-tests `((0 -1) (-1 0) (1 0) (2 1) (-2 1) |
| 2258 |
(1048576 20) (-1048576 20) |
| 2259 |
(,(expt 2 100) 100) |
| 2260 |
(,(- (expt 2 100)) 100) |
| 2261 |
(,(* 7 (expt 2 50)) 50)) |
| 2262 |
(test* "twos-exponent-factor" 2s-exponent-factor-tests |
| 2263 |
(map (lambda [t] (list (car t) (twos-exponent-factor (car t)))) |
| 2264 |
2s-exponent-factor-tests))) |
| 2265 |
|
| 2266 |
(let1 2s-exponent-tests `(0 1 -1 2 -2 4 8 65535 65536 |
| 2267 |
131072 ,(* 3 131072) |
| 2268 |
,(expt 2 80) ,(- (expt 2 80)) |
| 2269 |
,(* 3 (expt 2 80))) |
| 2270 |
(define (dumb-test n k) |
| 2271 |
(cond [(<= n 0) #f] |
| 2272 |
[(= n 1) k] |
| 2273 |
[(odd? n) #f] |
| 2274 |
[else (dumb-test (/ n 2) (+ k 1))])) |
| 2275 |
(test* "twos-exponent" |
| 2276 |
(map (lambda [k] (cons k (dumb-test k 0))) 2s-exponent-tests) |
| 2277 |
(map (lambda [k] (cons k (twos-exponent k))) 2s-exponent-tests))) |
| 2278 |
|
| 2279 |
;;------------------------------------------------------------------ |
| 2280 |
(test-section "inexact arithmetics") |
| 2281 |
|
| 2282 |
;; +. etc are inlined, so we want to test both inlined case and |
| 2283 |
;; explicitly called case. |
| 2284 |
(define-syntax inexact-arith-test |
| 2285 |
(syntax-rules () |
| 2286 |
[(_ msg exp (op . args)) |
| 2287 |
(begin |
| 2288 |
(test* (string-append msg " inlined") exp (op . args)) |
| 2289 |
(test* (string-append msg " applied") exp (Apply op (list . args))))])) |
| 2290 |
|
| 2291 |
(inexact-arith-test "+. (0)" 0.0 (+.)) |
| 2292 |
(inexact-arith-test "+. (1)" 1.0 (+. 1)) |
| 2293 |
(inexact-arith-test "+. (1big)" 1.0e20 (+. 100000000000000000000)) |
| 2294 |
(inexact-arith-test "+. (1rat)" 1.5 (+. 3/2)) |
| 2295 |
(inexact-arith-test "+. (1cmp)" 1.0+i (+. 1+i)) |
| 2296 |
(inexact-arith-test "+. (2)" 1.0 (+. 0 1)) |
| 2297 |
(inexact-arith-test "+. (2big)" 1.0e20 (+. 1 100000000000000000000)) |
| 2298 |
(inexact-arith-test "+. (2rat)" 1.5 (+. 1 1/2)) |
| 2299 |
(inexact-arith-test "+. (many)" 15.0 (+. 1 2 3 4 5)) |
| 2300 |
|
| 2301 |
(inexact-arith-test "-. (1)" -1.0 (-. 1)) |
| 2302 |
(inexact-arith-test "-. (1big)" -1.0e20 (-. 100000000000000000000)) |
| 2303 |
(inexact-arith-test "-. (1rat)" -1.5 (-. 3/2)) |
| 2304 |
(inexact-arith-test "-. (1cmp)" -1.0-i (-. 1+i)) |
| 2305 |
(inexact-arith-test "-. (2)" -1.0 (-. 0 1)) |
| 2306 |
(inexact-arith-test "-. (2big)" -1.0e20 (-. 1 100000000000000000000)) |
| 2307 |
(inexact-arith-test "-. (2rat)" 0.5 (-. 1 1/2)) |
| 2308 |
(inexact-arith-test "-. (many)" -13.0 (-. 1 2 3 4 5)) |
| 2309 |
|
| 2310 |
(inexact-arith-test "*. (0)" 1.0 (*.)) |
| 2311 |
(inexact-arith-test "*. (1)" 1.0 (*. 1)) |
| 2312 |
(inexact-arith-test "*. (1big)" 1.0e20 (*. 100000000000000000000)) |
| 2313 |
(inexact-arith-test "*. (1rat)" 1.5 (*. 3/2)) |
| 2314 |
(inexact-arith-test "*. (1cmp)" 1.0+i (*. 1+i)) |
| 2315 |
(inexact-arith-test "*. (2)" 0.0 (*. 0 1)) |
| 2316 |
(inexact-arith-test "*. (2big)" 1.0e20 (*. 1 100000000000000000000)) |
| 2317 |
(inexact-arith-test "*. (2rat)" 0.5 (*. 1 1/2)) |
| 2318 |
(inexact-arith-test "*. (many)" 120.0 (*. 1 2 3 4 5)) |
| 2319 |
|
| 2320 |
(inexact-arith-test "/. (1)" 1.0 (/. 1)) |
| 2321 |
(inexact-arith-test "/. (1big)" 1.0e-20 (/. 100000000000000000000)) |
| 2322 |
(inexact-arith-test "/. (1rat)" 0.6666666666666666 (/. 3/2)) |
| 2323 |
(inexact-arith-test "/. (1cmp)" 0.5-0.5i (/. 1+i)) |
| 2324 |
(inexact-arith-test "/. (2)" 0.0 (/. 0 1)) |
| 2325 |
(inexact-arith-test "/. (2big)" 1.0e-20 (/. 1 100000000000000000000)) |
| 2326 |
(inexact-arith-test "/. (2rat)" 2.0 (/. 1 1/2)) |
| 2327 |
(inexact-arith-test "/. (2rat1)" 0.5 (/. 1/2 1)) |
| 2328 |
(inexact-arith-test "/. (2rat2)" 2.0 (/. 1/2 1/4)) |
| 2329 |
(inexact-arith-test "/. (many)" 0.1 (/. 1 2 5)) |
| 2330 |
|
| 2331 |
;; The following takes a special path to avoid overflow. |
| 2332 |
(inexact-arith-test "/. fixnum bignum" 0.0 (/. (expt 10 400))) |
| 2333 |
(inexact-arith-test "/. bignum fixnum" +inf.0 (/. (expt 10 400) 1)) |
| 2334 |
(inexact-arith-test "/. bignum fixnum" -inf.0 (/. (expt 10 400) -1)) |
| 2335 |
(inexact-arith-test "/. bignum bignum" 10.0 (/. (expt 10 401) (expt 10 400))) |
| 2336 |
|
| 2337 |
;;------------------------------------------------------------------ |
| 2338 |
(test-section "sqrt") |
| 2339 |
|
| 2340 |
(define (integer-sqrt-tester k) |
| 2341 |
(test* (format "exact-integer-sqrt ~a" k) '(#t #t #t) |
| 2342 |
(receive (s r) (exact-integer-sqrt k) |
| 2343 |
(list (>= r 0) |
| 2344 |
(= k (+ (* s s) r)) |
| 2345 |
(< k (* (+ s 1) (+ s 1))))))) |
| 2346 |
|
| 2347 |
(integer-sqrt-tester 0) |
| 2348 |
(integer-sqrt-tester 1) |
| 2349 |
(integer-sqrt-tester 2) |
| 2350 |
(integer-sqrt-tester 3) |
| 2351 |
(integer-sqrt-tester 4) |
| 2352 |
(integer-sqrt-tester 10) |
| 2353 |
(integer-sqrt-tester (expt 2 32)) |
| 2354 |
(integer-sqrt-tester (- (expt 2 52) 1)) |
| 2355 |
(integer-sqrt-tester (expt 2 52)) |
| 2356 |
(integer-sqrt-tester (+ (expt 2 52) 1)) |
| 2357 |
(integer-sqrt-tester 9007199136250224) |
| 2358 |
(integer-sqrt-tester 9007199136250226) |
| 2359 |
(integer-sqrt-tester (- (expt 2 53) 1)) |
| 2360 |
(integer-sqrt-tester (expt 2 53)) |
| 2361 |
(integer-sqrt-tester (+ (expt 2 53) 1)) |
| 2362 |
(integer-sqrt-tester 9999999999999999999999999999999999999999999999999999) |
| 2363 |
(integer-sqrt-tester (+ (expt 10 400) 3141592653589)) ; double range overflow |
| 2364 |
|
| 2365 |
(test* "exact-integer-sqrt -1" (test-error) (exact-integer-sqrt -1)) |
| 2366 |
(test* "exact-integer-sqrt 1.0" (test-error) (exact-integer-sqrt 1.0)) |
| 2367 |
(test* "exact-integer-sqrt 1/4" (test-error) (exact-integer-sqrt 1/4)) |
| 2368 |
|
| 2369 |
;; try to cover various paths in sqrt of exact numbers |
| 2370 |
(test* "sqrt, exact" 0 (sqrt 0) eqv?) |
| 2371 |
(test* "sqrt, exact" 4 (sqrt 16) eqv?) |
| 2372 |
(test* "sqrt, exact" (expt 2 64) (sqrt (expt 2 128)) eqv?) |
| 2373 |
|
| 2374 |
(test* "sqrt, inexact" 4.0 (sqrt 16.0) eqv?) |
| 2375 |
(test* "sqrt, inexact" +4.0i (sqrt -16.0) eqv?) |
| 2376 |
(test* "sqrt, inexact" (real-sqrt (- (expt 2 64) 1)) |
| 2377 |
(sqrt (- (expt 2 64) 1)) eqv?) |
| 2378 |
|
| 2379 |
(test* "sqrt, exact" 1/4 (sqrt 1/16) eqv?) |
| 2380 |
(test* "sqrt, exact" (/ 1 (expt 2 64)) (sqrt (/ 1 (expt 2 128))) eqv?) |
| 2381 |
(test* "sqrt, exact" (/ (expt 2 64) 3) (sqrt (/ (expt 2 128) 9)) eqv?) |
| 2382 |
(test* "sqrt, exact" (/ (expt 2 64) (expt 3 30)) |
| 2383 |
(sqrt (/ (expt 2 128) (expt 3 60))) eqv?) |
| 2384 |
|
| 2385 |
(test* "sqrt, inexact" 0.25 (sqrt (exact->inexact 1/16)) eqv?) |
| 2386 |
(test* "sqrt, inexact" (real-sqrt (/ (- (expt 2 64) 1) (expt 3 30))) |
| 2387 |
(sqrt (/ (- (expt 2 64) 1) (expt 3 30))) eqv?) |
| 2388 |
|
| 2389 |
;;------------------------------------------------------------------ |
| 2390 |
(test-section "posix math functions") |
| 2391 |
|
| 2392 |
(test* "fmod" 0.25 (fmod 5.25 1) (^[x y] (approx=? x y (* 8 (flonum-epsilon))))) |
| 2393 |
(test* "fmod" 2.3 (fmod 8.3 3) (^[x y] (approx=? x y (* 8 (flonum-epsilon))))) |
| 2394 |
(test* "fmod" 8.3 (fmod 8.3 33) (^[x y] (approx=? x y (* 8 (flonum-epsilon))))) |
| 2395 |
|
| 2396 |
(test* "frexp" '(0.785 2) |
| 2397 |
(values->list (frexp 3.14)) |
| 2398 |
(^[x y] (and (approx=? (car x) (car y)) |
| 2399 |
(approx=? (cadr x) (cadr y))))) |
| 2400 |
|
| 2401 |
(test* "ldexp" 3.14 (ldexp 0.785 2) (^[x y] (approx=? x y))) |
| 2402 |
|
| 2403 |
(test* "modf" '(0.14 3.0) |
| 2404 |
(values->list (modf 3.14)) |
| 2405 |
(^[x y] (and (approx=? (car x) (car y) (* 4 (flonum-epsilon))) |
| 2406 |
(approx=? (cadr x) (cadr y))))) |
| 2407 |
|
| 2408 |
;; This is to check alternative gamma implementation assuming we can use |
| 2409 |
;; system's tgamma and lgamma. |
| 2410 |
'(let () |
| 2411 |
(define (test-gamma name fn0 fn1) |
| 2412 |
(test* #"alt-~name" #f |
| 2413 |
(any (^[x] (let* ([y0 (fn0 x)] |
| 2414 |
[y1 (fn1 x)] |
| 2415 |
[e (/ (abs (- y0 y1)) y0)]) |
| 2416 |
(and (> e 1e-6) |
| 2417 |
(format "Error too big (~s) at x=~s (~a=~s alt-~a=~s" |
| 2418 |
e x name y0 name y1)))) |
| 2419 |
(map (cut expt 10 <>) (iota 150 -5 0.05))))) |
| 2420 |
(test-gamma "gamma" |
| 2421 |
(with-module gauche.internal %gamma) |
| 2422 |
(with-module gauche.internal %alt-gamma)) |
| 2423 |
(test-gamma "lgamma" |
| 2424 |
(with-module gauche.internal %lgamma) |
| 2425 |
(with-module gauche.internal %alt-lgamma)) |
| 2426 |
) |
| 2427 |
|
| 2428 |
;; log on huge number - naive use of Scm_GetDouble overflows |
| 2429 |
(let-syntax ([log-tester |
| 2430 |
(syntax-rules () |
| 2431 |
[(_ input) |
| 2432 |
(let1 factor (expt 2 (integer-length input)) |
| 2433 |
(test* (write-to-string '(log input)) |
| 2434 |
(+ (log factor) (log (/ input factor))) |
| 2435 |
(log input)))])]) |
| 2436 |
(log-tester (expt 2 2048)) |
| 2437 |
(log-tester (- (expt 2 2048))) |
| 2438 |
(log-tester (+ (expt 3 2048) (expt 3 2047))) |
| 2439 |
(log-tester (- (expt 7 7715))) |
| 2440 |
) |
| 2441 |
|
| 2442 |
;; https://github.com/shirok/Gauche/issues/980 |
| 2443 |
(test* "log bignum precision" |
| 2444 |
842.7813755971428 |
| 2445 |
(log 1035859546035934049504829565510983225895287916488306950857981070740695366743867100031078278751721263935546679975493752204390207097630982126800619395521726105345471977984263375536153668125171048087514699084009194566637387873758291708865130695817935612736040318981321824597512811923654763186057026190157753057552061059245522812282412116919248265575561928861628323407993)) |
| 2446 |
|
| 2447 |
;; log on infinities |
| 2448 |
(test* "log on infinities" '(+inf.0 +inf.0+3.141592653589793i) |
| 2449 |
(list (log +inf.0) (log -inf.0))) |
| 2450 |
|
| 2451 |
;;------------------------------------------------------------------ |
| 2452 |
(test-section "trigonometric functions") |
| 2453 |
|
| 2454 |
(let () |
| 2455 |
(define (check trig trig-pi) |
| 2456 |
(let loop ([x -4]) |
| 2457 |
(if (> x 4) |
| 2458 |
#f |
| 2459 |
(let ([t0 (trig (* x 3.141592653589793))] |
| 2460 |
[t1 (trig-pi x)]) |
| 2461 |
(if (or (and (> (abs t0) 1e15) |
| 2462 |
(> (abs t1) 1e15)) |
| 2463 |
(< (abs (- t0 t1)) 1e-10)) |
| 2464 |
(loop (+ x 1/16)) |
| 2465 |
`(((,trig (* pi ,x)) ,t0) |
| 2466 |
((,trig-pi ,x) ,t1))))))) |
| 2467 |
(test* "sin vs sinpi" #f (check real-sin real-sinpi)) |
| 2468 |
(test* "cos vs cospi" #f (check real-cos real-cospi)) |
| 2469 |
(test* "tan vs tanpi" #f (check real-tan real-tanpi))) |
| 2470 |
|
| 2471 |
;; atan branch cuts (R7RS 6.2.6) |
| 2472 |
(let ((pi 3.141592653589793)) |
| 2473 |
(test* "atan +0.0 +x" +0.0 (atan 0.0 1) eqv?) |
| 2474 |
(test* "atan -0.0 +x" -0.0 (atan -0.0 1) eqv?) |
| 2475 |
(test* "atan +0.0 -x" pi (atan +0.0 -1) eqv?) |
| 2476 |
(test* "atan -0.0 -x" (- pi) (atan -0.0 -1) eqv?) |
| 2477 |
(test* "atan +0.0 +0.0" +0.0 (atan +0.0 +0.0) eqv?) |
| 2478 |
(test* "atan -0.0 +0.0" -0.0 (atan -0.0 +0.0) eqv?) |
| 2479 |
(test* "atan +0.0 -0.0" pi (atan +0.0 -0.0) eqv?) |
| 2480 |
(test* "atan -0.0 -0.0" (- pi) (atan -0.0 -0.0) eqv?) |
| 2481 |
(test* "atan +0.0 0" (/ pi 2) (atan +0.0 0) eqv?) |
| 2482 |
(test* "atan -0.0 0" (- (/ pi 2)) (atan -0.0 0) eqv?) |
| 2483 |
(test* "atan 0 0" #t (nan? (atan 0 0))) |
| 2484 |
) |
| 2485 |
|
| 2486 |
(let () |
| 2487 |
(test* "cos + degrees->radians" -1 |
| 2488 |
(cos (degrees->radians 180)) |
| 2489 |
approx=?) |
| 2490 |
(test* "radians->degrees + degrees->radians" 90 |
| 2491 |
(radians->degrees (degrees->radians 90)) |
| 2492 |
approx=?)) |
| 2493 |
|
| 2494 |
;;------------------------------------------------------------------ |
| 2495 |
(test-section "ffx optimization") |
| 2496 |
|
| 2497 |
;; This code is provided by naoya_t to reproduce the FFX bug |
| 2498 |
;; existed until r6714. The bug was that the ARGP words of |
| 2499 |
;; in-stack continuations were not scanned when flonum register |
| 2500 |
;; bank was cleared. This code exhibits the case by putting |
| 2501 |
;; the result of (sqrt 2) as an unfinished argument, then calling |
| 2502 |
;; inverse-erf which caused flushing flonum regs (see "NG" line). |
| 2503 |
|
| 2504 |
(use math.const) |
| 2505 |
(let () |
| 2506 |
(define *epsilon* 1e-12) |
| 2507 |
|
| 2508 |
;; |
| 2509 |
;; normal quantile function (probit function) |
| 2510 |
;; |
| 2511 |
(define (probit-ok p) |
| 2512 |
(define (probit>0 p) |
| 2513 |
(* (inverse-erf (- (* p 2) 1)) (sqrt 2))) ;; OK |
| 2514 |
(if (< p 0) |
| 2515 |
(- 1 (probit>0 (- p))) |
| 2516 |
(probit>0 p) )) |
| 2517 |
|
| 2518 |
(define (probit-ng p) |
| 2519 |
(define (probit>0 p) |
| 2520 |
(* (sqrt 2) (inverse-erf (- (* p 2) 1)))) ;; NG |
| 2521 |
(if (< p 0) |
| 2522 |
(- 1 (probit>0 (- p))) |
| 2523 |
(probit>0 p) )) |
| 2524 |
|
| 2525 |
;; |
| 2526 |
;; inverse error function (erf-1) |
| 2527 |
;; |
| 2528 |
(define (inverse-erf z) |
| 2529 |
(define (calc-next-ck k c) |
| 2530 |
(let loop ((m 0) (sum 0) (ca c) (cz (reverse c))) |
| 2531 |
(if (= m k) sum |
| 2532 |
(loop (+ m 1) |
| 2533 |
(+ sum (/. (* (car ca) (car cz)) (+ m 1) (+ m m 1))) |
| 2534 |
(cdr ca) (cdr cz))))) |
| 2535 |
(define (calc-cks k) |
| 2536 |
(let loop ((i 0) (cks '(1))) |
| 2537 |
(if (= i k) cks |
| 2538 |
(loop (+ i 1) (cons (calc-next-ck (+ i 1) cks) cks))))) |
| 2539 |
(define (calc-ck k) (car (calc-cks k))) |
| 2540 |
|
| 2541 |
(define (inverse-erf>0 z) |
| 2542 |
(let1 r (* pi z z 1/4) ; (pi*z^2)/4 |
| 2543 |
(let loop ((k 0) (cks '(1)) (sum 0) (a 1)) |
| 2544 |
(let1 delta (* a (/ (car cks) (+ k k 1))) |
| 2545 |
(if (< delta (* sum *epsilon*)) |
| 2546 |
(* 1/2 z (sqrt pi) sum) |
| 2547 |
(loop (+ k 1) |
| 2548 |
(cons (calc-next-ck (+ k 1) cks) cks) |
| 2549 |
(+ sum delta) |
| 2550 |
(* a r))))))) |
| 2551 |
|
| 2552 |
(cond [(< z 0) (- (inverse-erf>0 (- z)))] |
| 2553 |
[(= z 0) 0] |
| 2554 |
[else (inverse-erf>0 z)]) ) |
| 2555 |
|
| 2556 |
(define ~= (lambda (x y) (< (abs (- x y)) 1e-7))) |
| 2557 |
;; |
| 2558 |
;; TEST |
| 2559 |
;; |
| 2560 |
(test* "probit(0.025) ok pattern" -1.959964 (probit-ok 0.025) ~=) |
| 2561 |
(test* "probit(0.025) ng pattern" -1.959964 (probit-ng 0.025) ~=) |
| 2562 |
(test* "probit(0.975) ok pattern" 1.959964 (probit-ok 0.975) ~=) |
| 2563 |
(test* "probit(0.975) ng pattern" 1.959964 (probit-ng 0.975) ~=) |
| 2564 |
) |
| 2565 |
|
| 2566 |
;;------------------------------------------------------------------ |
| 2567 |
(test-section "arithmetic operation overload") |
| 2568 |
|
| 2569 |
;; NB: these tests requires the object system working. |
| 2570 |
|
| 2571 |
;; These code are only for tests, and do not suggest the real use of |
| 2572 |
;; arithmetic operation override. For practical use, it is important |
| 2573 |
;; to define those operations consistently. Note that Gauche's compiler |
| 2574 |
;; may reorder or change operations based on the assumption of the |
| 2575 |
;; normal definition of those arithmetic operations. |
| 2576 |
|
| 2577 |
(define-method object-+ ((a <string>) b) #"~|a|+~|b|") |
| 2578 |
(define-method object-+ (a (b <string>)) #"~|a|+~|b|") |
| 2579 |
(define-method object-- ((a <string>) b) #"~|a|-~|b|") |
| 2580 |
(define-method object-- (a (b <string>)) #"~|a|-~|b|") |
| 2581 |
(define-method object-* ((a <string>) b) #"~|a|*~|b|") |
| 2582 |
(define-method object-* (a (b <string>)) #"~|a|*~|b|") |
| 2583 |
(define-method object-/ ((a <string>) b) #"~|a|/~|b|") |
| 2584 |
(define-method object-/ (a (b <string>)) #"~|a|/~|b|") |
| 2585 |
|
| 2586 |
(define-method object-+ ((a <string>)) #"+~|a|") |
| 2587 |
(define-method object-- ((a <string>)) #"-~|a|") |
| 2588 |
(define-method object-* ((a <string>)) #"*~|a|") |
| 2589 |
(define-method object-/ ((a <string>)) #"/~|a|") |
| 2590 |
|
| 2591 |
(test* "object-+" "a+b" (+ "a" "b")) |
| 2592 |
(test* "object-+" "a+b" (+ "a" 'b)) |
| 2593 |
(test* "object-+" "a+b" (+ 'a "b")) |
| 2594 |
(test* "object-+" "3+a" (+ 3 "a")) |
| 2595 |
;; NB: this becomes "3+a" instead of "a+3", because of compiler optimization. |
| 2596 |
;; DO NOT COUNT ON THIS BEHAVIOR IN THE REAL CODE. Might be changed in |
| 2597 |
;; the future release. |
| 2598 |
(test* "object-+" "3+a" (+ "a" 3)) |
| 2599 |
(test* "object-+" "+a" (+ "a")) |
| 2600 |
|
| 2601 |
(test* "object--" "a-b" (- "a" "b")) |
| 2602 |
(test* "object--" "a-b" (- "a" 'b)) |
| 2603 |
(test* "object--" "a-b" (- 'a "b")) |
| 2604 |
(test* "object--" "3-a" (- 3 "a")) |
| 2605 |
;; NB: this becomes "-3+a" instead of "a-3", because of compiler optimization |
| 2606 |
;; DO NOT COUNT ON THIS BEHAVIOR IN THE REAL CODE. Might be changed in |
| 2607 |
;; the future release. |
| 2608 |
(test* "object--" "-3+a" (- "a" 3)) |
| 2609 |
|
| 2610 |
(test* "object--" "-a" (- "a")) |
| 2611 |
|
| 2612 |
(test* "object-*" "a*b" (* "a" "b")) |
| 2613 |
(test* "object-*" "a*b" (* "a" 'b)) |
| 2614 |
(test* "object-*" "a*b" (* 'a "b")) |
| 2615 |
(test* "object-*" "3*a" (* 3 "a")) |
| 2616 |
(test* "object-*" "a*3" (* "a" 3)) |
| 2617 |
(test* "object-*" "*a" (* "a")) |
| 2618 |
|
| 2619 |
(test* "object-/" "a/b" (/ "a" "b")) |
| 2620 |
(test* "object-/" "a/b" (/ "a" 'b)) |
| 2621 |
(test* "object-/" "a/b" (/ 'a "b")) |
| 2622 |
(test* "object-/" "3/a" (/ 3 "a")) |
| 2623 |
(test* "object-/" "a/3" (/ "a" 3)) |
| 2624 |
(test* "object-/" "/a" (/ "a")) |
| 2625 |
|
| 2626 |
(test-end) |