Commit | Line | Data |
---|---|---|
de142bea | 1 | ;;;; numbers.test --- tests guile's numbers -*- scheme -*- |
f480a98e MW |
2 | ;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2009, 2010, 2011, |
3 | ;;;; 2012, 2013 Free Software Foundation, Inc. | |
de142bea | 4 | ;;;; |
73be1d9e MV |
5 | ;;;; This library is free software; you can redistribute it and/or |
6 | ;;;; modify it under the terms of the GNU Lesser General Public | |
7 | ;;;; License as published by the Free Software Foundation; either | |
53befeb7 | 8 | ;;;; version 3 of the License, or (at your option) any later version. |
73be1d9e MV |
9 | ;;;; |
10 | ;;;; This library is distributed in the hope that it will be useful, | |
de142bea | 11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
73be1d9e MV |
12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | ;;;; Lesser General Public License for more details. | |
14 | ;;;; | |
15 | ;;;; You should have received a copy of the GNU Lesser General Public | |
16 | ;;;; License along with this library; if not, write to the Free Software | |
92205699 | 17 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de142bea | 18 | |
a1fb3b1c KR |
19 | (define-module (test-suite test-numbers) |
20 | #:use-module (test-suite lib) | |
ff62c168 | 21 | #:use-module (ice-9 documentation) |
a8591a55 | 22 | #:use-module (srfi srfi-1) ; list library |
ff62c168 | 23 | #:use-module (srfi srfi-11)) ; let-values |
de142bea | 24 | |
de142bea DH |
25 | ;;; |
26 | ;;; miscellaneous | |
27 | ;;; | |
28 | ||
1b3a7932 DH |
29 | (define exception:numerical-overflow |
30 | (cons 'numerical-overflow "^Numerical overflow")) | |
31 | ||
cb18f2a8 | 32 | (define (documented? object) |
5c96bc39 | 33 | (not (not (object-documentation object)))) |
de142bea | 34 | |
8b7838b5 | 35 | (define fixnum-bit |
ba0e46ea MW |
36 | (do ((i 0 (+ 1 i)) |
37 | (n 1 (* 2 n))) | |
38 | ((> n most-positive-fixnum) | |
39 | (+ 1 i)))) | |
8b7838b5 | 40 | |
21e39e8f DH |
41 | (define fixnum-min most-negative-fixnum) |
42 | (define fixnum-max most-positive-fixnum) | |
de142bea | 43 | |
a1fb3b1c KR |
44 | ;; Divine the number of bits in the mantissa of a flonum. |
45 | ;; We look for when 2.0^i+1.0 gets rounded, ie. the difference between that | |
46 | ;; value and 2.0^k is not 1.0. | |
47 | ;; Of course this assumes flonums have a fixed precision mantissa, but | |
48 | ;; that's the case now and probably into the forseeable future. | |
49 | ;; On an IEEE system, which means pretty much everywhere, the value here is | |
50 | ;; the usual 53. | |
51 | ;; | |
52 | (define dbl-mant-dig | |
24475b86 MW |
53 | (do ((prec 0 (+ prec 1)) |
54 | (eps 1.0 (/ eps 2.0))) | |
55 | ((begin (when (> prec 1000000) | |
56 | (error "Unable to determine dbl-mant-dig")) | |
57 | (= 1.0 (+ 1.0 eps))) | |
58 | prec))) | |
59 | ||
60 | (define dbl-epsilon | |
61 | (expt 0.5 (- dbl-mant-dig 1))) | |
62 | ||
98237784 MW |
63 | (define dbl-epsilon-exact |
64 | (expt 1/2 (- dbl-mant-dig 1))) | |
65 | ||
24475b86 MW |
66 | (define dbl-min-exp |
67 | (do ((x 1.0 (/ x 2.0)) | |
68 | (y (+ 1.0 dbl-epsilon) (/ y 2.0)) | |
69 | (e 2 (- e 1))) | |
70 | ((begin (when (< e -100000000) | |
71 | (error "Unable to determine dbl-min-exp")) | |
72 | (= x y)) | |
73 | e))) | |
a1fb3b1c | 74 | |
98237784 MW |
75 | (define dbl-max-exp |
76 | (do ((x 1.0 (* x 2.0)) | |
77 | (e 0 (+ e 1))) | |
78 | ((begin (when (> e 100000000) | |
79 | (error "Unable to determine dbl-max-exp")) | |
80 | (inf? x)) | |
81 | e))) | |
82 | ||
a1fb3b1c KR |
83 | ;; like ash, but working on a flonum |
84 | (define (ash-flo x n) | |
85 | (while (> n 0) | |
86 | (set! x (* 2.0 x)) | |
87 | (set! n (1- n))) | |
88 | (while (< n 0) | |
89 | (set! x (* 0.5 x)) | |
90 | (set! n (1+ n))) | |
91 | x) | |
49579cbd KR |
92 | |
93 | ;; `quotient' but rounded towards -infinity, like `modulo' or `ash' do | |
94 | ;; note only positive D supported (that's all that's currently required) | |
95 | (define-public (quotient-floor n d) | |
96 | (if (negative? n) | |
97 | (quotient (- n d -1) d) ;; neg/pos | |
98 | (quotient n d))) ;; pos/pos | |
99 | ||
8ab3d8a0 KR |
100 | ;; return true of X is in the range LO to HI, inclusive |
101 | (define (within-range? lo hi x) | |
102 | (and (>= x (min lo hi)) | |
103 | (<= x (max lo hi)))) | |
104 | ||
105 | ;; return true if GOT is within +/- 0.01 of GOT | |
106 | ;; for a complex number both real and imaginary parts must be in that range | |
107 | (define (eqv-loosely? want got) | |
108 | (and (within-range? (- (real-part want) 0.01) | |
109 | (+ (real-part want) 0.01) | |
110 | (real-part got)) | |
111 | (within-range? (- (imag-part want) 0.01) | |
112 | (+ (imag-part want) 0.01) | |
113 | (imag-part got)))) | |
114 | ||
115 | ;; return true if OBJ is negative infinity | |
116 | (define (negative-infinity? obj) | |
117 | (and (real? obj) | |
118 | (negative? obj) | |
119 | (inf? obj))) | |
120 | ||
ff62c168 MW |
121 | ;; |
122 | ;; Tolerance used by test-eqv? for inexact numbers. | |
123 | ;; | |
124 | (define test-epsilon 1e-10) | |
125 | ||
126 | ;; | |
127 | ;; Like eqv?, except that inexact finite numbers need only be within | |
a8591a55 MW |
128 | ;; test-epsilon (1e-10) to be considered equal. For non-real complex |
129 | ;; numbers, each component is tested according to these rules. The | |
130 | ;; intent is that the known-correct value will be the first parameter. | |
ff62c168 MW |
131 | ;; |
132 | (define (test-eqv? x y) | |
133 | (cond ((real? x) | |
134 | (and (real? y) (test-real-eqv? x y))) | |
135 | ((complex? x) | |
136 | (and (not (real? y)) | |
137 | (test-real-eqv? (real-part x) (real-part y)) | |
138 | (test-real-eqv? (imag-part x) (imag-part y)))) | |
139 | (else (eqv? x y)))) | |
140 | ||
141 | ;; Auxiliary predicate used by test-eqv? | |
142 | (define (test-real-eqv? x y) | |
a8591a55 | 143 | (cond ((or (exact? x) (nan? x) (inf? x)) |
ff62c168 MW |
144 | (eqv? x y)) |
145 | (else (and (inexact? y) (> test-epsilon (abs (- x y))))))) | |
146 | ||
55a8b708 MW |
147 | ;; return true if OBJ is a real NaN |
148 | (define (real-nan? obj) | |
149 | (and (real? obj) | |
150 | (nan? obj))) | |
151 | ||
c7218482 MW |
152 | ;; return true if OBJ is a non-real complex number |
153 | ;; whose real part is a nan, and whose imaginary | |
154 | ;; part is an inexact zero. | |
155 | (define (almost-real-nan? obj) | |
156 | (and (not (real? obj)) | |
157 | (nan? (real-part obj)) | |
158 | (zero? (imag-part obj)))) | |
159 | ||
55a8b708 MW |
160 | ;; return true if both the real and imaginary |
161 | ;; parts of OBJ are NaNs | |
162 | (define (complex-nan? obj) | |
163 | (and (nan? (real-part obj)) | |
164 | (nan? (imag-part obj)))) | |
165 | ||
166 | ;; return true if the real part of OBJ is zero | |
167 | ;; and the imaginary part is a NaN. | |
168 | (define (imaginary-nan? obj) | |
169 | (and (zero? (real-part obj)) | |
170 | (nan? (imag-part obj)))) | |
171 | ||
c7218482 MW |
172 | ;; return true if OBJ is a non-real complex zero |
173 | (define (complex-zero? obj) | |
174 | (and (zero? obj) | |
175 | (complex? obj) | |
176 | (not (real? obj)))) | |
177 | ||
8ab3d8a0 KR |
178 | (define const-e 2.7182818284590452354) |
179 | (define const-e^2 7.3890560989306502274) | |
180 | (define const-1/e 0.3678794411714423215) | |
181 | ||
182 | ||
a580ebba KR |
183 | ;;; |
184 | ;;; 1+ | |
185 | ;;; | |
186 | ||
f13f1e9f | 187 | (with-test-prefix/c&e "1+" |
a580ebba KR |
188 | |
189 | (pass-if "documented?" | |
190 | (documented? 1+)) | |
191 | ||
f13f1e9f LC |
192 | (pass-if "0" (eqv? 1 (1+ 0))) |
193 | (pass-if "-1" (eqv? 0 (1+ -1))) | |
194 | (pass-if "100" (eqv? 101 (1+ 100))) | |
195 | (pass-if "-100" (eqv? -99 (1+ -100))) | |
e78d4bf9 LC |
196 | |
197 | ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1. | |
f13f1e9f LC |
198 | (pass-if "1+ fixnum = bignum (32-bit)" |
199 | (eqv? 536870912 (1+ 536870911))) | |
e78d4bf9 LC |
200 | |
201 | ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1. | |
f13f1e9f LC |
202 | (pass-if "1+ fixnum = bignum (64-bit)" |
203 | (eqv? 2305843009213693952 (1+ 2305843009213693951)))) | |
a580ebba KR |
204 | |
205 | ;;; | |
206 | ;;; 1- | |
207 | ;;; | |
208 | ||
f13f1e9f | 209 | (with-test-prefix/c&e "1-" |
a580ebba KR |
210 | |
211 | (pass-if "documented?" | |
212 | (documented? 1-)) | |
213 | ||
f13f1e9f LC |
214 | (pass-if "0" (eqv? -1 (1- 0))) |
215 | (pass-if "1" (eqv? 0 (1- 1))) | |
216 | (pass-if "100" (eqv? 99 (1- 100))) | |
217 | (pass-if "-100" (eqv? -101 (1- -100))) | |
e78d4bf9 LC |
218 | |
219 | ;; The minimum fixnum on a 32-bit architecture: -2^29. | |
f13f1e9f LC |
220 | (pass-if "1- fixnum = bignum (32-bit)" |
221 | (eqv? -536870913 (1- -536870912))) | |
e78d4bf9 LC |
222 | |
223 | ;; The minimum fixnum on a 64-bit architecture: -2^61. | |
f13f1e9f LC |
224 | (pass-if "1- fixnum = bignum (64-bit)" |
225 | (eqv? -2305843009213693953 (1- -2305843009213693952)))) | |
a580ebba | 226 | |
de142bea DH |
227 | ;;; |
228 | ;;; exact? | |
229 | ;;; | |
230 | ||
231 | (with-test-prefix "exact?" | |
232 | ||
de142bea | 233 | (pass-if "documented?" |
cb18f2a8 | 234 | (documented? exact?)) |
de142bea | 235 | |
21e39e8f | 236 | (with-test-prefix "integers" |
de142bea | 237 | |
21e39e8f DH |
238 | (pass-if "0" |
239 | (exact? 0)) | |
de142bea | 240 | |
21e39e8f DH |
241 | (pass-if "fixnum-max" |
242 | (exact? fixnum-max)) | |
de142bea | 243 | |
21e39e8f DH |
244 | (pass-if "fixnum-max + 1" |
245 | (exact? (+ fixnum-max 1))) | |
de142bea | 246 | |
21e39e8f DH |
247 | (pass-if "fixnum-min" |
248 | (exact? fixnum-min)) | |
de142bea | 249 | |
21e39e8f DH |
250 | (pass-if "fixnum-min - 1" |
251 | (exact? (- fixnum-min 1)))) | |
252 | ||
253 | (with-test-prefix "reals" | |
254 | ||
255 | ;; (FIXME: need better examples.) | |
256 | ||
257 | (pass-if "sqrt (fixnum-max^2 - 1)" | |
258 | (eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1))))) | |
259 | ||
260 | (pass-if "sqrt ((fixnum-max+1)^2 - 1)" | |
41df63cf MW |
261 | (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1))))) |
262 | ||
263 | (pass-if (not (exact? +inf.0))) | |
264 | (pass-if (not (exact? -inf.0))) | |
265 | (pass-if (not (exact? +nan.0))))) | |
de142bea | 266 | |
8ab3d8a0 KR |
267 | ;;; |
268 | ;;; exp | |
269 | ;;; | |
270 | ||
271 | (with-test-prefix "exp" | |
2519490c | 272 | (pass-if (documented? exp)) |
8ab3d8a0 KR |
273 | |
274 | (pass-if-exception "no args" exception:wrong-num-args | |
275 | (exp)) | |
276 | (pass-if-exception "two args" exception:wrong-num-args | |
277 | (exp 123 456)) | |
278 | ||
279 | (pass-if (eqv? 0.0 (exp -inf.0))) | |
280 | (pass-if (eqv-loosely? 1.0 (exp 0))) | |
281 | (pass-if (eqv-loosely? 1.0 (exp 0.0))) | |
282 | (pass-if (eqv-loosely? const-e (exp 1.0))) | |
283 | (pass-if (eqv-loosely? const-e^2 (exp 2.0))) | |
284 | (pass-if (eqv-loosely? const-1/e (exp -1))) | |
285 | ||
286 | (pass-if "exp(pi*i) = -1" | |
287 | (eqv-loosely? -1.0 (exp 0+3.14159i))) | |
288 | (pass-if "exp(-pi*i) = -1" | |
289 | (eqv-loosely? -1.0 (exp 0-3.14159i))) | |
290 | (pass-if "exp(2*pi*i) = +1" | |
291 | (eqv-loosely? 1.0 (exp 0+6.28318i))) | |
292 | ||
293 | (pass-if "exp(2-pi*i) = -e^2" | |
639fd0a4 | 294 | (eqv-loosely? (- const-e^2) (exp 2.0-3.14159i)))) |
8ab3d8a0 | 295 | |
de142bea DH |
296 | ;;; |
297 | ;;; odd? | |
298 | ;;; | |
299 | ||
7c24e528 RB |
300 | (with-test-prefix "odd?" |
301 | (pass-if (documented? odd?)) | |
302 | (pass-if (odd? 1)) | |
303 | (pass-if (odd? -1)) | |
4d332f19 DH |
304 | (pass-if (not (odd? 0))) |
305 | (pass-if (not (odd? 2))) | |
306 | (pass-if (not (odd? -2))) | |
7c24e528 | 307 | (pass-if (odd? (+ (* 2 fixnum-max) 1))) |
4d332f19 | 308 | (pass-if (not (odd? (* 2 fixnum-max)))) |
7c24e528 | 309 | (pass-if (odd? (- (* 2 fixnum-min) 1))) |
4d332f19 | 310 | (pass-if (not (odd? (* 2 fixnum-min))))) |
de142bea DH |
311 | |
312 | ;;; | |
313 | ;;; even? | |
314 | ;;; | |
315 | ||
7c24e528 RB |
316 | (with-test-prefix "even?" |
317 | (pass-if (documented? even?)) | |
318 | (pass-if (even? 2)) | |
319 | (pass-if (even? -2)) | |
320 | (pass-if (even? 0)) | |
4d332f19 DH |
321 | (pass-if (not (even? 1))) |
322 | (pass-if (not (even? -1))) | |
323 | (pass-if (not (even? (+ (* 2 fixnum-max) 1)))) | |
7c24e528 | 324 | (pass-if (even? (* 2 fixnum-max))) |
4d332f19 | 325 | (pass-if (not (even? (- (* 2 fixnum-min) 1)))) |
7c24e528 | 326 | (pass-if (even? (* 2 fixnum-min)))) |
de142bea | 327 | |
7112615f MW |
328 | ;;; |
329 | ;;; finite? | |
330 | ;;; | |
331 | ||
332 | (with-test-prefix "finite?" | |
333 | (pass-if (documented? finite?)) | |
334 | (pass-if (not (finite? (inf)))) | |
335 | (pass-if (not (finite? +inf.0))) | |
336 | (pass-if (not (finite? -inf.0))) | |
10391e06 | 337 | (pass-if-exception |
c9cf90d4 | 338 | "complex numbers not in domain of finite?" |
10391e06 AW |
339 | exception:wrong-type-arg |
340 | (finite? +inf.0+1i)) | |
341 | (pass-if-exception | |
c9cf90d4 | 342 | "complex numbers not in domain of finite? (2)" |
10391e06 AW |
343 | exception:wrong-type-arg |
344 | (finite? +1+inf.0i)) | |
345 | (pass-if-exception | |
c9cf90d4 | 346 | "complex numbers not in domain of finite? (3)" |
10391e06 AW |
347 | exception:wrong-type-arg |
348 | (finite? +1+1i)) | |
349 | (pass-if (finite? 3+0i)) | |
7112615f MW |
350 | (pass-if (not (finite? (nan)))) |
351 | (pass-if (not (finite? +nan.0))) | |
7112615f MW |
352 | (pass-if (finite? 0)) |
353 | (pass-if (finite? 0.0)) | |
354 | (pass-if (finite? -0.0)) | |
355 | (pass-if (finite? 42.0)) | |
356 | (pass-if (finite? 1/2)) | |
7112615f MW |
357 | (pass-if (finite? (+ fixnum-max 1))) |
358 | (pass-if (finite? (- fixnum-min 1)))) | |
359 | ||
de142bea | 360 | ;;; |
7c24e528 RB |
361 | ;;; inf? and inf |
362 | ;;; | |
363 | ||
364 | (with-test-prefix "inf?" | |
365 | (pass-if (documented? inf?)) | |
366 | (pass-if (inf? (inf))) | |
367 | ;; FIXME: what are the expected behaviors? | |
368 | ;; (pass-if (inf? (/ 1.0 0.0)) | |
369 | ;; (pass-if (inf? (/ 1 0.0)) | |
10391e06 | 370 | (pass-if-exception |
c9cf90d4 | 371 | "complex numbers not in domain of inf?" |
10391e06 AW |
372 | exception:wrong-type-arg |
373 | (inf? +1+inf.0i)) | |
374 | (pass-if (inf? +inf.0+0i)) | |
4d332f19 DH |
375 | (pass-if (not (inf? 0))) |
376 | (pass-if (not (inf? 42.0))) | |
377 | (pass-if (not (inf? (+ fixnum-max 1)))) | |
378 | (pass-if (not (inf? (- fixnum-min 1))))) | |
7c24e528 RB |
379 | |
380 | ;;; | |
381 | ;;; nan? and nan | |
de142bea DH |
382 | ;;; |
383 | ||
7c24e528 RB |
384 | (with-test-prefix "nan?" |
385 | (pass-if (documented? nan?)) | |
386 | (pass-if (nan? (nan))) | |
387 | ;; FIXME: other ways we should be able to generate NaN? | |
4d332f19 DH |
388 | (pass-if (not (nan? 0))) |
389 | (pass-if (not (nan? 42.0))) | |
390 | (pass-if (not (nan? (+ fixnum-max 1)))) | |
391 | (pass-if (not (nan? (- fixnum-min 1))))) | |
de142bea | 392 | |
7c24e528 RB |
393 | ;;; |
394 | ;;; abs | |
395 | ;;; | |
396 | ||
397 | (with-test-prefix "abs" | |
398 | (pass-if (documented? abs)) | |
9b9ef10c MW |
399 | (pass-if (eqv? 0 (abs 0))) |
400 | (pass-if (eqv? 1 (abs 1))) | |
401 | (pass-if (eqv? 1 (abs -1))) | |
402 | ||
403 | (with-test-prefix "double-negation of fixnum-min" | |
404 | (pass-if (eqv? fixnum-min (- (abs fixnum-min))))) | |
405 | ||
406 | (pass-if (eqv? (+ fixnum-max 1) (abs (+ fixnum-max 1)))) | |
407 | (pass-if (eqv? (+ (- fixnum-min) 1) (abs (- fixnum-min 1)))) | |
408 | ||
409 | (pass-if (eqv? 0.0 (abs 0.0))) | |
410 | (pass-if (eqv? 0.0 (abs -0.0))) | |
411 | (pass-if (eqv? 1.0 (abs 1.0))) | |
412 | (pass-if (eqv? 1.0 (abs -1.0))) | |
413 | (pass-if (real-nan? (abs +nan.0))) | |
414 | (pass-if (eqv? +inf.0 (abs +inf.0))) | |
415 | (pass-if (eqv? +inf.0 (abs -inf.0)))) | |
76903a31 | 416 | |
de142bea DH |
417 | ;;; |
418 | ;;; quotient | |
419 | ;;; | |
420 | ||
421 | (with-test-prefix "quotient" | |
2519490c | 422 | (pass-if (documented? quotient)) |
de142bea | 423 | |
de142bea DH |
424 | (with-test-prefix "0 / n" |
425 | ||
426 | (pass-if "n = 1" | |
427 | (eqv? 0 (quotient 0 1))) | |
428 | ||
429 | (pass-if "n = -1" | |
430 | (eqv? 0 (quotient 0 -1))) | |
431 | ||
21e39e8f DH |
432 | (pass-if "n = 2" |
433 | (eqv? 0 (quotient 0 2))) | |
434 | ||
435 | (pass-if "n = fixnum-max" | |
436 | (eqv? 0 (quotient 0 fixnum-max))) | |
437 | ||
438 | (pass-if "n = fixnum-max + 1" | |
439 | (eqv? 0 (quotient 0 (+ fixnum-max 1)))) | |
440 | ||
441 | (pass-if "n = fixnum-min" | |
442 | (eqv? 0 (quotient 0 fixnum-min))) | |
443 | ||
444 | (pass-if "n = fixnum-min - 1" | |
445 | (eqv? 0 (quotient 0 (- fixnum-min 1))))) | |
de142bea | 446 | |
21e39e8f | 447 | (with-test-prefix "1 / n" |
de142bea DH |
448 | |
449 | (pass-if "n = 1" | |
450 | (eqv? 1 (quotient 1 1))) | |
451 | ||
452 | (pass-if "n = -1" | |
21e39e8f DH |
453 | (eqv? -1 (quotient 1 -1))) |
454 | ||
455 | (pass-if "n = 2" | |
456 | (eqv? 0 (quotient 1 2))) | |
de142bea | 457 | |
21e39e8f DH |
458 | (pass-if "n = fixnum-max" |
459 | (eqv? 0 (quotient 1 fixnum-max))) | |
de142bea | 460 | |
21e39e8f DH |
461 | (pass-if "n = fixnum-max + 1" |
462 | (eqv? 0 (quotient 1 (+ fixnum-max 1)))) | |
de142bea | 463 | |
21e39e8f DH |
464 | (pass-if "n = fixnum-min" |
465 | (eqv? 0 (quotient 1 fixnum-min))) | |
466 | ||
467 | (pass-if "n = fixnum-min - 1" | |
468 | (eqv? 0 (quotient 1 (- fixnum-min 1))))) | |
469 | ||
470 | (with-test-prefix "-1 / n" | |
de142bea DH |
471 | |
472 | (pass-if "n = 1" | |
21e39e8f | 473 | (eqv? -1 (quotient -1 1))) |
de142bea DH |
474 | |
475 | (pass-if "n = -1" | |
476 | (eqv? 1 (quotient -1 -1))) | |
477 | ||
21e39e8f DH |
478 | (pass-if "n = 2" |
479 | (eqv? 0 (quotient -1 2))) | |
480 | ||
481 | (pass-if "n = fixnum-max" | |
482 | (eqv? 0 (quotient -1 fixnum-max))) | |
483 | ||
484 | (pass-if "n = fixnum-max + 1" | |
485 | (eqv? 0 (quotient -1 (+ fixnum-max 1)))) | |
486 | ||
487 | (pass-if "n = fixnum-min" | |
488 | (eqv? 0 (quotient -1 fixnum-min))) | |
489 | ||
490 | (pass-if "n = fixnum-min - 1" | |
491 | (eqv? 0 (quotient -1 (- fixnum-min 1))))) | |
492 | ||
493 | (with-test-prefix "fixnum-max / n" | |
494 | ||
495 | (pass-if "n = 1" | |
496 | (eqv? fixnum-max (quotient fixnum-max 1))) | |
497 | ||
498 | (pass-if "n = -1" | |
499 | (eqv? (- fixnum-max) (quotient fixnum-max -1))) | |
500 | ||
501 | (pass-if "n = 2" | |
502 | (eqv? fixnum-max (+ (* (quotient fixnum-max 2) 2) 1))) | |
503 | ||
504 | (pass-if "n = fixnum-max" | |
505 | (eqv? 1 (quotient fixnum-max fixnum-max))) | |
506 | ||
507 | (pass-if "n = fixnum-max + 1" | |
508 | (eqv? 0 (quotient fixnum-max (+ fixnum-max 1)))) | |
509 | ||
510 | (pass-if "n = fixnum-min" | |
511 | (eqv? 0 (quotient fixnum-max fixnum-min))) | |
512 | ||
513 | (pass-if "n = fixnum-min - 1" | |
514 | (eqv? 0 (quotient fixnum-max (- fixnum-min 1))))) | |
515 | ||
516 | (with-test-prefix "(fixnum-max + 1) / n" | |
517 | ||
518 | (pass-if "n = 1" | |
519 | (eqv? (+ fixnum-max 1) (quotient (+ fixnum-max 1) 1))) | |
520 | ||
521 | (pass-if "n = -1" | |
522 | (eqv? (- (+ fixnum-max 1)) (quotient (+ fixnum-max 1) -1))) | |
523 | ||
524 | (pass-if "n = 2" | |
525 | (eqv? (+ fixnum-max 1) (* (quotient (+ fixnum-max 1) 2) 2))) | |
526 | ||
527 | (pass-if "n = fixnum-max" | |
528 | (eqv? 1 (quotient (+ fixnum-max 1) fixnum-max))) | |
529 | ||
530 | (pass-if "n = fixnum-max + 1" | |
531 | (eqv? 1 (quotient (+ fixnum-max 1) (+ fixnum-max 1)))) | |
532 | ||
533 | (pass-if "n = fixnum-min" | |
534 | (eqv? -1 (quotient (+ fixnum-max 1) fixnum-min))) | |
535 | ||
536 | (pass-if "n = fixnum-min - 1" | |
537 | (eqv? 0 (quotient (+ fixnum-max 1) (- fixnum-min 1))))) | |
538 | ||
539 | (with-test-prefix "fixnum-min / n" | |
540 | ||
541 | (pass-if "n = 1" | |
542 | (eqv? fixnum-min (quotient fixnum-min 1))) | |
543 | ||
544 | (pass-if "n = -1" | |
545 | (eqv? (- fixnum-min) (quotient fixnum-min -1))) | |
546 | ||
547 | (pass-if "n = 2" | |
548 | (eqv? fixnum-min (* (quotient fixnum-min 2) 2))) | |
549 | ||
550 | (pass-if "n = fixnum-max" | |
551 | (eqv? -1 (quotient fixnum-min fixnum-max))) | |
552 | ||
553 | (pass-if "n = fixnum-max + 1" | |
554 | (eqv? -1 (quotient fixnum-min (+ fixnum-max 1)))) | |
555 | ||
556 | (pass-if "n = fixnum-min" | |
557 | (eqv? 1 (quotient fixnum-min fixnum-min))) | |
558 | ||
559 | (pass-if "n = fixnum-min - 1" | |
ad22fe7c KR |
560 | (eqv? 0 (quotient fixnum-min (- fixnum-min 1)))) |
561 | ||
562 | (pass-if "n = - fixnum-min - 1" | |
563 | (eqv? -1 (quotient fixnum-min (1- (- fixnum-min))))) | |
564 | ||
565 | ;; special case, normally inum/big is zero | |
566 | (pass-if "n = - fixnum-min" | |
567 | (eqv? -1 (quotient fixnum-min (- fixnum-min)))) | |
568 | ||
569 | (pass-if "n = - fixnum-min + 1" | |
570 | (eqv? 0 (quotient fixnum-min (1+ (- fixnum-min)))))) | |
21e39e8f DH |
571 | |
572 | (with-test-prefix "(fixnum-min - 1) / n" | |
573 | ||
574 | (pass-if "n = 1" | |
575 | (eqv? (- fixnum-min 1) (quotient (- fixnum-min 1) 1))) | |
576 | ||
577 | (pass-if "n = -1" | |
578 | (eqv? (- (- fixnum-min 1)) (quotient (- fixnum-min 1) -1))) | |
579 | ||
580 | (pass-if "n = 2" | |
581 | (eqv? fixnum-min (* (quotient (- fixnum-min 1) 2) 2))) | |
582 | ||
583 | (pass-if "n = fixnum-max" | |
584 | (eqv? -1 (quotient (- fixnum-min 1) fixnum-max))) | |
585 | ||
586 | (pass-if "n = fixnum-max + 1" | |
587 | (eqv? -1 (quotient (- fixnum-min 1) (+ fixnum-max 1)))) | |
588 | ||
589 | (pass-if "n = fixnum-min" | |
590 | (eqv? 1 (quotient (- fixnum-min 1) fixnum-min))) | |
591 | ||
592 | (pass-if "n = fixnum-min - 1" | |
593 | (eqv? 1 (quotient (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea | 594 | |
495a39c4 MW |
595 | ;; Inexact integers |
596 | ||
597 | (pass-if (eqv? 5.0 (quotient 35.0 7.0))) | |
598 | (pass-if (eqv? 5.0 (quotient 35 7.0))) | |
599 | (pass-if (eqv? 5.0 (quotient 35.0 7 ))) | |
600 | ||
de142bea DH |
601 | ;; Positive dividend and divisor |
602 | ||
603 | (pass-if "35 / 7" | |
604 | (eqv? 5 (quotient 35 7))) | |
605 | ||
606 | ;; Negative dividend, positive divisor | |
607 | ||
608 | (pass-if "-35 / 7" | |
609 | (eqv? -5 (quotient -35 7))) | |
610 | ||
611 | ;; Positive dividend, negative divisor | |
612 | ||
613 | (pass-if "35 / -7" | |
614 | (eqv? -5 (quotient 35 -7))) | |
615 | ||
616 | ;; Negative dividend and divisor | |
617 | ||
618 | (pass-if "-35 / -7" | |
619 | (eqv? 5 (quotient -35 -7))) | |
620 | ||
621 | ;; Are numerical overflows detected correctly? | |
622 | ||
80074d77 DH |
623 | (with-test-prefix "division by zero" |
624 | ||
625 | (pass-if-exception "(quotient 1 0)" | |
626 | exception:numerical-overflow | |
627 | (quotient 1 0)) | |
628 | ||
629 | (pass-if-exception "(quotient bignum 0)" | |
630 | exception:numerical-overflow | |
631 | (quotient (+ fixnum-max 1) 0))) | |
632 | ||
de142bea DH |
633 | ;; Are wrong type arguments detected correctly? |
634 | ||
635 | ) | |
636 | ||
637 | ;;; | |
638 | ;;; remainder | |
639 | ;;; | |
640 | ||
641 | (with-test-prefix "remainder" | |
2519490c | 642 | (pass-if (documented? remainder)) |
de142bea | 643 | |
de142bea DH |
644 | (with-test-prefix "0 / n" |
645 | ||
646 | (pass-if "n = 1" | |
647 | (eqv? 0 (remainder 0 1))) | |
648 | ||
649 | (pass-if "n = -1" | |
650 | (eqv? 0 (remainder 0 -1))) | |
651 | ||
21e39e8f DH |
652 | (pass-if "n = fixnum-max" |
653 | (eqv? 0 (remainder 0 fixnum-max))) | |
654 | ||
655 | (pass-if "n = fixnum-max + 1" | |
656 | (eqv? 0 (remainder 0 (+ fixnum-max 1)))) | |
657 | ||
658 | (pass-if "n = fixnum-min" | |
659 | (eqv? 0 (remainder 0 fixnum-min))) | |
de142bea | 660 | |
21e39e8f DH |
661 | (pass-if "n = fixnum-min - 1" |
662 | (eqv? 0 (remainder 0 (- fixnum-min 1))))) | |
de142bea | 663 | |
21e39e8f | 664 | (with-test-prefix "1 / n" |
de142bea DH |
665 | |
666 | (pass-if "n = 1" | |
667 | (eqv? 0 (remainder 1 1))) | |
668 | ||
669 | (pass-if "n = -1" | |
21e39e8f DH |
670 | (eqv? 0 (remainder 1 -1))) |
671 | ||
672 | (pass-if "n = fixnum-max" | |
673 | (eqv? 1 (remainder 1 fixnum-max))) | |
de142bea | 674 | |
21e39e8f DH |
675 | (pass-if "n = fixnum-max + 1" |
676 | (eqv? 1 (remainder 1 (+ fixnum-max 1)))) | |
de142bea | 677 | |
21e39e8f DH |
678 | (pass-if "n = fixnum-min" |
679 | (eqv? 1 (remainder 1 fixnum-min))) | |
de142bea | 680 | |
21e39e8f DH |
681 | (pass-if "n = fixnum-min - 1" |
682 | (eqv? 1 (remainder 1 (- fixnum-min 1))))) | |
683 | ||
684 | (with-test-prefix "-1 / n" | |
de142bea DH |
685 | |
686 | (pass-if "n = 1" | |
21e39e8f | 687 | (eqv? 0 (remainder -1 1))) |
de142bea DH |
688 | |
689 | (pass-if "n = -1" | |
690 | (eqv? 0 (remainder -1 -1))) | |
691 | ||
21e39e8f DH |
692 | (pass-if "n = fixnum-max" |
693 | (eqv? -1 (remainder -1 fixnum-max))) | |
694 | ||
695 | (pass-if "n = fixnum-max + 1" | |
696 | (eqv? -1 (remainder -1 (+ fixnum-max 1)))) | |
697 | ||
698 | (pass-if "n = fixnum-min" | |
699 | (eqv? -1 (remainder -1 fixnum-min))) | |
700 | ||
701 | (pass-if "n = fixnum-min - 1" | |
702 | (eqv? -1 (remainder -1 (- fixnum-min 1))))) | |
703 | ||
704 | (with-test-prefix "fixnum-max / n" | |
705 | ||
706 | (pass-if "n = 1" | |
707 | (eqv? 0 (remainder fixnum-max 1))) | |
708 | ||
709 | (pass-if "n = -1" | |
710 | (eqv? 0 (remainder fixnum-max -1))) | |
711 | ||
712 | (pass-if "n = fixnum-max" | |
713 | (eqv? 0 (remainder fixnum-max fixnum-max))) | |
714 | ||
715 | (pass-if "n = fixnum-max + 1" | |
716 | (eqv? fixnum-max (remainder fixnum-max (+ fixnum-max 1)))) | |
717 | ||
718 | (pass-if "n = fixnum-min" | |
719 | (eqv? fixnum-max (remainder fixnum-max fixnum-min))) | |
720 | ||
721 | (pass-if "n = fixnum-min - 1" | |
722 | (eqv? fixnum-max (remainder fixnum-max (- fixnum-min 1))))) | |
723 | ||
724 | (with-test-prefix "(fixnum-max + 1) / n" | |
725 | ||
726 | (pass-if "n = 1" | |
727 | (eqv? 0 (remainder (+ fixnum-max 1) 1))) | |
728 | ||
729 | (pass-if "n = -1" | |
730 | (eqv? 0 (remainder (+ fixnum-max 1) -1))) | |
731 | ||
732 | (pass-if "n = fixnum-max" | |
733 | (eqv? 1 (remainder (+ fixnum-max 1) fixnum-max))) | |
734 | ||
735 | (pass-if "n = fixnum-max + 1" | |
736 | (eqv? 0 (remainder (+ fixnum-max 1) (+ fixnum-max 1)))) | |
737 | ||
738 | (pass-if "n = fixnum-min" | |
739 | (eqv? 0 (remainder (+ fixnum-max 1) fixnum-min))) | |
740 | ||
741 | (pass-if "n = fixnum-min - 1" | |
742 | (eqv? (+ fixnum-max 1) (remainder (+ fixnum-max 1) (- fixnum-min 1))))) | |
743 | ||
744 | (with-test-prefix "fixnum-min / n" | |
745 | ||
746 | (pass-if "n = 1" | |
747 | (eqv? 0 (remainder fixnum-min 1))) | |
748 | ||
749 | (pass-if "n = -1" | |
750 | (eqv? 0 (remainder fixnum-min -1))) | |
751 | ||
752 | (pass-if "n = fixnum-max" | |
753 | (eqv? -1 (remainder fixnum-min fixnum-max))) | |
754 | ||
755 | (pass-if "n = fixnum-max + 1" | |
756 | (eqv? 0 (remainder fixnum-min (+ fixnum-max 1)))) | |
757 | ||
758 | (pass-if "n = fixnum-min" | |
759 | (eqv? 0 (remainder fixnum-min fixnum-min))) | |
760 | ||
761 | (pass-if "n = fixnum-min - 1" | |
ad22fe7c KR |
762 | (eqv? fixnum-min (remainder fixnum-min (- fixnum-min 1)))) |
763 | ||
764 | (pass-if "n = - fixnum-min - 1" | |
765 | (eqv? -1 (remainder fixnum-min (1- (- fixnum-min))))) | |
766 | ||
767 | ;; special case, normally inum%big is the inum | |
768 | (pass-if "n = - fixnum-min" | |
769 | (eqv? 0 (remainder fixnum-min (- fixnum-min)))) | |
770 | ||
771 | (pass-if "n = - fixnum-min + 1" | |
772 | (eqv? fixnum-min (remainder fixnum-min (1+ (- fixnum-min)))))) | |
21e39e8f DH |
773 | |
774 | (with-test-prefix "(fixnum-min - 1) / n" | |
775 | ||
776 | (pass-if "n = 1" | |
777 | (eqv? 0 (remainder (- fixnum-min 1) 1))) | |
778 | ||
779 | (pass-if "n = -1" | |
780 | (eqv? 0 (remainder (- fixnum-min 1) -1))) | |
781 | ||
782 | (pass-if "n = fixnum-max" | |
783 | (eqv? -2 (remainder (- fixnum-min 1) fixnum-max))) | |
784 | ||
785 | (pass-if "n = fixnum-max + 1" | |
786 | (eqv? -1 (remainder (- fixnum-min 1) (+ fixnum-max 1)))) | |
787 | ||
788 | (pass-if "n = fixnum-min" | |
789 | (eqv? -1 (remainder (- fixnum-min 1) fixnum-min))) | |
790 | ||
791 | (pass-if "n = fixnum-min - 1" | |
792 | (eqv? 0 (remainder (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea | 793 | |
495a39c4 MW |
794 | ;; Inexact integers |
795 | ||
796 | (pass-if (eqv? 2.0 (remainder 37.0 7.0))) | |
797 | (pass-if (eqv? 2.0 (remainder 37 7.0))) | |
798 | (pass-if (eqv? 2.0 (remainder 37.0 7 ))) | |
799 | ||
de142bea DH |
800 | ;; Positive dividend and divisor |
801 | ||
802 | (pass-if "35 / 7" | |
803 | (eqv? 0 (remainder 35 7))) | |
804 | ||
805 | ;; Negative dividend, positive divisor | |
806 | ||
807 | (pass-if "-35 / 7" | |
808 | (eqv? 0 (remainder -35 7))) | |
809 | ||
810 | ;; Positive dividend, negative divisor | |
811 | ||
812 | (pass-if "35 / -7" | |
813 | (eqv? 0 (remainder 35 -7))) | |
814 | ||
815 | ;; Negative dividend and divisor | |
816 | ||
817 | (pass-if "-35 / -7" | |
818 | (eqv? 0 (remainder -35 -7))) | |
819 | ||
820 | ;; Are numerical overflows detected correctly? | |
821 | ||
80074d77 DH |
822 | (with-test-prefix "division by zero" |
823 | ||
824 | (pass-if-exception "(remainder 1 0)" | |
825 | exception:numerical-overflow | |
826 | (remainder 1 0)) | |
827 | ||
828 | (pass-if-exception "(remainder bignum 0)" | |
829 | exception:numerical-overflow | |
830 | (remainder (+ fixnum-max 1) 0))) | |
831 | ||
de142bea DH |
832 | ;; Are wrong type arguments detected correctly? |
833 | ||
834 | ) | |
835 | ||
836 | ;;; | |
837 | ;;; modulo | |
838 | ;;; | |
839 | ||
840 | (with-test-prefix "modulo" | |
2519490c | 841 | (pass-if (documented? modulo)) |
de142bea | 842 | |
de142bea DH |
843 | (with-test-prefix "0 % n" |
844 | ||
845 | (pass-if "n = 1" | |
846 | (eqv? 0 (modulo 0 1))) | |
847 | ||
848 | (pass-if "n = -1" | |
849 | (eqv? 0 (modulo 0 -1))) | |
850 | ||
21e39e8f DH |
851 | (pass-if "n = fixnum-max" |
852 | (eqv? 0 (modulo 0 fixnum-max))) | |
853 | ||
854 | (pass-if "n = fixnum-max + 1" | |
855 | (eqv? 0 (modulo 0 (+ fixnum-max 1)))) | |
de142bea | 856 | |
21e39e8f DH |
857 | (pass-if "n = fixnum-min" |
858 | (eqv? 0 (modulo 0 fixnum-min))) | |
de142bea | 859 | |
21e39e8f DH |
860 | (pass-if "n = fixnum-min - 1" |
861 | (eqv? 0 (modulo 0 (- fixnum-min 1))))) | |
862 | ||
863 | (with-test-prefix "1 % n" | |
de142bea DH |
864 | |
865 | (pass-if "n = 1" | |
866 | (eqv? 0 (modulo 1 1))) | |
867 | ||
868 | (pass-if "n = -1" | |
21e39e8f | 869 | (eqv? 0 (modulo 1 -1))) |
de142bea | 870 | |
21e39e8f DH |
871 | (pass-if "n = fixnum-max" |
872 | (eqv? 1 (modulo 1 fixnum-max))) | |
de142bea | 873 | |
21e39e8f DH |
874 | (pass-if "n = fixnum-max + 1" |
875 | (eqv? 1 (modulo 1 (+ fixnum-max 1)))) | |
de142bea | 876 | |
21e39e8f DH |
877 | (pass-if "n = fixnum-min" |
878 | (eqv? (+ fixnum-min 1) (modulo 1 fixnum-min))) | |
879 | ||
880 | (pass-if "n = fixnum-min - 1" | |
881 | (eqv? fixnum-min (modulo 1 (- fixnum-min 1))))) | |
882 | ||
883 | (with-test-prefix "-1 % n" | |
de142bea DH |
884 | |
885 | (pass-if "n = 1" | |
21e39e8f | 886 | (eqv? 0 (modulo -1 1))) |
de142bea DH |
887 | |
888 | (pass-if "n = -1" | |
889 | (eqv? 0 (modulo -1 -1))) | |
890 | ||
21e39e8f DH |
891 | (pass-if "n = fixnum-max" |
892 | (eqv? (- fixnum-max 1) (modulo -1 fixnum-max))) | |
893 | ||
894 | (pass-if "n = fixnum-max + 1" | |
895 | (eqv? fixnum-max (modulo -1 (+ fixnum-max 1)))) | |
896 | ||
897 | (pass-if "n = fixnum-min" | |
898 | (eqv? -1 (modulo -1 fixnum-min))) | |
899 | ||
900 | (pass-if "n = fixnum-min - 1" | |
901 | (eqv? -1 (modulo -1 (- fixnum-min 1))))) | |
902 | ||
903 | (with-test-prefix "fixnum-max % n" | |
904 | ||
905 | (pass-if "n = 1" | |
906 | (eqv? 0 (modulo fixnum-max 1))) | |
907 | ||
908 | (pass-if "n = -1" | |
909 | (eqv? 0 (modulo fixnum-max -1))) | |
910 | ||
911 | (pass-if "n = fixnum-max" | |
912 | (eqv? 0 (modulo fixnum-max fixnum-max))) | |
913 | ||
914 | (pass-if "n = fixnum-max + 1" | |
915 | (eqv? fixnum-max (modulo fixnum-max (+ fixnum-max 1)))) | |
916 | ||
917 | (pass-if "n = fixnum-min" | |
918 | (eqv? -1 (modulo fixnum-max fixnum-min))) | |
919 | ||
920 | (pass-if "n = fixnum-min - 1" | |
921 | (eqv? -2 (modulo fixnum-max (- fixnum-min 1))))) | |
922 | ||
923 | (with-test-prefix "(fixnum-max + 1) % n" | |
924 | ||
925 | (pass-if "n = 1" | |
926 | (eqv? 0 (modulo (+ fixnum-max 1) 1))) | |
927 | ||
928 | (pass-if "n = -1" | |
929 | (eqv? 0 (modulo (+ fixnum-max 1) -1))) | |
930 | ||
931 | (pass-if "n = fixnum-max" | |
932 | (eqv? 1 (modulo (+ fixnum-max 1) fixnum-max))) | |
933 | ||
934 | (pass-if "n = fixnum-max + 1" | |
935 | (eqv? 0 (modulo (+ fixnum-max 1) (+ fixnum-max 1)))) | |
936 | ||
937 | (pass-if "n = fixnum-min" | |
938 | (eqv? 0 (modulo (+ fixnum-max 1) fixnum-min))) | |
939 | ||
940 | (pass-if "n = fixnum-min - 1" | |
941 | (eqv? -1 (modulo (+ fixnum-max 1) (- fixnum-min 1))))) | |
942 | ||
943 | (with-test-prefix "fixnum-min % n" | |
944 | ||
945 | (pass-if "n = 1" | |
946 | (eqv? 0 (modulo fixnum-min 1))) | |
947 | ||
948 | (pass-if "n = -1" | |
949 | (eqv? 0 (modulo fixnum-min -1))) | |
950 | ||
951 | (pass-if "n = fixnum-max" | |
952 | (eqv? (- fixnum-max 1) (modulo fixnum-min fixnum-max))) | |
953 | ||
954 | (pass-if "n = fixnum-max + 1" | |
955 | (eqv? 0 (modulo fixnum-min (+ fixnum-max 1)))) | |
956 | ||
957 | (pass-if "n = fixnum-min" | |
958 | (eqv? 0 (modulo fixnum-min fixnum-min))) | |
959 | ||
960 | (pass-if "n = fixnum-min - 1" | |
961 | (eqv? fixnum-min (modulo fixnum-min (- fixnum-min 1))))) | |
962 | ||
963 | (with-test-prefix "(fixnum-min - 1) % n" | |
964 | ||
965 | (pass-if "n = 1" | |
966 | (eqv? 0 (modulo (- fixnum-min 1) 1))) | |
967 | ||
968 | (pass-if "n = -1" | |
969 | (eqv? 0 (modulo (- fixnum-min 1) -1))) | |
970 | ||
971 | (pass-if "n = fixnum-max" | |
972 | (eqv? (- fixnum-max 2) (modulo (- fixnum-min 1) fixnum-max))) | |
973 | ||
974 | (pass-if "n = fixnum-max + 1" | |
975 | (eqv? fixnum-max (modulo (- fixnum-min 1) (+ fixnum-max 1)))) | |
976 | ||
977 | (pass-if "n = fixnum-min" | |
978 | (eqv? -1 (modulo (- fixnum-min 1) fixnum-min))) | |
979 | ||
980 | (pass-if "n = fixnum-min - 1" | |
981 | (eqv? 0 (modulo (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea | 982 | |
495a39c4 MW |
983 | ;; Inexact integers |
984 | ||
985 | (pass-if (eqv? 1.0 (modulo 13.0 4.0))) | |
986 | (pass-if (eqv? 1.0 (modulo 13 4.0))) | |
987 | (pass-if (eqv? 1.0 (modulo 13.0 4 ))) | |
988 | ||
de142bea DH |
989 | ;; Positive dividend and divisor |
990 | ||
991 | (pass-if "13 % 4" | |
992 | (eqv? 1 (modulo 13 4))) | |
993 | ||
994 | (pass-if "2177452800 % 86400" | |
995 | (eqv? 0 (modulo 2177452800 86400))) | |
996 | ||
997 | ;; Negative dividend, positive divisor | |
998 | ||
999 | (pass-if "-13 % 4" | |
1000 | (eqv? 3 (modulo -13 4))) | |
1001 | ||
1002 | (pass-if "-2177452800 % 86400" | |
1003 | (eqv? 0 (modulo -2177452800 86400))) | |
1004 | ||
1005 | ;; Positive dividend, negative divisor | |
1006 | ||
1007 | (pass-if "13 % -4" | |
1008 | (eqv? -3 (modulo 13 -4))) | |
1009 | ||
1010 | (pass-if "2177452800 % -86400" | |
1011 | (eqv? 0 (modulo 2177452800 -86400))) | |
1012 | ||
1013 | ;; Negative dividend and divisor | |
1014 | ||
1015 | (pass-if "-13 % -4" | |
1016 | (eqv? -1 (modulo -13 -4))) | |
1017 | ||
1018 | (pass-if "-2177452800 % -86400" | |
1019 | (eqv? 0 (modulo -2177452800 -86400))) | |
1020 | ||
1021 | ;; Are numerical overflows detected correctly? | |
1022 | ||
80074d77 DH |
1023 | (with-test-prefix "division by zero" |
1024 | ||
1025 | (pass-if-exception "(modulo 1 0)" | |
1026 | exception:numerical-overflow | |
1027 | (modulo 1 0)) | |
1028 | ||
1029 | (pass-if-exception "(modulo bignum 0)" | |
1030 | exception:numerical-overflow | |
1031 | (modulo (+ fixnum-max 1) 0))) | |
1032 | ||
de142bea DH |
1033 | ;; Are wrong type arguments detected correctly? |
1034 | ||
1035 | ) | |
1036 | ||
24360e11 KR |
1037 | ;;; |
1038 | ;;; modulo-expt | |
1039 | ;;; | |
1040 | ||
1041 | (with-test-prefix "modulo-expt" | |
1042 | (pass-if (= 1 (modulo-expt 17 23 47))) | |
1043 | ||
1044 | (pass-if (= 1 (modulo-expt 17 -23 47))) | |
1045 | ||
1046 | (pass-if (= 17 (modulo-expt 17 -22 47))) | |
1047 | ||
1048 | (pass-if (= 36 (modulo-expt 17 22 47))) | |
1049 | ||
1050 | (pass-if (= 183658794479969134816674175082294846241553725240 (modulo-expt 111122223333444455556666 111122223333444455556666 1153478690012629968439432872520758982731022934717))) | |
1051 | ||
1052 | (pass-if-exception | |
1053 | "Proper exception with 0 modulus" | |
18ee5de9 | 1054 | exception:numerical-overflow |
24360e11 KR |
1055 | (modulo-expt 17 23 0)) |
1056 | ||
1057 | (pass-if-exception | |
1058 | "Proper exception when result not invertible" | |
18ee5de9 | 1059 | exception:numerical-overflow |
24360e11 KR |
1060 | (modulo-expt 10 -1 48)) |
1061 | ||
1062 | (pass-if-exception | |
1063 | "Proper exception with wrong type argument" | |
18ee5de9 | 1064 | exception:wrong-type-arg |
24360e11 KR |
1065 | (modulo-expt "Sam" 23 10)) |
1066 | ||
1067 | (pass-if-exception | |
1068 | "Proper exception with wrong type argument" | |
18ee5de9 | 1069 | exception:wrong-type-arg |
24360e11 KR |
1070 | (modulo-expt 17 9.9 10)) |
1071 | ||
1072 | (pass-if-exception | |
1073 | "Proper exception with wrong type argument" | |
18ee5de9 | 1074 | exception:wrong-type-arg |
24360e11 KR |
1075 | (modulo-expt 17 23 'Ethel))) |
1076 | ||
ba46895c KR |
1077 | ;;; |
1078 | ;;; numerator | |
1079 | ;;; | |
1080 | ||
1081 | (with-test-prefix "numerator" | |
1082 | (pass-if "0" | |
1083 | (eqv? 0 (numerator 0))) | |
1084 | (pass-if "1" | |
1085 | (eqv? 1 (numerator 1))) | |
1086 | (pass-if "2" | |
1087 | (eqv? 2 (numerator 2))) | |
1088 | (pass-if "-1" | |
1089 | (eqv? -1 (numerator -1))) | |
1090 | (pass-if "-2" | |
1091 | (eqv? -2 (numerator -2))) | |
1092 | ||
1093 | (pass-if "0.0" | |
1094 | (eqv? 0.0 (numerator 0.0))) | |
1095 | (pass-if "1.0" | |
1096 | (eqv? 1.0 (numerator 1.0))) | |
1097 | (pass-if "2.0" | |
1098 | (eqv? 2.0 (numerator 2.0))) | |
1099 | (pass-if "-1.0" | |
1100 | (eqv? -1.0 (numerator -1.0))) | |
1101 | (pass-if "-2.0" | |
1102 | (eqv? -2.0 (numerator -2.0))) | |
1103 | ||
1104 | (pass-if "0.5" | |
1105 | (eqv? 1.0 (numerator 0.5))) | |
1106 | (pass-if "0.25" | |
1107 | (eqv? 1.0 (numerator 0.25))) | |
1108 | (pass-if "0.75" | |
1109 | (eqv? 3.0 (numerator 0.75)))) | |
1110 | ||
1111 | ;;; | |
1112 | ;;; denominator | |
1113 | ;;; | |
1114 | ||
1115 | (with-test-prefix "denominator" | |
1116 | (pass-if "0" | |
1117 | (eqv? 1 (denominator 0))) | |
1118 | (pass-if "1" | |
1119 | (eqv? 1 (denominator 1))) | |
1120 | (pass-if "2" | |
1121 | (eqv? 1 (denominator 2))) | |
1122 | (pass-if "-1" | |
1123 | (eqv? 1 (denominator -1))) | |
1124 | (pass-if "-2" | |
1125 | (eqv? 1 (denominator -2))) | |
1126 | ||
1127 | (pass-if "0.0" | |
1128 | (eqv? 1.0 (denominator 0.0))) | |
1129 | (pass-if "1.0" | |
1130 | (eqv? 1.0 (denominator 1.0))) | |
1131 | (pass-if "2.0" | |
1132 | (eqv? 1.0 (denominator 2.0))) | |
1133 | (pass-if "-1.0" | |
1134 | (eqv? 1.0 (denominator -1.0))) | |
1135 | (pass-if "-2.0" | |
1136 | (eqv? 1.0 (denominator -2.0))) | |
1137 | ||
1138 | (pass-if "0.5" | |
1139 | (eqv? 2.0 (denominator 0.5))) | |
1140 | (pass-if "0.25" | |
1141 | (eqv? 4.0 (denominator 0.25))) | |
1142 | (pass-if "0.75" | |
1143 | (eqv? 4.0 (denominator 0.75)))) | |
1144 | ||
de142bea DH |
1145 | ;;; |
1146 | ;;; gcd | |
1147 | ;;; | |
1148 | ||
1149 | (with-test-prefix "gcd" | |
1150 | ||
d389e966 | 1151 | (pass-if "documented?" |
cb18f2a8 | 1152 | (documented? gcd)) |
de142bea | 1153 | |
1dd79792 NJ |
1154 | (with-test-prefix "(n)" |
1155 | ||
1156 | (pass-if "n = -2" | |
1157 | (eqv? 2 (gcd -2)))) | |
1158 | ||
de142bea DH |
1159 | (with-test-prefix "(0 n)" |
1160 | ||
21e39e8f DH |
1161 | (pass-if "n = 0" |
1162 | (eqv? 0 (gcd 0 0))) | |
1163 | ||
de142bea DH |
1164 | (pass-if "n = 1" |
1165 | (eqv? 1 (gcd 0 1))) | |
1166 | ||
1167 | (pass-if "n = -1" | |
1168 | (eqv? 1 (gcd 0 -1))) | |
1169 | ||
21e39e8f DH |
1170 | (pass-if "n = fixnum-max" |
1171 | (eqv? fixnum-max (gcd 0 fixnum-max))) | |
de142bea | 1172 | |
21e39e8f DH |
1173 | (pass-if "n = fixnum-max + 1" |
1174 | (eqv? (+ fixnum-max 1) (gcd 0 (+ fixnum-max 1)))) | |
de142bea | 1175 | |
21e39e8f DH |
1176 | (pass-if "n = fixnum-min" |
1177 | (eqv? (- fixnum-min) (gcd 0 fixnum-min))) | |
de142bea | 1178 | |
21e39e8f DH |
1179 | (pass-if "n = fixnum-min - 1" |
1180 | (eqv? (- (- fixnum-min 1)) (gcd 0 (- fixnum-min 1))))) | |
1181 | ||
db386f80 KR |
1182 | (with-test-prefix "(n 0)" |
1183 | ||
1184 | (pass-if "n = 2^128 * fixnum-max" | |
1185 | (eqv? (ash fixnum-max 128) (gcd (ash fixnum-max 128) 0)))) | |
1186 | ||
21e39e8f DH |
1187 | (with-test-prefix "(1 n)" |
1188 | ||
1189 | (pass-if "n = 0" | |
de142bea DH |
1190 | (eqv? 1 (gcd 1 0))) |
1191 | ||
21e39e8f DH |
1192 | (pass-if "n = 1" |
1193 | (eqv? 1 (gcd 1 1))) | |
1194 | ||
de142bea | 1195 | (pass-if "n = -1" |
21e39e8f DH |
1196 | (eqv? 1 (gcd 1 -1))) |
1197 | ||
1198 | (pass-if "n = fixnum-max" | |
1199 | (eqv? 1 (gcd 1 fixnum-max))) | |
1200 | ||
1201 | (pass-if "n = fixnum-max + 1" | |
1202 | (eqv? 1 (gcd 1 (+ fixnum-max 1)))) | |
1203 | ||
1204 | (pass-if "n = fixnum-min" | |
1205 | (eqv? 1 (gcd 1 fixnum-min))) | |
1206 | ||
1207 | (pass-if "n = fixnum-min - 1" | |
1208 | (eqv? 1 (gcd 1 (- fixnum-min 1))))) | |
1209 | ||
1210 | (with-test-prefix "(-1 n)" | |
1211 | ||
1212 | (pass-if "n = 0" | |
de142bea DH |
1213 | (eqv? 1 (gcd -1 0))) |
1214 | ||
21e39e8f DH |
1215 | (pass-if "n = 1" |
1216 | (eqv? 1 (gcd -1 1))) | |
de142bea | 1217 | |
21e39e8f DH |
1218 | (pass-if "n = -1" |
1219 | (eqv? 1 (gcd -1 -1))) | |
de142bea | 1220 | |
21e39e8f DH |
1221 | (pass-if "n = fixnum-max" |
1222 | (eqv? 1 (gcd -1 fixnum-max))) | |
1223 | ||
1224 | (pass-if "n = fixnum-max + 1" | |
1225 | (eqv? 1 (gcd -1 (+ fixnum-max 1)))) | |
1226 | ||
1227 | (pass-if "n = fixnum-min" | |
1228 | (eqv? 1 (gcd -1 fixnum-min))) | |
1229 | ||
1230 | (pass-if "n = fixnum-min - 1" | |
1231 | (eqv? 1 (gcd -1 (- fixnum-min 1))))) | |
1232 | ||
1233 | (with-test-prefix "(fixnum-max n)" | |
1234 | ||
1235 | (pass-if "n = 0" | |
1236 | (eqv? fixnum-max (gcd fixnum-max 0))) | |
de142bea DH |
1237 | |
1238 | (pass-if "n = 1" | |
21e39e8f | 1239 | (eqv? 1 (gcd fixnum-max 1))) |
de142bea DH |
1240 | |
1241 | (pass-if "n = -1" | |
21e39e8f DH |
1242 | (eqv? 1 (gcd fixnum-max -1))) |
1243 | ||
1244 | (pass-if "n = fixnum-max" | |
1245 | (eqv? fixnum-max (gcd fixnum-max fixnum-max))) | |
de142bea | 1246 | |
21e39e8f DH |
1247 | (pass-if "n = fixnum-max + 1" |
1248 | (eqv? 1 (gcd fixnum-max (+ fixnum-max 1)))) | |
de142bea | 1249 | |
21e39e8f DH |
1250 | (pass-if "n = fixnum-min" |
1251 | (eqv? 1 (gcd fixnum-max fixnum-min))) | |
de142bea | 1252 | |
21e39e8f DH |
1253 | (pass-if "n = fixnum-min - 1" |
1254 | (eqv? 1 (gcd fixnum-max (- fixnum-min 1))))) | |
1255 | ||
1256 | (with-test-prefix "((+ fixnum-max 1) n)" | |
1257 | ||
1258 | (pass-if "n = 0" | |
1259 | (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) 0))) | |
1260 | ||
1261 | (pass-if "n = 1" | |
1262 | (eqv? 1 (gcd (+ fixnum-max 1) 1))) | |
de142bea DH |
1263 | |
1264 | (pass-if "n = -1" | |
21e39e8f | 1265 | (eqv? 1 (gcd (+ fixnum-max 1) -1))) |
de142bea | 1266 | |
21e39e8f DH |
1267 | (pass-if "n = fixnum-max" |
1268 | (eqv? 1 (gcd (+ fixnum-max 1) fixnum-max))) | |
de142bea | 1269 | |
21e39e8f DH |
1270 | (pass-if "n = fixnum-max + 1" |
1271 | (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) (+ fixnum-max 1)))) | |
de142bea | 1272 | |
21e39e8f DH |
1273 | (pass-if "n = fixnum-min" |
1274 | (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) fixnum-min))) | |
1275 | ||
1276 | (pass-if "n = fixnum-min - 1" | |
1277 | (eqv? 1 (gcd (+ fixnum-max 1) (- fixnum-min 1))))) | |
1278 | ||
1279 | (with-test-prefix "(fixnum-min n)" | |
1280 | ||
1281 | (pass-if "n = 0" | |
1282 | (eqv? (- fixnum-min) (gcd fixnum-min 0))) | |
1283 | ||
1284 | (pass-if "n = 1" | |
1285 | (eqv? 1 (gcd fixnum-min 1))) | |
de142bea DH |
1286 | |
1287 | (pass-if "n = -1" | |
21e39e8f DH |
1288 | (eqv? 1 (gcd fixnum-min -1))) |
1289 | ||
1290 | (pass-if "n = fixnum-max" | |
1291 | (eqv? 1 (gcd fixnum-min fixnum-max))) | |
1292 | ||
1293 | (pass-if "n = fixnum-max + 1" | |
1294 | (eqv? (+ fixnum-max 1) (gcd fixnum-min (+ fixnum-max 1)))) | |
de142bea | 1295 | |
21e39e8f DH |
1296 | (pass-if "n = fixnum-min" |
1297 | (eqv? (- fixnum-min) (gcd fixnum-min fixnum-min))) | |
1298 | ||
1299 | (pass-if "n = fixnum-min - 1" | |
1300 | (eqv? 1 (gcd fixnum-min (- fixnum-min 1))))) | |
1301 | ||
1302 | (with-test-prefix "((- fixnum-min 1) n)" | |
1303 | ||
1304 | (pass-if "n = 0" | |
1305 | (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) 0))) | |
1306 | ||
1307 | (pass-if "n = 1" | |
1308 | (eqv? 1 (gcd (- fixnum-min 1) 1))) | |
1309 | ||
1310 | (pass-if "n = -1" | |
1311 | (eqv? 1 (gcd (- fixnum-min 1) -1))) | |
1312 | ||
1313 | (pass-if "n = fixnum-max" | |
1314 | (eqv? 1 (gcd (- fixnum-min 1) fixnum-max))) | |
1315 | ||
1316 | (pass-if "n = fixnum-max + 1" | |
1317 | (eqv? 1 (gcd (- fixnum-min 1) (+ fixnum-max 1)))) | |
1318 | ||
1319 | (pass-if "n = fixnum-min" | |
1320 | (eqv? 1 (gcd (- fixnum-min 1) fixnum-min))) | |
1321 | ||
1322 | (pass-if "n = fixnum-min - 1" | |
1323 | (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea | 1324 | |
3bbca1f7 MW |
1325 | (with-test-prefix "flonum arguments" |
1326 | ||
1327 | (pass-if-equal "flonum" | |
1328 | 15.0 | |
1329 | (gcd -15.0)) | |
1330 | ||
1331 | (pass-if-equal "flonum/flonum" | |
1332 | 3.0 | |
1333 | (gcd 6.0 -15.0)) | |
1334 | ||
1335 | (pass-if-equal "flonum/fixnum" | |
1336 | 3.0 | |
1337 | (gcd 6.0 -15)) | |
1338 | ||
1339 | (pass-if-equal "fixnum/flonum" | |
1340 | 3.0 | |
1341 | (gcd -6 15.0)) | |
1342 | ||
1343 | (pass-if-equal "flonum/bignum" | |
1344 | 2.0 | |
1345 | (gcd -6.0 (expt 2 fixnum-bit))) | |
1346 | ||
1347 | (pass-if-equal "bignum/flonum" | |
1348 | 3.0 | |
1349 | (gcd (- (expt 3 fixnum-bit)) 6.0))) | |
1350 | ||
de142bea DH |
1351 | ;; Are wrong type arguments detected correctly? |
1352 | ||
1353 | ) | |
1354 | ||
f29b3454 DH |
1355 | ;;; |
1356 | ;;; lcm | |
1357 | ;;; | |
1358 | ||
7c24e528 RB |
1359 | (with-test-prefix "lcm" |
1360 | ;; FIXME: more tests? | |
1361 | ;; (some of these are already in r4rs.test) | |
d389e966 | 1362 | (pass-if (documented? lcm)) |
3bbca1f7 MW |
1363 | (pass-if-equal 1 (lcm)) |
1364 | (pass-if-equal 15 (lcm -15)) | |
1365 | (pass-if-equal 288 (lcm 32 -36)) | |
1366 | ||
1367 | (with-test-prefix "flonum arguments" | |
1368 | ||
1369 | (pass-if-equal "flonum" | |
1370 | 15.0 | |
1371 | (lcm -15.0)) | |
1372 | ||
1373 | (pass-if-equal "flonum/flonum" | |
1374 | 30.0 | |
1375 | (lcm 6.0 -15.0)) | |
1376 | ||
1377 | (pass-if-equal "flonum/fixnum" | |
1378 | 30.0 | |
1379 | (lcm 6.0 -15)) | |
1380 | ||
1381 | (pass-if-equal "fixnum/flonum" | |
1382 | 30.0 | |
1383 | (lcm -6 15.0)) | |
1384 | ||
1385 | (pass-if "flonum/bignum" | |
1386 | (let ((want (* 3.0 (expt 2 fixnum-bit))) | |
1387 | (got (lcm -6.0 (expt 2 fixnum-bit)))) | |
1388 | (and (inexact? got) | |
1389 | (test-eqv? 1.0 (/ want got))))) | |
1390 | ||
1391 | (pass-if "bignum/flonum" | |
1392 | (let ((want (* 2.0 (expt 3 fixnum-bit))) | |
1393 | (got (lcm (- (expt 3 fixnum-bit)) 6.0))) | |
1394 | (and (inexact? got) | |
1395 | (test-eqv? 1.0 (/ want got)))))) | |
1396 | ||
7c24e528 RB |
1397 | (let ((big-n 115792089237316195423570985008687907853269984665640564039457584007913129639936) ; 2 ^ 256 |
1398 | (lcm-of-big-n-and-11 1273712981610478149659280835095566986385969831322046204434033424087044426039296)) | |
1399 | (pass-if (= lcm-of-big-n-and-11 (lcm big-n 11))) | |
1400 | (pass-if (= lcm-of-big-n-and-11 (lcm 11 big-n 11))))) | |
1401 | ||
605f6980 MW |
1402 | ;;; |
1403 | ;;; rationalize | |
1404 | ;;; | |
1405 | (with-test-prefix "rationalize" | |
1406 | (pass-if (documented? rationalize)) | |
1407 | (pass-if (eqv? 2 (rationalize 4 2 ))) | |
1408 | (pass-if (eqv? -2 (rationalize -4 2 ))) | |
1409 | (pass-if (eqv? 2.0 (rationalize 4 2.0))) | |
1410 | (pass-if (eqv? -2.0 (rationalize -4.0 2 ))) | |
1411 | ||
1412 | (pass-if (eqv? 0 (rationalize 4 8 ))) | |
1413 | (pass-if (eqv? 0 (rationalize -4 8 ))) | |
1414 | (pass-if (eqv? 0.0 (rationalize 4 8.0))) | |
1415 | (pass-if (eqv? 0.0 (rationalize -4.0 8 ))) | |
1416 | ||
1417 | (pass-if (eqv? 0.0 (rationalize 3 +inf.0))) | |
1418 | (pass-if (eqv? 0.0 (rationalize -3 +inf.0))) | |
1419 | ||
55a8b708 MW |
1420 | (pass-if (real-nan? (rationalize +inf.0 +inf.0))) |
1421 | (pass-if (real-nan? (rationalize +nan.0 +inf.0))) | |
1422 | (pass-if (real-nan? (rationalize +nan.0 4))) | |
605f6980 MW |
1423 | (pass-if (eqv? +inf.0 (rationalize +inf.0 3))) |
1424 | ||
1425 | (pass-if (eqv? 3/10 (rationalize 3/10 0))) | |
1426 | (pass-if (eqv? -3/10 (rationalize -3/10 0))) | |
1427 | ||
1428 | (pass-if (eqv? 1/3 (rationalize 3/10 1/10))) | |
1429 | (pass-if (eqv? -1/3 (rationalize -3/10 1/10))) | |
1430 | ||
1431 | (pass-if (eqv? 1/3 (rationalize 3/10 -1/10))) | |
1432 | (pass-if (eqv? -1/3 (rationalize -3/10 -1/10))) | |
1433 | ||
1434 | (pass-if (test-eqv? (/ 1.0 3) (rationalize 0.3 1/10))) | |
1435 | (pass-if (test-eqv? (/ -1.0 3) (rationalize -0.3 1/10))) | |
1436 | (pass-if (test-eqv? (/ 1.0 3) (rationalize 0.3 -1/10))) | |
1437 | (pass-if (test-eqv? (/ -1.0 3) (rationalize -0.3 -1/10)))) | |
1438 | ||
f29b3454 DH |
1439 | ;;; |
1440 | ;;; number->string | |
1441 | ;;; | |
1442 | ||
7c24e528 RB |
1443 | (with-test-prefix "number->string" |
1444 | (let ((num->str->num | |
1445 | (lambda (n radix) | |
1446 | (string->number (number->string n radix) radix)))) | |
1447 | ||
1ea37620 MW |
1448 | (define (test num) |
1449 | (pass-if-equal (list num 'pos) | |
1450 | num | |
1451 | (num->str->num num 10)) | |
1452 | (pass-if-equal (list num 'neg) | |
1453 | (- num) | |
1454 | (num->str->num (- num) 10))) | |
1455 | ||
7c24e528 RB |
1456 | (pass-if (documented? number->string)) |
1457 | (pass-if (string=? (number->string 0) "0")) | |
1458 | (pass-if (string=? (number->string 171) "171")) | |
1459 | (pass-if (= (+ fixnum-max 1) (num->str->num (+ fixnum-max 1) 10))) | |
1460 | (pass-if (= (- fixnum-min 1) (num->str->num (- fixnum-min 1) 10))) | |
d39a7b58 | 1461 | |
1ea37620 MW |
1462 | (test (inf)) |
1463 | (test 1.3) | |
1464 | (test (acos -1)) ; pi | |
1465 | (test (exp 1)) ; e | |
1466 | (test (/ 3.0)) | |
1467 | (test (/ 7.0)) | |
1468 | (test 2.2250738585072011e-308) | |
1469 | (test 2.2250738585072012e-308) | |
1470 | ||
1471 | ;; Largest finite inexact | |
1472 | (test (* (- (expt 2.0 dbl-mant-dig) 1) | |
1473 | (expt 2.0 (- dbl-max-exp dbl-mant-dig)))) | |
1474 | ||
1475 | (pass-if (string=? "0.0" (number->string 0.0))) | |
1476 | (pass-if (or (eqv? 0.0 -0.0) | |
1477 | (string=? "-0.0" (number->string -0.0)))) | |
eb73f94b | 1478 | |
3740c788 | 1479 | (pass-if (string=? (number->string 35.25 36) "z.9")) |
1ea37620 | 1480 | (pass-if (string=? (number->string 0.25 2) "0.01")) |
3740c788 | 1481 | (pass-if (string=? (number->string 255.0625 16) "ff.1")) |
d39a7b58 | 1482 | (pass-if (string=? (number->string (/ 1 3) 3) "1/10")) |
23f2b9a3 | 1483 | |
a6f3af16 | 1484 | (pass-if (string=? (number->string 10) "10")) |
3740c788 | 1485 | (pass-if (string=? (number->string 10 11) "a")) |
a6f3af16 AW |
1486 | (pass-if (string=? (number->string 36 36) "10")) |
1487 | (pass-if (= (num->str->num 36 36) 36)) | |
1488 | (pass-if (= (string->number "z" 36) 35)) | |
1489 | (pass-if (= (string->number "Z" 36) 35)) | |
1490 | (pass-if (not (string->number "Z" 35))) | |
3740c788 | 1491 | (pass-if (string=? (number->string 35 36) "z")) |
a6f3af16 AW |
1492 | (pass-if (= (num->str->num 35 36) 35)) |
1493 | ||
8150dfa1 MW |
1494 | (pass-if (string=? (number->string 12342342340000.0) "1.234234234e13")) |
1495 | (pass-if (string=? (number->string 1234234234000.0) "1234234234000.0")) | |
1496 | (pass-if (string=? (number->string 1240000.0) "1240000.0")) | |
1497 | ||
1ea37620 MW |
1498 | (with-test-prefix "powers of radix" |
1499 | (for-each | |
1500 | (lambda (radix) | |
1501 | (for-each (lambda (k) | |
1502 | (let ((val (exact->inexact (expt radix k))) | |
1503 | (str (if (<= -3 k 6) | |
1504 | (assoc-ref '((-3 . "0.001") | |
1505 | (-2 . "0.01") | |
1506 | (-1 . "0.1") | |
1507 | ( 0 . "1.0") | |
1508 | ( 1 . "10.0") | |
1509 | ( 2 . "100.0") | |
1510 | ( 3 . "1000.0") | |
1511 | ( 4 . "10000.0") | |
1512 | ( 5 . "100000.0") | |
1513 | ( 6 . "1000000.0")) | |
1514 | k) | |
1515 | (string-append "1.0e" | |
1516 | (number->string k radix))))) | |
1517 | (pass-if-equal (list radix k 'pos) | |
1518 | str | |
1519 | (number->string val radix)) | |
1520 | (pass-if-equal (list radix k 'neg) | |
1521 | (string-append "-" str) | |
1522 | (number->string (- val) radix)))) | |
1523 | (iota 41 -20))) | |
1524 | (iota 35 2))) | |
1525 | ||
1526 | (with-test-prefix "multiples of smallest inexact" | |
1527 | (for-each (lambda (k) | |
1528 | (let ((val (* k (expt 2.0 (- dbl-min-exp dbl-mant-dig))))) | |
1529 | (test val))) | |
1530 | (iota 40 1))) | |
1531 | ||
1532 | (with-test-prefix "one plus multiples of epsilon" | |
1533 | (for-each (lambda (k) | |
1534 | (let ((val (+ 1.0 (* k dbl-epsilon)))) | |
1535 | (test val))) | |
1536 | (iota 40 1))) | |
1537 | ||
1538 | (with-test-prefix "one minus multiples of 1/2 epsilon" | |
1539 | (for-each (lambda (k) | |
1540 | (let ((val (- 1.0 (* k 1/2 dbl-epsilon)))) | |
1541 | (test val))) | |
1542 | (iota 40 1))) | |
1543 | ||
6ebecdeb MW |
1544 | ;; Before Guile 2.0.1, even in the presence of a #e forced exactness |
1545 | ;; specifier, negative exponents were applied inexactly and then | |
1546 | ;; later coerced to exact, yielding an incorrect fraction. | |
1547 | (pass-if (eqv? (string->number "#e1e-10") 1/10000000000)) | |
1548 | ||
1ea37620 MW |
1549 | (pass-if (string=? (number->string 11.33333333333333333 12) |
1550 | "b.4")) | |
1551 | (pass-if (string=? (number->string 1.324e44 16) | |
1552 | "5.efe0a14fafdf8e24")))) | |
7c24e528 | 1553 | |
f29b3454 DH |
1554 | ;;; |
1555 | ;;; string->number | |
1556 | ;;; | |
1557 | ||
2f4a254a DH |
1558 | (with-test-prefix "string->number" |
1559 | ||
ff758237 | 1560 | (pass-if "documented?" |
2f4a254a DH |
1561 | (documented? string->number)) |
1562 | ||
1563 | (pass-if "non number strings" | |
1564 | (for-each (lambda (x) (if (string->number x) (throw 'fail))) | |
569c483b | 1565 | '("" "q" "1q" "6+7iq" "8+9q" "10+11" "13+" "18@19q" "20@q" "23@" |
2f4a254a | 1566 | "+25iq" "26i" "-q" "-iq" "i" "5#.0" "8/" "10#11" ".#" "." |
569c483b | 1567 | "#o.2" "3.4q" "15.16e17q" "18.19e+q" ".q" ".17#18" "10q" "#b2" |
2f4a254a DH |
1568 | "#b3" "#b4" "#b5" "#b6" "#b7" "#b8" "#b9" "#ba" "#bb" "#bc" |
1569 | "#bd" "#be" "#bf" "#q" "#b#b1" "#o#o1" "#d#d1" "#x#x1" "#e#e1" | |
929d11b2 MW |
1570 | "#i#i1" "12@12+0i" "3/0" "0/0" "4+3/0i" "4/0-3i" "2+0/0i" |
1571 | "nan.0" "inf.0" "#e+nan.0" "#e+inf.0" "#e-inf.0" | |
1572 | "3@inf.0" "4@nan.0")) | |
2f4a254a DH |
1573 | #t) |
1574 | ||
b7d9b1cf DH |
1575 | (pass-if "valid number strings" |
1576 | (for-each (lambda (couple) | |
1577 | (apply | |
1578 | (lambda (x y) | |
9dd9857f MV |
1579 | (let ((xx (string->number x))) |
1580 | (if (or (eq? xx #f) (not (eqv? xx y))) | |
ca2b31fe MV |
1581 | (begin |
1582 | (pk x y) | |
1583 | (throw 'fail))))) | |
b7d9b1cf DH |
1584 | couple)) |
1585 | `(;; Radix: | |
1586 | ("#b0" 0) ("#B0" 0) ("#b1" 1) ("#B1" 1) ("#o0" 0) ("#O0" 0) | |
1587 | ("#o1" 1) ("#O1" 1) ("#o2" 2) ("#O2" 2) ("#o3" 3) ("#O3" 3) | |
1588 | ("#o4" 4) ("#O4" 4) ("#o5" 5) ("#O5" 5) ("#o6" 6) ("#O6" 6) | |
1589 | ("#o7" 7) ("#O7" 7) ("#d0" 0) ("#D0" 0) ("#d1" 1) ("#D1" 1) | |
1590 | ("#d2" 2) ("#D2" 2) ("#d3" 3) ("#D3" 3) ("#d4" 4) ("#D4" 4) | |
1591 | ("#d5" 5) ("#D5" 5) ("#d6" 6) ("#D6" 6) ("#d7" 7) ("#D7" 7) | |
1592 | ("#d8" 8) ("#D8" 8) ("#d9" 9) ("#D9" 9) | |
1593 | ("#xa" 10) ("#Xa" 10) ("#xb" 11) ("#Xb" 11) | |
1594 | ("#xc" 12) ("#Xc" 12) ("#xd" 13) ("#Xd" 13) | |
1595 | ("#xe" 14) ("#Xe" 14) ("#xf" 15) ("#Xf" 15) | |
1596 | ("#b1010" 10) | |
1597 | ("#o12345670" 2739128) | |
1598 | ("#d1234567890" 1234567890) | |
1599 | ("#x1234567890abcdef" 1311768467294899695) | |
1600 | ;; Exactness: | |
ca2b31fe | 1601 | ("#e1" 1) ("#e1.2" 12/10) |
9dd9857f | 1602 | ("#i1.1" 1.1) ("#i1" 1.0) |
b7d9b1cf DH |
1603 | ;; Integers: |
1604 | ("1" ,(1+ 0)) ("23" ,(+ 9 9 5)) ("-1" ,(- 0 1)) | |
1605 | ("-45" ,(- 0 45)) ("2#" 20.0) ("2##" 200.0) ("12##" 1200.0) | |
1606 | ("#b#i100" 4.0) | |
9dd9857f MV |
1607 | ;; Fractions: |
1608 | ("1/1" 1) ("1/2" 1/2) ("-1/2" -1/2) ("1#/1" 10.0) | |
1609 | ("10/1#" 1.0) ("1#/1#" 1.0) ("#e9/10" 9/10) ("#e10/1#" 1) | |
b7d9b1cf | 1610 | ("#i6/8" 0.75) ("#i1/1" 1.0) |
929d11b2 MW |
1611 | ;; Infinities and NaNs: |
1612 | ("+inf.0" ,(inf)) ("-inf.0" ,(- (inf))) | |
1613 | ("+Inf.0" ,(inf)) ("-Inf.0" ,(- (inf))) | |
1614 | ("+InF.0" ,(inf)) ("-InF.0" ,(- (inf))) | |
1615 | ("+INF.0" ,(inf)) ("-INF.0" ,(- (inf))) | |
1616 | ("#i+InF.0" ,(inf)) ("#i-InF.0" ,(- (inf))) | |
1617 | ("+nan.0" ,(nan)) ("-nan.0" ,(nan)) | |
1618 | ("#i+nan.0" ,(nan)) ("#i-nan.0" ,(nan)) | |
b7d9b1cf DH |
1619 | ;; Decimal numbers: |
1620 | ;; * <uinteger 10> <suffix> | |
1621 | ("1e2" 100.0) ("1E2" 100.0) ("1s2" 100.0) ("1S2" 100.0) | |
1622 | ("1f2" 100.0) ("1F2" 100.0) ("1d2" 100.0) ("1D2" 100.0) | |
1623 | ("1l2" 100.0) ("1L2" 100.0) ("1e+2" 100.0) ("1e-2" 0.01) | |
1624 | ;; * . <digit 10>+ #* <suffix> | |
1625 | (".1" .1) (".0123456789" 123456789e-10) (".16#" 0.16) | |
1626 | (".0123456789e10" 123456789.0) (".16#e3" 160.0) ("#d.3" 0.3) | |
1627 | ;; * <digit 10>+ . <digit 10>* #* <suffix> | |
1628 | ("3." ,(exact->inexact 3)) ("3.e0" ,(exact->inexact 3)) | |
1629 | ("3.1" ,(exact->inexact 31/10)) ("3.1e0" 3.1) ("3.1#" 3.1) | |
1630 | ("3.1#e0" 3.1) | |
1631 | ;; * <digit 10>+ #+ . #* <suffix> | |
1632 | ("3#." 30.0) ("3#.e0" 30.0) ("3#.#" 30.0) ("3#.#e0" 30.0) | |
9d427b2c MW |
1633 | )) |
1634 | #t) | |
1635 | ||
1636 | (pass-if "valid complex number strings" | |
1637 | (for-each (lambda (triple) | |
1638 | (apply | |
1639 | (lambda (str re im) | |
1640 | (let ((z (string->number str))) | |
1641 | (if (or (eq? z #f) | |
1642 | (not (and (eqv? (real-part z) re) | |
1643 | (eqv? (imag-part z) im)))) | |
1644 | (begin | |
1645 | (pk str re im) | |
1646 | (throw 'fail))))) | |
1647 | triple)) | |
1648 | `(("1@0" 1 0) ("1@+0" 1 0) ("1@-0" 1 0) ("1/2@0" 1/2 0) | |
1649 | ("1.0@0" 1.0 0) ("1.0@-0" 1.0 0) | |
1650 | ("#e1@0" 1 0) ("#e1@+0" 1 0) ("#e1@-0" 1 0) ("#e0.5@0.0" 1/2 0) | |
1651 | ("#e1.0@0" 1 0) ("#e1.0@-0" 1 0) | |
1652 | ("#i1@0" 1.0 0.0) ("#i1@+0" 1.0 0.0) ("#i1@-0" 1.0 -0.0) ("#i1/2@0" 0.5 0.0) | |
1653 | ("#i1.0@0" 1.0 0.0) ("#i1.0@-0" 1.0 -0.0) | |
1654 | ("1@+0.0" 1.0 0.0) ("1.0@-0.0" 1.0 -0.0) | |
1655 | ("2+3i" 2.0 3.0) ("4-5i" 4.0 -5.0) | |
1656 | ("1+i" 1.0 1.0) ("1-i" 1.0 -1.0) ("+1i" 0.0 1.0) ("-1i" 0.0 -1.0) | |
1657 | ("+i" 0.0 1.0) ("-i" 0.0 -1.0) | |
1658 | ("1.0+.1i" 1.0 0.1) ("1.0-.1i" 1.0 -0.1) | |
1659 | (".1+.0i" 0.1 0.0) ("1.+.0i" 1.0 0.0) (".1+.1i" 0.1 0.1) | |
1660 | ("1e1+.1i" 10.0 0.1) | |
1661 | ("0@+nan.0" 0 0) ("0@+inf.0" 0 0) ("0@-inf.0" 0 0) | |
1662 | ("0.0@+nan.0" 0.0 0.0) ("0.0@+inf.0" 0.0 0.0) ("0.0@-inf.0" 0.0 0.0) | |
1663 | ("#i0@+nan.0" 0.0 0.0) ("#i0@+inf.0" 0.0 0.0) ("#i0@-inf.0" 0.0 0.0) | |
1664 | ("0.0@1" 0.0 0.0) ("0.0@2" -0.0 0.0) ("0.0@4" -0.0 -0.0) ("0.0@5" 0.0 -0.0) | |
40f89215 | 1665 | )) |
b7d9b1cf DH |
1666 | #t) |
1667 | ||
2f4a254a DH |
1668 | (pass-if-exception "exponent too big" |
1669 | exception:out-of-range | |
48e78ba6 KR |
1670 | (string->number "12.13e141414")) |
1671 | ||
1672 | ;; in guile 1.6.7 and earlier, bad polar forms (where the conversion of | |
1673 | ;; the angle gave #f) caused a segv | |
1674 | (pass-if "1@a" | |
1675 | (eq? #f (string->number "1@a")))) | |
2f4a254a | 1676 | |
f29b3454 DH |
1677 | ;;; |
1678 | ;;; number? | |
1679 | ;;; | |
1680 | ||
7c24e528 RB |
1681 | (with-test-prefix "number?" |
1682 | (pass-if (documented? number?)) | |
1683 | (pass-if (number? 0)) | |
1684 | (pass-if (number? 7)) | |
1685 | (pass-if (number? -7)) | |
1686 | (pass-if (number? 1.3)) | |
1687 | (pass-if (number? (+ 1 fixnum-max))) | |
1688 | (pass-if (number? (- 1 fixnum-min))) | |
1689 | (pass-if (number? 3+4i)) | |
4d332f19 DH |
1690 | (pass-if (not (number? #\a))) |
1691 | (pass-if (not (number? "a"))) | |
1692 | (pass-if (not (number? (make-vector 0)))) | |
1693 | (pass-if (not (number? (cons 1 2)))) | |
1694 | (pass-if (not (number? #t))) | |
1695 | (pass-if (not (number? (lambda () #t)))) | |
1696 | (pass-if (not (number? (current-input-port))))) | |
7c24e528 | 1697 | |
f29b3454 DH |
1698 | ;;; |
1699 | ;;; complex? | |
1700 | ;;; | |
1701 | ||
7c24e528 RB |
1702 | (with-test-prefix "complex?" |
1703 | (pass-if (documented? complex?)) | |
1704 | (pass-if (complex? 0)) | |
1705 | (pass-if (complex? 7)) | |
1706 | (pass-if (complex? -7)) | |
1707 | (pass-if (complex? (+ 1 fixnum-max))) | |
1708 | (pass-if (complex? (- 1 fixnum-min))) | |
1709 | (pass-if (complex? 1.3)) | |
1710 | (pass-if (complex? 3+4i)) | |
4d332f19 DH |
1711 | (pass-if (not (complex? #\a))) |
1712 | (pass-if (not (complex? "a"))) | |
1713 | (pass-if (not (complex? (make-vector 0)))) | |
1714 | (pass-if (not (complex? (cons 1 2)))) | |
1715 | (pass-if (not (complex? #t))) | |
1716 | (pass-if (not (complex? (lambda () #t)))) | |
1717 | (pass-if (not (complex? (current-input-port))))) | |
7c24e528 | 1718 | |
f29b3454 DH |
1719 | ;;; |
1720 | ;;; real? | |
1721 | ;;; | |
1722 | ||
7c24e528 RB |
1723 | (with-test-prefix "real?" |
1724 | (pass-if (documented? real?)) | |
1725 | (pass-if (real? 0)) | |
1726 | (pass-if (real? 7)) | |
1727 | (pass-if (real? -7)) | |
1728 | (pass-if (real? (+ 1 fixnum-max))) | |
1729 | (pass-if (real? (- 1 fixnum-min))) | |
1730 | (pass-if (real? 1.3)) | |
c960e556 MW |
1731 | (pass-if (real? +inf.0)) |
1732 | (pass-if (real? -inf.0)) | |
1733 | (pass-if (real? +nan.0)) | |
1734 | (pass-if (not (real? +inf.0-inf.0i))) | |
1735 | (pass-if (not (real? +nan.0+nan.0i))) | |
4d332f19 DH |
1736 | (pass-if (not (real? 3+4i))) |
1737 | (pass-if (not (real? #\a))) | |
1738 | (pass-if (not (real? "a"))) | |
1739 | (pass-if (not (real? (make-vector 0)))) | |
1740 | (pass-if (not (real? (cons 1 2)))) | |
1741 | (pass-if (not (real? #t))) | |
1742 | (pass-if (not (real? (lambda () #t)))) | |
1743 | (pass-if (not (real? (current-input-port))))) | |
7c24e528 | 1744 | |
f29b3454 | 1745 | ;;; |
c960e556 | 1746 | ;;; rational? |
f29b3454 DH |
1747 | ;;; |
1748 | ||
7c24e528 RB |
1749 | (with-test-prefix "rational?" |
1750 | (pass-if (documented? rational?)) | |
1751 | (pass-if (rational? 0)) | |
1752 | (pass-if (rational? 7)) | |
1753 | (pass-if (rational? -7)) | |
1754 | (pass-if (rational? (+ 1 fixnum-max))) | |
1755 | (pass-if (rational? (- 1 fixnum-min))) | |
1756 | (pass-if (rational? 1.3)) | |
c960e556 MW |
1757 | (pass-if (not (rational? +inf.0))) |
1758 | (pass-if (not (rational? -inf.0))) | |
1759 | (pass-if (not (rational? +nan.0))) | |
1760 | (pass-if (not (rational? +inf.0-inf.0i))) | |
1761 | (pass-if (not (rational? +nan.0+nan.0i))) | |
4d332f19 DH |
1762 | (pass-if (not (rational? 3+4i))) |
1763 | (pass-if (not (rational? #\a))) | |
1764 | (pass-if (not (rational? "a"))) | |
1765 | (pass-if (not (rational? (make-vector 0)))) | |
1766 | (pass-if (not (rational? (cons 1 2)))) | |
1767 | (pass-if (not (rational? #t))) | |
1768 | (pass-if (not (rational? (lambda () #t)))) | |
1769 | (pass-if (not (rational? (current-input-port))))) | |
7c24e528 | 1770 | |
f29b3454 DH |
1771 | ;;; |
1772 | ;;; integer? | |
1773 | ;;; | |
1774 | ||
7c24e528 RB |
1775 | (with-test-prefix "integer?" |
1776 | (pass-if (documented? integer?)) | |
1777 | (pass-if (integer? 0)) | |
1778 | (pass-if (integer? 7)) | |
1779 | (pass-if (integer? -7)) | |
1780 | (pass-if (integer? (+ 1 fixnum-max))) | |
1781 | (pass-if (integer? (- 1 fixnum-min))) | |
1782 | (pass-if (and (= 3+0i (round 3+0i)) (integer? 3+0i))) | |
1783 | (pass-if (and (= 1.0 (round 1.0)) (integer? 1.0))) | |
4d332f19 | 1784 | (pass-if (not (integer? 1.3))) |
8e43ed5d AW |
1785 | (pass-if (not (integer? +inf.0))) |
1786 | (pass-if (not (integer? -inf.0))) | |
c1122753 | 1787 | (pass-if (not (integer? +nan.0))) |
c7218482 MW |
1788 | (pass-if (not (integer? +inf.0-inf.0i))) |
1789 | (pass-if (not (integer? +nan.0+nan.0i))) | |
4d332f19 DH |
1790 | (pass-if (not (integer? 3+4i))) |
1791 | (pass-if (not (integer? #\a))) | |
1792 | (pass-if (not (integer? "a"))) | |
1793 | (pass-if (not (integer? (make-vector 0)))) | |
1794 | (pass-if (not (integer? (cons 1 2)))) | |
1795 | (pass-if (not (integer? #t))) | |
1796 | (pass-if (not (integer? (lambda () #t)))) | |
1797 | (pass-if (not (integer? (current-input-port))))) | |
7c24e528 | 1798 | |
f29b3454 DH |
1799 | ;;; |
1800 | ;;; inexact? | |
1801 | ;;; | |
1802 | ||
7c24e528 RB |
1803 | (with-test-prefix "inexact?" |
1804 | (pass-if (documented? inexact?)) | |
4d332f19 DH |
1805 | (pass-if (not (inexact? 0))) |
1806 | (pass-if (not (inexact? 7))) | |
1807 | (pass-if (not (inexact? -7))) | |
1808 | (pass-if (not (inexact? (+ 1 fixnum-max)))) | |
1809 | (pass-if (not (inexact? (- 1 fixnum-min)))) | |
7c24e528 RB |
1810 | (pass-if (inexact? 1.3)) |
1811 | (pass-if (inexact? 3.1+4.2i)) | |
41df63cf MW |
1812 | (pass-if (inexact? +inf.0)) |
1813 | (pass-if (inexact? -inf.0)) | |
1814 | (pass-if (inexact? +nan.0)) | |
ca2b31fe MV |
1815 | (pass-if-exception "char" |
1816 | exception:wrong-type-arg | |
1817 | (not (inexact? #\a))) | |
1818 | (pass-if-exception "string" | |
1819 | exception:wrong-type-arg | |
1820 | (not (inexact? "a"))) | |
1821 | (pass-if-exception "vector" | |
1822 | exception:wrong-type-arg | |
1823 | (not (inexact? (make-vector 0)))) | |
1824 | (pass-if-exception "cons" | |
1825 | exception:wrong-type-arg | |
1826 | (not (inexact? (cons 1 2)))) | |
1827 | (pass-if-exception "bool" | |
1828 | exception:wrong-type-arg | |
1829 | (not (inexact? #t))) | |
1830 | (pass-if-exception "procedure" | |
1831 | exception:wrong-type-arg | |
1832 | (not (inexact? (lambda () #t)))) | |
1833 | (pass-if-exception "port" | |
1834 | exception:wrong-type-arg | |
1835 | (not (inexact? (current-input-port))))) | |
7c24e528 | 1836 | |
47ae1f0e DH |
1837 | ;;; |
1838 | ;;; equal? | |
1839 | ;;; | |
1840 | ||
1841 | (with-test-prefix "equal?" | |
1842 | (pass-if (documented? equal?)) | |
2e6e1933 MW |
1843 | |
1844 | ;; The following test will fail on platforms | |
1845 | ;; without distinct signed zeroes 0.0 and -0.0. | |
1846 | (pass-if (not (equal? 0.0 -0.0))) | |
1847 | ||
47ae1f0e DH |
1848 | (pass-if (equal? 0 0)) |
1849 | (pass-if (equal? 7 7)) | |
1850 | (pass-if (equal? -7 -7)) | |
1851 | (pass-if (equal? (+ 1 fixnum-max) (+ 1 fixnum-max))) | |
1852 | (pass-if (equal? (- fixnum-min 1) (- fixnum-min 1))) | |
2e6e1933 MW |
1853 | (pass-if (equal? 0.0 0.0)) |
1854 | (pass-if (equal? -0.0 -0.0)) | |
c7218482 MW |
1855 | (pass-if (equal? 0.0+0.0i 0.0+0.0i)) |
1856 | (pass-if (equal? 0.0-0.0i 0.0-0.0i)) | |
1857 | (pass-if (equal? -0.0+0.0i -0.0+0.0i)) | |
47ae1f0e | 1858 | (pass-if (not (equal? 0 1))) |
2e6e1933 MW |
1859 | (pass-if (not (equal? 0 0.0))) |
1860 | (pass-if (not (equal? 1 1.0))) | |
1861 | (pass-if (not (equal? 0.0 0))) | |
1862 | (pass-if (not (equal? 1.0 1))) | |
1863 | (pass-if (not (equal? -1.0 -1))) | |
c7218482 MW |
1864 | (pass-if (not (equal? 1.0 1.0+0.0i))) |
1865 | (pass-if (not (equal? 0.0 0.0+0.0i))) | |
1866 | (pass-if (not (equal? 0.0+0.0i 0.0-0.0i))) | |
1867 | (pass-if (not (equal? 0.0+0.0i -0.0+0.0i))) | |
47ae1f0e DH |
1868 | (pass-if (not (equal? fixnum-max (+ 1 fixnum-max)))) |
1869 | (pass-if (not (equal? (+ 1 fixnum-max) fixnum-max))) | |
1870 | (pass-if (not (equal? (+ 1 fixnum-max) (+ 2 fixnum-max)))) | |
1871 | (pass-if (not (equal? fixnum-min (- fixnum-min 1)))) | |
1872 | (pass-if (not (equal? (- fixnum-min 1) fixnum-min))) | |
1873 | (pass-if (not (equal? (- fixnum-min 1) (- fixnum-min 2)))) | |
1874 | (pass-if (not (equal? (+ fixnum-max 1) (- fixnum-min 1)))) | |
1875 | ||
1876 | (pass-if (not (equal? (ash 1 256) +inf.0))) | |
1877 | (pass-if (not (equal? +inf.0 (ash 1 256)))) | |
1878 | (pass-if (not (equal? (ash 1 256) -inf.0))) | |
1879 | (pass-if (not (equal? -inf.0 (ash 1 256)))) | |
1880 | ||
1881 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
1882 | ;; sure we've avoided that | |
1883 | (pass-if (not (equal? (ash 1 1024) +inf.0))) | |
1884 | (pass-if (not (equal? +inf.0 (ash 1 1024)))) | |
1885 | (pass-if (not (equal? (- (ash 1 1024)) -inf.0))) | |
1886 | (pass-if (not (equal? -inf.0 (- (ash 1 1024))))) | |
1887 | ||
2e6e1933 MW |
1888 | (pass-if (equal? +nan.0 +nan.0)) |
1889 | (pass-if (equal? +nan.0 +nan.0)) | |
1890 | (pass-if (not (equal? +nan.0 0.0+nan.0i))) | |
1891 | ||
47ae1f0e DH |
1892 | (pass-if (not (equal? 0 +nan.0))) |
1893 | (pass-if (not (equal? +nan.0 0))) | |
1894 | (pass-if (not (equal? 1 +nan.0))) | |
1895 | (pass-if (not (equal? +nan.0 1))) | |
1896 | (pass-if (not (equal? -1 +nan.0))) | |
1897 | (pass-if (not (equal? +nan.0 -1))) | |
1898 | ||
1899 | (pass-if (not (equal? (ash 1 256) +nan.0))) | |
1900 | (pass-if (not (equal? +nan.0 (ash 1 256)))) | |
1901 | (pass-if (not (equal? (- (ash 1 256)) +nan.0))) | |
1902 | (pass-if (not (equal? +nan.0 (- (ash 1 256))))) | |
1903 | ||
1904 | (pass-if (not (equal? (ash 1 8192) +nan.0))) | |
1905 | (pass-if (not (equal? +nan.0 (ash 1 8192)))) | |
1906 | (pass-if (not (equal? (- (ash 1 8192)) +nan.0))) | |
1907 | (pass-if (not (equal? +nan.0 (- (ash 1 8192))))) | |
1908 | ||
1909 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
1910 | ;; sure we've avoided that | |
1911 | (pass-if (not (equal? (ash 3 1023) +nan.0))) | |
1912 | (pass-if (not (equal? +nan.0 (ash 3 1023))))) | |
1913 | ||
2e6e1933 MW |
1914 | ;;; |
1915 | ;;; eqv? | |
1916 | ;;; | |
1917 | ||
1918 | (with-test-prefix "eqv?" | |
1919 | (pass-if (documented? eqv?)) | |
1920 | ||
1921 | ;; The following test will fail on platforms | |
1922 | ;; without distinct signed zeroes 0.0 and -0.0. | |
1923 | (pass-if (not (eqv? 0.0 -0.0))) | |
1924 | ||
1925 | (pass-if (eqv? 0 0)) | |
1926 | (pass-if (eqv? 7 7)) | |
1927 | (pass-if (eqv? -7 -7)) | |
1928 | (pass-if (eqv? (+ 1 fixnum-max) (+ 1 fixnum-max))) | |
1929 | (pass-if (eqv? (- fixnum-min 1) (- fixnum-min 1))) | |
1930 | (pass-if (eqv? 0.0 0.0)) | |
1931 | (pass-if (eqv? -0.0 -0.0)) | |
c7218482 MW |
1932 | (pass-if (eqv? 0.0+0.0i 0.0+0.0i)) |
1933 | (pass-if (eqv? 0.0-0.0i 0.0-0.0i)) | |
1934 | (pass-if (eqv? -0.0+0.0i -0.0+0.0i)) | |
1935 | (pass-if (not (eqv? 0.0 -0.0))) | |
1936 | (pass-if (not (eqv? 0.0 0.0+0.0i))) | |
1937 | (pass-if (not (eqv? 0.0+0.0i 0.0-0.0i))) | |
1938 | (pass-if (not (eqv? 0.0+0.0i -0.0+0.0i))) | |
2e6e1933 MW |
1939 | (pass-if (not (eqv? 0 1))) |
1940 | (pass-if (not (eqv? 0 0.0))) | |
1941 | (pass-if (not (eqv? 1 1.0))) | |
1942 | (pass-if (not (eqv? 0.0 0))) | |
1943 | (pass-if (not (eqv? 1.0 1))) | |
1944 | (pass-if (not (eqv? -1.0 -1))) | |
c7218482 MW |
1945 | (pass-if (not (eqv? 1.0 1.0+0.0i))) |
1946 | (pass-if (not (eqv? 0.0 0.0+0.0i))) | |
2e6e1933 MW |
1947 | (pass-if (not (eqv? fixnum-max (+ 1 fixnum-max)))) |
1948 | (pass-if (not (eqv? (+ 1 fixnum-max) fixnum-max))) | |
1949 | (pass-if (not (eqv? (+ 1 fixnum-max) (+ 2 fixnum-max)))) | |
1950 | (pass-if (not (eqv? fixnum-min (- fixnum-min 1)))) | |
1951 | (pass-if (not (eqv? (- fixnum-min 1) fixnum-min))) | |
1952 | (pass-if (not (eqv? (- fixnum-min 1) (- fixnum-min 2)))) | |
1953 | (pass-if (not (eqv? (+ fixnum-max 1) (- fixnum-min 1)))) | |
1954 | ||
1955 | (pass-if (not (eqv? (ash 1 256) +inf.0))) | |
1956 | (pass-if (not (eqv? +inf.0 (ash 1 256)))) | |
1957 | (pass-if (not (eqv? (ash 1 256) -inf.0))) | |
1958 | (pass-if (not (eqv? -inf.0 (ash 1 256)))) | |
1959 | ||
1960 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
1961 | ;; sure we've avoided that | |
1962 | (pass-if (not (eqv? (ash 1 1024) +inf.0))) | |
1963 | (pass-if (not (eqv? +inf.0 (ash 1 1024)))) | |
1964 | (pass-if (not (eqv? (- (ash 1 1024)) -inf.0))) | |
1965 | (pass-if (not (eqv? -inf.0 (- (ash 1 1024))))) | |
1966 | ||
1967 | (pass-if (eqv? +nan.0 +nan.0)) | |
1968 | (pass-if (not (eqv? +nan.0 0.0+nan.0i))) | |
1969 | ||
1970 | (pass-if (not (eqv? 0 +nan.0))) | |
1971 | (pass-if (not (eqv? +nan.0 0))) | |
1972 | (pass-if (not (eqv? 1 +nan.0))) | |
1973 | (pass-if (not (eqv? +nan.0 1))) | |
1974 | (pass-if (not (eqv? -1 +nan.0))) | |
1975 | (pass-if (not (eqv? +nan.0 -1))) | |
1976 | ||
1977 | (pass-if (not (eqv? (ash 1 256) +nan.0))) | |
1978 | (pass-if (not (eqv? +nan.0 (ash 1 256)))) | |
1979 | (pass-if (not (eqv? (- (ash 1 256)) +nan.0))) | |
1980 | (pass-if (not (eqv? +nan.0 (- (ash 1 256))))) | |
1981 | ||
1982 | (pass-if (not (eqv? (ash 1 8192) +nan.0))) | |
1983 | (pass-if (not (eqv? +nan.0 (ash 1 8192)))) | |
1984 | (pass-if (not (eqv? (- (ash 1 8192)) +nan.0))) | |
1985 | (pass-if (not (eqv? +nan.0 (- (ash 1 8192))))) | |
1986 | ||
1987 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
1988 | ;; sure we've avoided that | |
1989 | (pass-if (not (eqv? (ash 3 1023) +nan.0))) | |
1990 | (pass-if (not (eqv? +nan.0 (ash 3 1023))))) | |
1991 | ||
f29b3454 DH |
1992 | ;;; |
1993 | ;;; = | |
1994 | ;;; | |
1995 | ||
7c24e528 | 1996 | (with-test-prefix "=" |
8a1f4f98 | 1997 | (pass-if (documented? =)) |
7c24e528 RB |
1998 | (pass-if (= 7 7)) |
1999 | (pass-if (= -7 -7)) | |
c7218482 MW |
2000 | (pass-if (= 1.0 1)) |
2001 | (pass-if (= 1 1.0)) | |
2002 | (pass-if (= -1 -1.0)) | |
2003 | (pass-if (= 0.0 0.0)) | |
2004 | (pass-if (= 0.0 -0.0)) | |
2005 | (pass-if (= 1 1.0+0.0i)) | |
2006 | ||
2007 | (pass-if (= 0 0)) | |
2008 | (pass-if (= 0 0.0)) | |
2009 | (pass-if (= 0 -0.0)) | |
2010 | (pass-if (= 0 0.0+0.0i)) | |
2011 | (pass-if (= 0 0.0-0.0i)) | |
2012 | (pass-if (= 0 0.0+0.0i)) | |
2013 | (pass-if (= 0 -0.0-0.0i)) | |
2014 | ||
2015 | (pass-if (= 0 0)) | |
2016 | (pass-if (= 0.0 0)) | |
2017 | (pass-if (= -0.0 0)) | |
2018 | (pass-if (= 0.0+0.0i 0)) | |
2019 | (pass-if (= 0.0-0.0i 0)) | |
2020 | (pass-if (= 0.0+0.0i 0)) | |
2021 | (pass-if (= -0.0-0.0i 0)) | |
2022 | ||
2023 | (pass-if (= 0.0+0.0i 0.0-0.0i)) | |
2024 | (pass-if (= 0.0+0.0i -0.0+0.0i)) | |
2025 | ||
7c24e528 | 2026 | (pass-if (= (+ 1 fixnum-max) (+ 1 fixnum-max))) |
47ae1f0e | 2027 | (pass-if (= (- fixnum-min 1) (- fixnum-min 1))) |
4d332f19 DH |
2028 | (pass-if (not (= 0 1))) |
2029 | (pass-if (not (= fixnum-max (+ 1 fixnum-max)))) | |
2030 | (pass-if (not (= (+ 1 fixnum-max) fixnum-max))) | |
47ae1f0e | 2031 | (pass-if (not (= (+ 1 fixnum-max) (+ 2 fixnum-max)))) |
4d332f19 DH |
2032 | (pass-if (not (= fixnum-min (- fixnum-min 1)))) |
2033 | (pass-if (not (= (- fixnum-min 1) fixnum-min))) | |
47ae1f0e | 2034 | (pass-if (not (= (- fixnum-min 1) (- fixnum-min 2)))) |
4d332f19 | 2035 | (pass-if (not (= (+ fixnum-max 1) (- fixnum-min 1)))) |
2cfcaed5 | 2036 | |
adda36ed KR |
2037 | (pass-if (not (= (ash 1 256) +inf.0))) |
2038 | (pass-if (not (= +inf.0 (ash 1 256)))) | |
2039 | (pass-if (not (= (ash 1 256) -inf.0))) | |
2040 | (pass-if (not (= -inf.0 (ash 1 256)))) | |
2041 | ||
2042 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
2043 | ;; sure we've avoided that | |
2044 | (pass-if (not (= (ash 1 1024) +inf.0))) | |
2045 | (pass-if (not (= +inf.0 (ash 1 1024)))) | |
2046 | (pass-if (not (= (- (ash 1 1024)) -inf.0))) | |
2047 | (pass-if (not (= -inf.0 (- (ash 1 1024))))) | |
2048 | ||
2cfcaed5 KR |
2049 | (pass-if (not (= +nan.0 +nan.0))) |
2050 | (pass-if (not (= 0 +nan.0))) | |
2051 | (pass-if (not (= +nan.0 0))) | |
2052 | (pass-if (not (= 1 +nan.0))) | |
2053 | (pass-if (not (= +nan.0 1))) | |
2054 | (pass-if (not (= -1 +nan.0))) | |
2055 | (pass-if (not (= +nan.0 -1))) | |
2056 | ||
2057 | (pass-if (not (= (ash 1 256) +nan.0))) | |
2058 | (pass-if (not (= +nan.0 (ash 1 256)))) | |
2059 | (pass-if (not (= (- (ash 1 256)) +nan.0))) | |
2060 | (pass-if (not (= +nan.0 (- (ash 1 256))))) | |
2061 | ||
2062 | (pass-if (not (= (ash 1 8192) +nan.0))) | |
2063 | (pass-if (not (= +nan.0 (ash 1 8192)))) | |
2064 | (pass-if (not (= (- (ash 1 8192)) +nan.0))) | |
2065 | (pass-if (not (= +nan.0 (- (ash 1 8192))))) | |
2066 | ||
2067 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
2068 | ;; sure we've avoided that | |
2069 | (pass-if (not (= (ash 3 1023) +nan.0))) | |
2a8b5e04 KR |
2070 | (pass-if (not (= +nan.0 (ash 3 1023)))) |
2071 | ||
2072 | (pass-if (= 1/2 0.5)) | |
2073 | (pass-if (not (= 1/3 0.333333333333333333333333333333333))) | |
2074 | (pass-if (not (= 2/3 0.5))) | |
2075 | (pass-if (not (= 0.5 (+ 1/2 (/ 1 (ash 1 1000)))))) | |
2076 | ||
2077 | (pass-if (= 1/2 0.5+0i)) | |
2078 | (pass-if (not (= 0.333333333333333333333333333333333 1/3))) | |
2079 | (pass-if (not (= 2/3 0.5+0i))) | |
2080 | (pass-if (not (= 1/2 0+0.5i))) | |
2081 | ||
2082 | (pass-if (= 0.5 1/2)) | |
2083 | (pass-if (not (= 0.5 2/3))) | |
2084 | (pass-if (not (= (+ 1/2 (/ 1 (ash 1 1000))) 0.5))) | |
2085 | ||
2086 | (pass-if (= 0.5+0i 1/2)) | |
2087 | (pass-if (not (= 0.5+0i 2/3))) | |
6f6847fa KR |
2088 | (pass-if (not (= 0+0.5i 1/2))) |
2089 | ||
2090 | ;; prior to guile 1.8, inum/flonum comparisons were done just by | |
2091 | ;; converting the inum to a double, which on a 64-bit would round making | |
2092 | ;; say inexact 2^58 appear equal to exact 2^58+1 | |
2093 | (pass-if (= (ash-flo 1.0 58) (ash 1 58))) | |
2094 | (pass-if (not (= (ash-flo 1.0 58) (1+ (ash 1 58))))) | |
2095 | (pass-if (not (= (ash-flo 1.0 58) (1- (ash 1 58))))) | |
2096 | (pass-if (= (ash 1 58) (ash-flo 1.0 58))) | |
2097 | (pass-if (not (= (1+ (ash 1 58)) (ash-flo 1.0 58)))) | |
01329288 MW |
2098 | (pass-if (not (= (1- (ash 1 58)) (ash-flo 1.0 58)))) |
2099 | ||
2100 | ;; prior to guile 2.0.10, inum/complex comparisons were done just by | |
2101 | ;; converting the inum to a double, which on a 64-bit would round making | |
2102 | ;; say inexact 2^58 appear equal to exact 2^58+1 | |
2103 | (pass-if (= (+ +0.0i (ash-flo 1.0 58)) (ash 1 58))) | |
2104 | (pass-if (not (= (+ +0.0i (ash-flo 1.0 58)) (1+ (ash 1 58))))) | |
2105 | (pass-if (not (= (+ +0.0i (ash-flo 1.0 58)) (1- (ash 1 58))))) | |
2106 | (pass-if (= (ash 1 58) (+ +0.0i (ash-flo 1.0 58)))) | |
2107 | (pass-if (not (= (1+ (ash 1 58)) (+ +0.0i (ash-flo 1.0 58))))) | |
2108 | (pass-if (not (= (1- (ash 1 58)) (+ +0.0i (ash-flo 1.0 58))))) | |
2109 | ||
2110 | ;; prior to guile 2.0.10, fraction/flonum and fraction/complex | |
2111 | ;; comparisons mishandled infinities. | |
2112 | (pass-if (not (= 1/2 +inf.0))) | |
2113 | (pass-if (not (= 1/2 -inf.0))) | |
2114 | (pass-if (not (= +inf.0 1/2))) | |
2115 | (pass-if (not (= -inf.0 1/2))) | |
2116 | (pass-if (not (= 1/2 +inf.0+0.0i))) | |
2117 | (pass-if (not (= 1/2 -inf.0+0.0i))) | |
2118 | (pass-if (not (= +inf.0+0.0i 1/2))) | |
2119 | (pass-if (not (= -inf.0+0.0i 1/2)))) | |
7c24e528 | 2120 | |
de142bea DH |
2121 | ;;; |
2122 | ;;; < | |
2123 | ;;; | |
2124 | ||
2125 | (with-test-prefix "<" | |
2126 | ||
8a1f4f98 | 2127 | (pass-if "documented?" |
cb18f2a8 | 2128 | (documented? <)) |
de142bea | 2129 | |
de142bea DH |
2130 | (with-test-prefix "(< 0 n)" |
2131 | ||
2132 | (pass-if "n = 0" | |
2133 | (not (< 0 0))) | |
2134 | ||
2135 | (pass-if "n = 0.0" | |
2136 | (not (< 0 0.0))) | |
2137 | ||
2138 | (pass-if "n = 1" | |
2139 | (< 0 1)) | |
2140 | ||
2141 | (pass-if "n = 1.0" | |
2142 | (< 0 1.0)) | |
2143 | ||
2144 | (pass-if "n = -1" | |
2145 | (not (< 0 -1))) | |
2146 | ||
2147 | (pass-if "n = -1.0" | |
2148 | (not (< 0 -1.0))) | |
2149 | ||
21e39e8f DH |
2150 | (pass-if "n = fixnum-max" |
2151 | (< 0 fixnum-max)) | |
2152 | ||
2153 | (pass-if "n = fixnum-max + 1" | |
2154 | (< 0 (+ fixnum-max 1))) | |
2155 | ||
2156 | (pass-if "n = fixnum-min" | |
2157 | (not (< 0 fixnum-min))) | |
de142bea | 2158 | |
21e39e8f DH |
2159 | (pass-if "n = fixnum-min - 1" |
2160 | (not (< 0 (- fixnum-min 1))))) | |
2161 | ||
de142bea DH |
2162 | (with-test-prefix "(< 0.0 n)" |
2163 | ||
2164 | (pass-if "n = 0" | |
2165 | (not (< 0.0 0))) | |
2166 | ||
2167 | (pass-if "n = 0.0" | |
2168 | (not (< 0.0 0.0))) | |
2169 | ||
01329288 MW |
2170 | (pass-if "n = -0.0" |
2171 | (not (< 0.0 -0.0))) | |
2172 | ||
de142bea DH |
2173 | (pass-if "n = 1" |
2174 | (< 0.0 1)) | |
2175 | ||
2176 | (pass-if "n = 1.0" | |
2177 | (< 0.0 1.0)) | |
2178 | ||
2179 | (pass-if "n = -1" | |
2180 | (not (< 0.0 -1))) | |
2181 | ||
2182 | (pass-if "n = -1.0" | |
2183 | (not (< 0.0 -1.0))) | |
2184 | ||
21e39e8f DH |
2185 | (pass-if "n = fixnum-max" |
2186 | (< 0.0 fixnum-max)) | |
2187 | ||
2188 | (pass-if "n = fixnum-max + 1" | |
2189 | (< 0.0 (+ fixnum-max 1))) | |
de142bea | 2190 | |
21e39e8f DH |
2191 | (pass-if "n = fixnum-min" |
2192 | (not (< 0.0 fixnum-min))) | |
2193 | ||
2194 | (pass-if "n = fixnum-min - 1" | |
2195 | (not (< 0.0 (- fixnum-min 1))))) | |
95ed2217 MW |
2196 | |
2197 | (pass-if (not (< -0.0 0.0))) | |
2198 | (pass-if (not (< -0.0 -0.0))) | |
21e39e8f DH |
2199 | |
2200 | (with-test-prefix "(< 1 n)" | |
de142bea | 2201 | |
21e39e8f | 2202 | (pass-if "n = 0" |
de142bea DH |
2203 | (not (< 1 0))) |
2204 | ||
21e39e8f DH |
2205 | (pass-if "n = 0.0" |
2206 | (not (< 1 0.0))) | |
2207 | ||
2208 | (pass-if "n = 1" | |
2209 | (not (< 1 1))) | |
2210 | ||
de142bea | 2211 | (pass-if "n = 1.0" |
21e39e8f DH |
2212 | (not (< 1 1.0))) |
2213 | ||
2214 | (pass-if "n = -1" | |
2215 | (not (< 1 -1))) | |
2216 | ||
2217 | (pass-if "n = -1.0" | |
2218 | (not (< 1 -1.0))) | |
2219 | ||
2220 | (pass-if "n = fixnum-max" | |
2221 | (< 1 fixnum-max)) | |
2222 | ||
2223 | (pass-if "n = fixnum-max + 1" | |
2224 | (< 1 (+ fixnum-max 1))) | |
2225 | ||
2226 | (pass-if "n = fixnum-min" | |
2227 | (not (< 1 fixnum-min))) | |
2228 | ||
2229 | (pass-if "n = fixnum-min - 1" | |
2230 | (not (< 1 (- fixnum-min 1))))) | |
2231 | ||
2232 | (with-test-prefix "(< 1.0 n)" | |
2233 | ||
2234 | (pass-if "n = 0" | |
de142bea DH |
2235 | (not (< 1.0 0))) |
2236 | ||
21e39e8f DH |
2237 | (pass-if "n = 0.0" |
2238 | (not (< 1.0 0.0))) | |
2239 | ||
2240 | (pass-if "n = 1" | |
2241 | (not (< 1.0 1))) | |
2242 | ||
2243 | (pass-if "n = 1.0" | |
2244 | (not (< 1.0 1.0))) | |
2245 | ||
de142bea | 2246 | (pass-if "n = -1" |
21e39e8f DH |
2247 | (not (< 1.0 -1))) |
2248 | ||
2249 | (pass-if "n = -1.0" | |
2250 | (not (< 1.0 -1.0))) | |
2251 | ||
2252 | (pass-if "n = fixnum-max" | |
2253 | (< 1.0 fixnum-max)) | |
2254 | ||
2255 | (pass-if "n = fixnum-max + 1" | |
2256 | (< 1.0 (+ fixnum-max 1))) | |
2257 | ||
2258 | (pass-if "n = fixnum-min" | |
2259 | (not (< 1.0 fixnum-min))) | |
2260 | ||
2261 | (pass-if "n = fixnum-min - 1" | |
2262 | (not (< 1.0 (- fixnum-min 1))))) | |
2263 | ||
2264 | (with-test-prefix "(< -1 n)" | |
2265 | ||
2266 | (pass-if "n = 0" | |
de142bea DH |
2267 | (< -1 0)) |
2268 | ||
21e39e8f DH |
2269 | (pass-if "n = 0.0" |
2270 | (< -1 0.0)) | |
2271 | ||
2272 | (pass-if "n = 1" | |
2273 | (< -1 1)) | |
2274 | ||
2275 | (pass-if "n = 1.0" | |
2276 | (< -1 1.0)) | |
2277 | ||
2278 | (pass-if "n = -1" | |
2279 | (not (< -1 -1))) | |
2280 | ||
de142bea | 2281 | (pass-if "n = -1.0" |
21e39e8f DH |
2282 | (not (< -1 -1.0))) |
2283 | ||
2284 | (pass-if "n = fixnum-max" | |
2285 | (< -1 fixnum-max)) | |
2286 | ||
2287 | (pass-if "n = fixnum-max + 1" | |
2288 | (< -1 (+ fixnum-max 1))) | |
2289 | ||
2290 | (pass-if "n = fixnum-min" | |
2291 | (not (< -1 fixnum-min))) | |
2292 | ||
2293 | (pass-if "n = fixnum-min - 1" | |
2294 | (not (< -1 (- fixnum-min 1))))) | |
2295 | ||
2296 | (with-test-prefix "(< -1.0 n)" | |
2297 | ||
2298 | (pass-if "n = 0" | |
de142bea DH |
2299 | (< -1.0 0)) |
2300 | ||
21e39e8f DH |
2301 | (pass-if "n = 0.0" |
2302 | (< -1.0 0.0)) | |
2303 | ||
2304 | (pass-if "n = 1" | |
2305 | (< -1.0 1)) | |
2306 | ||
2307 | (pass-if "n = 1.0" | |
2308 | (< -1.0 1.0)) | |
2309 | ||
2310 | (pass-if "n = -1" | |
2311 | (not (< -1.0 -1))) | |
2312 | ||
2313 | (pass-if "n = -1.0" | |
2314 | (not (< -1.0 -1.0))) | |
2315 | ||
2316 | (pass-if "n = fixnum-max" | |
2317 | (< -1.0 fixnum-max)) | |
2318 | ||
2319 | (pass-if "n = fixnum-max + 1" | |
2320 | (< -1.0 (+ fixnum-max 1))) | |
de142bea | 2321 | |
21e39e8f DH |
2322 | (pass-if "n = fixnum-min" |
2323 | (not (< -1.0 fixnum-min))) | |
2324 | ||
2325 | (pass-if "n = fixnum-min - 1" | |
2326 | (not (< -1.0 (- fixnum-min 1))))) | |
2327 | ||
2328 | (with-test-prefix "(< fixnum-max n)" | |
2329 | ||
2330 | (pass-if "n = 0" | |
2331 | (not (< fixnum-max 0))) | |
2332 | ||
2333 | (pass-if "n = 0.0" | |
2334 | (not (< fixnum-max 0.0))) | |
de142bea DH |
2335 | |
2336 | (pass-if "n = 1" | |
21e39e8f | 2337 | (not (< fixnum-max 1))) |
de142bea DH |
2338 | |
2339 | (pass-if "n = 1.0" | |
21e39e8f | 2340 | (not (< fixnum-max 1.0))) |
de142bea DH |
2341 | |
2342 | (pass-if "n = -1" | |
21e39e8f | 2343 | (not (< fixnum-max -1))) |
de142bea DH |
2344 | |
2345 | (pass-if "n = -1.0" | |
21e39e8f | 2346 | (not (< fixnum-max -1.0))) |
de142bea | 2347 | |
21e39e8f DH |
2348 | (pass-if "n = fixnum-max" |
2349 | (not (< fixnum-max fixnum-max))) | |
de142bea | 2350 | |
21e39e8f DH |
2351 | (pass-if "n = fixnum-max + 1" |
2352 | (< fixnum-max (+ fixnum-max 1))) | |
2353 | ||
2354 | (pass-if "n = fixnum-min" | |
2355 | (not (< fixnum-max fixnum-min))) | |
2356 | ||
2357 | (pass-if "n = fixnum-min - 1" | |
2358 | (not (< fixnum-max (- fixnum-min 1))))) | |
2359 | ||
2360 | (with-test-prefix "(< (+ fixnum-max 1) n)" | |
2361 | ||
2362 | (pass-if "n = 0" | |
2363 | (not (< (+ fixnum-max 1) 0))) | |
2364 | ||
2365 | (pass-if "n = 0.0" | |
2366 | (not (< (+ fixnum-max 1) 0.0))) | |
de142bea DH |
2367 | |
2368 | (pass-if "n = 1" | |
21e39e8f | 2369 | (not (< (+ fixnum-max 1) 1))) |
de142bea DH |
2370 | |
2371 | (pass-if "n = 1.0" | |
21e39e8f | 2372 | (not (< (+ fixnum-max 1) 1.0))) |
de142bea DH |
2373 | |
2374 | (pass-if "n = -1" | |
21e39e8f | 2375 | (not (< (+ fixnum-max 1) -1))) |
de142bea DH |
2376 | |
2377 | (pass-if "n = -1.0" | |
21e39e8f | 2378 | (not (< (+ fixnum-max 1) -1.0))) |
de142bea | 2379 | |
21e39e8f DH |
2380 | (pass-if "n = fixnum-max" |
2381 | (not (< (+ fixnum-max 1) fixnum-max))) | |
de142bea | 2382 | |
21e39e8f DH |
2383 | (pass-if "n = fixnum-max + 1" |
2384 | (not (< (+ fixnum-max 1) (+ fixnum-max 1)))) | |
de142bea | 2385 | |
21e39e8f DH |
2386 | (pass-if "n = fixnum-min" |
2387 | (not (< (+ fixnum-max 1) fixnum-min))) | |
2388 | ||
2389 | (pass-if "n = fixnum-min - 1" | |
2390 | (not (< (+ fixnum-max 1) (- fixnum-min 1))))) | |
2391 | ||
2392 | (with-test-prefix "(< fixnum-min n)" | |
2393 | ||
2394 | (pass-if "n = 0" | |
2395 | (< fixnum-min 0)) | |
2396 | ||
2397 | (pass-if "n = 0.0" | |
2398 | (< fixnum-min 0.0)) | |
de142bea DH |
2399 | |
2400 | (pass-if "n = 1" | |
21e39e8f | 2401 | (< fixnum-min 1)) |
de142bea DH |
2402 | |
2403 | (pass-if "n = 1.0" | |
21e39e8f | 2404 | (< fixnum-min 1.0)) |
de142bea DH |
2405 | |
2406 | (pass-if "n = -1" | |
21e39e8f | 2407 | (< fixnum-min -1)) |
de142bea DH |
2408 | |
2409 | (pass-if "n = -1.0" | |
21e39e8f | 2410 | (< fixnum-min -1.0)) |
de142bea | 2411 | |
21e39e8f DH |
2412 | (pass-if "n = fixnum-max" |
2413 | (< fixnum-min fixnum-max)) | |
2414 | ||
2415 | (pass-if "n = fixnum-max + 1" | |
2416 | (< fixnum-min (+ fixnum-max 1))) | |
de142bea | 2417 | |
21e39e8f DH |
2418 | (pass-if "n = fixnum-min" |
2419 | (not (< fixnum-min fixnum-min))) | |
de142bea | 2420 | |
21e39e8f DH |
2421 | (pass-if "n = fixnum-min - 1" |
2422 | (not (< fixnum-min (- fixnum-min 1))))) | |
2423 | ||
2424 | (with-test-prefix "(< (- fixnum-min 1) n)" | |
2425 | ||
2426 | (pass-if "n = 0" | |
2427 | (< (- fixnum-min 1) 0)) | |
2428 | ||
2429 | (pass-if "n = 0.0" | |
2430 | (< (- fixnum-min 1) 0.0)) | |
2431 | ||
2432 | (pass-if "n = 1" | |
2433 | (< (- fixnum-min 1) 1)) | |
2434 | ||
2435 | (pass-if "n = 1.0" | |
2436 | (< (- fixnum-min 1) 1.0)) | |
de142bea DH |
2437 | |
2438 | (pass-if "n = -1" | |
21e39e8f | 2439 | (< (- fixnum-min 1) -1)) |
de142bea DH |
2440 | |
2441 | (pass-if "n = -1.0" | |
21e39e8f DH |
2442 | (< (- fixnum-min 1) -1.0)) |
2443 | ||
2444 | (pass-if "n = fixnum-max" | |
2445 | (< (- fixnum-min 1) fixnum-max)) | |
2446 | ||
2447 | (pass-if "n = fixnum-max + 1" | |
2448 | (< (- fixnum-min 1) (+ fixnum-max 1))) | |
2449 | ||
2450 | (pass-if "n = fixnum-min" | |
2451 | (< (- fixnum-min 1) fixnum-min)) | |
2452 | ||
2453 | (pass-if "n = fixnum-min - 1" | |
2cfcaed5 KR |
2454 | (not (< (- fixnum-min 1) (- fixnum-min 1))))) |
2455 | ||
adda36ed KR |
2456 | (pass-if (< (ash 1 256) +inf.0)) |
2457 | (pass-if (not (< +inf.0 (ash 1 256)))) | |
2458 | (pass-if (not (< (ash 1 256) -inf.0))) | |
2459 | (pass-if (< -inf.0 (ash 1 256))) | |
2460 | ||
2461 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
2462 | ;; sure we've avoided that | |
2463 | (pass-if (< (1- (ash 1 1024)) +inf.0)) | |
2464 | (pass-if (< (ash 1 1024) +inf.0)) | |
2465 | (pass-if (< (1+ (ash 1 1024)) +inf.0)) | |
2466 | (pass-if (not (< +inf.0 (1- (ash 1 1024))))) | |
2467 | (pass-if (not (< +inf.0 (ash 1 1024)))) | |
2468 | (pass-if (not (< +inf.0 (1+ (ash 1 1024))))) | |
2469 | (pass-if (< -inf.0 (- (1- (ash 1 1024))))) | |
2470 | (pass-if (< -inf.0 (- (ash 1 1024)))) | |
2471 | (pass-if (< -inf.0 (- (1+ (ash 1 1024))))) | |
2472 | (pass-if (not (< (- (1- (ash 1 1024))) -inf.0))) | |
2473 | (pass-if (not (< (- (ash 1 1024)) -inf.0))) | |
2474 | (pass-if (not (< (- (1+ (ash 1 1024))) -inf.0))) | |
2475 | ||
2cfcaed5 KR |
2476 | (pass-if (not (< +nan.0 +nan.0))) |
2477 | (pass-if (not (< 0 +nan.0))) | |
2478 | (pass-if (not (< +nan.0 0))) | |
2479 | (pass-if (not (< 1 +nan.0))) | |
2480 | (pass-if (not (< +nan.0 1))) | |
2481 | (pass-if (not (< -1 +nan.0))) | |
2482 | (pass-if (not (< +nan.0 -1))) | |
2483 | ||
2484 | (pass-if (not (< (ash 1 256) +nan.0))) | |
2485 | (pass-if (not (< +nan.0 (ash 1 256)))) | |
2486 | (pass-if (not (< (- (ash 1 256)) +nan.0))) | |
2487 | (pass-if (not (< +nan.0 (- (ash 1 256))))) | |
2488 | ||
2489 | (pass-if (not (< (ash 1 8192) +nan.0))) | |
2490 | (pass-if (not (< +nan.0 (ash 1 8192)))) | |
2491 | (pass-if (not (< (- (ash 1 8192)) +nan.0))) | |
2492 | (pass-if (not (< +nan.0 (- (ash 1 8192))))) | |
2493 | ||
2494 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
2495 | ;; sure we've avoided that | |
2496 | (pass-if (not (< (ash 3 1023) +nan.0))) | |
2497 | (pass-if (not (< (1+ (ash 3 1023)) +nan.0))) | |
2498 | (pass-if (not (< (1- (ash 3 1023)) +nan.0))) | |
2499 | (pass-if (not (< +nan.0 (ash 3 1023)))) | |
2500 | (pass-if (not (< +nan.0 (1+ (ash 3 1023))))) | |
fe89421e KR |
2501 | (pass-if (not (< +nan.0 (1- (ash 3 1023))))) |
2502 | ||
2503 | (with-test-prefix "inum/frac" | |
2504 | (pass-if (< 2 9/4)) | |
2505 | (pass-if (< -2 9/4)) | |
2506 | (pass-if (< -2 7/4)) | |
2507 | (pass-if (< -2 -7/4)) | |
2508 | (pass-if (eq? #f (< 2 7/4))) | |
2509 | (pass-if (eq? #f (< 2 -7/4))) | |
2510 | (pass-if (eq? #f (< 2 -9/4))) | |
2511 | (pass-if (eq? #f (< -2 -9/4)))) | |
2512 | ||
2513 | (with-test-prefix "bignum/frac" | |
2514 | (let ((x (ash 1 2048))) | |
2515 | (pass-if (< x (* 4/3 x))) | |
2516 | (pass-if (< (- x) (* 4/3 x))) | |
2517 | (pass-if (< (- x) (* 2/3 x))) | |
2518 | (pass-if (< (- x) (* -2/3 x))) | |
2519 | (pass-if (eq? #f (< x (* 2/3 x)))) | |
2520 | (pass-if (eq? #f (< x (* -2/3 x)))) | |
2521 | (pass-if (eq? #f (< x (* -4/3 x)))) | |
2522 | (pass-if (eq? #f (< (- x) (* -4/3 x)))))) | |
2523 | ||
95ed2217 MW |
2524 | (with-test-prefix "inum/flonum" |
2525 | (pass-if (< 4 4.5)) | |
2526 | (pass-if (< 4.5 5)) | |
2527 | (pass-if (< -5 -4.5)) | |
2528 | (pass-if (< -4.5 4)) | |
2529 | (pass-if (not (< 4.5 4))) | |
2530 | (pass-if (not (< 5 4.5))) | |
2531 | (pass-if (not (< -4.5 -5))) | |
2532 | (pass-if (not (< 4 -4.5))) | |
2533 | ||
2534 | (pass-if (< 4 +inf.0)) | |
2535 | (pass-if (< -4 +inf.0)) | |
2536 | (pass-if (< -inf.0 4)) | |
2537 | (pass-if (< -inf.0 -4)) | |
2538 | (pass-if (not (< +inf.0 4))) | |
2539 | (pass-if (not (< +inf.0 -4))) | |
2540 | (pass-if (not (< 4 -inf.0))) | |
2541 | (pass-if (not (< -4 -inf.0))) | |
2542 | ||
2543 | (pass-if (not (< +nan.0 4))) | |
2544 | (pass-if (not (< +nan.0 -4))) | |
2545 | (pass-if (not (< 4 +nan.0))) | |
2546 | (pass-if (not (< -4 +nan.0))) | |
2547 | ||
2548 | (pass-if (< most-positive-fixnum (expt 2.0 fixnum-bit))) | |
2549 | (pass-if (not (< (expt 2.0 fixnum-bit) most-positive-fixnum))) | |
2550 | ||
2551 | (pass-if (< (- (expt 2.0 fixnum-bit)) most-negative-fixnum)) | |
2552 | (pass-if (not (< most-negative-fixnum (- (expt 2.0 fixnum-bit))))) | |
2553 | ||
2554 | ;; Prior to guile 2.0.10, we would unconditionally convert the inum | |
2555 | ;; to a double, which on a 64-bit system could result in a | |
2556 | ;; significant change in its value, thus corrupting the comparison. | |
284859c2 MW |
2557 | (pass-if (< most-positive-fixnum (exact->inexact (+ 1 most-positive-fixnum)))) |
2558 | (pass-if (< (exact->inexact (- (+ 1 most-positive-fixnum))) (- most-positive-fixnum)))) | |
95ed2217 | 2559 | |
fe89421e KR |
2560 | (with-test-prefix "flonum/frac" |
2561 | (pass-if (< 0.75 4/3)) | |
2562 | (pass-if (< -0.75 4/3)) | |
2563 | (pass-if (< -0.75 2/3)) | |
2564 | (pass-if (< -0.75 -2/3)) | |
2565 | (pass-if (eq? #f (< 0.75 2/3))) | |
2566 | (pass-if (eq? #f (< 0.75 -2/3))) | |
2567 | (pass-if (eq? #f (< 0.75 -4/3))) | |
2568 | (pass-if (eq? #f (< -0.75 -4/3))) | |
2569 | ||
2570 | (pass-if (< -inf.0 4/3)) | |
2571 | (pass-if (< -inf.0 -4/3)) | |
2572 | (pass-if (eq? #f (< +inf.0 4/3))) | |
2573 | (pass-if (eq? #f (< +inf.0 -4/3))) | |
2574 | ||
2575 | (pass-if (eq? #f (< +nan.0 4/3))) | |
2576 | (pass-if (eq? #f (< +nan.0 -4/3)))) | |
2577 | ||
2578 | (with-test-prefix "frac/inum" | |
2579 | (pass-if (< 7/4 2)) | |
2580 | (pass-if (< -7/4 2)) | |
2581 | (pass-if (< -9/4 2)) | |
2582 | (pass-if (< -9/4 -2)) | |
2583 | (pass-if (eq? #f (< 9/4 2))) | |
2584 | (pass-if (eq? #f (< 9/4 -2))) | |
2585 | (pass-if (eq? #f (< 7/4 -2))) | |
2586 | (pass-if (eq? #f (< -7/4 -2)))) | |
2587 | ||
2588 | (with-test-prefix "frac/bignum" | |
2589 | (let ((x (ash 1 2048))) | |
2590 | (pass-if (< (* 2/3 x) x)) | |
2591 | (pass-if (< (* -2/3 x) x)) | |
2592 | (pass-if (< (* -4/3 x) x)) | |
2593 | (pass-if (< (* -4/3 x) (- x))) | |
2594 | (pass-if (eq? #f (< (* 4/3 x) x))) | |
2595 | (pass-if (eq? #f (< (* 4/3 x) (- x)))) | |
2596 | (pass-if (eq? #f (< (* 2/3 x) (- x)))) | |
2597 | (pass-if (eq? #f (< (* -2/3 x) (- x)))))) | |
2598 | ||
2599 | (with-test-prefix "frac/flonum" | |
2600 | (pass-if (< 2/3 0.75)) | |
2601 | (pass-if (< -2/3 0.75)) | |
2602 | (pass-if (< -4/3 0.75)) | |
2603 | (pass-if (< -4/3 -0.75)) | |
2604 | (pass-if (eq? #f (< 4/3 0.75))) | |
2605 | (pass-if (eq? #f (< 4/3 -0.75))) | |
2606 | (pass-if (eq? #f (< 2/3 -0.75))) | |
2607 | (pass-if (eq? #f (< -2/3 -0.75))) | |
2608 | ||
2609 | (pass-if (< 4/3 +inf.0)) | |
2610 | (pass-if (< -4/3 +inf.0)) | |
2611 | (pass-if (eq? #f (< 4/3 -inf.0))) | |
2612 | (pass-if (eq? #f (< -4/3 -inf.0))) | |
2613 | ||
2614 | (pass-if (eq? #f (< 4/3 +nan.0))) | |
2615 | (pass-if (eq? #f (< -4/3 +nan.0)))) | |
2616 | ||
2617 | (with-test-prefix "frac/frac" | |
2618 | (pass-if (< 2/3 6/7)) | |
2619 | (pass-if (< -2/3 6/7)) | |
2620 | (pass-if (< -4/3 6/7)) | |
2621 | (pass-if (< -4/3 -6/7)) | |
2622 | (pass-if (eq? #f (< 4/3 6/7))) | |
2623 | (pass-if (eq? #f (< 4/3 -6/7))) | |
2624 | (pass-if (eq? #f (< 2/3 -6/7))) | |
2625 | (pass-if (eq? #f (< -2/3 -6/7))))) | |
f29b3454 DH |
2626 | |
2627 | ;;; | |
2628 | ;;; > | |
2629 | ;;; | |
2630 | ||
7c24e528 RB |
2631 | ;; currently not tested -- implementation is trivial |
2632 | ;; (> x y) is implemented as (< y x) | |
2633 | ;; FIXME: tests should probably be added in case we change implementation. | |
2634 | ||
f29b3454 DH |
2635 | ;;; |
2636 | ;;; <= | |
2637 | ;;; | |
2638 | ||
7c24e528 RB |
2639 | ;; currently not tested -- implementation is trivial |
2640 | ;; (<= x y) is implemented as (not (< y x)) | |
2641 | ;; FIXME: tests should probably be added in case we change implementation. | |
2642 | ||
f29b3454 DH |
2643 | ;;; |
2644 | ;;; >= | |
2645 | ;;; | |
2646 | ||
7c24e528 RB |
2647 | ;; currently not tested -- implementation is trivial |
2648 | ;; (>= x y) is implemented as (not (< x y)) | |
2649 | ;; FIXME: tests should probably be added in case we change implementation. | |
2650 | ||
f29b3454 DH |
2651 | ;;; |
2652 | ;;; zero? | |
2653 | ;;; | |
2654 | ||
7c24e528 | 2655 | (with-test-prefix "zero?" |
2519490c | 2656 | (pass-if (documented? zero?)) |
c7218482 MW |
2657 | |
2658 | (pass-if (zero? 0)) | |
2659 | (pass-if (zero? 0.0)) | |
2660 | (pass-if (zero? -0.0)) | |
2661 | ||
2662 | (pass-if (zero? 0.0+0.0i)) | |
2663 | (pass-if (zero? 0.0-0.0i)) | |
2664 | (pass-if (zero? 0.0+0.0i)) | |
2665 | (pass-if (zero? -0.0-0.0i)) | |
2666 | ||
2667 | (pass-if (not (zero? 7))) | |
4d332f19 | 2668 | (pass-if (not (zero? -7))) |
c7218482 MW |
2669 | (pass-if (not (zero? 1/7))) |
2670 | (pass-if (not (zero? -inf.0))) | |
2671 | (pass-if (not (zero? +inf.0))) | |
2672 | (pass-if (not (zero? +nan.0))) | |
4d332f19 DH |
2673 | (pass-if (not (zero? (+ 1 fixnum-max)))) |
2674 | (pass-if (not (zero? (- 1 fixnum-min)))) | |
2675 | (pass-if (not (zero? 1.3))) | |
c7218482 MW |
2676 | (pass-if (not (zero? 3.1+4.2i))) |
2677 | (pass-if (not (zero? 1.0+0.0i))) | |
2678 | (pass-if (not (zero? 0.0-1.0i)))) | |
7c24e528 | 2679 | |
f29b3454 DH |
2680 | ;;; |
2681 | ;;; positive? | |
2682 | ;;; | |
2683 | ||
7c24e528 | 2684 | (with-test-prefix "positive?" |
2519490c | 2685 | (pass-if (documented? positive?)) |
7c24e528 RB |
2686 | (pass-if (positive? 1)) |
2687 | (pass-if (positive? (+ fixnum-max 1))) | |
2688 | (pass-if (positive? 1.3)) | |
4d332f19 DH |
2689 | (pass-if (not (positive? 0))) |
2690 | (pass-if (not (positive? -1))) | |
2691 | (pass-if (not (positive? (- fixnum-min 1)))) | |
2692 | (pass-if (not (positive? -1.3)))) | |
7c24e528 | 2693 | |
f29b3454 DH |
2694 | ;;; |
2695 | ;;; negative? | |
2696 | ;;; | |
2697 | ||
7c24e528 | 2698 | (with-test-prefix "negative?" |
2519490c | 2699 | (pass-if (documented? negative?)) |
4d332f19 DH |
2700 | (pass-if (not (negative? 1))) |
2701 | (pass-if (not (negative? (+ fixnum-max 1)))) | |
2702 | (pass-if (not (negative? 1.3))) | |
2703 | (pass-if (not (negative? 0))) | |
7c24e528 RB |
2704 | (pass-if (negative? -1)) |
2705 | (pass-if (negative? (- fixnum-min 1))) | |
2706 | (pass-if (negative? -1.3))) | |
2707 | ||
f29b3454 DH |
2708 | ;;; |
2709 | ;;; max | |
2710 | ;;; | |
2711 | ||
adda36ed | 2712 | (with-test-prefix "max" |
593a4c2f KR |
2713 | (pass-if-exception "no args" exception:wrong-num-args |
2714 | (max)) | |
2715 | ||
2716 | (pass-if-exception "one complex" exception:wrong-type-arg | |
2717 | (max 1+i)) | |
2718 | ||
2719 | (pass-if-exception "inum/complex" exception:wrong-type-arg | |
2720 | (max 123 1+i)) | |
2721 | (pass-if-exception "big/complex" exception:wrong-type-arg | |
2722 | (max 9999999999999999999999999999999999999999 1+i)) | |
2723 | (pass-if-exception "real/complex" exception:wrong-type-arg | |
2724 | (max 123.0 1+i)) | |
2725 | (pass-if-exception "frac/complex" exception:wrong-type-arg | |
2726 | (max 123/456 1+i)) | |
2727 | ||
2728 | (pass-if-exception "complex/inum" exception:wrong-type-arg | |
2729 | (max 1+i 123)) | |
2730 | (pass-if-exception "complex/big" exception:wrong-type-arg | |
2731 | (max 1+i 9999999999999999999999999999999999999999)) | |
2732 | (pass-if-exception "complex/real" exception:wrong-type-arg | |
2733 | (max 1+i 123.0)) | |
2734 | (pass-if-exception "complex/frac" exception:wrong-type-arg | |
2735 | (max 1+i 123/456)) | |
2736 | ||
adda36ed KR |
2737 | (let ((big*2 (* fixnum-max 2)) |
2738 | (big*3 (* fixnum-max 3)) | |
2739 | (big*4 (* fixnum-max 4)) | |
2740 | (big*5 (* fixnum-max 5))) | |
501da403 | 2741 | |
2530518e | 2742 | (with-test-prefix "inum / frac" |
2e274311 MW |
2743 | (pass-if (eqv? 3 (max 3 5/2))) |
2744 | (pass-if (eqv? 5/2 (max 2 5/2)))) | |
2530518e KR |
2745 | |
2746 | (with-test-prefix "frac / inum" | |
2e274311 MW |
2747 | (pass-if (eqv? 3 (max 5/2 3))) |
2748 | (pass-if (eqv? 5/2 (max 5/2 2)))) | |
2749 | ||
2750 | (with-test-prefix "infinities and NaNs" | |
b4c55c9c | 2751 | ;; +inf.0 beats everything except NaNs |
2e274311 MW |
2752 | (pass-if (eqv? +inf.0 (max +inf.0 123 ))) |
2753 | (pass-if (eqv? +inf.0 (max 123 +inf.0 ))) | |
2754 | (pass-if (eqv? +inf.0 (max +inf.0 -123.3 ))) | |
2755 | (pass-if (eqv? +inf.0 (max -123.3 +inf.0 ))) | |
2756 | (pass-if (eqv? +inf.0 (max +inf.0 -7/2 ))) | |
2757 | (pass-if (eqv? +inf.0 (max -7/2 +inf.0 ))) | |
2758 | (pass-if (eqv? +inf.0 (max +inf.0 -1e20 ))) | |
2759 | (pass-if (eqv? +inf.0 (max -1e20 +inf.0 ))) | |
2760 | (pass-if (eqv? +inf.0 (max +inf.0 (- big*2)))) | |
2761 | (pass-if (eqv? +inf.0 (max (- big*2) +inf.0 ))) | |
2762 | (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 ))) | |
2763 | (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 ))) | |
2e274311 MW |
2764 | (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 ))) |
2765 | ||
b4c55c9c | 2766 | ;; NaNs beat everything |
2e274311 MW |
2767 | (pass-if (real-nan? (max +nan.0 123 ))) |
2768 | (pass-if (real-nan? (max 123 +nan.0 ))) | |
2769 | (pass-if (real-nan? (max +nan.0 123.3 ))) | |
2770 | (pass-if (real-nan? (max 123.3 +nan.0 ))) | |
2771 | (pass-if (real-nan? (max +nan.0 -7/2 ))) | |
2772 | (pass-if (real-nan? (max -7/2 +nan.0 ))) | |
2773 | (pass-if (real-nan? (max +nan.0 -1e20 ))) | |
2774 | (pass-if (real-nan? (max -1e20 +nan.0 ))) | |
2775 | (pass-if (real-nan? (max +nan.0 (- big*2)))) | |
2776 | (pass-if (real-nan? (max (- big*2) +nan.0 ))) | |
2777 | (pass-if (real-nan? (max +nan.0 -inf.0 ))) | |
2778 | (pass-if (real-nan? (max -inf.0 +nan.0 ))) | |
2779 | (pass-if (real-nan? (max +nan.0 +nan.0 ))) | |
b4c55c9c MW |
2780 | (pass-if (real-nan? (max +inf.0 +nan.0 ))) |
2781 | (pass-if (real-nan? (max +nan.0 +inf.0 ))) | |
2e274311 MW |
2782 | |
2783 | ;; -inf.0 always loses, except against itself | |
2784 | (pass-if (eqv? -inf.0 (max -inf.0 -inf.0 ))) | |
2785 | (pass-if (eqv? -123.0 (max -inf.0 -123 ))) | |
2786 | (pass-if (eqv? -123.0 (max -123 -inf.0 ))) | |
2787 | (pass-if (eqv? -123.3 (max -inf.0 -123.3 ))) | |
2788 | (pass-if (eqv? -123.3 (max -123.3 -inf.0 ))) | |
2789 | (pass-if (eqv? -3.5 (max -inf.0 -7/2 ))) | |
2790 | (pass-if (eqv? -3.5 (max -7/2 -inf.0 ))) | |
2791 | (pass-if (eqv? -1.0e20 (max -inf.0 -1e20 ))) | |
2792 | (pass-if (eqv? -1.0e20 (max -1e20 -inf.0 ))) | |
2793 | (pass-if (eqv? (exact->inexact (- big*2)) | |
2794 | (max -inf.0 (- big*2)))) | |
2795 | (pass-if (eqv? (exact->inexact (- big*2)) | |
2796 | (max (- big*2) -inf.0 )))) | |
2797 | ||
2798 | (with-test-prefix "signed zeroes" | |
2799 | (pass-if (eqv? 0.0 (max 0.0 0.0))) | |
2800 | (pass-if (eqv? 0.0 (max 0.0 -0.0))) | |
2801 | (pass-if (eqv? 0.0 (max -0.0 0.0))) | |
2802 | (pass-if (eqv? -0.0 (max -0.0 -0.0))) | |
2803 | (pass-if (eqv? 0.0 (max -0.0 0 ))) | |
2804 | (pass-if (eqv? 0.0 (max 0.0 0 ))) | |
2805 | (pass-if (eqv? 0.0 (max 0 -0.0))) | |
2806 | (pass-if (eqv? 0.0 (max 0 0.0))) | |
2807 | (pass-if (eqv? 0 (min 0 0 )))) | |
23d77957 | 2808 | |
2530518e | 2809 | (with-test-prefix "big / frac" |
2e274311 MW |
2810 | (pass-if (eqv? big*2 (max big*2 5/2))) |
2811 | (pass-if (eqv? 5/2 (max (- big*2) 5/2)))) | |
2530518e KR |
2812 | |
2813 | (with-test-prefix "frac / big" | |
2e274311 MW |
2814 | (pass-if (eqv? big*2 (max 5/2 big*2))) |
2815 | (pass-if (eqv? 5/2 (max 5/2 (- big*2))))) | |
2530518e | 2816 | |
23d77957 | 2817 | (with-test-prefix "big / real" |
55a8b708 | 2818 | (pass-if (real-nan? (max big*5 +nan.0))) |
23d72566 KR |
2819 | (pass-if (eqv? (exact->inexact big*5) (max big*5 -inf.0))) |
2820 | (pass-if (eqv? (exact->inexact big*5) (max big*5 1.0))) | |
2821 | (pass-if (eqv? +inf.0 (max big*5 +inf.0))) | |
2822 | (pass-if (eqv? 1.0 (max (- big*5) 1.0)))) | |
23d77957 KR |
2823 | |
2824 | (with-test-prefix "real / big" | |
55a8b708 | 2825 | (pass-if (real-nan? (max +nan.0 big*5))) |
23d72566 KR |
2826 | (pass-if (eqv? (exact->inexact big*5) (max -inf.0 big*5))) |
2827 | (pass-if (eqv? (exact->inexact big*5) (max 1.0 big*5))) | |
2828 | (pass-if (eqv? +inf.0 (max +inf.0 big*5))) | |
2829 | (pass-if (eqv? 1.0 (max 1.0 (- big*5))))) | |
23d77957 | 2830 | |
2530518e | 2831 | (with-test-prefix "frac / frac" |
2e274311 MW |
2832 | (pass-if (eqv? 2/3 (max 1/2 2/3))) |
2833 | (pass-if (eqv? 2/3 (max 2/3 1/2))) | |
2834 | (pass-if (eqv? -1/2 (max -1/2 -2/3))) | |
2835 | (pass-if (eqv? -1/2 (max -2/3 -1/2)))) | |
2530518e | 2836 | |
23d77957 | 2837 | (with-test-prefix "real / real" |
55a8b708 MW |
2838 | (pass-if (real-nan? (max 123.0 +nan.0))) |
2839 | (pass-if (real-nan? (max +nan.0 123.0))) | |
2840 | (pass-if (real-nan? (max +nan.0 +nan.0))) | |
2e274311 MW |
2841 | (pass-if (eqv? 456.0 (max 123.0 456.0))) |
2842 | (pass-if (eqv? 456.0 (max 456.0 123.0))))) | |
adda36ed KR |
2843 | |
2844 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
2845 | ;; sure we've avoided that | |
2846 | (for-each (lambda (b) | |
2847 | (pass-if (list b +inf.0) | |
2e274311 | 2848 | (eqv? +inf.0 (max b +inf.0))) |
adda36ed | 2849 | (pass-if (list +inf.0 b) |
2e274311 | 2850 | (eqv? +inf.0 (max b +inf.0))) |
adda36ed | 2851 | (pass-if (list b -inf.0) |
2e274311 | 2852 | (eqv? (exact->inexact b) (max b -inf.0))) |
adda36ed | 2853 | (pass-if (list -inf.0 b) |
2e274311 | 2854 | (eqv? (exact->inexact b) (max b -inf.0)))) |
adda36ed KR |
2855 | (list (1- (ash 1 1024)) |
2856 | (ash 1 1024) | |
2857 | (1+ (ash 1 1024)) | |
2858 | (- (1- (ash 1 1024))) | |
2859 | (- (ash 1 1024)) | |
501da403 KR |
2860 | (- (1+ (ash 1 1024))))) |
2861 | ||
2862 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
2863 | ;; sure we've avoided that | |
55a8b708 MW |
2864 | (pass-if (real-nan? (max (ash 1 2048) +nan.0))) |
2865 | (pass-if (real-nan? (max +nan.0 (ash 1 2048))))) | |
adda36ed | 2866 | |
f29b3454 DH |
2867 | ;;; |
2868 | ;;; min | |
2869 | ;;; | |
2870 | ||
7c24e528 RB |
2871 | ;; FIXME: unfinished... |
2872 | ||
2873 | (with-test-prefix "min" | |
593a4c2f KR |
2874 | (pass-if-exception "no args" exception:wrong-num-args |
2875 | (min)) | |
2876 | ||
2877 | (pass-if-exception "one complex" exception:wrong-type-arg | |
2878 | (min 1+i)) | |
2879 | ||
2880 | (pass-if-exception "inum/complex" exception:wrong-type-arg | |
2881 | (min 123 1+i)) | |
2882 | (pass-if-exception "big/complex" exception:wrong-type-arg | |
2883 | (min 9999999999999999999999999999999999999999 1+i)) | |
2884 | (pass-if-exception "real/complex" exception:wrong-type-arg | |
2885 | (min 123.0 1+i)) | |
2886 | (pass-if-exception "frac/complex" exception:wrong-type-arg | |
2887 | (min 123/456 1+i)) | |
2888 | ||
2889 | (pass-if-exception "complex/inum" exception:wrong-type-arg | |
2890 | (min 1+i 123)) | |
2891 | (pass-if-exception "complex/big" exception:wrong-type-arg | |
2892 | (min 1+i 9999999999999999999999999999999999999999)) | |
2893 | (pass-if-exception "complex/real" exception:wrong-type-arg | |
2894 | (min 1+i 123.0)) | |
2895 | (pass-if-exception "complex/frac" exception:wrong-type-arg | |
2896 | (min 1+i 123/456)) | |
2897 | ||
7c24e528 RB |
2898 | (let ((big*2 (* fixnum-max 2)) |
2899 | (big*3 (* fixnum-max 3)) | |
2900 | (big*4 (* fixnum-max 4)) | |
2901 | (big*5 (* fixnum-max 5))) | |
23d77957 | 2902 | |
d389e966 | 2903 | (pass-if (documented? min)) |
2e274311 MW |
2904 | (pass-if (eqv? 1 (min 7 3 1 5))) |
2905 | (pass-if (eqv? 1 (min 1 7 3 5))) | |
2906 | (pass-if (eqv? 1 (min 7 3 5 1))) | |
2907 | (pass-if (eqv? -7 (min 2 3 4 -2 5 -7 1 -1 4 2))) | |
2908 | (pass-if (eqv? -7 (min -7 2 3 4 -2 5 1 -1 4 2))) | |
2909 | (pass-if (eqv? -7 (min 2 3 4 -2 5 1 -1 4 2 -7))) | |
2910 | (pass-if (eqv? big*2 (min big*3 big*5 big*2 big*4))) | |
2911 | (pass-if (eqv? big*2 (min big*2 big*3 big*5 big*4))) | |
2912 | (pass-if (eqv? big*2 (min big*3 big*5 big*4 big*2))) | |
7c24e528 | 2913 | (pass-if |
2e274311 | 2914 | (eqv? (- fixnum-min 1) (min 2 4 (- fixnum-min 1) 3 (* 2 fixnum-max)))) |
7c24e528 | 2915 | (pass-if |
2e274311 | 2916 | (eqv? (- fixnum-min 1) (min (- fixnum-min 1) 2 4 3 (* 2 fixnum-max)))) |
7c24e528 | 2917 | (pass-if |
2e274311 | 2918 | (eqv? (- fixnum-min 1) (min 2 4 3 (* 2 fixnum-max) (- fixnum-min 1)))) |
23d77957 | 2919 | |
2530518e | 2920 | (with-test-prefix "inum / frac" |
2e274311 MW |
2921 | (pass-if (eqv? 5/2 (min 3 5/2))) |
2922 | (pass-if (eqv? 2 (min 2 5/2)))) | |
2530518e KR |
2923 | |
2924 | (with-test-prefix "frac / inum" | |
2e274311 MW |
2925 | (pass-if (eqv? 5/2 (min 5/2 3))) |
2926 | (pass-if (eqv? 2 (min 5/2 2)))) | |
2927 | ||
2928 | (with-test-prefix "infinities and NaNs" | |
b4c55c9c | 2929 | ;; -inf.0 beats everything except NaNs |
2e274311 MW |
2930 | (pass-if (eqv? -inf.0 (min -inf.0 123 ))) |
2931 | (pass-if (eqv? -inf.0 (min 123 -inf.0 ))) | |
2932 | (pass-if (eqv? -inf.0 (min -inf.0 -123.3 ))) | |
2933 | (pass-if (eqv? -inf.0 (min -123.3 -inf.0 ))) | |
2934 | (pass-if (eqv? -inf.0 (min -inf.0 -7/2 ))) | |
2935 | (pass-if (eqv? -inf.0 (min -7/2 -inf.0 ))) | |
2936 | (pass-if (eqv? -inf.0 (min -inf.0 -1e20 ))) | |
2937 | (pass-if (eqv? -inf.0 (min -1e20 -inf.0 ))) | |
2938 | (pass-if (eqv? -inf.0 (min -inf.0 (- big*2)))) | |
2939 | (pass-if (eqv? -inf.0 (min (- big*2) -inf.0 ))) | |
2940 | (pass-if (eqv? -inf.0 (min -inf.0 +inf.0 ))) | |
2941 | (pass-if (eqv? -inf.0 (min +inf.0 -inf.0 ))) | |
2e274311 MW |
2942 | (pass-if (eqv? -inf.0 (min -inf.0 -inf.0 ))) |
2943 | ||
b4c55c9c | 2944 | ;; NaNs beat everything |
2e274311 MW |
2945 | (pass-if (real-nan? (min +nan.0 123 ))) |
2946 | (pass-if (real-nan? (min 123 +nan.0 ))) | |
2947 | (pass-if (real-nan? (min +nan.0 123.3 ))) | |
2948 | (pass-if (real-nan? (min 123.3 +nan.0 ))) | |
2949 | (pass-if (real-nan? (min +nan.0 -7/2 ))) | |
2950 | (pass-if (real-nan? (min -7/2 +nan.0 ))) | |
2951 | (pass-if (real-nan? (min +nan.0 -1e20 ))) | |
2952 | (pass-if (real-nan? (min -1e20 +nan.0 ))) | |
2953 | (pass-if (real-nan? (min +nan.0 (- big*2)))) | |
2954 | (pass-if (real-nan? (min (- big*2) +nan.0 ))) | |
2955 | (pass-if (real-nan? (min +nan.0 +inf.0 ))) | |
2956 | (pass-if (real-nan? (min +inf.0 +nan.0 ))) | |
2957 | (pass-if (real-nan? (min +nan.0 +nan.0 ))) | |
b4c55c9c MW |
2958 | (pass-if (real-nan? (min -inf.0 +nan.0 ))) |
2959 | (pass-if (real-nan? (min +nan.0 -inf.0 ))) | |
2e274311 MW |
2960 | |
2961 | ;; +inf.0 always loses, except against itself | |
2962 | (pass-if (eqv? +inf.0 (min +inf.0 +inf.0 ))) | |
2963 | (pass-if (eqv? -123.0 (min +inf.0 -123 ))) | |
2964 | (pass-if (eqv? -123.0 (min -123 +inf.0 ))) | |
2965 | (pass-if (eqv? -123.3 (min +inf.0 -123.3 ))) | |
2966 | (pass-if (eqv? -123.3 (min -123.3 +inf.0 ))) | |
2967 | (pass-if (eqv? -3.5 (min +inf.0 -7/2 ))) | |
2968 | (pass-if (eqv? -3.5 (min -7/2 +inf.0 ))) | |
2969 | (pass-if (eqv? -1.0e20 (min +inf.0 -1e20 ))) | |
2970 | (pass-if (eqv? -1.0e20 (min -1e20 +inf.0 ))) | |
2971 | (pass-if (eqv? (exact->inexact (- big*2)) | |
2972 | (min +inf.0 (- big*2)))) | |
2973 | (pass-if (eqv? (exact->inexact (- big*2)) | |
2974 | (min (- big*2) +inf.0 )))) | |
2975 | ||
2976 | (with-test-prefix "signed zeroes" | |
2977 | (pass-if (eqv? 0.0 (min 0.0 0.0))) | |
2978 | (pass-if (eqv? -0.0 (min 0.0 -0.0))) | |
2979 | (pass-if (eqv? -0.0 (min -0.0 0.0))) | |
2980 | (pass-if (eqv? -0.0 (min -0.0 -0.0))) | |
2981 | (pass-if (eqv? -0.0 (min -0.0 0 ))) | |
2982 | (pass-if (eqv? 0.0 (min 0.0 0 ))) | |
2983 | (pass-if (eqv? -0.0 (min 0 -0.0))) | |
2984 | (pass-if (eqv? 0.0 (min 0 0.0))) | |
2985 | (pass-if (eqv? 0 (min 0 0 )))) | |
23d77957 | 2986 | |
2530518e | 2987 | (with-test-prefix "big / frac" |
2e274311 MW |
2988 | (pass-if (eqv? 5/2 (min big*2 5/2))) |
2989 | (pass-if (eqv? (- big*2) (min (- big*2) 5/2)))) | |
2530518e KR |
2990 | |
2991 | (with-test-prefix "frac / big" | |
2e274311 MW |
2992 | (pass-if (eqv? 5/2 (min 5/2 big*2))) |
2993 | (pass-if (eqv? (- big*2) (min 5/2 (- big*2))))) | |
2530518e | 2994 | |
23d77957 | 2995 | (with-test-prefix "big / real" |
55a8b708 | 2996 | (pass-if (real-nan? (min big*5 +nan.0))) |
23d72566 KR |
2997 | (pass-if (eqv? (exact->inexact big*5) (min big*5 +inf.0))) |
2998 | (pass-if (eqv? -inf.0 (min big*5 -inf.0))) | |
2999 | (pass-if (eqv? 1.0 (min big*5 1.0))) | |
3000 | (pass-if (eqv? (exact->inexact (- big*5)) (min (- big*5) 1.0)))) | |
23d77957 KR |
3001 | |
3002 | (with-test-prefix "real / big" | |
55a8b708 | 3003 | (pass-if (real-nan? (min +nan.0 big*5))) |
23d72566 KR |
3004 | (pass-if (eqv? (exact->inexact big*5) (min +inf.0 big*5))) |
3005 | (pass-if (eqv? -inf.0 (min -inf.0 big*5))) | |
3006 | (pass-if (eqv? 1.0 (min 1.0 big*5))) | |
3007 | (pass-if (eqv? (exact->inexact (- big*5)) (min 1.0 (- big*5))))) | |
23d77957 | 3008 | |
2530518e | 3009 | (with-test-prefix "frac / frac" |
2e274311 MW |
3010 | (pass-if (eqv? 1/2 (min 1/2 2/3))) |
3011 | (pass-if (eqv? 1/2 (min 2/3 1/2))) | |
3012 | (pass-if (eqv? -2/3 (min -1/2 -2/3))) | |
3013 | (pass-if (eqv? -2/3 (min -2/3 -1/2)))) | |
2530518e | 3014 | |
23d77957 | 3015 | (with-test-prefix "real / real" |
55a8b708 MW |
3016 | (pass-if (real-nan? (min 123.0 +nan.0))) |
3017 | (pass-if (real-nan? (min +nan.0 123.0))) | |
3018 | (pass-if (real-nan? (min +nan.0 +nan.0))) | |
2e274311 MW |
3019 | (pass-if (eqv? 123.0 (min 123.0 456.0))) |
3020 | (pass-if (eqv? 123.0 (min 456.0 123.0))))) | |
23d77957 KR |
3021 | |
3022 | ||
adda36ed KR |
3023 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make |
3024 | ;; sure we've avoided that | |
3025 | (for-each (lambda (b) | |
3026 | (pass-if (list b +inf.0) | |
2e274311 | 3027 | (eqv? (exact->inexact b) (min b +inf.0))) |
adda36ed | 3028 | (pass-if (list +inf.0 b) |
2e274311 | 3029 | (eqv? (exact->inexact b) (min b +inf.0))) |
adda36ed | 3030 | (pass-if (list b -inf.0) |
2e274311 | 3031 | (eqv? -inf.0 (min b -inf.0))) |
adda36ed | 3032 | (pass-if (list -inf.0 b) |
2e274311 | 3033 | (eqv? -inf.0 (min b -inf.0)))) |
adda36ed KR |
3034 | (list (1- (ash 1 1024)) |
3035 | (ash 1 1024) | |
3036 | (1+ (ash 1 1024)) | |
3037 | (- (1- (ash 1 1024))) | |
3038 | (- (ash 1 1024)) | |
501da403 KR |
3039 | (- (1+ (ash 1 1024))))) |
3040 | ||
3041 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
3042 | ;; sure we've avoided that | |
55a8b708 MW |
3043 | (pass-if (real-nan? (min (- (ash 1 2048)) (- +nan.0)))) |
3044 | (pass-if (real-nan? (min (- +nan.0) (- (ash 1 2048)))))) | |
adda36ed | 3045 | |
f29b3454 DH |
3046 | ;;; |
3047 | ;;; + | |
3048 | ;;; | |
3049 | ||
0c57673a | 3050 | (with-test-prefix/c&e "+" |
f29b3454 | 3051 | |
d389e966 | 3052 | (pass-if "documented?" |
0c57673a LC |
3053 | (documented? +)) |
3054 | ||
c7218482 MW |
3055 | (pass-if "simple" |
3056 | (and (eqv? 7 (+ 3 4)) | |
3057 | (eqv? 3 (+ 3)) | |
3058 | (eqv? 0 (+)))) | |
3059 | ||
3060 | (pass-if "exactness propagation" | |
3061 | (and (eqv? 8 (+ 3 5)) | |
3062 | (eqv? 8.0 (+ 3 5.0)) | |
3063 | (eqv? 8.0 (+ 3.0 5)) | |
3064 | (eqv? 8.0 (+ 3.0 5.0)) | |
3065 | ||
3066 | (eqv? 5/6 (+ 1/2 1/3)) | |
3067 | (eqv? 5.5 (+ 1/2 5.0)) | |
3068 | (eqv? 3.25 (+ 3.0 1/4)))) | |
3069 | ||
3070 | (pass-if "signed zeroes" | |
3071 | (and (eqv? 0.0 (+ 0.0)) | |
3072 | (eqv? -0.0 (+ -0.0)) | |
3073 | (eqv? 0.0 (+ 0.0 0.0)) | |
3074 | (eqv? 0.0 (+ 0.0 -0.0)) | |
3075 | (eqv? 0.0 (+ -0.0 0.0)) | |
3076 | (eqv? -0.0 (+ -0.0 -0.0)))) | |
3077 | ||
3078 | (pass-if "NaNs" | |
3079 | (and (real-nan? (+ +nan.0 +nan.0)) | |
3080 | (real-nan? (+ 0 +nan.0)) | |
3081 | (real-nan? (+ +nan.0 0)) | |
3082 | (real-nan? (+ 1 +nan.0)) | |
3083 | (real-nan? (+ +nan.0 1)) | |
3084 | (real-nan? (+ -1 +nan.0)) | |
3085 | (real-nan? (+ +nan.0 -1)) | |
3086 | (real-nan? (+ -7/2 +nan.0)) | |
3087 | (real-nan? (+ +nan.0 -7/2)) | |
3088 | (real-nan? (+ 1e20 +nan.0)) | |
3089 | (real-nan? (+ +nan.0 1e20)) | |
3090 | (real-nan? (+ +inf.0 +nan.0)) | |
3091 | (real-nan? (+ +nan.0 +inf.0)) | |
3092 | (real-nan? (+ -inf.0 +nan.0)) | |
3093 | (real-nan? (+ +nan.0 -inf.0)) | |
3094 | (real-nan? (+ (* fixnum-max 2) +nan.0)) | |
3095 | (real-nan? (+ +nan.0 (* fixnum-max 2))))) | |
3096 | ||
3097 | (pass-if "infinities" | |
3098 | (and (eqv? +inf.0 (+ +inf.0 +inf.0)) | |
3099 | (eqv? -inf.0 (+ -inf.0 -inf.0)) | |
3100 | (real-nan? (+ +inf.0 -inf.0)) | |
3101 | (real-nan? (+ -inf.0 +inf.0)))) | |
3102 | ||
0c57673a LC |
3103 | ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1. |
3104 | (pass-if "fixnum + fixnum = bignum (32-bit)" | |
3105 | (eqv? 536870912 (+ 536870910 2))) | |
3106 | ||
3107 | ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1. | |
3108 | (pass-if "fixnum + fixnum = bignum (64-bit)" | |
3109 | (eqv? 2305843009213693952 (+ 2305843009213693950 2))) | |
3110 | ||
3111 | (pass-if "bignum + fixnum = fixnum" | |
3112 | (eqv? 0 (+ (1+ most-positive-fixnum) most-negative-fixnum)))) | |
f29b3454 | 3113 | |
f29b3454 DH |
3114 | ;;; |
3115 | ;;; - | |
3116 | ;;; | |
3117 | ||
0c57673a | 3118 | (with-test-prefix/c&e "-" |
072e6de2 | 3119 | |
b5c40589 MW |
3120 | (pass-if "double-negation of fixnum-min: =" |
3121 | (= fixnum-min (- (- fixnum-min)))) | |
3122 | (pass-if "double-negation of fixnum-min: eqv?" | |
3123 | (eqv? fixnum-min (- (- fixnum-min)))) | |
3124 | (pass-if "double-negation of fixnum-min: equal?" | |
3125 | (equal? fixnum-min (- (- fixnum-min)))) | |
3126 | ||
3127 | (pass-if "binary double-negation of fixnum-min: =" | |
3128 | (= fixnum-min (- 0 (- 0 fixnum-min)))) | |
3129 | (pass-if "binary double-negation of fixnum-min: eqv?" | |
3130 | (eqv? fixnum-min (- 0 (- 0 fixnum-min)))) | |
3131 | (pass-if "binary double-negation of fixnum-min: equal?" | |
3132 | (equal? fixnum-min (- 0 (- 0 fixnum-min)))) | |
3133 | ||
9b9ef10c MW |
3134 | (pass-if "signed zeroes" |
3135 | (and (eqv? +0.0 (- -0.0)) | |
3136 | (eqv? -0.0 (- +0.0)) | |
3137 | (eqv? 0.0 (- 0.0 0.0)) | |
3138 | (eqv? 0.0 (- 0.0 -0.0)) | |
3139 | (eqv? 0.0 (- -0.0 -0.0)) | |
3140 | (eqv? -0.0 (- -0.0 0.0)))) | |
3141 | ||
3142 | (pass-if "exactness propagation" | |
3143 | (and (eqv? 3 (- 8 5)) | |
3144 | (eqv? 3.0 (- 8 5.0)) | |
3145 | (eqv? 3.0 (- 8.0 5)) | |
3146 | (eqv? 3.0 (- 8.0 5.0)) | |
3147 | (eqv? -1/6 (- 1/3 1/2)) | |
3148 | (eqv? -4.5 (- 1/2 5.0)) | |
3149 | (eqv? 2.75 (- 3.0 1/4)))) | |
3150 | ||
3151 | (pass-if "infinities" | |
3152 | (and (eqv? +inf.0 (- +inf.0 -inf.0)) | |
3153 | (eqv? -inf.0 (- -inf.0 +inf.0)) | |
3154 | (real-nan? (- +inf.0 +inf.0)) | |
3155 | (real-nan? (- -inf.0 -inf.0)))) | |
3156 | ||
3157 | (pass-if "NaNs" | |
3158 | (and (real-nan? (- +nan.0 +nan.0)) | |
3159 | (real-nan? (- 0 +nan.0)) | |
3160 | (real-nan? (- +nan.0 0)) | |
3161 | (real-nan? (- 1 +nan.0)) | |
3162 | (real-nan? (- +nan.0 1)) | |
3163 | (real-nan? (- -1 +nan.0)) | |
3164 | (real-nan? (- +nan.0 -1)) | |
3165 | (real-nan? (- -7/2 +nan.0)) | |
3166 | (real-nan? (- +nan.0 -7/2)) | |
3167 | (real-nan? (- 1e20 +nan.0)) | |
3168 | (real-nan? (- +nan.0 1e20)) | |
3169 | (real-nan? (- +inf.0 +nan.0)) | |
3170 | (real-nan? (- +nan.0 +inf.0)) | |
3171 | (real-nan? (- -inf.0 +nan.0)) | |
3172 | (real-nan? (- +nan.0 -inf.0)) | |
3173 | (real-nan? (- (* fixnum-max 2) +nan.0)) | |
3174 | (real-nan? (- +nan.0 (* fixnum-max 2))))) | |
3175 | ||
3176 | (pass-if "(eqv? fixnum-min (- (- fixnum-min)))" | |
3177 | (eqv? fixnum-min (- (- fixnum-min)))) | |
3178 | (pass-if "(eqv? fixnum-min (- 0 (- 0 fixnum-min)))" | |
3179 | (eqv? fixnum-min (- 0 (- 0 fixnum-min)))) | |
3180 | (pass-if "(eqv? fixnum-num (apply - (list (apply - (list fixnum-min)))))" | |
3181 | (eqv? fixnum-min (apply - (list (apply - (list fixnum-min)))))) | |
3182 | ||
072e6de2 KR |
3183 | (pass-if "-inum - +bignum" |
3184 | (= #x-100000000000000000000000000000001 | |
ef016629 KR |
3185 | (- -1 #x100000000000000000000000000000000))) |
3186 | ||
3187 | (pass-if "big - inum" | |
3188 | (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF | |
3189 | (- #x100000000000000000000000000000000 1))) | |
3190 | ||
3191 | (pass-if "big - -inum" | |
3192 | (= #x100000000000000000000000000000001 | |
0c57673a LC |
3193 | (- #x100000000000000000000000000000000 -1))) |
3194 | ||
3195 | ;; The mininum fixnum on a 32-bit architecture: -2^29. | |
3196 | (pass-if "fixnum - fixnum = bignum (32-bit)" | |
3197 | (eqv? -536870912 (- -536870910 2))) | |
3198 | ||
3199 | ;; The minimum fixnum on a 64-bit architecture: -2^61. | |
3200 | (pass-if "fixnum - fixnum = bignum (64-bit)" | |
3201 | (eqv? -2305843009213693952 (- -2305843009213693950 2))) | |
3202 | ||
3203 | (pass-if "bignum - fixnum = fixnum" | |
3204 | (eqv? most-positive-fixnum (- (1+ most-positive-fixnum) 1)))) | |
072e6de2 | 3205 | |
f29b3454 DH |
3206 | ;;; |
3207 | ;;; * | |
3208 | ;;; | |
3209 | ||
65ea251e KR |
3210 | (with-test-prefix "*" |
3211 | ||
b5c40589 MW |
3212 | (with-test-prefix "double-negation of fixnum-min" |
3213 | (pass-if (= fixnum-min (* -1 (* -1 fixnum-min)))) | |
3214 | (pass-if (eqv? fixnum-min (* -1 (* -1 fixnum-min)))) | |
3215 | (pass-if (equal? fixnum-min (* -1 (* -1 fixnum-min)))) | |
3216 | (pass-if (= fixnum-min (* (* fixnum-min -1) -1))) | |
3217 | (pass-if (eqv? fixnum-min (* (* fixnum-min -1) -1))) | |
3218 | (pass-if (equal? fixnum-min (* (* fixnum-min -1) -1)))) | |
3219 | ||
2355f017 MW |
3220 | (with-test-prefix "signed fixnum overflow" |
3221 | (pass-if (eqv? (* 65536 65536) 4294967296)) | |
3222 | (pass-if (eqv? (* -65536 65536) -4294967296)) | |
3223 | (pass-if (eqv? (* 65536 -65536) -4294967296)) | |
3224 | (pass-if (eqv? (* -65536 -65536) 4294967296)) | |
3225 | (pass-if (eqv? (* 4294967296 4294967296) 18446744073709551616)) | |
3226 | (pass-if (eqv? (* -4294967296 4294967296) -18446744073709551616)) | |
3227 | (pass-if (eqv? (* 4294967296 -4294967296) -18446744073709551616)) | |
3228 | (pass-if (eqv? (* -4294967296 -4294967296) 18446744073709551616))) | |
3229 | ||
c7218482 MW |
3230 | (with-test-prefix "signed zeroes" |
3231 | (pass-if (eqv? +0.0 (* +0.0 +0.0))) | |
3232 | (pass-if (eqv? -0.0 (* -0.0 +0.0))) | |
3233 | (pass-if (eqv? +0.0 (* -0.0 -0.0))) | |
3234 | (pass-if (eqv? -0.0 (* +0.0 -0.0))) | |
3235 | (pass-if (eqv? +0.0+0.0i (* +i +0.0))) | |
3236 | (pass-if (eqv? +0.0-0.0i (* -i +0.0))) | |
3237 | (pass-if (eqv? -0.0-0.0i (* +i -0.0))) | |
3238 | (pass-if (eqv? -0.0+0.0i (* -i -0.0)))) | |
3239 | ||
5e791807 MW |
3240 | (with-test-prefix "exactness propagation" |
3241 | (pass-if (eqv? -0.0 (* 0 -1.0 ))) | |
3242 | (pass-if (eqv? 0.0 (* 0 1.0 ))) | |
3243 | (pass-if (eqv? -0.0 (* -1.0 0 ))) | |
3244 | (pass-if (eqv? 0.0 (* 1.0 0 ))) | |
3245 | (pass-if (eqv? 0 (* 0 1/2 ))) | |
3246 | (pass-if (eqv? 0 (* 1/2 0 ))) | |
3247 | (pass-if (eqv? 0.0+0.0i (* 0 1+i ))) | |
3248 | (pass-if (eqv? 0.0+0.0i (* 1+i 0 ))) | |
3249 | (pass-if (eqv? -1.0 (* 1 -1.0 ))) | |
3250 | (pass-if (eqv? 1.0 (* 1 1.0 ))) | |
3251 | (pass-if (eqv? -1.0 (* -1.0 1 ))) | |
3252 | (pass-if (eqv? 1.0 (* 1.0 1 ))) | |
3253 | (pass-if (eqv? 1/2 (* 1 1/2 ))) | |
3254 | (pass-if (eqv? 1/2 (* 1/2 1 ))) | |
3255 | (pass-if (eqv? 1+i (* 1 1+i ))) | |
3256 | (pass-if (eqv? 1+i (* 1+i 1 )))) | |
3257 | ||
3258 | (with-test-prefix "propagation of NaNs" | |
3259 | (pass-if (real-nan? (* +nan.0 +nan.0))) | |
3260 | (pass-if (real-nan? (* +nan.0 1 ))) | |
3261 | (pass-if (real-nan? (* +nan.0 -1 ))) | |
3262 | (pass-if (real-nan? (* +nan.0 -7/2 ))) | |
3263 | (pass-if (real-nan? (* +nan.0 1e20 ))) | |
3264 | (pass-if (real-nan? (* 1 +nan.0))) | |
3265 | (pass-if (real-nan? (* -1 +nan.0))) | |
3266 | (pass-if (real-nan? (* -7/2 +nan.0))) | |
3267 | (pass-if (real-nan? (* 1e20 +nan.0))) | |
3268 | (pass-if (real-nan? (* +inf.0 +nan.0))) | |
3269 | (pass-if (real-nan? (* +nan.0 +inf.0))) | |
3270 | (pass-if (real-nan? (* -inf.0 +nan.0))) | |
3271 | (pass-if (real-nan? (* +nan.0 -inf.0))) | |
3272 | (pass-if (real-nan? (* (* fixnum-max 2) +nan.0))) | |
3273 | (pass-if (real-nan? (* +nan.0 (* fixnum-max 2)))) | |
3274 | ||
c7218482 MW |
3275 | (pass-if (real-nan? (* 0 +nan.0 ))) |
3276 | (pass-if (real-nan? (* +nan.0 0 ))) | |
3277 | (pass-if (almost-real-nan? (* 0 +nan.0+i))) | |
3278 | (pass-if (almost-real-nan? (* +nan.0+i 0 ))) | |
5e791807 MW |
3279 | |
3280 | (pass-if (imaginary-nan? (* 0 +nan.0i ))) | |
3281 | (pass-if (imaginary-nan? (* +nan.0i 0 ))) | |
3282 | (pass-if (imaginary-nan? (* 0 1+nan.0i ))) | |
3283 | (pass-if (imaginary-nan? (* 1+nan.0i 0 ))) | |
3284 | ||
3285 | (pass-if (complex-nan? (* 0 +nan.0+nan.0i ))) | |
3286 | (pass-if (complex-nan? (* +nan.0+nan.0i 0 )))) | |
3287 | ||
3288 | (with-test-prefix "infinities" | |
3289 | (pass-if (eqv? +inf.0 (* +inf.0 5 ))) | |
3290 | (pass-if (eqv? -inf.0 (* +inf.0 -5 ))) | |
3291 | (pass-if (eqv? +inf.0 (* +inf.0 73.1))) | |
3292 | (pass-if (eqv? -inf.0 (* +inf.0 -9.2))) | |
3293 | (pass-if (eqv? +inf.0 (* +inf.0 5/2))) | |
3294 | (pass-if (eqv? -inf.0 (* +inf.0 -5/2))) | |
3295 | (pass-if (eqv? -inf.0 (* -5 +inf.0))) | |
3296 | (pass-if (eqv? +inf.0 (* 73.1 +inf.0))) | |
3297 | (pass-if (eqv? -inf.0 (* -9.2 +inf.0))) | |
3298 | (pass-if (eqv? +inf.0 (* 5/2 +inf.0))) | |
3299 | (pass-if (eqv? -inf.0 (* -5/2 +inf.0))) | |
3300 | ||
3301 | (pass-if (eqv? -inf.0 (* -inf.0 5 ))) | |
3302 | (pass-if (eqv? +inf.0 (* -inf.0 -5 ))) | |
3303 | (pass-if (eqv? -inf.0 (* -inf.0 73.1))) | |
3304 | (pass-if (eqv? +inf.0 (* -inf.0 -9.2))) | |
3305 | (pass-if (eqv? -inf.0 (* -inf.0 5/2))) | |
3306 | (pass-if (eqv? +inf.0 (* -inf.0 -5/2))) | |
3307 | (pass-if (eqv? +inf.0 (* -5 -inf.0))) | |
3308 | (pass-if (eqv? -inf.0 (* 73.1 -inf.0))) | |
3309 | (pass-if (eqv? +inf.0 (* -9.2 -inf.0))) | |
3310 | (pass-if (eqv? -inf.0 (* 5/2 -inf.0))) | |
3311 | (pass-if (eqv? +inf.0 (* -5/2 -inf.0))) | |
3312 | ||
3313 | (pass-if (real-nan? (* 0.0 +inf.0))) | |
3314 | (pass-if (real-nan? (* -0.0 +inf.0))) | |
3315 | (pass-if (real-nan? (* +inf.0 0.0))) | |
3316 | (pass-if (real-nan? (* +inf.0 -0.0))) | |
3317 | ||
3318 | (pass-if (real-nan? (* 0.0 -inf.0))) | |
3319 | (pass-if (real-nan? (* -0.0 -inf.0))) | |
3320 | (pass-if (real-nan? (* -inf.0 0.0))) | |
3321 | (pass-if (real-nan? (* -inf.0 -0.0))) | |
3322 | ||
3323 | (pass-if (real-nan? (* 0 +inf.0 ))) | |
3324 | (pass-if (real-nan? (* +inf.0 0 ))) | |
5e791807 MW |
3325 | (pass-if (real-nan? (* 0 -inf.0 ))) |
3326 | (pass-if (real-nan? (* -inf.0 0 ))) | |
c7218482 MW |
3327 | |
3328 | (pass-if (almost-real-nan? (* 0 +inf.0+i))) | |
3329 | (pass-if (almost-real-nan? (* +inf.0+i 0 ))) | |
3330 | (pass-if (almost-real-nan? (* 0 -inf.0+i))) | |
3331 | (pass-if (almost-real-nan? (* -inf.0+i 0 ))) | |
5e791807 MW |
3332 | |
3333 | (pass-if (imaginary-nan? (* 0 +inf.0i ))) | |
3334 | (pass-if (imaginary-nan? (* +inf.0i 0 ))) | |
3335 | (pass-if (imaginary-nan? (* 0 1+inf.0i ))) | |
3336 | (pass-if (imaginary-nan? (* 1+inf.0i 0 ))) | |
3337 | ||
3338 | (pass-if (imaginary-nan? (* 0 -inf.0i ))) | |
3339 | (pass-if (imaginary-nan? (* -inf.0i 0 ))) | |
3340 | (pass-if (imaginary-nan? (* 0 1-inf.0i ))) | |
3341 | (pass-if (imaginary-nan? (* 1-inf.0i 0 ))) | |
3342 | ||
3343 | (pass-if (complex-nan? (* 0 +inf.0+inf.0i ))) | |
3344 | (pass-if (complex-nan? (* +inf.0+inf.0i 0 ))) | |
3345 | ||
3346 | (pass-if (complex-nan? (* 0 +inf.0-inf.0i ))) | |
3347 | (pass-if (complex-nan? (* -inf.0+inf.0i 0 )))) | |
3348 | ||
23d72566 KR |
3349 | (with-test-prefix "inum * bignum" |
3350 | ||
3351 | (pass-if "0 * 2^256 = 0" | |
3352 | (eqv? 0 (* 0 (ash 1 256))))) | |
3353 | ||
3354 | (with-test-prefix "inum * flonum" | |
3355 | ||
5e791807 MW |
3356 | (pass-if "0 * 1.0 = 0.0" |
3357 | (eqv? 0.0 (* 0 1.0)))) | |
23d72566 KR |
3358 | |
3359 | (with-test-prefix "inum * complex" | |
3360 | ||
5e791807 MW |
3361 | (pass-if "0 * 1+1i = 0.0+0.0i" |
3362 | (eqv? 0.0+0.0i (* 0 1+1i)))) | |
23d72566 KR |
3363 | |
3364 | (with-test-prefix "inum * frac" | |
3365 | ||
3366 | (pass-if "0 * 2/3 = 0" | |
3367 | (eqv? 0 (* 0 2/3)))) | |
3368 | ||
3369 | (with-test-prefix "bignum * inum" | |
3370 | ||
3371 | (pass-if "2^256 * 0 = 0" | |
3372 | (eqv? 0 (* (ash 1 256) 0)))) | |
3373 | ||
3374 | (with-test-prefix "flonum * inum" | |
5e791807 MW |
3375 | (pass-if "1.0 * 0 = 0.0" |
3376 | (eqv? 0.0 (* 1.0 0)))) | |
23d72566 KR |
3377 | |
3378 | (with-test-prefix "complex * inum" | |
5e791807 MW |
3379 | (pass-if "1+1i * 0 = 0.0+0.0i" |
3380 | (eqv? 0.0+0.0i (* 1+1i 0)))) | |
23d72566 | 3381 | |
65ea251e KR |
3382 | (pass-if "complex * bignum" |
3383 | (let ((big (ash 1 90))) | |
3384 | (= (make-rectangular big big) | |
23d72566 KR |
3385 | (* 1+1i big)))) |
3386 | ||
3387 | (with-test-prefix "frac * inum" | |
3388 | ||
3389 | (pass-if "2/3 * 0 = 0" | |
3390 | (eqv? 0 (* 2/3 0))))) | |
65ea251e | 3391 | |
f29b3454 DH |
3392 | ;;; |
3393 | ;;; / | |
3394 | ;;; | |
3395 | ||
1b3a7932 DH |
3396 | (with-test-prefix "/" |
3397 | ||
b5c40589 MW |
3398 | (with-test-prefix "double-negation of fixnum-min" |
3399 | (pass-if (= fixnum-min (/ (/ fixnum-min -1) -1))) | |
3400 | (pass-if (eqv? fixnum-min (/ (/ fixnum-min -1) -1))) | |
3401 | (pass-if (equal? fixnum-min (/ (/ fixnum-min -1) -1)))) | |
3402 | ||
d389e966 | 3403 | (pass-if "documented?" |
1b3a7932 DH |
3404 | (documented? /)) |
3405 | ||
3406 | (with-test-prefix "division by zero" | |
3407 | ||
3408 | (pass-if-exception "(/ 0)" | |
2f359170 | 3409 | exception:numerical-overflow |
1b3a7932 DH |
3410 | (/ 0)) |
3411 | ||
cdf52e3d MV |
3412 | (pass-if "(/ 0.0)" |
3413 | (= +inf.0 (/ 0.0))) | |
80074d77 | 3414 | |
1b3a7932 | 3415 | (pass-if-exception "(/ 1 0)" |
2f359170 | 3416 | exception:numerical-overflow |
80074d77 DH |
3417 | (/ 1 0)) |
3418 | ||
cdf52e3d MV |
3419 | (pass-if "(/ 1 0.0)" |
3420 | (= +inf.0 (/ 1 0.0))) | |
80074d77 DH |
3421 | |
3422 | (pass-if-exception "(/ bignum 0)" | |
2f359170 | 3423 | exception:numerical-overflow |
80074d77 DH |
3424 | (/ (+ fixnum-max 1) 0)) |
3425 | ||
cdf52e3d MV |
3426 | (pass-if "(/ bignum 0.0)" |
3427 | (= +inf.0 (/ (+ fixnum-max 1) 0.0))) | |
80074d77 DH |
3428 | |
3429 | (pass-if-exception "(/ 1.0 0)" | |
2f359170 | 3430 | exception:numerical-overflow |
80074d77 DH |
3431 | (/ 1.0 0)) |
3432 | ||
cdf52e3d MV |
3433 | (pass-if "(/ 1.0 0.0)" |
3434 | (= +inf.0 (/ 1.0 0.0))) | |
80074d77 DH |
3435 | |
3436 | (pass-if-exception "(/ +i 0)" | |
2f359170 | 3437 | exception:numerical-overflow |
80074d77 DH |
3438 | (/ +i 0)) |
3439 | ||
cdf52e3d MV |
3440 | (pass-if "(/ +i 0.0)" |
3441 | (= +inf.0 (imag-part (/ +i 0.0))))) | |
469b963c | 3442 | |
2f359170 KR |
3443 | (with-test-prefix "1/complex" |
3444 | ||
3445 | (pass-if "0+1i" | |
3446 | (eqv? 0-1i (/ 0+1i))) | |
3447 | ||
3448 | ;; in guile 1.6 through 1.6.7 this incorrectly resulted in nans | |
3449 | (pass-if "0-1i" | |
3450 | (eqv? 0+1i (/ 0-1i))) | |
3451 | ||
3452 | (pass-if "1+1i" | |
3453 | (eqv? 0.5-0.5i (/ 1+1i))) | |
3454 | ||
3455 | (pass-if "1-1i" | |
3456 | (eqv? 0.5+0.5i (/ 1-1i))) | |
3457 | ||
3458 | (pass-if "-1+1i" | |
3459 | (eqv? -0.5-0.5i (/ -1+1i))) | |
3460 | ||
3461 | (pass-if "-1-1i" | |
3462 | (eqv? -0.5+0.5i (/ -1-1i))) | |
469b963c MV |
3463 | |
3464 | (pass-if "(/ 3+4i)" | |
3465 | (= (/ 3+4i) 0.12-0.16i)) | |
3466 | ||
3467 | (pass-if "(/ 4+3i)" | |
3468 | (= (/ 4+3i) 0.16-0.12i)) | |
3469 | ||
2f359170 KR |
3470 | (pass-if "(/ 1e200+1e200i)" |
3471 | (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i))) | |
469b963c | 3472 | |
2f359170 | 3473 | (with-test-prefix "inum/complex" |
469b963c MV |
3474 | |
3475 | (pass-if "(/ 25 3+4i)" | |
3476 | (= (/ 25 3+4i) 3.0-4.0i)) | |
3477 | ||
3478 | (pass-if "(/ 25 4+3i)" | |
2f359170 | 3479 | (= (/ 25 4+3i) 4.0-3.0i))) |
469b963c | 3480 | |
2f359170 KR |
3481 | (with-test-prefix "complex/complex" |
3482 | ||
3483 | (pass-if "(/ 25+125i 3+4i)" | |
3484 | (= (/ 25+125i 3+4i) 23.0+11.0i)) | |
3485 | ||
3486 | (pass-if "(/ 25+125i 4+3i)" | |
3487 | (= (/ 25+125i 4+3i) 19.0+17.0i)))) | |
1b3a7932 | 3488 | |
8b56bcec MW |
3489 | ;;; |
3490 | ;;; floor | |
3491 | ;;; | |
3492 | ||
3493 | (with-test-prefix "floor" | |
3494 | (pass-if (= 1 (floor 1.75))) | |
3495 | (pass-if (= 1 (floor 1.5))) | |
3496 | (pass-if (= 1 (floor 1.25))) | |
3497 | (pass-if (= 0 (floor 0.75))) | |
3498 | (pass-if (= 0 (floor 0.5))) | |
3499 | (pass-if (= 0 (floor 0.0))) | |
3500 | (pass-if (= -1 (floor -0.5))) | |
3501 | (pass-if (= -2 (floor -1.25))) | |
3502 | (pass-if (= -2 (floor -1.5))) | |
3503 | ||
3504 | (with-test-prefix "inum" | |
3505 | (pass-if "0" | |
3506 | (and (= 0 (floor 0)) | |
3507 | (exact? (floor 0)))) | |
3508 | ||
3509 | (pass-if "1" | |
3510 | (and (= 1 (floor 1)) | |
3511 | (exact? (floor 1)))) | |
3512 | ||
3513 | (pass-if "-1" | |
3514 | (and (= -1 (floor -1)) | |
3515 | (exact? (floor -1))))) | |
3516 | ||
3517 | (with-test-prefix "bignum" | |
3518 | (let ((x (1+ most-positive-fixnum))) | |
3519 | (pass-if "(1+ most-positive-fixnum)" | |
3520 | (and (= x (floor x)) | |
3521 | (exact? (floor x))))) | |
3522 | ||
3523 | (let ((x (1- most-negative-fixnum))) | |
3524 | (pass-if "(1- most-negative-fixnum)" | |
3525 | (and (= x (floor x)) | |
3526 | (exact? (floor x)))))) | |
3527 | ||
3528 | (with-test-prefix "frac" | |
3529 | (define (=exact x y) | |
3530 | (and (= x y) | |
3531 | (exact? y))) | |
3532 | ||
3533 | (pass-if (=exact -3 (floor -7/3))) | |
3534 | (pass-if (=exact -2 (floor -5/3))) | |
3535 | (pass-if (=exact -2 (floor -4/3))) | |
3536 | (pass-if (=exact -1 (floor -2/3))) | |
3537 | (pass-if (=exact -1 (floor -1/3))) | |
3538 | (pass-if (=exact 0 (floor 1/3))) | |
3539 | (pass-if (=exact 0 (floor 2/3))) | |
3540 | (pass-if (=exact 1 (floor 4/3))) | |
3541 | (pass-if (=exact 1 (floor 5/3))) | |
3542 | (pass-if (=exact 2 (floor 7/3))) | |
3543 | ||
3544 | (pass-if (=exact -3 (floor -17/6))) | |
3545 | (pass-if (=exact -3 (floor -16/6))) | |
3546 | (pass-if (=exact -3 (floor -15/6))) | |
3547 | (pass-if (=exact -3 (floor -14/6))) | |
3548 | (pass-if (=exact -3 (floor -13/6))) | |
3549 | (pass-if (=exact -2 (floor -11/6))) | |
3550 | (pass-if (=exact -2 (floor -10/6))) | |
3551 | (pass-if (=exact -2 (floor -9/6))) | |
3552 | (pass-if (=exact -2 (floor -8/6))) | |
3553 | (pass-if (=exact -2 (floor -7/6))) | |
3554 | (pass-if (=exact -1 (floor -5/6))) | |
3555 | (pass-if (=exact -1 (floor -4/6))) | |
3556 | (pass-if (=exact -1 (floor -3/6))) | |
3557 | (pass-if (=exact -1 (floor -2/6))) | |
3558 | (pass-if (=exact -1 (floor -1/6))) | |
3559 | (pass-if (=exact 0 (floor 1/6))) | |
3560 | (pass-if (=exact 0 (floor 2/6))) | |
3561 | (pass-if (=exact 0 (floor 3/6))) | |
3562 | (pass-if (=exact 0 (floor 4/6))) | |
3563 | (pass-if (=exact 0 (floor 5/6))) | |
3564 | (pass-if (=exact 1 (floor 7/6))) | |
3565 | (pass-if (=exact 1 (floor 8/6))) | |
3566 | (pass-if (=exact 1 (floor 9/6))) | |
3567 | (pass-if (=exact 1 (floor 10/6))) | |
3568 | (pass-if (=exact 1 (floor 11/6))) | |
3569 | (pass-if (=exact 2 (floor 13/6))) | |
3570 | (pass-if (=exact 2 (floor 14/6))) | |
3571 | (pass-if (=exact 2 (floor 15/6))) | |
3572 | (pass-if (=exact 2 (floor 16/6))) | |
3573 | (pass-if (=exact 2 (floor 17/6)))) | |
3574 | ||
3575 | (with-test-prefix "real" | |
3576 | (pass-if "0.0" | |
3577 | (and (= 0.0 (floor 0.0)) | |
3578 | (inexact? (floor 0.0)))) | |
3579 | ||
3580 | (pass-if "1.0" | |
3581 | (and (= 1.0 (floor 1.0)) | |
3582 | (inexact? (floor 1.0)))) | |
3583 | ||
3584 | (pass-if "-1.0" | |
3585 | (and (= -1.0 (floor -1.0)) | |
3586 | (inexact? (floor -1.0)))) | |
3587 | ||
3588 | (pass-if "-3.1" | |
3589 | (and (= -4.0 (floor -3.1)) | |
3590 | (inexact? (floor -3.1)))) | |
3591 | ||
3592 | (pass-if "3.1" | |
3593 | (and (= 3.0 (floor 3.1)) | |
3594 | (inexact? (floor 3.1)))) | |
3595 | ||
3596 | (pass-if "3.9" | |
3597 | (and (= 3.0 (floor 3.9)) | |
3598 | (inexact? (floor 3.9)))) | |
3599 | ||
3600 | (pass-if "-3.9" | |
3601 | (and (= -4.0 (floor -3.9)) | |
3602 | (inexact? (floor -3.9)))) | |
3603 | ||
3604 | (pass-if "1.5" | |
3605 | (and (= 1.0 (floor 1.5)) | |
3606 | (inexact? (floor 1.5)))) | |
3607 | ||
3608 | (pass-if "2.5" | |
3609 | (and (= 2.0 (floor 2.5)) | |
3610 | (inexact? (floor 2.5)))) | |
3611 | ||
3612 | (pass-if "3.5" | |
3613 | (and (= 3.0 (floor 3.5)) | |
3614 | (inexact? (floor 3.5)))) | |
3615 | ||
3616 | (pass-if "-1.5" | |
3617 | (and (= -2.0 (floor -1.5)) | |
3618 | (inexact? (floor -1.5)))) | |
3619 | ||
3620 | (pass-if "-2.5" | |
3621 | (and (= -3.0 (floor -2.5)) | |
3622 | (inexact? (floor -2.5)))) | |
3623 | ||
3624 | (pass-if "-3.5" | |
3625 | (and (= -4.0 (floor -3.5)) | |
3626 | (inexact? (floor -3.5)))))) | |
3627 | ||
3628 | ;;; | |
3629 | ;;; ceiling | |
3630 | ;;; | |
3631 | ||
3632 | (with-test-prefix "ceiling" | |
3633 | (pass-if (= 2 (ceiling 1.75))) | |
3634 | (pass-if (= 2 (ceiling 1.5))) | |
3635 | (pass-if (= 2 (ceiling 1.25))) | |
3636 | (pass-if (= 1 (ceiling 0.75))) | |
3637 | (pass-if (= 1 (ceiling 0.5))) | |
3638 | (pass-if (= 0 (ceiling 0.0))) | |
3639 | (pass-if (= 0 (ceiling -0.5))) | |
3640 | (pass-if (= -1 (ceiling -1.25))) | |
3641 | (pass-if (= -1 (ceiling -1.5))) | |
3642 | ||
3643 | (with-test-prefix "inum" | |
3644 | (pass-if "0" | |
3645 | (and (= 0 (ceiling 0)) | |
3646 | (exact? (ceiling 0)))) | |
3647 | ||
3648 | (pass-if "1" | |
3649 | (and (= 1 (ceiling 1)) | |
3650 | (exact? (ceiling 1)))) | |
3651 | ||
3652 | (pass-if "-1" | |
3653 | (and (= -1 (ceiling -1)) | |
3654 | (exact? (ceiling -1))))) | |
3655 | ||
3656 | (with-test-prefix "bignum" | |
3657 | (let ((x (1+ most-positive-fixnum))) | |
3658 | (pass-if "(1+ most-positive-fixnum)" | |
3659 | (and (= x (ceiling x)) | |
3660 | (exact? (ceiling x))))) | |
3661 | ||
3662 | (let ((x (1- most-negative-fixnum))) | |
3663 | (pass-if "(1- most-negative-fixnum)" | |
3664 | (and (= x (ceiling x)) | |
3665 | (exact? (ceiling x)))))) | |
3666 | ||
3667 | (with-test-prefix "frac" | |
3668 | (define (=exact x y) | |
3669 | (and (= x y) | |
3670 | (exact? y))) | |
3671 | ||
3672 | (pass-if (=exact -2 (ceiling -7/3))) | |
3673 | (pass-if (=exact -1 (ceiling -5/3))) | |
3674 | (pass-if (=exact -1 (ceiling -4/3))) | |
3675 | (pass-if (=exact 0 (ceiling -2/3))) | |
3676 | (pass-if (=exact 0 (ceiling -1/3))) | |
3677 | (pass-if (=exact 1 (ceiling 1/3))) | |
3678 | (pass-if (=exact 1 (ceiling 2/3))) | |
3679 | (pass-if (=exact 2 (ceiling 4/3))) | |
3680 | (pass-if (=exact 2 (ceiling 5/3))) | |
3681 | (pass-if (=exact 3 (ceiling 7/3))) | |
3682 | ||
3683 | (pass-if (=exact -2 (ceiling -17/6))) | |
3684 | (pass-if (=exact -2 (ceiling -16/6))) | |
3685 | (pass-if (=exact -2 (ceiling -15/6))) | |
3686 | (pass-if (=exact -2 (ceiling -14/6))) | |
3687 | (pass-if (=exact -2 (ceiling -13/6))) | |
3688 | (pass-if (=exact -1 (ceiling -11/6))) | |
3689 | (pass-if (=exact -1 (ceiling -10/6))) | |
3690 | (pass-if (=exact -1 (ceiling -9/6))) | |
3691 | (pass-if (=exact -1 (ceiling -8/6))) | |
3692 | (pass-if (=exact -1 (ceiling -7/6))) | |
3693 | (pass-if (=exact 0 (ceiling -5/6))) | |
3694 | (pass-if (=exact 0 (ceiling -4/6))) | |
3695 | (pass-if (=exact 0 (ceiling -3/6))) | |
3696 | (pass-if (=exact 0 (ceiling -2/6))) | |
3697 | (pass-if (=exact 0 (ceiling -1/6))) | |
3698 | (pass-if (=exact 1 (ceiling 1/6))) | |
3699 | (pass-if (=exact 1 (ceiling 2/6))) | |
3700 | (pass-if (=exact 1 (ceiling 3/6))) | |
3701 | (pass-if (=exact 1 (ceiling 4/6))) | |
3702 | (pass-if (=exact 1 (ceiling 5/6))) | |
3703 | (pass-if (=exact 2 (ceiling 7/6))) | |
3704 | (pass-if (=exact 2 (ceiling 8/6))) | |
3705 | (pass-if (=exact 2 (ceiling 9/6))) | |
3706 | (pass-if (=exact 2 (ceiling 10/6))) | |
3707 | (pass-if (=exact 2 (ceiling 11/6))) | |
3708 | (pass-if (=exact 3 (ceiling 13/6))) | |
3709 | (pass-if (=exact 3 (ceiling 14/6))) | |
3710 | (pass-if (=exact 3 (ceiling 15/6))) | |
3711 | (pass-if (=exact 3 (ceiling 16/6))) | |
3712 | (pass-if (=exact 3 (ceiling 17/6)))) | |
3713 | ||
3714 | (with-test-prefix "real" | |
3715 | (pass-if "0.0" | |
3716 | (and (= 0.0 (ceiling 0.0)) | |
3717 | (inexact? (ceiling 0.0)))) | |
3718 | ||
3719 | (pass-if "1.0" | |
3720 | (and (= 1.0 (ceiling 1.0)) | |
3721 | (inexact? (ceiling 1.0)))) | |
3722 | ||
3723 | (pass-if "-1.0" | |
3724 | (and (= -1.0 (ceiling -1.0)) | |
3725 | (inexact? (ceiling -1.0)))) | |
3726 | ||
3727 | (pass-if "-3.1" | |
3728 | (and (= -3.0 (ceiling -3.1)) | |
3729 | (inexact? (ceiling -3.1)))) | |
3730 | ||
3731 | (pass-if "3.1" | |
3732 | (and (= 4.0 (ceiling 3.1)) | |
3733 | (inexact? (ceiling 3.1)))) | |
3734 | ||
3735 | (pass-if "3.9" | |
3736 | (and (= 4.0 (ceiling 3.9)) | |
3737 | (inexact? (ceiling 3.9)))) | |
3738 | ||
3739 | (pass-if "-3.9" | |
3740 | (and (= -3.0 (ceiling -3.9)) | |
3741 | (inexact? (ceiling -3.9)))) | |
3742 | ||
3743 | (pass-if "1.5" | |
3744 | (and (= 2.0 (ceiling 1.5)) | |
3745 | (inexact? (ceiling 1.5)))) | |
3746 | ||
3747 | (pass-if "2.5" | |
3748 | (and (= 3.0 (ceiling 2.5)) | |
3749 | (inexact? (ceiling 2.5)))) | |
3750 | ||
3751 | (pass-if "3.5" | |
3752 | (and (= 4.0 (ceiling 3.5)) | |
3753 | (inexact? (ceiling 3.5)))) | |
3754 | ||
3755 | (pass-if "-1.5" | |
3756 | (and (= -1.0 (ceiling -1.5)) | |
3757 | (inexact? (ceiling -1.5)))) | |
3758 | ||
3759 | (pass-if "-2.5" | |
3760 | (and (= -2.0 (ceiling -2.5)) | |
3761 | (inexact? (ceiling -2.5)))) | |
3762 | ||
3763 | (pass-if "-3.5" | |
3764 | (and (= -3.0 (ceiling -3.5)) | |
3765 | (inexact? (ceiling -3.5)))))) | |
3766 | ||
f29b3454 DH |
3767 | ;;; |
3768 | ;;; truncate | |
3769 | ;;; | |
3770 | ||
14a6784c KR |
3771 | (with-test-prefix "truncate" |
3772 | (pass-if (= 1 (truncate 1.75))) | |
3773 | (pass-if (= 1 (truncate 1.5))) | |
3774 | (pass-if (= 1 (truncate 1.25))) | |
3775 | (pass-if (= 0 (truncate 0.75))) | |
3776 | (pass-if (= 0 (truncate 0.5))) | |
3777 | (pass-if (= 0 (truncate 0.0))) | |
3778 | (pass-if (= 0 (truncate -0.5))) | |
3779 | (pass-if (= -1 (truncate -1.25))) | |
8b56bcec MW |
3780 | (pass-if (= -1 (truncate -1.5))) |
3781 | ||
3782 | (with-test-prefix "inum" | |
3783 | (pass-if "0" | |
3784 | (and (= 0 (truncate 0)) | |
3785 | (exact? (truncate 0)))) | |
3786 | ||
3787 | (pass-if "1" | |
3788 | (and (= 1 (truncate 1)) | |
3789 | (exact? (truncate 1)))) | |
3790 | ||
3791 | (pass-if "-1" | |
3792 | (and (= -1 (truncate -1)) | |
3793 | (exact? (truncate -1))))) | |
3794 | ||
3795 | (with-test-prefix "bignum" | |
3796 | (let ((x (1+ most-positive-fixnum))) | |
3797 | (pass-if "(1+ most-positive-fixnum)" | |
3798 | (and (= x (truncate x)) | |
3799 | (exact? (truncate x))))) | |
3800 | ||
3801 | (let ((x (1- most-negative-fixnum))) | |
3802 | (pass-if "(1- most-negative-fixnum)" | |
3803 | (and (= x (truncate x)) | |
3804 | (exact? (truncate x)))))) | |
3805 | ||
3806 | (with-test-prefix "frac" | |
3807 | (define (=exact x y) | |
3808 | (and (= x y) | |
3809 | (exact? y))) | |
3810 | ||
3811 | (pass-if (=exact -2 (truncate -7/3))) | |
3812 | (pass-if (=exact -1 (truncate -5/3))) | |
3813 | (pass-if (=exact -1 (truncate -4/3))) | |
3814 | (pass-if (=exact 0 (truncate -2/3))) | |
3815 | (pass-if (=exact 0 (truncate -1/3))) | |
3816 | (pass-if (=exact 0 (truncate 1/3))) | |
3817 | (pass-if (=exact 0 (truncate 2/3))) | |
3818 | (pass-if (=exact 1 (truncate 4/3))) | |
3819 | (pass-if (=exact 1 (truncate 5/3))) | |
3820 | (pass-if (=exact 2 (truncate 7/3))) | |
3821 | ||
3822 | (pass-if (=exact -2 (truncate -17/6))) | |
3823 | (pass-if (=exact -2 (truncate -16/6))) | |
3824 | (pass-if (=exact -2 (truncate -15/6))) | |
3825 | (pass-if (=exact -2 (truncate -14/6))) | |
3826 | (pass-if (=exact -2 (truncate -13/6))) | |
3827 | (pass-if (=exact -1 (truncate -11/6))) | |
3828 | (pass-if (=exact -1 (truncate -10/6))) | |
3829 | (pass-if (=exact -1 (truncate -9/6))) | |
3830 | (pass-if (=exact -1 (truncate -8/6))) | |
3831 | (pass-if (=exact -1 (truncate -7/6))) | |
3832 | (pass-if (=exact 0 (truncate -5/6))) | |
3833 | (pass-if (=exact 0 (truncate -4/6))) | |
3834 | (pass-if (=exact 0 (truncate -3/6))) | |
3835 | (pass-if (=exact 0 (truncate -2/6))) | |
3836 | (pass-if (=exact 0 (truncate -1/6))) | |
3837 | (pass-if (=exact 0 (truncate 1/6))) | |
3838 | (pass-if (=exact 0 (truncate 2/6))) | |
3839 | (pass-if (=exact 0 (truncate 3/6))) | |
3840 | (pass-if (=exact 0 (truncate 4/6))) | |
3841 | (pass-if (=exact 0 (truncate 5/6))) | |
3842 | (pass-if (=exact 1 (truncate 7/6))) | |
3843 | (pass-if (=exact 1 (truncate 8/6))) | |
3844 | (pass-if (=exact 1 (truncate 9/6))) | |
3845 | (pass-if (=exact 1 (truncate 10/6))) | |
3846 | (pass-if (=exact 1 (truncate 11/6))) | |
3847 | (pass-if (=exact 2 (truncate 13/6))) | |
3848 | (pass-if (=exact 2 (truncate 14/6))) | |
3849 | (pass-if (=exact 2 (truncate 15/6))) | |
3850 | (pass-if (=exact 2 (truncate 16/6))) | |
3851 | (pass-if (=exact 2 (truncate 17/6)))) | |
3852 | ||
3853 | (with-test-prefix "real" | |
3854 | (pass-if "0.0" | |
3855 | (and (= 0.0 (truncate 0.0)) | |
3856 | (inexact? (truncate 0.0)))) | |
3857 | ||
3858 | (pass-if "1.0" | |
3859 | (and (= 1.0 (truncate 1.0)) | |
3860 | (inexact? (truncate 1.0)))) | |
3861 | ||
3862 | (pass-if "-1.0" | |
3863 | (and (= -1.0 (truncate -1.0)) | |
3864 | (inexact? (truncate -1.0)))) | |
3865 | ||
3866 | (pass-if "-3.1" | |
3867 | (and (= -3.0 (truncate -3.1)) | |
3868 | (inexact? (truncate -3.1)))) | |
3869 | ||
3870 | (pass-if "3.1" | |
3871 | (and (= 3.0 (truncate 3.1)) | |
3872 | (inexact? (truncate 3.1)))) | |
3873 | ||
3874 | (pass-if "3.9" | |
3875 | (and (= 3.0 (truncate 3.9)) | |
3876 | (inexact? (truncate 3.9)))) | |
3877 | ||
3878 | (pass-if "-3.9" | |
3879 | (and (= -3.0 (truncate -3.9)) | |
3880 | (inexact? (truncate -3.9)))) | |
3881 | ||
3882 | (pass-if "1.5" | |
3883 | (and (= 1.0 (truncate 1.5)) | |
3884 | (inexact? (truncate 1.5)))) | |
3885 | ||
3886 | (pass-if "2.5" | |
3887 | (and (= 2.0 (truncate 2.5)) | |
3888 | (inexact? (truncate 2.5)))) | |
3889 | ||
3890 | (pass-if "3.5" | |
3891 | (and (= 3.0 (truncate 3.5)) | |
3892 | (inexact? (truncate 3.5)))) | |
3893 | ||
3894 | (pass-if "-1.5" | |
3895 | (and (= -1.0 (truncate -1.5)) | |
3896 | (inexact? (truncate -1.5)))) | |
3897 | ||
3898 | (pass-if "-2.5" | |
3899 | (and (= -2.0 (truncate -2.5)) | |
3900 | (inexact? (truncate -2.5)))) | |
3901 | ||
3902 | (pass-if "-3.5" | |
3903 | (and (= -3.0 (truncate -3.5)) | |
3904 | (inexact? (truncate -3.5)))))) | |
14a6784c | 3905 | |
f29b3454 DH |
3906 | ;;; |
3907 | ;;; round | |
3908 | ;;; | |
3909 | ||
14a6784c KR |
3910 | (with-test-prefix "round" |
3911 | (pass-if (= 2 (round 1.75))) | |
3912 | (pass-if (= 2 (round 1.5))) | |
3913 | (pass-if (= 1 (round 1.25))) | |
3914 | (pass-if (= 1 (round 0.75))) | |
3915 | (pass-if (= 0 (round 0.5))) | |
3916 | (pass-if (= 0 (round 0.0))) | |
3917 | (pass-if (= 0 (round -0.5))) | |
3918 | (pass-if (= -1 (round -1.25))) | |
abff733b KR |
3919 | (pass-if (= -2 (round -1.5))) |
3920 | ||
3921 | (with-test-prefix "inum" | |
3922 | (pass-if "0" | |
3923 | (and (= 0 (round 0)) | |
3924 | (exact? (round 0)))) | |
3925 | ||
3926 | (pass-if "1" | |
3927 | (and (= 1 (round 1)) | |
3928 | (exact? (round 1)))) | |
3929 | ||
3930 | (pass-if "-1" | |
3931 | (and (= -1 (round -1)) | |
3932 | (exact? (round -1))))) | |
3933 | ||
3934 | (with-test-prefix "bignum" | |
3935 | (let ((x (1+ most-positive-fixnum))) | |
3936 | (pass-if "(1+ most-positive-fixnum)" | |
3937 | (and (= x (round x)) | |
3938 | (exact? (round x))))) | |
3939 | ||
3940 | (let ((x (1- most-negative-fixnum))) | |
3941 | (pass-if "(1- most-negative-fixnum)" | |
3942 | (and (= x (round x)) | |
3943 | (exact? (round x)))))) | |
3944 | ||
6203b5f5 KR |
3945 | (with-test-prefix "frac" |
3946 | (define (=exact x y) | |
3947 | (and (= x y) | |
3948 | (exact? y))) | |
3949 | ||
3950 | (pass-if (=exact -2 (round -7/3))) | |
3951 | (pass-if (=exact -2 (round -5/3))) | |
3952 | (pass-if (=exact -1 (round -4/3))) | |
3953 | (pass-if (=exact -1 (round -2/3))) | |
3954 | (pass-if (=exact 0 (round -1/3))) | |
3955 | (pass-if (=exact 0 (round 1/3))) | |
3956 | (pass-if (=exact 1 (round 2/3))) | |
3957 | (pass-if (=exact 1 (round 4/3))) | |
3958 | (pass-if (=exact 2 (round 5/3))) | |
3959 | (pass-if (=exact 2 (round 7/3))) | |
3960 | ||
3961 | (pass-if (=exact -3 (round -17/6))) | |
3962 | (pass-if (=exact -3 (round -16/6))) | |
3963 | (pass-if (=exact -2 (round -15/6))) | |
3964 | (pass-if (=exact -2 (round -14/6))) | |
3965 | (pass-if (=exact -2 (round -13/6))) | |
3966 | (pass-if (=exact -2 (round -11/6))) | |
3967 | (pass-if (=exact -2 (round -10/6))) | |
3968 | (pass-if (=exact -2 (round -9/6))) | |
3969 | (pass-if (=exact -1 (round -8/6))) | |
3970 | (pass-if (=exact -1 (round -7/6))) | |
3971 | (pass-if (=exact -1 (round -5/6))) | |
3972 | (pass-if (=exact -1 (round -4/6))) | |
3973 | (pass-if (=exact 0 (round -3/6))) | |
3974 | (pass-if (=exact 0 (round -2/6))) | |
3975 | (pass-if (=exact 0 (round -1/6))) | |
3976 | (pass-if (=exact 0 (round 1/6))) | |
3977 | (pass-if (=exact 0 (round 2/6))) | |
3978 | (pass-if (=exact 0 (round 3/6))) | |
3979 | (pass-if (=exact 1 (round 4/6))) | |
3980 | (pass-if (=exact 1 (round 5/6))) | |
3981 | (pass-if (=exact 1 (round 7/6))) | |
3982 | (pass-if (=exact 1 (round 8/6))) | |
3983 | (pass-if (=exact 2 (round 9/6))) | |
3984 | (pass-if (=exact 2 (round 10/6))) | |
3985 | (pass-if (=exact 2 (round 11/6))) | |
3986 | (pass-if (=exact 2 (round 13/6))) | |
3987 | (pass-if (=exact 2 (round 14/6))) | |
3988 | (pass-if (=exact 2 (round 15/6))) | |
3989 | (pass-if (=exact 3 (round 16/6))) | |
3990 | (pass-if (=exact 3 (round 17/6)))) | |
3991 | ||
abff733b KR |
3992 | (with-test-prefix "real" |
3993 | (pass-if "0.0" | |
3994 | (and (= 0.0 (round 0.0)) | |
3995 | (inexact? (round 0.0)))) | |
3996 | ||
3997 | (pass-if "1.0" | |
3998 | (and (= 1.0 (round 1.0)) | |
3999 | (inexact? (round 1.0)))) | |
4000 | ||
4001 | (pass-if "-1.0" | |
4002 | (and (= -1.0 (round -1.0)) | |
4003 | (inexact? (round -1.0)))) | |
4004 | ||
4005 | (pass-if "-3.1" | |
4006 | (and (= -3.0 (round -3.1)) | |
4007 | (inexact? (round -3.1)))) | |
4008 | ||
4009 | (pass-if "3.1" | |
4010 | (and (= 3.0 (round 3.1)) | |
4011 | (inexact? (round 3.1)))) | |
4012 | ||
4013 | (pass-if "3.9" | |
4014 | (and (= 4.0 (round 3.9)) | |
4015 | (inexact? (round 3.9)))) | |
4016 | ||
4017 | (pass-if "-3.9" | |
4018 | (and (= -4.0 (round -3.9)) | |
4019 | (inexact? (round -3.9)))) | |
4020 | ||
4021 | (pass-if "1.5" | |
4022 | (and (= 2.0 (round 1.5)) | |
4023 | (inexact? (round 1.5)))) | |
4024 | ||
4025 | (pass-if "2.5" | |
4026 | (and (= 2.0 (round 2.5)) | |
4027 | (inexact? (round 2.5)))) | |
4028 | ||
4029 | (pass-if "3.5" | |
4030 | (and (= 4.0 (round 3.5)) | |
4031 | (inexact? (round 3.5)))) | |
4032 | ||
4033 | (pass-if "-1.5" | |
4034 | (and (= -2.0 (round -1.5)) | |
4035 | (inexact? (round -1.5)))) | |
4036 | ||
4037 | (pass-if "-2.5" | |
4038 | (and (= -2.0 (round -2.5)) | |
4039 | (inexact? (round -2.5)))) | |
4040 | ||
4041 | (pass-if "-3.5" | |
4042 | (and (= -4.0 (round -3.5)) | |
4043 | (inexact? (round -3.5)))) | |
4044 | ||
4045 | ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a | |
4046 | ;; float with mantissa all ones) came out as 2^53 from `round' (except | |
4047 | ;; on i386 and m68k systems using the coprocessor and optimizing, where | |
4048 | ;; extra precision hid the problem) | |
4049 | (pass-if "2^53-1" | |
4050 | (let ((x (exact->inexact (1- (ash 1 53))))) | |
4051 | (and (= x (round x)) | |
4052 | (inexact? (round x))))) | |
4053 | (pass-if "-(2^53-1)" | |
4054 | (let ((x (exact->inexact (- (1- (ash 1 53)))))) | |
4055 | (and (= x (round x)) | |
4056 | (inexact? (round x))))))) | |
14a6784c | 4057 | |
f29b3454 DH |
4058 | ;;; |
4059 | ;;; exact->inexact | |
4060 | ;;; | |
4061 | ||
a1fb3b1c | 4062 | (with-test-prefix "exact->inexact" |
1eb6a33a MW |
4063 | |
4064 | ;; Test "(exact->inexact n)", expect "want". | |
4065 | (define (test name n want) | |
4066 | (with-test-prefix name | |
4067 | (pass-if-equal "pos" want (exact->inexact n)) | |
4068 | (pass-if-equal "neg" (- want) (exact->inexact (- n))))) | |
4069 | ||
a1fb3b1c KR |
4070 | ;; Test "(exact->inexact n)", expect "want". |
4071 | ;; "i" is a index, for diagnostic purposes. | |
4072 | (define (try-i i n want) | |
1eb6a33a | 4073 | (test (list i n want) n want)) |
a1fb3b1c KR |
4074 | |
4075 | (with-test-prefix "2^i, no round" | |
4076 | (do ((i 0 (1+ i)) | |
4077 | (n 1 (* 2 n)) | |
4078 | (want 1.0 (* 2.0 want))) | |
4079 | ((> i 100)) | |
4080 | (try-i i n want))) | |
4081 | ||
4082 | (with-test-prefix "2^i+1, no round" | |
4083 | (do ((i 1 (1+ i)) | |
4084 | (n 3 (1- (* 2 n))) | |
4085 | (want 3.0 (- (* 2.0 want) 1.0))) | |
4086 | ((>= i dbl-mant-dig)) | |
4087 | (try-i i n want))) | |
4088 | ||
4089 | (with-test-prefix "(2^i+1)*2^100, no round" | |
4090 | (do ((i 1 (1+ i)) | |
4091 | (n 3 (1- (* 2 n))) | |
4092 | (want 3.0 (- (* 2.0 want) 1.0))) | |
4093 | ((>= i dbl-mant-dig)) | |
4094 | (try-i i (ash n 100) (ash-flo want 100)))) | |
4095 | ||
4096 | ;; bit pattern: 1111....11100.00 | |
4097 | ;; <-mantdig-><-i-> | |
4098 | ;; | |
4099 | (with-test-prefix "mantdig ones then zeros, no rounding" | |
4100 | (do ((i 0 (1+ i)) | |
4101 | (n (- (ash 1 dbl-mant-dig) 1) (* 2 n)) | |
4102 | (want (- (ash-flo 1.0 dbl-mant-dig) 1.0) (* 2.0 want))) | |
4103 | ((> i 100)) | |
4104 | (try-i i n want))) | |
4105 | ||
4106 | ;; bit pattern: 1111....111011..1 | |
4107 | ;; <-mantdig-> <-i-> | |
4108 | ;; This sort of value was incorrectly rounded upwards in Guile 1.6.4 when | |
4109 | ;; i >= 11 (that's when the total is 65 or more bits). | |
4110 | ;; | |
4111 | (with-test-prefix "mantdig ones then 011..11, round down" | |
4112 | (do ((i 0 (1+ i)) | |
4113 | (n (- (ash 1 (+ 1 dbl-mant-dig)) 2) (+ 1 (* 2 n))) | |
4114 | (want (- (ash-flo 1.0 (+ 1 dbl-mant-dig)) 2.0) (* 2.0 want))) | |
4115 | ((> i 100)) | |
4116 | (try-i i n want))) | |
4117 | ||
4118 | ;; bit pattern: 1111....111100..001 | |
4119 | ;; <-mantdig-> <--i-> | |
4120 | ;; | |
4121 | (with-test-prefix "mantdig ones then 100..001, round up" | |
4122 | (do ((i 0 (1+ i)) | |
4123 | (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n))) | |
4124 | (want (ash-flo 1.0 (+ 2 dbl-mant-dig)) (* 2.0 want))) | |
4125 | ((> i 100)) | |
4126 | (try-i i n want))) | |
4127 | ||
4128 | ;; bit pattern: 1000....000100..001 | |
4129 | ;; <-mantdig-> <--i-> | |
4130 | ;; | |
4131 | (with-test-prefix "2^mantdig then 100..001, round up" | |
4132 | (do ((i 0 (1+ i)) | |
4133 | (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n))) | |
4134 | (want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want))) | |
4135 | ((> i 100)) | |
23f2b9a3 KR |
4136 | (try-i i n want))) |
4137 | ||
4138 | (pass-if "frac big/big" | |
4139 | (let ((big (ash 1 256))) | |
4140 | (= 1.0 (exact->inexact (/ (1+ big) big))))) | |
4141 | ||
4142 | ;; In guile 1.8.0 this failed, giving back "nan" because it tried to | |
4143 | ;; convert the num and den to doubles, resulting in infs. | |
4144 | (pass-if "frac big/big, exceeding double" | |
4145 | (let ((big (ash 1 4096))) | |
1eb6a33a MW |
4146 | (= 1.0 (exact->inexact (/ (1+ big) big))))) |
4147 | ||
4cc2e41c MW |
4148 | ;; In guile 2.0.9, 'exact->inexact' guaranteed proper rounding when |
4149 | ;; applied to non-negative fractions, but on 64-bit systems would | |
4150 | ;; sometimes double-round when applied to negative fractions, | |
4151 | ;; specifically when the numerator was a fixnum not exactly | |
4152 | ;; representable as a double. | |
4153 | (with-test-prefix "frac inum/inum, numerator not exactly representable as a double" | |
4154 | (let ((n (+ 1 (expt 2 dbl-mant-dig)))) | |
4155 | (for-each (lambda (d) | |
4156 | (test (/ n d) | |
4157 | (/ n d) | |
4158 | (exact->inexact (/ n d)))) | |
4159 | '(3 5 6 7 9 11 13 17 19 23 0.0 -0.0 +nan.0 +inf.0 -inf.0)))) | |
4160 | ||
1eb6a33a MW |
4161 | (test "round up to odd" |
4162 | ;; ===================================================== | |
4163 | ;; 11111111111111111111111111111111111111111111111111000101 -> | |
4164 | ;; 11111111111111111111111111111111111111111111111111001000 | |
4165 | (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b000101) | |
4166 | (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b001000)) | |
4167 | ||
4168 | (test "round down to odd" | |
4169 | ;; ===================================================== | |
4170 | ;; 11111111111111111111111111111111111111111111111111001011 -> | |
4171 | ;; 11111111111111111111111111111111111111111111111111001000 | |
4172 | (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b001011) | |
4173 | (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b001000)) | |
4174 | ||
4175 | (test "round tie up to even" | |
4176 | ;; ===================================================== | |
4177 | ;; 11111111111111111111111111111111111111111111111111011100 -> | |
4178 | ;; 11111111111111111111111111111111111111111111111111100000 | |
4179 | (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b011100) | |
4180 | (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b100000)) | |
4181 | ||
4182 | (test "round tie down to even" | |
4183 | ;; ===================================================== | |
4184 | ;; 11111111111111111111111111111111111111111111111111000100 -> | |
4185 | ;; 11111111111111111111111111111111111111111111111111000000 | |
4186 | (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b000100) | |
4187 | (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b000000)) | |
4188 | ||
4189 | (test "round tie up to next power of two" | |
4190 | ;; ===================================================== | |
4191 | ;; 11111111111111111111111111111111111111111111111111111100 -> | |
4192 | ;; 100000000000000000000000000000000000000000000000000000000 | |
4193 | (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b111100) | |
98237784 MW |
4194 | (expt 2.0 (+ dbl-mant-dig 3))) |
4195 | ||
4196 | (test "miniscule value rounds to zero of appropriate sign" | |
4197 | (expt 17 (- dbl-min-exp dbl-mant-dig)) | |
4198 | 0.0) | |
4199 | ||
4200 | (test "smallest inexact" | |
4201 | (expt 2 (- dbl-min-exp dbl-mant-dig)) | |
4202 | (expt 2.0 (- dbl-min-exp dbl-mant-dig))) | |
4203 | ||
4204 | (test "1/2 smallest inexact rounds down to zero" | |
4205 | (* 1/2 (expt 2 (- dbl-min-exp dbl-mant-dig))) | |
4206 | 0.0) | |
4207 | ||
4208 | (test "just over 1/2 smallest inexact rounds up" | |
4209 | (+ (* 1/2 (expt 2 (- dbl-min-exp dbl-mant-dig))) | |
4210 | (expt 7 (- dbl-min-exp dbl-mant-dig))) | |
4211 | (expt 2.0 (- dbl-min-exp dbl-mant-dig))) | |
4212 | ||
4213 | (test "3/2 smallest inexact rounds up to twice smallest inexact" | |
4214 | (* 3/2 (expt 2 (- dbl-min-exp dbl-mant-dig))) | |
4215 | (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig)))) | |
4216 | ||
4217 | (test "just under 3/2 smallest inexact rounds down" | |
4218 | (- (* 3/2 (expt 2 (- dbl-min-exp dbl-mant-dig))) | |
4219 | (expt 11 (- dbl-min-exp dbl-mant-dig))) | |
4220 | (expt 2.0 (- dbl-min-exp dbl-mant-dig))) | |
4221 | ||
4222 | (test "5/2 smallest inexact rounds down to twice smallest inexact" | |
4223 | (* 5/2 (expt 2 (- dbl-min-exp dbl-mant-dig))) | |
4224 | (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig)))) | |
4225 | ||
4226 | (test "just over 5/2 smallest inexact rounds up" | |
4227 | (+ (* 5/2 (expt 2 (- dbl-min-exp dbl-mant-dig))) | |
4228 | (expt 13 (- dbl-min-exp dbl-mant-dig))) | |
4229 | (* 3.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig)))) | |
4230 | ||
4231 | (test "one plus dbl-epsilon" | |
4232 | (+ 1 dbl-epsilon-exact) | |
4233 | (+ 1.0 dbl-epsilon)) | |
4234 | ||
4235 | (test "one plus 1/2 dbl-epsilon rounds down to 1.0" | |
4236 | (+ 1 (* 1/2 dbl-epsilon-exact)) | |
4237 | 1.0) | |
4238 | ||
4239 | (test "just over one plus 1/2 dbl-epsilon rounds up" | |
4240 | (+ 1 | |
4241 | (* 1/2 dbl-epsilon-exact) | |
4242 | (expt 13 (- dbl-min-exp dbl-mant-dig))) | |
4243 | (+ 1.0 dbl-epsilon)) | |
4244 | ||
4245 | (test "one plus 3/2 dbl-epsilon rounds up" | |
4246 | (+ 1 (* 3/2 dbl-epsilon-exact)) | |
4247 | (+ 1.0 (* 2.0 dbl-epsilon))) | |
4248 | ||
4249 | (test "just under one plus 3/2 dbl-epsilon rounds down" | |
4250 | (+ 1 | |
4251 | (* 3/2 dbl-epsilon-exact) | |
4252 | (- (expt 17 (- dbl-min-exp dbl-mant-dig)))) | |
4253 | (+ 1.0 dbl-epsilon)) | |
4254 | ||
4255 | (test "one plus 5/2 dbl-epsilon rounds down" | |
4256 | (+ 1 (* 5/2 dbl-epsilon-exact)) | |
4257 | (+ 1.0 (* 2.0 dbl-epsilon))) | |
4258 | ||
4259 | (test "just over one plus 5/2 dbl-epsilon rounds up" | |
4260 | (+ 1 | |
4261 | (* 5/2 dbl-epsilon-exact) | |
4262 | (expt 13 (- dbl-min-exp dbl-mant-dig))) | |
4263 | (+ 1.0 (* 3.0 dbl-epsilon))) | |
4264 | ||
4265 | (test "largest finite inexact" | |
4266 | (* (- (expt 2 dbl-mant-dig) 1) | |
4267 | (expt 2 (- dbl-max-exp dbl-mant-dig))) | |
4268 | (* (- (expt 2.0 dbl-mant-dig) 1) | |
4269 | (expt 2.0 (- dbl-max-exp dbl-mant-dig)))) | |
4270 | ||
4271 | (test "largest finite inexact plus 1/2 epsilon rounds up to infinity" | |
4272 | (* (+ (expt 2 dbl-mant-dig) -1 1/2) | |
4273 | (expt 2 (- dbl-max-exp dbl-mant-dig))) | |
4274 | (inf)) | |
4275 | ||
4276 | (test "largest finite inexact plus just under 1/2 epsilon rounds down" | |
4277 | (* (+ (expt 2 dbl-mant-dig) -1 1/2 | |
4278 | (- (expt 13 (- dbl-min-exp dbl-mant-dig)))) | |
4279 | (expt 2 (- dbl-max-exp dbl-mant-dig))) | |
4280 | (* (- (expt 2.0 dbl-mant-dig) 1) | |
4281 | (expt 2.0 (- dbl-max-exp dbl-mant-dig)))) | |
4282 | ||
4283 | (test "1/2 largest finite inexact" | |
4284 | (* (- (expt 2 dbl-mant-dig) 1) | |
4285 | (expt 2 (- dbl-max-exp dbl-mant-dig 1))) | |
4286 | (* (- (expt 2.0 dbl-mant-dig) 1) | |
4287 | (expt 2.0 (- dbl-max-exp dbl-mant-dig 1)))) | |
4288 | ||
4289 | (test "1/2 largest finite inexact plus 1/2 epsilon rounds up to next power of two" | |
4290 | (* (+ (expt 2 dbl-mant-dig) -1 1/2) | |
4291 | (expt 2 (- dbl-max-exp dbl-mant-dig 1))) | |
4292 | (expt 2.0 (- dbl-max-exp 1))) | |
4293 | ||
4294 | (test "1/2 largest finite inexact plus just over 1/2 epsilon rounds up to next power of two" | |
4295 | (* (+ (expt 2 dbl-mant-dig) -1 1/2 | |
4296 | (expt 13 (- dbl-min-exp dbl-mant-dig))) | |
4297 | (expt 2 (- dbl-max-exp dbl-mant-dig 1))) | |
4298 | (expt 2.0 (- dbl-max-exp 1))) | |
4299 | ||
4300 | (test "1/2 largest finite inexact plus just under 1/2 epsilon rounds down" | |
4301 | (* (+ (expt 2 dbl-mant-dig) -1 1/2 | |
4302 | (- (expt 13 (- dbl-min-exp dbl-mant-dig)))) | |
4303 | (expt 2 (- dbl-max-exp dbl-mant-dig 1))) | |
4304 | (* (- (expt 2.0 dbl-mant-dig) 1) | |
4305 | (expt 2.0 (- dbl-max-exp dbl-mant-dig 1)))) | |
4306 | ||
4307 | ) | |
a1fb3b1c | 4308 | |
46f2c0f1 RB |
4309 | ;;; |
4310 | ;;; expt | |
4311 | ;;; | |
4312 | ||
4313 | (with-test-prefix "expt" | |
2519490c | 4314 | (pass-if (documented? expt)) |
bfe1f03a MW |
4315 | |
4316 | ;; | |
4317 | ;; expt no longer requires its first argument to be a scheme number, | |
4318 | ;; for the sake of extensibility, and expt calls integer-expt for | |
4319 | ;; integer powers. To raise to a positive power, all that is required | |
4320 | ;; is that it can be multiplied using `*'. For negative powers we | |
4321 | ;; must also be able to find the reciprocal. If we try to raise #t to | |
4322 | ;; any power other than 0 or 1 it may throw an exception, depending on | |
4323 | ;; whether * has been defined for #t. However, when raising to the 0 | |
4324 | ;; or 1 power, the first argument is not manipulated at all. | |
4325 | ;; | |
4326 | ;; (pass-if-exception "non-numeric base" exception:wrong-type-arg | |
4327 | ;; (expt #t 0)) | |
4328 | ;; | |
4329 | ||
01c7284a MW |
4330 | (pass-if (eqv? 1 (expt 0 0))) |
4331 | (pass-if (eqv? 1 (expt 0.0 0))) | |
4332 | (pass-if (eqv? 1.0 (expt 0 0.0))) | |
4333 | (pass-if (eqv? 1.0 (expt 0.0 0.0))) | |
55a8b708 MW |
4334 | (pass-if (real-nan? (expt 0 -1))) |
4335 | (pass-if (real-nan? (expt 0 -1.0))) | |
4336 | (pass-if (real-nan? (expt 0.0 -1))) | |
4337 | (pass-if (real-nan? (expt 0.0 -1.0))) | |
01c7284a MW |
4338 | (pass-if (eqv? 0 (expt 0 3))) |
4339 | (pass-if (= 0 (expt 0 4.0))) | |
4340 | (pass-if (eqv? 0.0 (expt 0.0 5))) | |
4341 | (pass-if (eqv? 0.0 (expt 0.0 6.0))) | |
4342 | (pass-if (eqv? -2742638075.5 (expt -2742638075.5 1))) | |
4343 | (pass-if (eqv? (* -2742638075.5 -2742638075.5) | |
4344 | (expt -2742638075.5 2))) | |
4345 | (pass-if (eqv? 4.0 (expt -2.0 2.0))) | |
4346 | (pass-if (eqv? -1/8 (expt -2 -3))) | |
4347 | (pass-if (eqv? -0.125 (expt -2.0 -3))) | |
4348 | (pass-if (eqv? -0.125 (expt -2 -3.0))) | |
4349 | (pass-if (eqv? -0.125 (expt -2.0 -3.0))) | |
4350 | (pass-if (eqv? 0.25 (expt 2.0 -2.0))) | |
a285b18c MW |
4351 | (pass-if (eqv? 32/243 (expt 2/3 5))) |
4352 | (pass-if (eqv? 243/32 (expt 2/3 -5))) | |
4353 | (pass-if (eqv? 32 (expt 1/2 -5))) | |
c7218482 | 4354 | (pass-if (test-eqv? (* -1.0+0.0i 12398 12398) (expt +12398i 2.0))) |
01c7284a MW |
4355 | (pass-if (eqv-loosely? +i (expt -1 0.5))) |
4356 | (pass-if (eqv-loosely? +i (expt -1 1/2))) | |
8e43ed5d AW |
4357 | (pass-if (eqv-loosely? 1.0+1.7320508075688i (expt -8 1/3))) |
4358 | (pass-if (eqv? +inf.0 (expt 2 +inf.0))) | |
4359 | (pass-if (eqv? +inf.0 (expt 2.0 +inf.0))) | |
4360 | (pass-if (eqv? 0.0 (expt 2 -inf.0))) | |
4361 | (pass-if (eqv? 0.0 (expt 2.0 -inf.0)))) | |
01c7284a | 4362 | |
46f2c0f1 | 4363 | |
8deddc94 MW |
4364 | ;;; |
4365 | ;;; sin | |
4366 | ;;; | |
4367 | ||
4368 | (with-test-prefix "sin" | |
4369 | (pass-if (eqv? 0 (sin 0))) | |
4370 | (pass-if (eqv? 0.0 (sin 0.0))) | |
4371 | (pass-if (eqv-loosely? 1.0 (sin 1.57))) | |
4372 | (pass-if (eqv-loosely? +1.175i (sin +i))) | |
4373 | (pass-if (real-nan? (sin +nan.0))) | |
4374 | (pass-if (real-nan? (sin +inf.0))) | |
4375 | (pass-if (real-nan? (sin -inf.0)))) | |
4376 | ||
4377 | ;;; | |
4378 | ;;; cos | |
4379 | ;;; | |
4380 | ||
4381 | (with-test-prefix "cos" | |
4382 | (pass-if (eqv? 1 (cos 0))) | |
4383 | (pass-if (eqv? 1.0 (cos 0.0))) | |
4384 | (pass-if (eqv-loosely? 0.0 (cos 1.57))) | |
4385 | (pass-if (eqv-loosely? 1.543 (cos +i))) | |
4386 | (pass-if (real-nan? (cos +nan.0))) | |
4387 | (pass-if (real-nan? (cos +inf.0))) | |
4388 | (pass-if (real-nan? (cos -inf.0)))) | |
4389 | ||
4390 | ;;; | |
4391 | ;;; tan | |
4392 | ;;; | |
4393 | ||
4394 | (with-test-prefix "tan" | |
4395 | (pass-if (eqv? 0 (tan 0))) | |
4396 | (pass-if (eqv? 0.0 (tan 0.0))) | |
4397 | (pass-if (eqv-loosely? 1.0 (tan 0.785))) | |
4398 | (pass-if (eqv-loosely? +0.76i (tan +i))) | |
4399 | (pass-if (real-nan? (tan +nan.0))) | |
4400 | (pass-if (real-nan? (tan +inf.0))) | |
4401 | (pass-if (real-nan? (tan -inf.0)))) | |
4402 | ||
4403 | ;;; | |
4404 | ;;; asin | |
4405 | ;;; | |
4406 | ||
4407 | (with-test-prefix "asin" | |
4408 | (pass-if (complex-nan? (asin +nan.0))) | |
4409 | (pass-if (eqv? 0 (asin 0))) | |
4410 | (pass-if (eqv? 0.0 (asin 0.0)))) | |
4411 | ||
4412 | ;;; | |
4413 | ;;; acos | |
4414 | ;;; | |
4415 | ||
4416 | (with-test-prefix "acos" | |
4417 | (pass-if (complex-nan? (acos +nan.0))) | |
4418 | (pass-if (eqv? 0 (acos 1))) | |
4419 | (pass-if (eqv? 0.0 (acos 1.0)))) | |
4420 | ||
4421 | ;;; | |
4422 | ;;; atan | |
4423 | ;;; | |
4424 | ;;; FIXME: add tests for two-argument atan | |
4425 | ;;; | |
4426 | (with-test-prefix "atan" | |
4427 | (pass-if (real-nan? (atan +nan.0))) | |
4428 | (pass-if (eqv? 0 (atan 0))) | |
4429 | (pass-if (eqv? 0.0 (atan 0.0))) | |
4430 | (pass-if (eqv-loosely? 1.57 (atan +inf.0))) | |
4431 | (pass-if (eqv-loosely? -1.57 (atan -inf.0)))) | |
4432 | ||
4433 | ;;; | |
4434 | ;;; sinh | |
4435 | ;;; | |
4436 | ||
4437 | (with-test-prefix "sinh" | |
4438 | (pass-if (= 0 (sinh 0))) | |
4439 | (pass-if (= 0.0 (sinh 0.0)))) | |
4440 | ||
4441 | ;;; | |
4442 | ;;; cosh | |
4443 | ;;; | |
4444 | ||
4445 | (with-test-prefix "cosh" | |
4446 | (pass-if (= 1 (cosh 0))) | |
4447 | (pass-if (= 1.0 (cosh 0.0)))) | |
4448 | ||
4449 | ;;; | |
4450 | ;;; tanh | |
4451 | ;;; | |
4452 | ||
4453 | (with-test-prefix "tanh" | |
4454 | (pass-if (= 0 (tanh 0))) | |
4455 | (pass-if (= 0.0 (tanh 0.0)))) | |
4456 | ||
14a6784c KR |
4457 | ;;; |
4458 | ;;; asinh | |
4459 | ;;; | |
4460 | ||
4461 | (with-test-prefix "asinh" | |
8deddc94 MW |
4462 | (pass-if (= 0 (asinh 0))) |
4463 | (pass-if (= 0.0 (asinh 0.0)))) | |
14a6784c KR |
4464 | |
4465 | ;;; | |
4466 | ;;; acosh | |
4467 | ;;; | |
4468 | ||
4469 | (with-test-prefix "acosh" | |
8deddc94 MW |
4470 | (pass-if (= 0 (acosh 1))) |
4471 | (pass-if (= 0.0 (acosh 1.0)))) | |
14a6784c KR |
4472 | |
4473 | ;;; | |
4474 | ;;; atanh | |
4475 | ;;; | |
4476 | ||
4477 | (with-test-prefix "atanh" | |
8deddc94 MW |
4478 | (pass-if (= 0 (atanh 0))) |
4479 | (pass-if (= 0.0 (atanh 0.0)))) | |
14a6784c | 4480 | |
f29b3454 DH |
4481 | ;;; |
4482 | ;;; make-rectangular | |
4483 | ;;; | |
c7218482 MW |
4484 | |
4485 | (with-test-prefix "make-rectangular" | |
4486 | (pass-if (real? (make-rectangular 5.0 0 ))) | |
4487 | (pass-if (not (real? (make-rectangular 5.0 0.0)))) | |
4488 | (pass-if (not (real? (make-rectangular 5.0 -0.0))))) | |
f29b3454 DH |
4489 | |
4490 | ;;; | |
4491 | ;;; make-polar | |
4492 | ;;; | |
4493 | ||
d40681ec KR |
4494 | (with-test-prefix "make-polar" |
4495 | (define pi 3.14159265358979323846) | |
4496 | (define (almost= x y) | |
4497 | (> 0.01 (magnitude (- x y)))) | |
4498 | ||
c7218482 MW |
4499 | (pass-if (real? (make-polar 0 1.0))) |
4500 | (pass-if (real? (make-polar 5.0 0 ))) | |
4501 | (pass-if (not (real? (make-polar 5.0 0.0)))) | |
4502 | (pass-if (not (real? (make-polar 5.0 -0.0)))) | |
4503 | ||
4504 | (pass-if (eqv? 0 (make-polar 0 0))) | |
4505 | (pass-if (eqv? 0 (make-polar 0 123.456))) | |
4506 | (pass-if (eqv? 1 (make-polar 1 0))) | |
4507 | (pass-if (eqv? -1 (make-polar -1 0))) | |
d40681ec KR |
4508 | |
4509 | (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi)))) | |
4510 | (pass-if (almost= -1 (make-polar 1 (* 1.0 pi)))) | |
4511 | (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi)))) | |
4512 | (pass-if (almost= 1 (make-polar 1 (* 2.0 pi))))) | |
4513 | ||
f29b3454 DH |
4514 | ;;; |
4515 | ;;; real-part | |
4516 | ;;; | |
4517 | ||
2519490c MW |
4518 | (with-test-prefix "real-part" |
4519 | (pass-if (documented? real-part)) | |
4520 | (pass-if (eqv? 5.0 (real-part 5.0))) | |
4521 | (pass-if (eqv? 0.0 (real-part +5.0i))) | |
4522 | (pass-if (eqv? 5 (real-part 5))) | |
4523 | (pass-if (eqv? 1/5 (real-part 1/5))) | |
4524 | (pass-if (eqv? (1+ fixnum-max) (real-part (1+ fixnum-max))))) | |
4525 | ||
f29b3454 DH |
4526 | ;;; |
4527 | ;;; imag-part | |
4528 | ;;; | |
4529 | ||
2519490c MW |
4530 | (with-test-prefix "imag-part" |
4531 | (pass-if (documented? imag-part)) | |
c7218482 | 4532 | (pass-if (eqv? 0 (imag-part 5.0))) |
2519490c MW |
4533 | (pass-if (eqv? 5.0 (imag-part +5.0i))) |
4534 | (pass-if (eqv? 0 (imag-part 5))) | |
4535 | (pass-if (eqv? 0 (imag-part 1/5))) | |
4536 | (pass-if (eqv? 0 (imag-part (1+ fixnum-max))))) | |
4537 | ||
f29b3454 DH |
4538 | ;;; |
4539 | ;;; magnitude | |
4540 | ;;; | |
4541 | ||
d40681ec | 4542 | (with-test-prefix "magnitude" |
2519490c | 4543 | (pass-if (documented? magnitude)) |
d40681ec KR |
4544 | (pass-if (= 0 (magnitude 0))) |
4545 | (pass-if (= 1 (magnitude 1))) | |
4546 | (pass-if (= 1 (magnitude -1))) | |
4547 | (pass-if (= 1 (magnitude 0+i))) | |
4548 | (pass-if (= 1 (magnitude 0-i))) | |
4549 | (pass-if (= 5 (magnitude 3+4i))) | |
4550 | (pass-if (= 5 (magnitude 3-4i))) | |
4551 | (pass-if (= 5 (magnitude -3+4i))) | |
4552 | (pass-if (= 5 (magnitude -3-4i)))) | |
4553 | ||
f29b3454 DH |
4554 | ;;; |
4555 | ;;; angle | |
4556 | ;;; | |
4557 | ||
cfc9fc1c KR |
4558 | (with-test-prefix "angle" |
4559 | (define pi 3.14159265358979323846) | |
4560 | (define (almost= x y) | |
10a97755 | 4561 | (> 0.000001 (magnitude (- x y)))) |
cfc9fc1c | 4562 | |
2519490c MW |
4563 | (pass-if (documented? angle)) |
4564 | ||
cfc9fc1c KR |
4565 | (pass-if "inum +ve" (= 0 (angle 1))) |
4566 | (pass-if "inum -ve" (almost= pi (angle -1))) | |
4567 | ||
4568 | (pass-if "bignum +ve" (= 0 (angle (1+ fixnum-max)))) | |
4569 | (pass-if "bignum -ve" (almost= pi (angle (1- fixnum-min)))) | |
4570 | ||
4571 | (pass-if "flonum +ve" (= 0 (angle 1.5))) | |
10a97755 MW |
4572 | (pass-if "flonum -ve" (almost= pi (angle -1.5))) |
4573 | ||
4574 | (pass-if "signed zero +ve" (= 0 (angle 0.0))) | |
4575 | (pass-if "signed zero -ve" (almost= pi (angle -0.0)))) | |
cfc9fc1c | 4576 | |
f29b3454 DH |
4577 | ;;; |
4578 | ;;; inexact->exact | |
4579 | ;;; | |
300c6a76 | 4580 | |
1259cb26 | 4581 | (with-test-prefix "inexact->exact" |
24475b86 MW |
4582 | |
4583 | ;; Test "(inexact->exact f)", expect "want". | |
4584 | (define (test name f want) | |
4585 | (with-test-prefix name | |
4586 | (pass-if-equal "pos" want (inexact->exact f)) | |
4587 | (pass-if-equal "neg" (- want) (inexact->exact (- f))))) | |
4588 | ||
2519490c MW |
4589 | (pass-if (documented? inexact->exact)) |
4590 | ||
9dd9857f | 4591 | (pass-if-exception "+inf" exception:out-of-range |
a409f865 | 4592 | (inexact->exact +inf.0)) |
1259cb26 | 4593 | |
9dd9857f | 4594 | (pass-if-exception "-inf" exception:out-of-range |
a409f865 | 4595 | (inexact->exact -inf.0)) |
1259cb26 | 4596 | |
9dd9857f | 4597 | (pass-if-exception "nan" exception:out-of-range |
a409f865 | 4598 | (inexact->exact +nan.0)) |
24475b86 MW |
4599 | |
4600 | (test "0.0" 0.0 0) | |
4601 | (test "small even integer" 72.0 72) | |
4602 | (test "small odd integer" 73.0 73) | |
4603 | ||
4604 | (test "largest inexact odd integer" | |
4605 | (- (expt 2.0 dbl-mant-dig) 1) | |
4606 | (- (expt 2 dbl-mant-dig) 1)) | |
4607 | ||
4608 | (test "largest inexact odd integer - 1" | |
4609 | (- (expt 2.0 dbl-mant-dig) 2) | |
4610 | (- (expt 2 dbl-mant-dig) 2)) | |
4611 | ||
4612 | (test "largest inexact odd integer + 3" | |
4613 | (+ (expt 2.0 dbl-mant-dig) 2) | |
4614 | (+ (expt 2 dbl-mant-dig) 2)) | |
4615 | ||
4616 | (test "largest inexact odd integer * 2^48" | |
4617 | (* (expt 2.0 48) (- (expt 2.0 dbl-mant-dig) 1)) | |
4618 | (* (expt 2 48) (- (expt 2 dbl-mant-dig) 1))) | |
4619 | ||
4620 | (test "largest inexact odd integer / 2^48" | |
4621 | (* (expt 0.5 48) (- (expt 2.0 dbl-mant-dig) 1)) | |
4622 | (* (expt 1/2 48) (- (expt 2 dbl-mant-dig) 1))) | |
4623 | ||
98237784 MW |
4624 | (test "largest finite inexact" |
4625 | (* (- (expt 2.0 dbl-mant-dig) 1) | |
4626 | (expt 2.0 (- dbl-max-exp dbl-mant-dig))) | |
4627 | (* (- (expt 2 dbl-mant-dig) 1) | |
4628 | (expt 2 (- dbl-max-exp dbl-mant-dig)))) | |
4629 | ||
24475b86 MW |
4630 | (test "smallest inexact" |
4631 | (expt 2.0 (- dbl-min-exp dbl-mant-dig)) | |
4632 | (expt 2 (- dbl-min-exp dbl-mant-dig))) | |
4633 | ||
4634 | (test "smallest inexact * 2" | |
4635 | (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))) | |
4636 | (* 2 (expt 2 (- dbl-min-exp dbl-mant-dig)))) | |
4637 | ||
4638 | (test "smallest inexact * 3" | |
4639 | (* 3.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))) | |
4640 | (* 3 (expt 2 (- dbl-min-exp dbl-mant-dig)))) | |
4641 | ||
4642 | (with-test-prefix "2.0**i to exact" | |
1259cb26 | 4643 | (do ((i 0 (1+ i)) |
24475b86 MW |
4644 | (n 1 (* 2 n)) |
4645 | (f 1.0 (* 2.0 f))) | |
1259cb26 | 4646 | ((> i 100)) |
24475b86 | 4647 | (test (list i n) f n)))) |
1259cb26 | 4648 | |
c1122753 KR |
4649 | ;;; |
4650 | ;;; integer-expt | |
4651 | ;;; | |
4652 | ||
4653 | (with-test-prefix "integer-expt" | |
2519490c | 4654 | (pass-if (documented? integer-expt)) |
c1122753 KR |
4655 | |
4656 | (pass-if-exception "2^+inf" exception:wrong-type-arg | |
4657 | (integer-expt 2 +inf.0)) | |
4658 | (pass-if-exception "2^-inf" exception:wrong-type-arg | |
4659 | (integer-expt 2 -inf.0)) | |
4660 | (pass-if-exception "2^nan" exception:wrong-type-arg | |
01c7284a MW |
4661 | (integer-expt 2 +nan.0)) |
4662 | ||
4663 | (pass-if (eqv? 1 (integer-expt 0 0))) | |
4664 | (pass-if (eqv? 1 (integer-expt 0.0 0))) | |
55a8b708 MW |
4665 | (pass-if (real-nan? (integer-expt 0 -1))) |
4666 | (pass-if (real-nan? (integer-expt 0.0 -1))) | |
01c7284a MW |
4667 | (pass-if (eqv? 0 (integer-expt 0 3))) |
4668 | (pass-if (eqv? 0.0 (integer-expt 0.0 5))) | |
4669 | (pass-if (eqv? -2742638075.5 (integer-expt -2742638075.5 1))) | |
4670 | (pass-if (eqv? (* -2742638075.5 -2742638075.5) | |
4671 | (integer-expt -2742638075.5 2))) | |
4672 | (pass-if (eqv? 4.0 (integer-expt -2.0 2))) | |
4673 | (pass-if (eqv? -1/8 (integer-expt -2 -3))) | |
4674 | (pass-if (eqv? -0.125 (integer-expt -2.0 -3))) | |
4675 | (pass-if (eqv? 0.25 (integer-expt 2.0 -2))) | |
a285b18c MW |
4676 | (pass-if (eqv? 32/243 (integer-expt 2/3 5))) |
4677 | (pass-if (eqv? 243/32 (integer-expt 2/3 -5))) | |
4678 | (pass-if (eqv? 32 (integer-expt 1/2 -5))) | |
c7218482 | 4679 | (pass-if (test-eqv? (* -1.0+0.0i 12398 12398) (integer-expt +12398.0i 2)))) |
01c7284a | 4680 | |
c1122753 | 4681 | |
a04a3604 KR |
4682 | ;;; |
4683 | ;;; integer-length | |
4684 | ;;; | |
4685 | ||
4686 | (with-test-prefix "integer-length" | |
2519490c | 4687 | (pass-if (documented? integer-length)) |
a04a3604 KR |
4688 | |
4689 | (with-test-prefix "-2^i, ...11100..00" | |
4690 | (do ((n -1 (ash n 1)) | |
4691 | (i 0 (1+ i))) | |
4692 | ((> i 256)) | |
4693 | (pass-if (list n "expect" i) | |
4694 | (= i (integer-length n))))) | |
4695 | ||
4696 | (with-test-prefix "-2^i+1 ...11100..01" | |
4697 | (do ((n -3 (logxor 3 (ash n 1))) | |
4698 | (i 2 (1+ i))) | |
4699 | ((> i 256)) | |
4700 | (pass-if n | |
4701 | (= i (integer-length n))))) | |
4702 | ||
4703 | (with-test-prefix "-2^i-1 ...111011..11" | |
4704 | (do ((n -2 (1+ (ash n 1))) | |
4705 | (i 1 (1+ i))) | |
4706 | ((> i 256)) | |
4707 | (pass-if n | |
4708 | (= i (integer-length n)))))) | |
4709 | ||
8ab3d8a0 KR |
4710 | ;;; |
4711 | ;;; log | |
4712 | ;;; | |
4713 | ||
4714 | (with-test-prefix "log" | |
2519490c | 4715 | (pass-if (documented? log)) |
8ab3d8a0 KR |
4716 | |
4717 | (pass-if-exception "no args" exception:wrong-num-args | |
4718 | (log)) | |
4719 | (pass-if-exception "two args" exception:wrong-num-args | |
4720 | (log 123 456)) | |
a5f6b751 MW |
4721 | (pass-if-exception "(log 0)" exception:numerical-overflow |
4722 | (log 0)) | |
4723 | ||
4724 | (pass-if (test-eqv? -inf.0 (log 0.0))) | |
4725 | (pass-if (test-eqv? +inf.0 (log +inf.0))) | |
4726 | (pass-if (test-eqv? -inf.0+3.14159265358979i (log -0.0))) | |
4727 | (pass-if (test-eqv? +inf.0+3.14159265358979i (log -inf.0))) | |
4728 | (pass-if (test-eqv? 0.0 (log 1 ))) | |
4729 | (pass-if (test-eqv? 0.0 (log 1.0))) | |
4730 | (pass-if (test-eqv? 1.0 (log const-e))) | |
4731 | (pass-if (test-eqv? 2.0 (log const-e^2))) | |
4732 | (pass-if (test-eqv? -1.0 (log const-1/e))) | |
4733 | (pass-if (test-eqv? -1.0+3.14159265358979i (log (- const-1/e)))) | |
4734 | (pass-if (test-eqv? 2.30258509299405 (log 10))) | |
4735 | (pass-if (test-eqv? 2.30258509299405+3.14159265358979i (log -10))) | |
4736 | ||
4737 | (pass-if (test-eqv? 1.0+0.0i (log (+ const-e +0.0i)))) | |
4738 | (pass-if (test-eqv? 1.0-0.0i (log (+ const-e -0.0i)))) | |
4739 | ||
4740 | (pass-if (eqv-loosely? 230258.509299405 (log (expt 10 100000)))) | |
4741 | (pass-if (eqv-loosely? -230258.509299405 (log (expt 10 -100000)))) | |
4742 | (pass-if (eqv-loosely? 230257.410687116 (log (/ (expt 10 100000) 3)))) | |
4743 | (pass-if (eqv-loosely? 230258.509299405+3.14159265358979i | |
4744 | (log (- (expt 10 100000))))) | |
4745 | (pass-if (eqv-loosely? -230258.509299405+3.14159265358979i | |
4746 | (log (- (expt 10 -100000))))) | |
4747 | (pass-if (eqv-loosely? 230257.410687116+3.14159265358979i | |
4748 | (log (- (/ (expt 10 100000) 3))))) | |
4749 | (pass-if (test-eqv? 3.05493636349961e-151 | |
4750 | (log (/ (1+ (expt 2 500)) (expt 2 500))))) | |
8ab3d8a0 KR |
4751 | |
4752 | (pass-if (eqv-loosely? 1.0+1.57079i (log 0+2.71828i))) | |
4753 | (pass-if (eqv-loosely? 1.0-1.57079i (log 0-2.71828i))) | |
4754 | ||
4755 | (pass-if (eqv-loosely? 0.0+3.14159i (log -1.0))) | |
4756 | (pass-if (eqv-loosely? 1.0+3.14159i (log -2.71828))) | |
4757 | (pass-if (eqv-loosely? 2.0+3.14159i (log (* -2.71828 2.71828))))) | |
4758 | ||
4759 | ;;; | |
4760 | ;;; log10 | |
4761 | ;;; | |
4762 | ||
4763 | (with-test-prefix "log10" | |
2519490c | 4764 | (pass-if (documented? log10)) |
8ab3d8a0 KR |
4765 | |
4766 | (pass-if-exception "no args" exception:wrong-num-args | |
4767 | (log10)) | |
4768 | (pass-if-exception "two args" exception:wrong-num-args | |
4769 | (log10 123 456)) | |
a5f6b751 MW |
4770 | (pass-if-exception "(log10 0)" exception:numerical-overflow |
4771 | (log10 0)) | |
4772 | ||
4773 | (pass-if (test-eqv? -inf.0 (log10 0.0))) | |
4774 | (pass-if (test-eqv? +inf.0 (log10 +inf.0))) | |
4775 | (pass-if (test-eqv? -inf.0+1.36437635384184i (log10 -0.0))) | |
4776 | (pass-if (test-eqv? +inf.0+1.36437635384184i (log10 -inf.0))) | |
4777 | (pass-if (test-eqv? 0.0 (log10 1 ))) | |
4778 | (pass-if (test-eqv? 0.0 (log10 1.0))) | |
4779 | (pass-if (test-eqv? 1.0 (log10 10 ))) | |
4780 | (pass-if (test-eqv? 1.0 (log10 10.0))) | |
4781 | (pass-if (test-eqv? 2.0 (log10 100.0))) | |
4782 | (pass-if (test-eqv? -1.0 (log10 0.1))) | |
4783 | (pass-if (test-eqv? -1.0+1.36437635384184i (log10 -0.1))) | |
4784 | (pass-if (test-eqv? 1.0+1.36437635384184i (log10 -10 ))) | |
4785 | ||
4786 | (pass-if (test-eqv? 1.0+0.0i (log10 10.0+0.0i))) | |
4787 | (pass-if (test-eqv? 1.0-0.0i (log10 10.0-0.0i))) | |
4788 | ||
4789 | (pass-if (eqv-loosely? 100000.0 (log10 (expt 10 100000)))) | |
4790 | (pass-if (eqv-loosely? -100000.0 (log10 (expt 10 -100000)))) | |
4791 | (pass-if (eqv-loosely? 99999.5228787453 (log10 (/ (expt 10 100000) 3)))) | |
4792 | (pass-if (eqv-loosely? 100000.0+1.36437635384184i | |
4793 | (log10 (- (expt 10 100000))))) | |
4794 | (pass-if (eqv-loosely? -100000.0+1.36437635384184i | |
4795 | (log10 (- (expt 10 -100000))))) | |
4796 | (pass-if (eqv-loosely? 99999.5228787453+1.36437635384184i | |
4797 | (log10 (- (/ (expt 10 100000) 3))))) | |
4798 | (pass-if (test-eqv? 1.32674200523347e-151 | |
4799 | (log10 (/ (1+ (expt 2 500)) (expt 2 500))))) | |
8ab3d8a0 KR |
4800 | |
4801 | (pass-if (eqv-loosely? 1.0+0.68218i (log10 0+10.0i))) | |
4802 | (pass-if (eqv-loosely? 1.0-0.68218i (log10 0-10.0i))) | |
4803 | ||
a5f6b751 MW |
4804 | (pass-if (eqv-loosely? 0.0+1.36437i (log10 -1))) |
4805 | (pass-if (eqv-loosely? 1.0+1.36437i (log10 -10))) | |
8ab3d8a0 KR |
4806 | (pass-if (eqv-loosely? 2.0+1.36437i (log10 -100)))) |
4807 | ||
abff733b KR |
4808 | ;;; |
4809 | ;;; logbit? | |
4810 | ;;; | |
4811 | ||
4812 | (with-test-prefix "logbit?" | |
2519490c MW |
4813 | (pass-if (documented? logbit?)) |
4814 | ||
abff733b KR |
4815 | (pass-if (eq? #f (logbit? 0 0))) |
4816 | (pass-if (eq? #f (logbit? 1 0))) | |
4817 | (pass-if (eq? #f (logbit? 31 0))) | |
4818 | (pass-if (eq? #f (logbit? 32 0))) | |
4819 | (pass-if (eq? #f (logbit? 33 0))) | |
4820 | (pass-if (eq? #f (logbit? 63 0))) | |
4821 | (pass-if (eq? #f (logbit? 64 0))) | |
4822 | (pass-if (eq? #f (logbit? 65 0))) | |
4823 | ||
4824 | ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap | |
4825 | ;; around and return #t where it ought to be #f | |
4826 | (pass-if (eq? #t (logbit? 0 1))) | |
4827 | (pass-if (eq? #f (logbit? 1 1))) | |
4828 | (pass-if (eq? #f (logbit? 31 1))) | |
4829 | (pass-if (eq? #f (logbit? 32 1))) | |
4830 | (pass-if (eq? #f (logbit? 33 1))) | |
4831 | (pass-if (eq? #f (logbit? 63 1))) | |
4832 | (pass-if (eq? #f (logbit? 64 1))) | |
4833 | (pass-if (eq? #f (logbit? 65 1))) | |
4834 | (pass-if (eq? #f (logbit? 128 1))) | |
4835 | ||
4836 | (pass-if (eq? #t (logbit? 0 -1))) | |
4837 | (pass-if (eq? #t (logbit? 1 -1))) | |
4838 | (pass-if (eq? #t (logbit? 31 -1))) | |
4839 | (pass-if (eq? #t (logbit? 32 -1))) | |
4840 | (pass-if (eq? #t (logbit? 33 -1))) | |
4841 | (pass-if (eq? #t (logbit? 63 -1))) | |
4842 | (pass-if (eq? #t (logbit? 64 -1))) | |
4843 | (pass-if (eq? #t (logbit? 65 -1)))) | |
4844 | ||
300c6a76 KR |
4845 | ;;; |
4846 | ;;; logcount | |
4847 | ;;; | |
4848 | ||
4849 | (with-test-prefix "logcount" | |
2519490c | 4850 | (pass-if (documented? logcount)) |
300c6a76 KR |
4851 | |
4852 | (with-test-prefix "-2^i, meaning ...11100..00" | |
4853 | (do ((n -1 (ash n 1)) | |
4854 | (i 0 (1+ i))) | |
4855 | ((> i 256)) | |
795c0bae KR |
4856 | (pass-if n |
4857 | (= i (logcount n))))) | |
4858 | ||
4859 | (with-test-prefix "2^i" | |
4860 | (do ((n 1 (ash n 1)) | |
4861 | (i 0 (1+ i))) | |
4862 | ((> i 256)) | |
4863 | (pass-if n | |
4864 | (= 1 (logcount n))))) | |
4865 | ||
4866 | (with-test-prefix "2^i-1" | |
4867 | (do ((n 0 (1+ (ash n 1))) | |
4868 | (i 0 (1+ i))) | |
4869 | ((> i 256)) | |
300c6a76 KR |
4870 | (pass-if n |
4871 | (= i (logcount n)))))) | |
795c0bae | 4872 | |
afd09cfb KR |
4873 | ;;; |
4874 | ;;; logior | |
4875 | ;;; | |
4876 | ||
4877 | (with-test-prefix "logior" | |
2519490c MW |
4878 | (pass-if (documented? logior)) |
4879 | ||
afd09cfb KR |
4880 | (pass-if (eqv? -1 (logior (ash -1 1) 1))) |
4881 | ||
4882 | ;; check that bignum or bignum+inum args will reduce to an inum | |
4883 | (let () | |
4884 | (define (test x y) | |
4885 | (pass-if (list x y '=> -1) | |
4886 | (eqv? -1 (logior x y))) | |
4887 | (pass-if (list y x '=> -1) | |
4888 | (eqv? -1 (logior y x)))) | |
4889 | (test (ash -1 8) #xFF) | |
4890 | (test (ash -1 28) #x0FFFFFFF) | |
4891 | (test (ash -1 29) #x1FFFFFFF) | |
4892 | (test (ash -1 30) #x3FFFFFFF) | |
4893 | (test (ash -1 31) #x7FFFFFFF) | |
4894 | (test (ash -1 32) #xFFFFFFFF) | |
4895 | (test (ash -1 33) #x1FFFFFFFF) | |
4896 | (test (ash -1 60) #x0FFFFFFFFFFFFFFF) | |
4897 | (test (ash -1 61) #x1FFFFFFFFFFFFFFF) | |
4898 | (test (ash -1 62) #x3FFFFFFFFFFFFFFF) | |
4899 | (test (ash -1 63) #x7FFFFFFFFFFFFFFF) | |
4900 | (test (ash -1 64) #xFFFFFFFFFFFFFFFF) | |
4901 | (test (ash -1 65) #x1FFFFFFFFFFFFFFFF) | |
4902 | (test (ash -1 128) #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))) | |
4903 | ||
1ec2dd6f KR |
4904 | ;;; |
4905 | ;;; lognot | |
4906 | ;;; | |
4907 | ||
4908 | (with-test-prefix "lognot" | |
2519490c MW |
4909 | (pass-if (documented? lognot)) |
4910 | ||
1ec2dd6f KR |
4911 | (pass-if (= -1 (lognot 0))) |
4912 | (pass-if (= 0 (lognot -1))) | |
4913 | (pass-if (= -2 (lognot 1))) | |
4914 | (pass-if (= 1 (lognot -2))) | |
4915 | ||
4916 | (pass-if (= #x-100000000000000000000000000000000 | |
4917 | (lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))) | |
4918 | (pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF | |
4919 | (lognot #x-100000000000000000000000000000000)))) | |
8ab3d8a0 | 4920 | |
882c8963 MW |
4921 | ;;; |
4922 | ;;; exact-integer-sqrt | |
4923 | ;;; | |
4924 | ||
4925 | (with-test-prefix "exact-integer-sqrt" | |
4926 | (define (non-negative-exact-integer? k) | |
4927 | (and (integer? k) (exact? k) (>= k 0))) | |
4928 | ||
4929 | (define (test k) | |
4930 | (pass-if k (let-values (((s r) (exact-integer-sqrt k))) | |
4931 | (and (non-negative-exact-integer? s) | |
4932 | (non-negative-exact-integer? r) | |
4933 | (= k (+ r (* s s))) | |
4934 | (< k (* (1+ s) (1+ s))))))) | |
4935 | ||
4936 | (define (test-wrong-type-arg k) | |
4937 | (pass-if-exception k exception:wrong-type-arg | |
4938 | (let-values (((s r) (exact-integer-sqrt k))) | |
4939 | #t))) | |
4940 | ||
4941 | (pass-if (documented? exact-integer-sqrt)) | |
4942 | ||
4943 | (pass-if-exception "no args" exception:wrong-num-args | |
4944 | (exact-integer-sqrt)) | |
4945 | (pass-if-exception "two args" exception:wrong-num-args | |
4946 | (exact-integer-sqrt 123 456)) | |
4947 | ||
4948 | (test 0) | |
4949 | (test 1) | |
4950 | (test 9) | |
4951 | (test 10) | |
4952 | (test fixnum-max) | |
4953 | (test (1+ fixnum-max)) | |
4954 | (test (* fixnum-max fixnum-max)) | |
4955 | (test (+ 1 (* fixnum-max fixnum-max))) | |
4956 | (test (expt 10 100)) | |
4957 | (test (+ 3 (expt 10 100))) | |
4958 | ||
4959 | (test-wrong-type-arg -1) | |
4960 | (test-wrong-type-arg 1/9) | |
4961 | (test-wrong-type-arg fixnum-min) | |
4962 | (test-wrong-type-arg (1- fixnum-min)) | |
4963 | (test-wrong-type-arg 1.0) | |
4964 | (test-wrong-type-arg 1.5) | |
4965 | (test-wrong-type-arg "foo") | |
4966 | (test-wrong-type-arg 'foo)) | |
4967 | ||
4968 | ||
8ab3d8a0 KR |
4969 | ;;; |
4970 | ;;; sqrt | |
4971 | ;;; | |
4972 | ||
4973 | (with-test-prefix "sqrt" | |
2519490c | 4974 | (pass-if (documented? sqrt)) |
8ab3d8a0 KR |
4975 | |
4976 | (pass-if-exception "no args" exception:wrong-num-args | |
4977 | (sqrt)) | |
4978 | (pass-if-exception "two args" exception:wrong-num-args | |
4979 | (sqrt 123 456)) | |
4980 | ||
44002664 MW |
4981 | (pass-if (eqv? 0 (sqrt 0))) |
4982 | (pass-if (eqv? 1 (sqrt 1))) | |
4983 | (pass-if (eqv? 2 (sqrt 4))) | |
4984 | (pass-if (eqv? 3 (sqrt 9))) | |
4985 | (pass-if (eqv? 4 (sqrt 16))) | |
4986 | (pass-if (eqv? fixnum-max (sqrt (expt fixnum-max 2)))) | |
4987 | (pass-if (eqv? (+ 1 fixnum-max) (sqrt (expt (+ 1 fixnum-max) 2)))) | |
4988 | (pass-if (eqv? (expt 10 400) (sqrt (expt 10 800)))) | |
4989 | (pass-if (eqv? (/ (expt 10 1000) | |
4990 | (expt 13 1000)) | |
4991 | (sqrt (/ (expt 10 2000) | |
4992 | (expt 13 2000))))) | |
4993 | ||
4994 | (with-test-prefix "exact sqrt" | |
4995 | ||
4996 | (define (test root) | |
4997 | (pass-if (list root 'exact) | |
4998 | (eqv? root (sqrt (expt root 2)))) | |
ddb71742 MW |
4999 | (pass-if (list root '*2) |
5000 | (let ((r (sqrt (* 2 (expt root 2))))) | |
5001 | (and (inexact? r) | |
5002 | (eqv-loosely? (* (sqrt 2) root) r)))) | |
44002664 MW |
5003 | (pass-if (list root '-1) |
5004 | (let ((r (sqrt (- (expt root 2) 1)))) | |
5005 | (and (inexact? r) | |
5006 | (eqv-loosely? root r)))) | |
5007 | (pass-if (list root '+1) | |
5008 | (let ((r (sqrt (+ (expt root 2) 1)))) | |
5009 | (and (inexact? r) | |
5010 | (eqv-loosely? root r)))) | |
5011 | (pass-if (list root 'negative) | |
5012 | (eqv-loosely? (* +i root) (sqrt (- (expt root 2)))))) | |
5013 | ||
5014 | (test (exact-integer-sqrt (+ -1 (expt 2 (+ 2 dbl-mant-dig))))) | |
5015 | (test (exact-integer-sqrt (+ -1 (expt 2 (+ 1 dbl-mant-dig))))) | |
5016 | (test (exact-integer-sqrt (+ -1 (expt 2 (+ 0 dbl-mant-dig))))) | |
5017 | (test (exact-integer-sqrt (+ -1 (expt 2 (+ -1 dbl-mant-dig))))) | |
ddb71742 MW |
5018 | (test (exact-integer-sqrt (+ -1 (expt 2 (+ -2 dbl-mant-dig))))) |
5019 | ||
5020 | ;; largest finite inexact | |
5021 | (test (* (- (expt 2 dbl-mant-dig) 1) | |
5022 | (expt 2 (- dbl-max-exp dbl-mant-dig))))) | |
5023 | ||
5024 | (pass-if-equal "smallest inexact" | |
5025 | (expt 2.0 (- dbl-min-exp dbl-mant-dig)) | |
5026 | (sqrt (/ (+ -1 (expt 2 (* 2 (- dbl-mant-dig dbl-min-exp))))))) | |
5027 | ||
5028 | (with-test-prefix "extreme ratios" | |
5029 | (define-syntax-rule (test want x) | |
5030 | (pass-if 'x | |
5031 | (let ((got (sqrt x))) | |
5032 | (and (inexact? got) | |
5033 | (test-eqv? 1.0 (/ want got)))))) | |
5034 | (test 1.511139943175573e176 (/ (expt 3 2001) (expt 2 2001))) | |
5035 | (test 2.1370746022826034e176 (/ (expt 3 2001) (expt 2 2000))) | |
5036 | (test 8.724570529756128e175 (/ (expt 3 2000) (expt 2 2001))) | |
5037 | (test 6.6175207962444435e-177 (/ (expt 2 2001) (expt 3 2001))) | |
5038 | (test 1.1461882239239027e-176 (/ (expt 2 2001) (expt 3 2000))) | |
5039 | (test 4.679293829667447e-177 (/ (expt 2 2000) (expt 3 2001)))) | |
5040 | ||
5041 | (pass-if (eqv? (/ (expt 2 1000) | |
5042 | (expt 3 1000)) | |
5043 | (sqrt (/ (expt 2 2000) | |
5044 | (expt 3 2000))))) | |
5045 | ||
5046 | (pass-if (eqv? (/ (expt 3 1000) | |
5047 | (expt 2 1000)) | |
5048 | (sqrt (/ (expt 3 2000) | |
5049 | (expt 2 2000))))) | |
44002664 MW |
5050 | |
5051 | (pass-if (eqv? +4i (sqrt -16))) | |
5052 | (pass-if (eqv-loosely? +1.0e150i (sqrt #e-1e300))) | |
5053 | (pass-if (eqv-loosely? +0.7071i (sqrt -1/2))) | |
5054 | ||
8ab3d8a0 KR |
5055 | (pass-if (eqv? 0.0 (sqrt 0.0))) |
5056 | (pass-if (eqv? 1.0 (sqrt 1.0))) | |
5057 | (pass-if (eqv-loosely? 2.0 (sqrt 4.0))) | |
5058 | (pass-if (eqv-loosely? 31.62 (sqrt 1000.0))) | |
5059 | ||
5060 | (pass-if (eqv? +1.0i (sqrt -1.0))) | |
5061 | (pass-if (eqv-loosely? +2.0i (sqrt -4.0))) | |
5062 | (pass-if (eqv-loosely? +31.62i (sqrt -1000.0))) | |
5063 | ||
5064 | (pass-if "+i swings back to 45deg angle" | |
5065 | (eqv-loosely? +0.7071+0.7071i (sqrt +1.0i))) | |
5066 | ||
5067 | ;; Note: glibc 2.3 csqrt() had a bug affecting this test case, so if it | |
5068 | ;; fails check whether that's the cause (there's a configure test to | |
5069 | ;; reject it, but when cross-compiling we assume the C library is ok). | |
5070 | (pass-if "-100i swings back to 45deg down" | |
5071 | (eqv-loosely? +7.071-7.071i (sqrt -100.0i)))) | |
5072 | ||
4a46bc2a | 5073 | |
c05696aa MW |
5074 | ;;; |
5075 | ;;; Tests for number-theoretic division operators: | |
ff62c168 MW |
5076 | ;;; |
5077 | ;;; euclidean/ | |
5078 | ;;; euclidean-quotient | |
5079 | ;;; euclidean-remainder | |
c05696aa MW |
5080 | ;;; floor/ |
5081 | ;;; floor-quotient | |
5082 | ;;; floor-remainder | |
5083 | ;;; ceiling/ | |
5084 | ;;; ceiling-quotient | |
5085 | ;;; ceiling-remainder | |
5086 | ;;; truncate/ | |
5087 | ;;; truncate-quotient | |
5088 | ;;; truncate-remainder | |
ff62c168 MW |
5089 | ;;; centered/ |
5090 | ;;; centered-quotient | |
5091 | ;;; centered-remainder | |
c05696aa MW |
5092 | ;;; round/ |
5093 | ;;; round-quotient | |
5094 | ;;; round-remainder | |
ff62c168 MW |
5095 | ;;; |
5096 | ||
5097 | (with-test-prefix "Number-theoretic division" | |
5098 | ||
4a46bc2a | 5099 | ;; Tests that (lo <1 x <2 hi), |
ff62c168 MW |
5100 | ;; but allowing for imprecision |
5101 | ;; if x is inexact. | |
4a46bc2a | 5102 | (define (test-within-range? lo <1 x <2 hi) |
ff62c168 | 5103 | (if (exact? x) |
4a46bc2a | 5104 | (and (<1 lo x) (<2 x hi)) |
ff62c168 MW |
5105 | (let ((lo (- lo test-epsilon)) |
5106 | (hi (+ hi test-epsilon))) | |
5107 | (<= lo x hi)))) | |
5108 | ||
a8591a55 | 5109 | (define (valid-euclidean-answer? x y q r) |
4a46bc2a MW |
5110 | (and (eq? (exact? q) |
5111 | (exact? r) | |
5112 | (and (exact? x) (exact? y))) | |
5113 | (test-eqv? r (- x (* q y))) | |
5114 | (if (and (finite? x) (finite? y)) | |
5115 | (and (integer? q) | |
5116 | (test-within-range? 0 <= r < (abs y))) | |
5117 | (test-eqv? q (/ x y))))) | |
a8591a55 | 5118 | |
8f9da340 MW |
5119 | (define (valid-floor-answer? x y q r) |
5120 | (and (eq? (exact? q) | |
5121 | (exact? r) | |
5122 | (and (exact? x) (exact? y))) | |
5123 | (test-eqv? r (- x (* q y))) | |
5124 | (if (and (finite? x) (finite? y)) | |
5125 | (and (integer? q) | |
5126 | (if (> y 0) | |
5127 | (test-within-range? 0 <= r < y) | |
5128 | (test-within-range? y < r <= 0))) | |
5129 | (test-eqv? q (/ x y))))) | |
5130 | ||
5131 | (define (valid-ceiling-answer? x y q r) | |
5132 | (and (eq? (exact? q) | |
5133 | (exact? r) | |
5134 | (and (exact? x) (exact? y))) | |
5135 | (test-eqv? r (- x (* q y))) | |
5136 | (if (and (finite? x) (finite? y)) | |
5137 | (and (integer? q) | |
5138 | (if (> y 0) | |
5139 | (test-within-range? (- y) < r <= 0) | |
5140 | (test-within-range? 0 <= r < (- y)))) | |
5141 | (test-eqv? q (/ x y))))) | |
5142 | ||
5143 | (define (valid-truncate-answer? x y q r) | |
5144 | (and (eq? (exact? q) | |
5145 | (exact? r) | |
5146 | (and (exact? x) (exact? y))) | |
5147 | (test-eqv? r (- x (* q y))) | |
5148 | (if (and (finite? x) (finite? y)) | |
5149 | (and (integer? q) | |
5150 | (if (> x 0) | |
5151 | (test-within-range? 0 <= r < (abs y)) | |
5152 | (test-within-range? (- (abs y)) < r <= 0))) | |
5153 | (test-eqv? q (/ x y))))) | |
5154 | ||
a8591a55 | 5155 | (define (valid-centered-answer? x y q r) |
4a46bc2a MW |
5156 | (and (eq? (exact? q) |
5157 | (exact? r) | |
5158 | (and (exact? x) (exact? y))) | |
5159 | (test-eqv? r (- x (* q y))) | |
5160 | (if (and (finite? x) (finite? y)) | |
5161 | (and (integer? q) | |
5162 | (test-within-range? | |
5163 | (* -1/2 (abs y)) <= r < (* +1/2 (abs y)))) | |
5164 | (test-eqv? q (/ x y))))) | |
5165 | ||
8f9da340 MW |
5166 | (define (valid-round-answer? x y q r) |
5167 | (and (eq? (exact? q) | |
5168 | (exact? r) | |
5169 | (and (exact? x) (exact? y))) | |
5170 | (test-eqv? r (- x (* q y))) | |
5171 | (if (and (finite? x) (finite? y)) | |
5172 | (and (integer? q) | |
5173 | (let ((ay/2 (/ (abs y) 2))) | |
5174 | (if (even? q) | |
5175 | (test-within-range? (- ay/2) <= r <= ay/2) | |
5176 | (test-within-range? (- ay/2) < r < ay/2)))) | |
5177 | (test-eqv? q (/ x y))))) | |
5178 | ||
4a46bc2a MW |
5179 | (define (for lsts f) (apply for-each f lsts)) |
5180 | ||
5181 | (define big (expt 10 (1+ (inexact->exact (ceiling (log10 fixnum-max)))))) | |
5182 | ||
5183 | (define (run-division-tests quo+rem quo rem valid-answer?) | |
5184 | (define (test n d) | |
5185 | (run-test (list n d) #t | |
5186 | (lambda () | |
5187 | (let-values (((q r) (quo+rem n d))) | |
5188 | (and (test-eqv? q (quo n d)) | |
5189 | (test-eqv? r (rem n d)) | |
5190 | (valid-answer? n d q r)))))) | |
5191 | (define (test+/- n d) | |
5192 | (test n d ) | |
5193 | (test n (- d)) | |
5194 | (cond ((not (zero? n)) | |
5195 | (test (- n) d ) | |
5196 | (test (- n) (- d))))) | |
5197 | ||
5198 | (define (test-for-exception n d exception) | |
5199 | (let ((name (list n d))) | |
5200 | (pass-if-exception name exception (quo+rem n d)) | |
5201 | (pass-if-exception name exception (quo n d)) | |
5202 | (pass-if-exception name exception (rem n d)))) | |
5203 | ||
5204 | (run-test "documented?" #t | |
5205 | (lambda () | |
5206 | (and (documented? quo+rem) | |
5207 | (documented? quo) | |
5208 | (documented? rem)))) | |
5209 | ||
5210 | (with-test-prefix "inum / inum" | |
5211 | (with-test-prefix "fixnum-min / -1" | |
5212 | (test fixnum-min -1)) | |
5213 | (for '((1 2 5 10)) ;; denominators | |
5214 | (lambda (d) | |
5215 | (for '((0 1 2 5 10)) ;; multiples | |
5216 | (lambda (m) | |
5217 | (for '((-2 -1 0 1 2 3 4 5 7 10 | |
5218 | 12 15 16 19 20)) ;; offsets | |
5219 | (lambda (b) | |
5220 | (test+/- (+ b (* m d)) | |
5221 | d)))))))) | |
5222 | ||
5223 | (with-test-prefix "inum / big" | |
5224 | (with-test-prefix "fixnum-min / -fixnum-min" | |
5225 | (test fixnum-min (- fixnum-min))) | |
5226 | (with-test-prefix "fixnum-max / (2*fixnum-max)" | |
5227 | (test+/- fixnum-max (* 2 fixnum-max))) | |
5228 | (for `((0 1 2 10 ,(1- fixnum-max) ,fixnum-max)) | |
5229 | (lambda (n) | |
5230 | (test n (1+ fixnum-max)) | |
5231 | (test (- n) (1+ fixnum-max)) | |
5232 | (test n (1- fixnum-min)) | |
5233 | (test (- n) (1- fixnum-min))))) | |
5234 | ||
5235 | (with-test-prefix "big / inum" | |
5236 | (with-test-prefix "-fixnum-min / fixnum-min" | |
5237 | (test (- fixnum-min) fixnum-min)) | |
5238 | (for '((1 4 5 10)) ;; denominators | |
5239 | (lambda (d) | |
5240 | (for `((1 2 5 ,@(if (even? d) | |
5241 | '(1/2 3/2 5/2) | |
5242 | '()))) ;; multiples | |
5243 | (lambda (m) | |
5244 | (for '((-2 -1 0 1 2)) ;; offsets | |
5245 | (lambda (b) | |
5246 | (test+/- (+ b (* m d big)) | |
5247 | d)))))))) | |
5248 | ||
5249 | (with-test-prefix "big / big" | |
5250 | (for `((,big ,(1+ big))) ;; denominators | |
5251 | (lambda (d) | |
5252 | (for `((1 2 5 ,@(if (even? d) | |
5253 | '(1/2 3/2 5/2) | |
5254 | '()))) ;; multiples | |
5255 | (lambda (m) | |
5256 | (for '((-2 -1 0 1 2)) ;; offsets | |
5257 | (lambda (b) | |
5258 | (test+/- (+ b (* m d)) | |
5259 | d)))))))) | |
5260 | ||
5261 | (with-test-prefix "inexact" | |
5262 | (for '((0.5 1.5 2.25 5.75)) ;; denominators | |
5263 | (lambda (d) | |
5264 | (for '((0 1 2 5 1/2 3/2 5/2)) ;; multiples | |
5265 | (lambda (m) | |
5266 | (for '((-2 -1 0 1 2)) ;; offsets | |
5267 | (lambda (b) | |
5268 | (test+/- (+ b (* m d)) | |
5269 | d)))))))) | |
5270 | ||
5271 | (with-test-prefix "fractions" | |
5272 | (for '((1/10 16/3 10/7)) ;; denominators | |
5273 | (lambda (d) | |
5274 | (for '((0 1 2 5 1/2 3/2 5/2)) ;; multiples | |
5275 | (lambda (m) | |
5276 | (for '((-2/9 -1/11 0 1/3 2/3)) ;; offsets | |
5277 | (lambda (b) | |
5278 | (test+/- (+ b (* m d)) | |
5279 | d)))))))) | |
5280 | ||
5281 | (with-test-prefix "mixed types" | |
5282 | (for `((10 ,big 12.0 10/7 +inf.0 -inf.0 +nan.0)) ;; denominators | |
5283 | (lambda (d) | |
5284 | (for `((25 ,(* 3/2 big) 130.0 15/7 | |
5285 | 0 0.0 -0.0 +inf.0 -inf.0 +nan.0)) ;; numerators | |
5286 | (lambda (n) | |
5287 | (test+/- n d)))))) | |
5288 | ||
5289 | (with-test-prefix "divide by zero" | |
fb210d8d | 5290 | (for `((0 0.0 -0.0)) ;; denominators |
4a46bc2a MW |
5291 | (lambda (d) |
5292 | (for `((15 ,(* 3/2 big) 18.0 33/7 | |
5293 | 0 0.0 -0.0 +inf.0 -inf.0 +nan.0)) ;; numerators | |
5294 | (lambda (n) | |
5295 | (test-for-exception | |
5296 | n d exception:numerical-overflow))))))) | |
ff62c168 | 5297 | |
ff62c168 | 5298 | (with-test-prefix "euclidean/" |
4a46bc2a MW |
5299 | (run-division-tests euclidean/ |
5300 | euclidean-quotient | |
5301 | euclidean-remainder | |
5302 | valid-euclidean-answer?)) | |
a8591a55 | 5303 | |
8f9da340 MW |
5304 | (with-test-prefix "floor/" |
5305 | (run-division-tests floor/ | |
5306 | floor-quotient | |
5307 | floor-remainder | |
5308 | valid-floor-answer?)) | |
5309 | ||
5310 | (with-test-prefix "ceiling/" | |
5311 | (run-division-tests ceiling/ | |
5312 | ceiling-quotient | |
5313 | ceiling-remainder | |
5314 | valid-ceiling-answer?)) | |
5315 | ||
5316 | (with-test-prefix "truncate/" | |
5317 | (run-division-tests truncate/ | |
5318 | truncate-quotient | |
5319 | truncate-remainder | |
5320 | valid-truncate-answer?)) | |
5321 | ||
ff62c168 | 5322 | (with-test-prefix "centered/" |
4a46bc2a MW |
5323 | (run-division-tests centered/ |
5324 | centered-quotient | |
5325 | centered-remainder | |
8f9da340 MW |
5326 | valid-centered-answer?)) |
5327 | ||
5328 | (with-test-prefix "round/" | |
5329 | (run-division-tests round/ | |
5330 | round-quotient | |
5331 | round-remainder | |
5332 | valid-round-answer?))) | |
e08a12b5 MW |
5333 | |
5334 | ;;; | |
5335 | ;;; ash | |
5336 | ;;; round-ash | |
5337 | ;;; | |
5338 | ||
5339 | (let () | |
5340 | (define (test-ash-variant name ash-variant round-variant) | |
5341 | (with-test-prefix name | |
5342 | (define (test n count) | |
5343 | (pass-if (list n count) | |
5344 | (eqv? (ash-variant n count) | |
5345 | (round-variant (* n (expt 2 count)))))) | |
5346 | ||
5347 | (pass-if "documented?" | |
5348 | (documented? ash-variant)) | |
5349 | ||
5350 | (for-each (lambda (n) | |
5351 | (for-each (lambda (count) (test n count)) | |
5352 | '(-1000 -3 -2 -1 0 1 2 3 1000))) | |
5353 | (list 0 1 3 23 -1 -3 -23 | |
5354 | fixnum-max | |
5355 | (1+ fixnum-max) | |
5356 | (1- fixnum-max) | |
5357 | (* fixnum-max 4) | |
5358 | (quotient fixnum-max 4) | |
5359 | fixnum-min | |
5360 | (1+ fixnum-min) | |
5361 | (1- fixnum-min) | |
5362 | (* fixnum-min 4) | |
5363 | (quotient fixnum-min 4))) | |
5364 | ||
5365 | (do ((count -2 (1- count)) | |
5366 | (vals '(1 3 5 7 9 11) | |
5367 | (map (lambda (n) (* 2 n)) vals))) | |
5368 | ((> (car vals) (* 2 fixnum-max)) 'done) | |
5369 | (for-each (lambda (n) | |
5370 | (test n count) | |
5371 | (test (- n) count)) | |
5372 | vals)) | |
5373 | ||
5374 | ;; Test rounding | |
5375 | (for-each (lambda (base) | |
5376 | (for-each (lambda (offset) (test (+ base offset) -3)) | |
5377 | '(#b11001 #b11100 #b11101 #b10001 #b10100 #b10101))) | |
5378 | (list 0 64 -64 (* 64 fixnum-max) (* 64 fixnum-min))))) | |
5379 | ||
5380 | (test-ash-variant 'ash ash floor) | |
5381 | (test-ash-variant 'round-ash round-ash round)) |