Commit | Line | Data |
---|---|---|
de142bea | 1 | ;;;; numbers.test --- tests guile's numbers -*- scheme -*- |
cedf24d8 | 2 | ;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2009, 2010 Free Software Foundation, Inc. |
de142bea | 3 | ;;;; |
73be1d9e MV |
4 | ;;;; This library is free software; you can redistribute it and/or |
5 | ;;;; modify it under the terms of the GNU Lesser General Public | |
6 | ;;;; License as published by the Free Software Foundation; either | |
53befeb7 | 7 | ;;;; version 3 of the License, or (at your option) any later version. |
73be1d9e MV |
8 | ;;;; |
9 | ;;;; This library is distributed in the hope that it will be useful, | |
de142bea | 10 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
73be1d9e MV |
11 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | ;;;; Lesser General Public License for more details. | |
13 | ;;;; | |
14 | ;;;; You should have received a copy of the GNU Lesser General Public | |
15 | ;;;; License along with this library; if not, write to the Free Software | |
92205699 | 16 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de142bea | 17 | |
a1fb3b1c KR |
18 | (define-module (test-suite test-numbers) |
19 | #:use-module (test-suite lib) | |
20 | #:use-module (ice-9 documentation)) | |
de142bea | 21 | |
de142bea DH |
22 | ;;; |
23 | ;;; miscellaneous | |
24 | ;;; | |
25 | ||
1b3a7932 DH |
26 | (define exception:numerical-overflow |
27 | (cons 'numerical-overflow "^Numerical overflow")) | |
28 | ||
cb18f2a8 | 29 | (define (documented? object) |
5c96bc39 | 30 | (not (not (object-documentation object)))) |
de142bea | 31 | |
8b7838b5 RB |
32 | (define fixnum-bit |
33 | (inexact->exact (+ (/ (log (+ most-positive-fixnum 1)) (log 2)) 1))) | |
34 | ||
21e39e8f DH |
35 | (define fixnum-min most-negative-fixnum) |
36 | (define fixnum-max most-positive-fixnum) | |
de142bea | 37 | |
a1fb3b1c KR |
38 | ;; Divine the number of bits in the mantissa of a flonum. |
39 | ;; We look for when 2.0^i+1.0 gets rounded, ie. the difference between that | |
40 | ;; value and 2.0^k is not 1.0. | |
41 | ;; Of course this assumes flonums have a fixed precision mantissa, but | |
42 | ;; that's the case now and probably into the forseeable future. | |
43 | ;; On an IEEE system, which means pretty much everywhere, the value here is | |
44 | ;; the usual 53. | |
45 | ;; | |
46 | (define dbl-mant-dig | |
47 | (let more ((i 1) | |
48 | (d 2.0)) | |
49 | (if (> i 1024) | |
50 | (error "Oops, cannot determine number of bits in mantissa of inexact")) | |
51 | (let* ((sum (+ 1.0 d)) | |
52 | (diff (- sum d))) | |
53 | (if (= diff 1.0) | |
54 | (more (1+ i) (* 2.0 d)) | |
55 | i)))) | |
56 | ||
57 | ;; like ash, but working on a flonum | |
58 | (define (ash-flo x n) | |
59 | (while (> n 0) | |
60 | (set! x (* 2.0 x)) | |
61 | (set! n (1- n))) | |
62 | (while (< n 0) | |
63 | (set! x (* 0.5 x)) | |
64 | (set! n (1+ n))) | |
65 | x) | |
49579cbd KR |
66 | |
67 | ;; `quotient' but rounded towards -infinity, like `modulo' or `ash' do | |
68 | ;; note only positive D supported (that's all that's currently required) | |
69 | (define-public (quotient-floor n d) | |
70 | (if (negative? n) | |
71 | (quotient (- n d -1) d) ;; neg/pos | |
72 | (quotient n d))) ;; pos/pos | |
73 | ||
8ab3d8a0 KR |
74 | ;; return true of X is in the range LO to HI, inclusive |
75 | (define (within-range? lo hi x) | |
76 | (and (>= x (min lo hi)) | |
77 | (<= x (max lo hi)))) | |
78 | ||
79 | ;; return true if GOT is within +/- 0.01 of GOT | |
80 | ;; for a complex number both real and imaginary parts must be in that range | |
81 | (define (eqv-loosely? want got) | |
82 | (and (within-range? (- (real-part want) 0.01) | |
83 | (+ (real-part want) 0.01) | |
84 | (real-part got)) | |
85 | (within-range? (- (imag-part want) 0.01) | |
86 | (+ (imag-part want) 0.01) | |
87 | (imag-part got)))) | |
88 | ||
89 | ;; return true if OBJ is negative infinity | |
90 | (define (negative-infinity? obj) | |
91 | (and (real? obj) | |
92 | (negative? obj) | |
93 | (inf? obj))) | |
94 | ||
95 | (define const-e 2.7182818284590452354) | |
96 | (define const-e^2 7.3890560989306502274) | |
97 | (define const-1/e 0.3678794411714423215) | |
98 | ||
99 | ||
a580ebba KR |
100 | ;;; |
101 | ;;; 1+ | |
102 | ;;; | |
103 | ||
104 | (with-test-prefix "1+" | |
105 | ||
106 | (pass-if "documented?" | |
107 | (documented? 1+)) | |
108 | ||
109 | (pass-if (eqv? 1 (1+ 0))) | |
110 | (pass-if (eqv? 0 (1+ -1))) | |
111 | (pass-if (eqv? 101 (1+ 100))) | |
e78d4bf9 LC |
112 | (pass-if (eqv? -99 (1+ -100))) |
113 | ||
114 | ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1. | |
115 | (pass-if (eqv? 536870912 (1+ 536870911))) | |
116 | ||
117 | ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1. | |
118 | (pass-if (eqv? 2305843009213693952 (1+ 2305843009213693951)))) | |
a580ebba KR |
119 | |
120 | ;;; | |
121 | ;;; 1- | |
122 | ;;; | |
123 | ||
124 | (with-test-prefix "1-" | |
125 | ||
126 | (pass-if "documented?" | |
127 | (documented? 1-)) | |
128 | ||
129 | (pass-if (eqv? -1 (1- 0))) | |
130 | (pass-if (eqv? 0 (1- 1))) | |
131 | (pass-if (eqv? 99 (1- 100))) | |
e78d4bf9 LC |
132 | (pass-if (eqv? -101 (1- -100))) |
133 | ||
134 | ;; The minimum fixnum on a 32-bit architecture: -2^29. | |
135 | (pass-if (eqv? -536870913 (1- -536870912))) | |
136 | ||
137 | ;; The minimum fixnum on a 64-bit architecture: -2^61. | |
138 | (pass-if (eqv? -2305843009213693953 (1- -2305843009213693952)))) | |
a580ebba | 139 | |
49579cbd KR |
140 | ;;; |
141 | ;;; ash | |
142 | ;;; | |
143 | ||
144 | (with-test-prefix "ash" | |
145 | ||
146 | (pass-if "documented?" | |
147 | (documented? ash)) | |
148 | ||
149 | (pass-if (eqv? 0 (ash 0 0))) | |
150 | (pass-if (eqv? 0 (ash 0 1))) | |
151 | (pass-if (eqv? 0 (ash 0 1000))) | |
152 | (pass-if (eqv? 0 (ash 0 -1))) | |
153 | (pass-if (eqv? 0 (ash 0 -1000))) | |
154 | ||
155 | (pass-if (eqv? 1 (ash 1 0))) | |
156 | (pass-if (eqv? 2 (ash 1 1))) | |
157 | (pass-if (eqv? 340282366920938463463374607431768211456 (ash 1 128))) | |
158 | (pass-if (eqv? 0 (ash 1 -1))) | |
159 | (pass-if (eqv? 0 (ash 1 -1000))) | |
160 | ||
161 | (pass-if (eqv? -1 (ash -1 0))) | |
162 | (pass-if (eqv? -2 (ash -1 1))) | |
163 | (pass-if (eqv? -340282366920938463463374607431768211456 (ash -1 128))) | |
164 | (pass-if (eqv? -1 (ash -1 -1))) | |
165 | (pass-if (eqv? -1 (ash -1 -1000))) | |
166 | ||
167 | (pass-if (eqv? -3 (ash -3 0))) | |
168 | (pass-if (eqv? -6 (ash -3 1))) | |
169 | (pass-if (eqv? -1020847100762815390390123822295304634368 (ash -3 128))) | |
170 | (pass-if (eqv? -2 (ash -3 -1))) | |
171 | (pass-if (eqv? -1 (ash -3 -1000))) | |
172 | ||
173 | (pass-if (eqv? -6 (ash -23 -2))) | |
174 | ||
175 | (pass-if (eqv? most-positive-fixnum (ash most-positive-fixnum 0))) | |
176 | (pass-if (eqv? (* 2 most-positive-fixnum) (ash most-positive-fixnum 1))) | |
177 | (pass-if (eqv? (* 4 most-positive-fixnum) (ash most-positive-fixnum 2))) | |
178 | (pass-if | |
179 | (eqv? (* most-positive-fixnum 340282366920938463463374607431768211456) | |
180 | (ash most-positive-fixnum 128))) | |
181 | (pass-if (eqv? (quotient most-positive-fixnum 2) | |
182 | (ash most-positive-fixnum -1))) | |
183 | (pass-if (eqv? 0 (ash most-positive-fixnum -1000))) | |
184 | ||
185 | (let ((mpf4 (quotient most-positive-fixnum 4))) | |
186 | (pass-if (eqv? (* 2 mpf4) (ash mpf4 1))) | |
187 | (pass-if (eqv? (* 4 mpf4) (ash mpf4 2))) | |
188 | (pass-if (eqv? (* 8 mpf4) (ash mpf4 3)))) | |
189 | ||
190 | (pass-if (eqv? most-negative-fixnum (ash most-negative-fixnum 0))) | |
191 | (pass-if (eqv? (* 2 most-negative-fixnum) (ash most-negative-fixnum 1))) | |
192 | (pass-if (eqv? (* 4 most-negative-fixnum) (ash most-negative-fixnum 2))) | |
193 | (pass-if | |
194 | (eqv? (* most-negative-fixnum 340282366920938463463374607431768211456) | |
195 | (ash most-negative-fixnum 128))) | |
196 | (pass-if (eqv? (quotient-floor most-negative-fixnum 2) | |
197 | (ash most-negative-fixnum -1))) | |
198 | (pass-if (eqv? -1 (ash most-negative-fixnum -1000))) | |
199 | ||
200 | (let ((mnf4 (quotient-floor most-negative-fixnum 4))) | |
201 | (pass-if (eqv? (* 2 mnf4) (ash mnf4 1))) | |
202 | (pass-if (eqv? (* 4 mnf4) (ash mnf4 2))) | |
203 | (pass-if (eqv? (* 8 mnf4) (ash mnf4 3))))) | |
a1fb3b1c | 204 | |
de142bea DH |
205 | ;;; |
206 | ;;; exact? | |
207 | ;;; | |
208 | ||
209 | (with-test-prefix "exact?" | |
210 | ||
de142bea | 211 | (pass-if "documented?" |
cb18f2a8 | 212 | (documented? exact?)) |
de142bea | 213 | |
21e39e8f | 214 | (with-test-prefix "integers" |
de142bea | 215 | |
21e39e8f DH |
216 | (pass-if "0" |
217 | (exact? 0)) | |
de142bea | 218 | |
21e39e8f DH |
219 | (pass-if "fixnum-max" |
220 | (exact? fixnum-max)) | |
de142bea | 221 | |
21e39e8f DH |
222 | (pass-if "fixnum-max + 1" |
223 | (exact? (+ fixnum-max 1))) | |
de142bea | 224 | |
21e39e8f DH |
225 | (pass-if "fixnum-min" |
226 | (exact? fixnum-min)) | |
de142bea | 227 | |
21e39e8f DH |
228 | (pass-if "fixnum-min - 1" |
229 | (exact? (- fixnum-min 1)))) | |
230 | ||
231 | (with-test-prefix "reals" | |
232 | ||
233 | ;; (FIXME: need better examples.) | |
234 | ||
235 | (pass-if "sqrt (fixnum-max^2 - 1)" | |
236 | (eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1))))) | |
237 | ||
238 | (pass-if "sqrt ((fixnum-max+1)^2 - 1)" | |
239 | (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1))))))) | |
de142bea | 240 | |
8ab3d8a0 KR |
241 | ;;; |
242 | ;;; exp | |
243 | ;;; | |
244 | ||
245 | (with-test-prefix "exp" | |
246 | (pass-if "documented?" | |
247 | (documented? exp)) | |
248 | ||
249 | (pass-if-exception "no args" exception:wrong-num-args | |
250 | (exp)) | |
251 | (pass-if-exception "two args" exception:wrong-num-args | |
252 | (exp 123 456)) | |
253 | ||
254 | (pass-if (eqv? 0.0 (exp -inf.0))) | |
255 | (pass-if (eqv-loosely? 1.0 (exp 0))) | |
256 | (pass-if (eqv-loosely? 1.0 (exp 0.0))) | |
257 | (pass-if (eqv-loosely? const-e (exp 1.0))) | |
258 | (pass-if (eqv-loosely? const-e^2 (exp 2.0))) | |
259 | (pass-if (eqv-loosely? const-1/e (exp -1))) | |
260 | ||
261 | (pass-if "exp(pi*i) = -1" | |
262 | (eqv-loosely? -1.0 (exp 0+3.14159i))) | |
263 | (pass-if "exp(-pi*i) = -1" | |
264 | (eqv-loosely? -1.0 (exp 0-3.14159i))) | |
265 | (pass-if "exp(2*pi*i) = +1" | |
266 | (eqv-loosely? 1.0 (exp 0+6.28318i))) | |
267 | ||
268 | (pass-if "exp(2-pi*i) = -e^2" | |
269 | (eqv-loosely? (- const-e^2) (exp 2.0-3.14159i)))) | |
270 | ||
de142bea DH |
271 | ;;; |
272 | ;;; odd? | |
273 | ;;; | |
274 | ||
7c24e528 RB |
275 | (with-test-prefix "odd?" |
276 | (pass-if (documented? odd?)) | |
277 | (pass-if (odd? 1)) | |
278 | (pass-if (odd? -1)) | |
4d332f19 DH |
279 | (pass-if (not (odd? 0))) |
280 | (pass-if (not (odd? 2))) | |
281 | (pass-if (not (odd? -2))) | |
7c24e528 | 282 | (pass-if (odd? (+ (* 2 fixnum-max) 1))) |
4d332f19 | 283 | (pass-if (not (odd? (* 2 fixnum-max)))) |
7c24e528 | 284 | (pass-if (odd? (- (* 2 fixnum-min) 1))) |
4d332f19 | 285 | (pass-if (not (odd? (* 2 fixnum-min))))) |
de142bea DH |
286 | |
287 | ;;; | |
288 | ;;; even? | |
289 | ;;; | |
290 | ||
7c24e528 RB |
291 | (with-test-prefix "even?" |
292 | (pass-if (documented? even?)) | |
293 | (pass-if (even? 2)) | |
294 | (pass-if (even? -2)) | |
295 | (pass-if (even? 0)) | |
4d332f19 DH |
296 | (pass-if (not (even? 1))) |
297 | (pass-if (not (even? -1))) | |
298 | (pass-if (not (even? (+ (* 2 fixnum-max) 1)))) | |
7c24e528 | 299 | (pass-if (even? (* 2 fixnum-max))) |
4d332f19 | 300 | (pass-if (not (even? (- (* 2 fixnum-min) 1)))) |
7c24e528 | 301 | (pass-if (even? (* 2 fixnum-min)))) |
de142bea DH |
302 | |
303 | ;;; | |
7c24e528 RB |
304 | ;;; inf? and inf |
305 | ;;; | |
306 | ||
307 | (with-test-prefix "inf?" | |
308 | (pass-if (documented? inf?)) | |
309 | (pass-if (inf? (inf))) | |
310 | ;; FIXME: what are the expected behaviors? | |
311 | ;; (pass-if (inf? (/ 1.0 0.0)) | |
312 | ;; (pass-if (inf? (/ 1 0.0)) | |
4d332f19 DH |
313 | (pass-if (not (inf? 0))) |
314 | (pass-if (not (inf? 42.0))) | |
315 | (pass-if (not (inf? (+ fixnum-max 1)))) | |
316 | (pass-if (not (inf? (- fixnum-min 1))))) | |
7c24e528 RB |
317 | |
318 | ;;; | |
319 | ;;; nan? and nan | |
de142bea DH |
320 | ;;; |
321 | ||
7c24e528 RB |
322 | (with-test-prefix "nan?" |
323 | (pass-if (documented? nan?)) | |
324 | (pass-if (nan? (nan))) | |
325 | ;; FIXME: other ways we should be able to generate NaN? | |
4d332f19 DH |
326 | (pass-if (not (nan? 0))) |
327 | (pass-if (not (nan? 42.0))) | |
328 | (pass-if (not (nan? (+ fixnum-max 1)))) | |
329 | (pass-if (not (nan? (- fixnum-min 1))))) | |
de142bea | 330 | |
7c24e528 RB |
331 | ;;; |
332 | ;;; abs | |
333 | ;;; | |
334 | ||
335 | (with-test-prefix "abs" | |
336 | (pass-if (documented? abs)) | |
337 | (pass-if (zero? (abs 0))) | |
338 | (pass-if (= 1 (abs 1))) | |
339 | (pass-if (= 1 (abs -1))) | |
340 | (pass-if (= (+ fixnum-max 1) (abs (+ fixnum-max 1)))) | |
341 | (pass-if (= (+ (- fixnum-min) 1) (abs (- fixnum-min 1)))) | |
76903a31 KR |
342 | (pass-if (= 0.0 (abs 0.0))) |
343 | (pass-if (= 1.0 (abs 1.0))) | |
344 | (pass-if (= 1.0 (abs -1.0))) | |
345 | (pass-if (nan? (abs +nan.0))) | |
346 | (pass-if (= +inf.0 (abs +inf.0))) | |
347 | (pass-if (= +inf.0 (abs -inf.0)))) | |
348 | ||
de142bea DH |
349 | ;;; |
350 | ;;; quotient | |
351 | ;;; | |
352 | ||
353 | (with-test-prefix "quotient" | |
354 | ||
de142bea | 355 | (expect-fail "documented?" |
cb18f2a8 | 356 | (documented? quotient)) |
de142bea | 357 | |
de142bea DH |
358 | (with-test-prefix "0 / n" |
359 | ||
360 | (pass-if "n = 1" | |
361 | (eqv? 0 (quotient 0 1))) | |
362 | ||
363 | (pass-if "n = -1" | |
364 | (eqv? 0 (quotient 0 -1))) | |
365 | ||
21e39e8f DH |
366 | (pass-if "n = 2" |
367 | (eqv? 0 (quotient 0 2))) | |
368 | ||
369 | (pass-if "n = fixnum-max" | |
370 | (eqv? 0 (quotient 0 fixnum-max))) | |
371 | ||
372 | (pass-if "n = fixnum-max + 1" | |
373 | (eqv? 0 (quotient 0 (+ fixnum-max 1)))) | |
374 | ||
375 | (pass-if "n = fixnum-min" | |
376 | (eqv? 0 (quotient 0 fixnum-min))) | |
377 | ||
378 | (pass-if "n = fixnum-min - 1" | |
379 | (eqv? 0 (quotient 0 (- fixnum-min 1))))) | |
de142bea | 380 | |
21e39e8f | 381 | (with-test-prefix "1 / n" |
de142bea DH |
382 | |
383 | (pass-if "n = 1" | |
384 | (eqv? 1 (quotient 1 1))) | |
385 | ||
386 | (pass-if "n = -1" | |
21e39e8f DH |
387 | (eqv? -1 (quotient 1 -1))) |
388 | ||
389 | (pass-if "n = 2" | |
390 | (eqv? 0 (quotient 1 2))) | |
de142bea | 391 | |
21e39e8f DH |
392 | (pass-if "n = fixnum-max" |
393 | (eqv? 0 (quotient 1 fixnum-max))) | |
de142bea | 394 | |
21e39e8f DH |
395 | (pass-if "n = fixnum-max + 1" |
396 | (eqv? 0 (quotient 1 (+ fixnum-max 1)))) | |
de142bea | 397 | |
21e39e8f DH |
398 | (pass-if "n = fixnum-min" |
399 | (eqv? 0 (quotient 1 fixnum-min))) | |
400 | ||
401 | (pass-if "n = fixnum-min - 1" | |
402 | (eqv? 0 (quotient 1 (- fixnum-min 1))))) | |
403 | ||
404 | (with-test-prefix "-1 / n" | |
de142bea DH |
405 | |
406 | (pass-if "n = 1" | |
21e39e8f | 407 | (eqv? -1 (quotient -1 1))) |
de142bea DH |
408 | |
409 | (pass-if "n = -1" | |
410 | (eqv? 1 (quotient -1 -1))) | |
411 | ||
21e39e8f DH |
412 | (pass-if "n = 2" |
413 | (eqv? 0 (quotient -1 2))) | |
414 | ||
415 | (pass-if "n = fixnum-max" | |
416 | (eqv? 0 (quotient -1 fixnum-max))) | |
417 | ||
418 | (pass-if "n = fixnum-max + 1" | |
419 | (eqv? 0 (quotient -1 (+ fixnum-max 1)))) | |
420 | ||
421 | (pass-if "n = fixnum-min" | |
422 | (eqv? 0 (quotient -1 fixnum-min))) | |
423 | ||
424 | (pass-if "n = fixnum-min - 1" | |
425 | (eqv? 0 (quotient -1 (- fixnum-min 1))))) | |
426 | ||
427 | (with-test-prefix "fixnum-max / n" | |
428 | ||
429 | (pass-if "n = 1" | |
430 | (eqv? fixnum-max (quotient fixnum-max 1))) | |
431 | ||
432 | (pass-if "n = -1" | |
433 | (eqv? (- fixnum-max) (quotient fixnum-max -1))) | |
434 | ||
435 | (pass-if "n = 2" | |
436 | (eqv? fixnum-max (+ (* (quotient fixnum-max 2) 2) 1))) | |
437 | ||
438 | (pass-if "n = fixnum-max" | |
439 | (eqv? 1 (quotient fixnum-max fixnum-max))) | |
440 | ||
441 | (pass-if "n = fixnum-max + 1" | |
442 | (eqv? 0 (quotient fixnum-max (+ fixnum-max 1)))) | |
443 | ||
444 | (pass-if "n = fixnum-min" | |
445 | (eqv? 0 (quotient fixnum-max fixnum-min))) | |
446 | ||
447 | (pass-if "n = fixnum-min - 1" | |
448 | (eqv? 0 (quotient fixnum-max (- fixnum-min 1))))) | |
449 | ||
450 | (with-test-prefix "(fixnum-max + 1) / n" | |
451 | ||
452 | (pass-if "n = 1" | |
453 | (eqv? (+ fixnum-max 1) (quotient (+ fixnum-max 1) 1))) | |
454 | ||
455 | (pass-if "n = -1" | |
456 | (eqv? (- (+ fixnum-max 1)) (quotient (+ fixnum-max 1) -1))) | |
457 | ||
458 | (pass-if "n = 2" | |
459 | (eqv? (+ fixnum-max 1) (* (quotient (+ fixnum-max 1) 2) 2))) | |
460 | ||
461 | (pass-if "n = fixnum-max" | |
462 | (eqv? 1 (quotient (+ fixnum-max 1) fixnum-max))) | |
463 | ||
464 | (pass-if "n = fixnum-max + 1" | |
465 | (eqv? 1 (quotient (+ fixnum-max 1) (+ fixnum-max 1)))) | |
466 | ||
467 | (pass-if "n = fixnum-min" | |
468 | (eqv? -1 (quotient (+ fixnum-max 1) fixnum-min))) | |
469 | ||
470 | (pass-if "n = fixnum-min - 1" | |
471 | (eqv? 0 (quotient (+ fixnum-max 1) (- fixnum-min 1))))) | |
472 | ||
473 | (with-test-prefix "fixnum-min / n" | |
474 | ||
475 | (pass-if "n = 1" | |
476 | (eqv? fixnum-min (quotient fixnum-min 1))) | |
477 | ||
478 | (pass-if "n = -1" | |
479 | (eqv? (- fixnum-min) (quotient fixnum-min -1))) | |
480 | ||
481 | (pass-if "n = 2" | |
482 | (eqv? fixnum-min (* (quotient fixnum-min 2) 2))) | |
483 | ||
484 | (pass-if "n = fixnum-max" | |
485 | (eqv? -1 (quotient fixnum-min fixnum-max))) | |
486 | ||
487 | (pass-if "n = fixnum-max + 1" | |
488 | (eqv? -1 (quotient fixnum-min (+ fixnum-max 1)))) | |
489 | ||
490 | (pass-if "n = fixnum-min" | |
491 | (eqv? 1 (quotient fixnum-min fixnum-min))) | |
492 | ||
493 | (pass-if "n = fixnum-min - 1" | |
ad22fe7c KR |
494 | (eqv? 0 (quotient fixnum-min (- fixnum-min 1)))) |
495 | ||
496 | (pass-if "n = - fixnum-min - 1" | |
497 | (eqv? -1 (quotient fixnum-min (1- (- fixnum-min))))) | |
498 | ||
499 | ;; special case, normally inum/big is zero | |
500 | (pass-if "n = - fixnum-min" | |
501 | (eqv? -1 (quotient fixnum-min (- fixnum-min)))) | |
502 | ||
503 | (pass-if "n = - fixnum-min + 1" | |
504 | (eqv? 0 (quotient fixnum-min (1+ (- fixnum-min)))))) | |
21e39e8f DH |
505 | |
506 | (with-test-prefix "(fixnum-min - 1) / n" | |
507 | ||
508 | (pass-if "n = 1" | |
509 | (eqv? (- fixnum-min 1) (quotient (- fixnum-min 1) 1))) | |
510 | ||
511 | (pass-if "n = -1" | |
512 | (eqv? (- (- fixnum-min 1)) (quotient (- fixnum-min 1) -1))) | |
513 | ||
514 | (pass-if "n = 2" | |
515 | (eqv? fixnum-min (* (quotient (- fixnum-min 1) 2) 2))) | |
516 | ||
517 | (pass-if "n = fixnum-max" | |
518 | (eqv? -1 (quotient (- fixnum-min 1) fixnum-max))) | |
519 | ||
520 | (pass-if "n = fixnum-max + 1" | |
521 | (eqv? -1 (quotient (- fixnum-min 1) (+ fixnum-max 1)))) | |
522 | ||
523 | (pass-if "n = fixnum-min" | |
524 | (eqv? 1 (quotient (- fixnum-min 1) fixnum-min))) | |
525 | ||
526 | (pass-if "n = fixnum-min - 1" | |
527 | (eqv? 1 (quotient (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea DH |
528 | |
529 | ;; Positive dividend and divisor | |
530 | ||
531 | (pass-if "35 / 7" | |
532 | (eqv? 5 (quotient 35 7))) | |
533 | ||
534 | ;; Negative dividend, positive divisor | |
535 | ||
536 | (pass-if "-35 / 7" | |
537 | (eqv? -5 (quotient -35 7))) | |
538 | ||
539 | ;; Positive dividend, negative divisor | |
540 | ||
541 | (pass-if "35 / -7" | |
542 | (eqv? -5 (quotient 35 -7))) | |
543 | ||
544 | ;; Negative dividend and divisor | |
545 | ||
546 | (pass-if "-35 / -7" | |
547 | (eqv? 5 (quotient -35 -7))) | |
548 | ||
549 | ;; Are numerical overflows detected correctly? | |
550 | ||
80074d77 DH |
551 | (with-test-prefix "division by zero" |
552 | ||
553 | (pass-if-exception "(quotient 1 0)" | |
554 | exception:numerical-overflow | |
555 | (quotient 1 0)) | |
556 | ||
557 | (pass-if-exception "(quotient bignum 0)" | |
558 | exception:numerical-overflow | |
559 | (quotient (+ fixnum-max 1) 0))) | |
560 | ||
de142bea DH |
561 | ;; Are wrong type arguments detected correctly? |
562 | ||
563 | ) | |
564 | ||
565 | ;;; | |
566 | ;;; remainder | |
567 | ;;; | |
568 | ||
569 | (with-test-prefix "remainder" | |
570 | ||
de142bea | 571 | (expect-fail "documented?" |
cb18f2a8 | 572 | (documented? remainder)) |
de142bea | 573 | |
de142bea DH |
574 | (with-test-prefix "0 / n" |
575 | ||
576 | (pass-if "n = 1" | |
577 | (eqv? 0 (remainder 0 1))) | |
578 | ||
579 | (pass-if "n = -1" | |
580 | (eqv? 0 (remainder 0 -1))) | |
581 | ||
21e39e8f DH |
582 | (pass-if "n = fixnum-max" |
583 | (eqv? 0 (remainder 0 fixnum-max))) | |
584 | ||
585 | (pass-if "n = fixnum-max + 1" | |
586 | (eqv? 0 (remainder 0 (+ fixnum-max 1)))) | |
587 | ||
588 | (pass-if "n = fixnum-min" | |
589 | (eqv? 0 (remainder 0 fixnum-min))) | |
de142bea | 590 | |
21e39e8f DH |
591 | (pass-if "n = fixnum-min - 1" |
592 | (eqv? 0 (remainder 0 (- fixnum-min 1))))) | |
de142bea | 593 | |
21e39e8f | 594 | (with-test-prefix "1 / n" |
de142bea DH |
595 | |
596 | (pass-if "n = 1" | |
597 | (eqv? 0 (remainder 1 1))) | |
598 | ||
599 | (pass-if "n = -1" | |
21e39e8f DH |
600 | (eqv? 0 (remainder 1 -1))) |
601 | ||
602 | (pass-if "n = fixnum-max" | |
603 | (eqv? 1 (remainder 1 fixnum-max))) | |
de142bea | 604 | |
21e39e8f DH |
605 | (pass-if "n = fixnum-max + 1" |
606 | (eqv? 1 (remainder 1 (+ fixnum-max 1)))) | |
de142bea | 607 | |
21e39e8f DH |
608 | (pass-if "n = fixnum-min" |
609 | (eqv? 1 (remainder 1 fixnum-min))) | |
de142bea | 610 | |
21e39e8f DH |
611 | (pass-if "n = fixnum-min - 1" |
612 | (eqv? 1 (remainder 1 (- fixnum-min 1))))) | |
613 | ||
614 | (with-test-prefix "-1 / n" | |
de142bea DH |
615 | |
616 | (pass-if "n = 1" | |
21e39e8f | 617 | (eqv? 0 (remainder -1 1))) |
de142bea DH |
618 | |
619 | (pass-if "n = -1" | |
620 | (eqv? 0 (remainder -1 -1))) | |
621 | ||
21e39e8f DH |
622 | (pass-if "n = fixnum-max" |
623 | (eqv? -1 (remainder -1 fixnum-max))) | |
624 | ||
625 | (pass-if "n = fixnum-max + 1" | |
626 | (eqv? -1 (remainder -1 (+ fixnum-max 1)))) | |
627 | ||
628 | (pass-if "n = fixnum-min" | |
629 | (eqv? -1 (remainder -1 fixnum-min))) | |
630 | ||
631 | (pass-if "n = fixnum-min - 1" | |
632 | (eqv? -1 (remainder -1 (- fixnum-min 1))))) | |
633 | ||
634 | (with-test-prefix "fixnum-max / n" | |
635 | ||
636 | (pass-if "n = 1" | |
637 | (eqv? 0 (remainder fixnum-max 1))) | |
638 | ||
639 | (pass-if "n = -1" | |
640 | (eqv? 0 (remainder fixnum-max -1))) | |
641 | ||
642 | (pass-if "n = fixnum-max" | |
643 | (eqv? 0 (remainder fixnum-max fixnum-max))) | |
644 | ||
645 | (pass-if "n = fixnum-max + 1" | |
646 | (eqv? fixnum-max (remainder fixnum-max (+ fixnum-max 1)))) | |
647 | ||
648 | (pass-if "n = fixnum-min" | |
649 | (eqv? fixnum-max (remainder fixnum-max fixnum-min))) | |
650 | ||
651 | (pass-if "n = fixnum-min - 1" | |
652 | (eqv? fixnum-max (remainder fixnum-max (- fixnum-min 1))))) | |
653 | ||
654 | (with-test-prefix "(fixnum-max + 1) / n" | |
655 | ||
656 | (pass-if "n = 1" | |
657 | (eqv? 0 (remainder (+ fixnum-max 1) 1))) | |
658 | ||
659 | (pass-if "n = -1" | |
660 | (eqv? 0 (remainder (+ fixnum-max 1) -1))) | |
661 | ||
662 | (pass-if "n = fixnum-max" | |
663 | (eqv? 1 (remainder (+ fixnum-max 1) fixnum-max))) | |
664 | ||
665 | (pass-if "n = fixnum-max + 1" | |
666 | (eqv? 0 (remainder (+ fixnum-max 1) (+ fixnum-max 1)))) | |
667 | ||
668 | (pass-if "n = fixnum-min" | |
669 | (eqv? 0 (remainder (+ fixnum-max 1) fixnum-min))) | |
670 | ||
671 | (pass-if "n = fixnum-min - 1" | |
672 | (eqv? (+ fixnum-max 1) (remainder (+ fixnum-max 1) (- fixnum-min 1))))) | |
673 | ||
674 | (with-test-prefix "fixnum-min / n" | |
675 | ||
676 | (pass-if "n = 1" | |
677 | (eqv? 0 (remainder fixnum-min 1))) | |
678 | ||
679 | (pass-if "n = -1" | |
680 | (eqv? 0 (remainder fixnum-min -1))) | |
681 | ||
682 | (pass-if "n = fixnum-max" | |
683 | (eqv? -1 (remainder fixnum-min fixnum-max))) | |
684 | ||
685 | (pass-if "n = fixnum-max + 1" | |
686 | (eqv? 0 (remainder fixnum-min (+ fixnum-max 1)))) | |
687 | ||
688 | (pass-if "n = fixnum-min" | |
689 | (eqv? 0 (remainder fixnum-min fixnum-min))) | |
690 | ||
691 | (pass-if "n = fixnum-min - 1" | |
ad22fe7c KR |
692 | (eqv? fixnum-min (remainder fixnum-min (- fixnum-min 1)))) |
693 | ||
694 | (pass-if "n = - fixnum-min - 1" | |
695 | (eqv? -1 (remainder fixnum-min (1- (- fixnum-min))))) | |
696 | ||
697 | ;; special case, normally inum%big is the inum | |
698 | (pass-if "n = - fixnum-min" | |
699 | (eqv? 0 (remainder fixnum-min (- fixnum-min)))) | |
700 | ||
701 | (pass-if "n = - fixnum-min + 1" | |
702 | (eqv? fixnum-min (remainder fixnum-min (1+ (- fixnum-min)))))) | |
21e39e8f DH |
703 | |
704 | (with-test-prefix "(fixnum-min - 1) / n" | |
705 | ||
706 | (pass-if "n = 1" | |
707 | (eqv? 0 (remainder (- fixnum-min 1) 1))) | |
708 | ||
709 | (pass-if "n = -1" | |
710 | (eqv? 0 (remainder (- fixnum-min 1) -1))) | |
711 | ||
712 | (pass-if "n = fixnum-max" | |
713 | (eqv? -2 (remainder (- fixnum-min 1) fixnum-max))) | |
714 | ||
715 | (pass-if "n = fixnum-max + 1" | |
716 | (eqv? -1 (remainder (- fixnum-min 1) (+ fixnum-max 1)))) | |
717 | ||
718 | (pass-if "n = fixnum-min" | |
719 | (eqv? -1 (remainder (- fixnum-min 1) fixnum-min))) | |
720 | ||
721 | (pass-if "n = fixnum-min - 1" | |
722 | (eqv? 0 (remainder (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea DH |
723 | |
724 | ;; Positive dividend and divisor | |
725 | ||
726 | (pass-if "35 / 7" | |
727 | (eqv? 0 (remainder 35 7))) | |
728 | ||
729 | ;; Negative dividend, positive divisor | |
730 | ||
731 | (pass-if "-35 / 7" | |
732 | (eqv? 0 (remainder -35 7))) | |
733 | ||
734 | ;; Positive dividend, negative divisor | |
735 | ||
736 | (pass-if "35 / -7" | |
737 | (eqv? 0 (remainder 35 -7))) | |
738 | ||
739 | ;; Negative dividend and divisor | |
740 | ||
741 | (pass-if "-35 / -7" | |
742 | (eqv? 0 (remainder -35 -7))) | |
743 | ||
744 | ;; Are numerical overflows detected correctly? | |
745 | ||
80074d77 DH |
746 | (with-test-prefix "division by zero" |
747 | ||
748 | (pass-if-exception "(remainder 1 0)" | |
749 | exception:numerical-overflow | |
750 | (remainder 1 0)) | |
751 | ||
752 | (pass-if-exception "(remainder bignum 0)" | |
753 | exception:numerical-overflow | |
754 | (remainder (+ fixnum-max 1) 0))) | |
755 | ||
de142bea DH |
756 | ;; Are wrong type arguments detected correctly? |
757 | ||
758 | ) | |
759 | ||
760 | ;;; | |
761 | ;;; modulo | |
762 | ;;; | |
763 | ||
764 | (with-test-prefix "modulo" | |
765 | ||
de142bea | 766 | (expect-fail "documented?" |
cb18f2a8 | 767 | (documented? modulo)) |
de142bea | 768 | |
de142bea DH |
769 | (with-test-prefix "0 % n" |
770 | ||
771 | (pass-if "n = 1" | |
772 | (eqv? 0 (modulo 0 1))) | |
773 | ||
774 | (pass-if "n = -1" | |
775 | (eqv? 0 (modulo 0 -1))) | |
776 | ||
21e39e8f DH |
777 | (pass-if "n = fixnum-max" |
778 | (eqv? 0 (modulo 0 fixnum-max))) | |
779 | ||
780 | (pass-if "n = fixnum-max + 1" | |
781 | (eqv? 0 (modulo 0 (+ fixnum-max 1)))) | |
de142bea | 782 | |
21e39e8f DH |
783 | (pass-if "n = fixnum-min" |
784 | (eqv? 0 (modulo 0 fixnum-min))) | |
de142bea | 785 | |
21e39e8f DH |
786 | (pass-if "n = fixnum-min - 1" |
787 | (eqv? 0 (modulo 0 (- fixnum-min 1))))) | |
788 | ||
789 | (with-test-prefix "1 % n" | |
de142bea DH |
790 | |
791 | (pass-if "n = 1" | |
792 | (eqv? 0 (modulo 1 1))) | |
793 | ||
794 | (pass-if "n = -1" | |
21e39e8f | 795 | (eqv? 0 (modulo 1 -1))) |
de142bea | 796 | |
21e39e8f DH |
797 | (pass-if "n = fixnum-max" |
798 | (eqv? 1 (modulo 1 fixnum-max))) | |
de142bea | 799 | |
21e39e8f DH |
800 | (pass-if "n = fixnum-max + 1" |
801 | (eqv? 1 (modulo 1 (+ fixnum-max 1)))) | |
de142bea | 802 | |
21e39e8f DH |
803 | (pass-if "n = fixnum-min" |
804 | (eqv? (+ fixnum-min 1) (modulo 1 fixnum-min))) | |
805 | ||
806 | (pass-if "n = fixnum-min - 1" | |
807 | (eqv? fixnum-min (modulo 1 (- fixnum-min 1))))) | |
808 | ||
809 | (with-test-prefix "-1 % n" | |
de142bea DH |
810 | |
811 | (pass-if "n = 1" | |
21e39e8f | 812 | (eqv? 0 (modulo -1 1))) |
de142bea DH |
813 | |
814 | (pass-if "n = -1" | |
815 | (eqv? 0 (modulo -1 -1))) | |
816 | ||
21e39e8f DH |
817 | (pass-if "n = fixnum-max" |
818 | (eqv? (- fixnum-max 1) (modulo -1 fixnum-max))) | |
819 | ||
820 | (pass-if "n = fixnum-max + 1" | |
821 | (eqv? fixnum-max (modulo -1 (+ fixnum-max 1)))) | |
822 | ||
823 | (pass-if "n = fixnum-min" | |
824 | (eqv? -1 (modulo -1 fixnum-min))) | |
825 | ||
826 | (pass-if "n = fixnum-min - 1" | |
827 | (eqv? -1 (modulo -1 (- fixnum-min 1))))) | |
828 | ||
829 | (with-test-prefix "fixnum-max % n" | |
830 | ||
831 | (pass-if "n = 1" | |
832 | (eqv? 0 (modulo fixnum-max 1))) | |
833 | ||
834 | (pass-if "n = -1" | |
835 | (eqv? 0 (modulo fixnum-max -1))) | |
836 | ||
837 | (pass-if "n = fixnum-max" | |
838 | (eqv? 0 (modulo fixnum-max fixnum-max))) | |
839 | ||
840 | (pass-if "n = fixnum-max + 1" | |
841 | (eqv? fixnum-max (modulo fixnum-max (+ fixnum-max 1)))) | |
842 | ||
843 | (pass-if "n = fixnum-min" | |
844 | (eqv? -1 (modulo fixnum-max fixnum-min))) | |
845 | ||
846 | (pass-if "n = fixnum-min - 1" | |
847 | (eqv? -2 (modulo fixnum-max (- fixnum-min 1))))) | |
848 | ||
849 | (with-test-prefix "(fixnum-max + 1) % n" | |
850 | ||
851 | (pass-if "n = 1" | |
852 | (eqv? 0 (modulo (+ fixnum-max 1) 1))) | |
853 | ||
854 | (pass-if "n = -1" | |
855 | (eqv? 0 (modulo (+ fixnum-max 1) -1))) | |
856 | ||
857 | (pass-if "n = fixnum-max" | |
858 | (eqv? 1 (modulo (+ fixnum-max 1) fixnum-max))) | |
859 | ||
860 | (pass-if "n = fixnum-max + 1" | |
861 | (eqv? 0 (modulo (+ fixnum-max 1) (+ fixnum-max 1)))) | |
862 | ||
863 | (pass-if "n = fixnum-min" | |
864 | (eqv? 0 (modulo (+ fixnum-max 1) fixnum-min))) | |
865 | ||
866 | (pass-if "n = fixnum-min - 1" | |
867 | (eqv? -1 (modulo (+ fixnum-max 1) (- fixnum-min 1))))) | |
868 | ||
869 | (with-test-prefix "fixnum-min % n" | |
870 | ||
871 | (pass-if "n = 1" | |
872 | (eqv? 0 (modulo fixnum-min 1))) | |
873 | ||
874 | (pass-if "n = -1" | |
875 | (eqv? 0 (modulo fixnum-min -1))) | |
876 | ||
877 | (pass-if "n = fixnum-max" | |
878 | (eqv? (- fixnum-max 1) (modulo fixnum-min fixnum-max))) | |
879 | ||
880 | (pass-if "n = fixnum-max + 1" | |
881 | (eqv? 0 (modulo fixnum-min (+ fixnum-max 1)))) | |
882 | ||
883 | (pass-if "n = fixnum-min" | |
884 | (eqv? 0 (modulo fixnum-min fixnum-min))) | |
885 | ||
886 | (pass-if "n = fixnum-min - 1" | |
887 | (eqv? fixnum-min (modulo fixnum-min (- fixnum-min 1))))) | |
888 | ||
889 | (with-test-prefix "(fixnum-min - 1) % n" | |
890 | ||
891 | (pass-if "n = 1" | |
892 | (eqv? 0 (modulo (- fixnum-min 1) 1))) | |
893 | ||
894 | (pass-if "n = -1" | |
895 | (eqv? 0 (modulo (- fixnum-min 1) -1))) | |
896 | ||
897 | (pass-if "n = fixnum-max" | |
898 | (eqv? (- fixnum-max 2) (modulo (- fixnum-min 1) fixnum-max))) | |
899 | ||
900 | (pass-if "n = fixnum-max + 1" | |
901 | (eqv? fixnum-max (modulo (- fixnum-min 1) (+ fixnum-max 1)))) | |
902 | ||
903 | (pass-if "n = fixnum-min" | |
904 | (eqv? -1 (modulo (- fixnum-min 1) fixnum-min))) | |
905 | ||
906 | (pass-if "n = fixnum-min - 1" | |
907 | (eqv? 0 (modulo (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea DH |
908 | |
909 | ;; Positive dividend and divisor | |
910 | ||
911 | (pass-if "13 % 4" | |
912 | (eqv? 1 (modulo 13 4))) | |
913 | ||
914 | (pass-if "2177452800 % 86400" | |
915 | (eqv? 0 (modulo 2177452800 86400))) | |
916 | ||
917 | ;; Negative dividend, positive divisor | |
918 | ||
919 | (pass-if "-13 % 4" | |
920 | (eqv? 3 (modulo -13 4))) | |
921 | ||
922 | (pass-if "-2177452800 % 86400" | |
923 | (eqv? 0 (modulo -2177452800 86400))) | |
924 | ||
925 | ;; Positive dividend, negative divisor | |
926 | ||
927 | (pass-if "13 % -4" | |
928 | (eqv? -3 (modulo 13 -4))) | |
929 | ||
930 | (pass-if "2177452800 % -86400" | |
931 | (eqv? 0 (modulo 2177452800 -86400))) | |
932 | ||
933 | ;; Negative dividend and divisor | |
934 | ||
935 | (pass-if "-13 % -4" | |
936 | (eqv? -1 (modulo -13 -4))) | |
937 | ||
938 | (pass-if "-2177452800 % -86400" | |
939 | (eqv? 0 (modulo -2177452800 -86400))) | |
940 | ||
941 | ;; Are numerical overflows detected correctly? | |
942 | ||
80074d77 DH |
943 | (with-test-prefix "division by zero" |
944 | ||
945 | (pass-if-exception "(modulo 1 0)" | |
946 | exception:numerical-overflow | |
947 | (modulo 1 0)) | |
948 | ||
949 | (pass-if-exception "(modulo bignum 0)" | |
950 | exception:numerical-overflow | |
951 | (modulo (+ fixnum-max 1) 0))) | |
952 | ||
de142bea DH |
953 | ;; Are wrong type arguments detected correctly? |
954 | ||
955 | ) | |
956 | ||
24360e11 KR |
957 | ;;; |
958 | ;;; modulo-expt | |
959 | ;;; | |
960 | ||
961 | (with-test-prefix "modulo-expt" | |
962 | (pass-if (= 1 (modulo-expt 17 23 47))) | |
963 | ||
964 | (pass-if (= 1 (modulo-expt 17 -23 47))) | |
965 | ||
966 | (pass-if (= 17 (modulo-expt 17 -22 47))) | |
967 | ||
968 | (pass-if (= 36 (modulo-expt 17 22 47))) | |
969 | ||
970 | (pass-if (= 183658794479969134816674175082294846241553725240 (modulo-expt 111122223333444455556666 111122223333444455556666 1153478690012629968439432872520758982731022934717))) | |
971 | ||
972 | (pass-if-exception | |
973 | "Proper exception with 0 modulus" | |
18ee5de9 | 974 | exception:numerical-overflow |
24360e11 KR |
975 | (modulo-expt 17 23 0)) |
976 | ||
977 | (pass-if-exception | |
978 | "Proper exception when result not invertible" | |
18ee5de9 | 979 | exception:numerical-overflow |
24360e11 KR |
980 | (modulo-expt 10 -1 48)) |
981 | ||
982 | (pass-if-exception | |
983 | "Proper exception with wrong type argument" | |
18ee5de9 | 984 | exception:wrong-type-arg |
24360e11 KR |
985 | (modulo-expt "Sam" 23 10)) |
986 | ||
987 | (pass-if-exception | |
988 | "Proper exception with wrong type argument" | |
18ee5de9 | 989 | exception:wrong-type-arg |
24360e11 KR |
990 | (modulo-expt 17 9.9 10)) |
991 | ||
992 | (pass-if-exception | |
993 | "Proper exception with wrong type argument" | |
18ee5de9 | 994 | exception:wrong-type-arg |
24360e11 KR |
995 | (modulo-expt 17 23 'Ethel))) |
996 | ||
ba46895c KR |
997 | ;;; |
998 | ;;; numerator | |
999 | ;;; | |
1000 | ||
1001 | (with-test-prefix "numerator" | |
1002 | (pass-if "0" | |
1003 | (eqv? 0 (numerator 0))) | |
1004 | (pass-if "1" | |
1005 | (eqv? 1 (numerator 1))) | |
1006 | (pass-if "2" | |
1007 | (eqv? 2 (numerator 2))) | |
1008 | (pass-if "-1" | |
1009 | (eqv? -1 (numerator -1))) | |
1010 | (pass-if "-2" | |
1011 | (eqv? -2 (numerator -2))) | |
1012 | ||
1013 | (pass-if "0.0" | |
1014 | (eqv? 0.0 (numerator 0.0))) | |
1015 | (pass-if "1.0" | |
1016 | (eqv? 1.0 (numerator 1.0))) | |
1017 | (pass-if "2.0" | |
1018 | (eqv? 2.0 (numerator 2.0))) | |
1019 | (pass-if "-1.0" | |
1020 | (eqv? -1.0 (numerator -1.0))) | |
1021 | (pass-if "-2.0" | |
1022 | (eqv? -2.0 (numerator -2.0))) | |
1023 | ||
1024 | (pass-if "0.5" | |
1025 | (eqv? 1.0 (numerator 0.5))) | |
1026 | (pass-if "0.25" | |
1027 | (eqv? 1.0 (numerator 0.25))) | |
1028 | (pass-if "0.75" | |
1029 | (eqv? 3.0 (numerator 0.75)))) | |
1030 | ||
1031 | ;;; | |
1032 | ;;; denominator | |
1033 | ;;; | |
1034 | ||
1035 | (with-test-prefix "denominator" | |
1036 | (pass-if "0" | |
1037 | (eqv? 1 (denominator 0))) | |
1038 | (pass-if "1" | |
1039 | (eqv? 1 (denominator 1))) | |
1040 | (pass-if "2" | |
1041 | (eqv? 1 (denominator 2))) | |
1042 | (pass-if "-1" | |
1043 | (eqv? 1 (denominator -1))) | |
1044 | (pass-if "-2" | |
1045 | (eqv? 1 (denominator -2))) | |
1046 | ||
1047 | (pass-if "0.0" | |
1048 | (eqv? 1.0 (denominator 0.0))) | |
1049 | (pass-if "1.0" | |
1050 | (eqv? 1.0 (denominator 1.0))) | |
1051 | (pass-if "2.0" | |
1052 | (eqv? 1.0 (denominator 2.0))) | |
1053 | (pass-if "-1.0" | |
1054 | (eqv? 1.0 (denominator -1.0))) | |
1055 | (pass-if "-2.0" | |
1056 | (eqv? 1.0 (denominator -2.0))) | |
1057 | ||
1058 | (pass-if "0.5" | |
1059 | (eqv? 2.0 (denominator 0.5))) | |
1060 | (pass-if "0.25" | |
1061 | (eqv? 4.0 (denominator 0.25))) | |
1062 | (pass-if "0.75" | |
1063 | (eqv? 4.0 (denominator 0.75)))) | |
1064 | ||
de142bea DH |
1065 | ;;; |
1066 | ;;; gcd | |
1067 | ;;; | |
1068 | ||
1069 | (with-test-prefix "gcd" | |
1070 | ||
d389e966 | 1071 | (pass-if "documented?" |
cb18f2a8 | 1072 | (documented? gcd)) |
de142bea | 1073 | |
1dd79792 NJ |
1074 | (with-test-prefix "(n)" |
1075 | ||
1076 | (pass-if "n = -2" | |
1077 | (eqv? 2 (gcd -2)))) | |
1078 | ||
de142bea DH |
1079 | (with-test-prefix "(0 n)" |
1080 | ||
21e39e8f DH |
1081 | (pass-if "n = 0" |
1082 | (eqv? 0 (gcd 0 0))) | |
1083 | ||
de142bea DH |
1084 | (pass-if "n = 1" |
1085 | (eqv? 1 (gcd 0 1))) | |
1086 | ||
1087 | (pass-if "n = -1" | |
1088 | (eqv? 1 (gcd 0 -1))) | |
1089 | ||
21e39e8f DH |
1090 | (pass-if "n = fixnum-max" |
1091 | (eqv? fixnum-max (gcd 0 fixnum-max))) | |
de142bea | 1092 | |
21e39e8f DH |
1093 | (pass-if "n = fixnum-max + 1" |
1094 | (eqv? (+ fixnum-max 1) (gcd 0 (+ fixnum-max 1)))) | |
de142bea | 1095 | |
21e39e8f DH |
1096 | (pass-if "n = fixnum-min" |
1097 | (eqv? (- fixnum-min) (gcd 0 fixnum-min))) | |
de142bea | 1098 | |
21e39e8f DH |
1099 | (pass-if "n = fixnum-min - 1" |
1100 | (eqv? (- (- fixnum-min 1)) (gcd 0 (- fixnum-min 1))))) | |
1101 | ||
db386f80 KR |
1102 | (with-test-prefix "(n 0)" |
1103 | ||
1104 | (pass-if "n = 2^128 * fixnum-max" | |
1105 | (eqv? (ash fixnum-max 128) (gcd (ash fixnum-max 128) 0)))) | |
1106 | ||
21e39e8f DH |
1107 | (with-test-prefix "(1 n)" |
1108 | ||
1109 | (pass-if "n = 0" | |
de142bea DH |
1110 | (eqv? 1 (gcd 1 0))) |
1111 | ||
21e39e8f DH |
1112 | (pass-if "n = 1" |
1113 | (eqv? 1 (gcd 1 1))) | |
1114 | ||
de142bea | 1115 | (pass-if "n = -1" |
21e39e8f DH |
1116 | (eqv? 1 (gcd 1 -1))) |
1117 | ||
1118 | (pass-if "n = fixnum-max" | |
1119 | (eqv? 1 (gcd 1 fixnum-max))) | |
1120 | ||
1121 | (pass-if "n = fixnum-max + 1" | |
1122 | (eqv? 1 (gcd 1 (+ fixnum-max 1)))) | |
1123 | ||
1124 | (pass-if "n = fixnum-min" | |
1125 | (eqv? 1 (gcd 1 fixnum-min))) | |
1126 | ||
1127 | (pass-if "n = fixnum-min - 1" | |
1128 | (eqv? 1 (gcd 1 (- fixnum-min 1))))) | |
1129 | ||
1130 | (with-test-prefix "(-1 n)" | |
1131 | ||
1132 | (pass-if "n = 0" | |
de142bea DH |
1133 | (eqv? 1 (gcd -1 0))) |
1134 | ||
21e39e8f DH |
1135 | (pass-if "n = 1" |
1136 | (eqv? 1 (gcd -1 1))) | |
de142bea | 1137 | |
21e39e8f DH |
1138 | (pass-if "n = -1" |
1139 | (eqv? 1 (gcd -1 -1))) | |
de142bea | 1140 | |
21e39e8f DH |
1141 | (pass-if "n = fixnum-max" |
1142 | (eqv? 1 (gcd -1 fixnum-max))) | |
1143 | ||
1144 | (pass-if "n = fixnum-max + 1" | |
1145 | (eqv? 1 (gcd -1 (+ fixnum-max 1)))) | |
1146 | ||
1147 | (pass-if "n = fixnum-min" | |
1148 | (eqv? 1 (gcd -1 fixnum-min))) | |
1149 | ||
1150 | (pass-if "n = fixnum-min - 1" | |
1151 | (eqv? 1 (gcd -1 (- fixnum-min 1))))) | |
1152 | ||
1153 | (with-test-prefix "(fixnum-max n)" | |
1154 | ||
1155 | (pass-if "n = 0" | |
1156 | (eqv? fixnum-max (gcd fixnum-max 0))) | |
de142bea DH |
1157 | |
1158 | (pass-if "n = 1" | |
21e39e8f | 1159 | (eqv? 1 (gcd fixnum-max 1))) |
de142bea DH |
1160 | |
1161 | (pass-if "n = -1" | |
21e39e8f DH |
1162 | (eqv? 1 (gcd fixnum-max -1))) |
1163 | ||
1164 | (pass-if "n = fixnum-max" | |
1165 | (eqv? fixnum-max (gcd fixnum-max fixnum-max))) | |
de142bea | 1166 | |
21e39e8f DH |
1167 | (pass-if "n = fixnum-max + 1" |
1168 | (eqv? 1 (gcd fixnum-max (+ fixnum-max 1)))) | |
de142bea | 1169 | |
21e39e8f DH |
1170 | (pass-if "n = fixnum-min" |
1171 | (eqv? 1 (gcd fixnum-max fixnum-min))) | |
de142bea | 1172 | |
21e39e8f DH |
1173 | (pass-if "n = fixnum-min - 1" |
1174 | (eqv? 1 (gcd fixnum-max (- fixnum-min 1))))) | |
1175 | ||
1176 | (with-test-prefix "((+ fixnum-max 1) n)" | |
1177 | ||
1178 | (pass-if "n = 0" | |
1179 | (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) 0))) | |
1180 | ||
1181 | (pass-if "n = 1" | |
1182 | (eqv? 1 (gcd (+ fixnum-max 1) 1))) | |
de142bea DH |
1183 | |
1184 | (pass-if "n = -1" | |
21e39e8f | 1185 | (eqv? 1 (gcd (+ fixnum-max 1) -1))) |
de142bea | 1186 | |
21e39e8f DH |
1187 | (pass-if "n = fixnum-max" |
1188 | (eqv? 1 (gcd (+ fixnum-max 1) fixnum-max))) | |
de142bea | 1189 | |
21e39e8f DH |
1190 | (pass-if "n = fixnum-max + 1" |
1191 | (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) (+ fixnum-max 1)))) | |
de142bea | 1192 | |
21e39e8f DH |
1193 | (pass-if "n = fixnum-min" |
1194 | (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) fixnum-min))) | |
1195 | ||
1196 | (pass-if "n = fixnum-min - 1" | |
1197 | (eqv? 1 (gcd (+ fixnum-max 1) (- fixnum-min 1))))) | |
1198 | ||
1199 | (with-test-prefix "(fixnum-min n)" | |
1200 | ||
1201 | (pass-if "n = 0" | |
1202 | (eqv? (- fixnum-min) (gcd fixnum-min 0))) | |
1203 | ||
1204 | (pass-if "n = 1" | |
1205 | (eqv? 1 (gcd fixnum-min 1))) | |
de142bea DH |
1206 | |
1207 | (pass-if "n = -1" | |
21e39e8f DH |
1208 | (eqv? 1 (gcd fixnum-min -1))) |
1209 | ||
1210 | (pass-if "n = fixnum-max" | |
1211 | (eqv? 1 (gcd fixnum-min fixnum-max))) | |
1212 | ||
1213 | (pass-if "n = fixnum-max + 1" | |
1214 | (eqv? (+ fixnum-max 1) (gcd fixnum-min (+ fixnum-max 1)))) | |
de142bea | 1215 | |
21e39e8f DH |
1216 | (pass-if "n = fixnum-min" |
1217 | (eqv? (- fixnum-min) (gcd fixnum-min fixnum-min))) | |
1218 | ||
1219 | (pass-if "n = fixnum-min - 1" | |
1220 | (eqv? 1 (gcd fixnum-min (- fixnum-min 1))))) | |
1221 | ||
1222 | (with-test-prefix "((- fixnum-min 1) n)" | |
1223 | ||
1224 | (pass-if "n = 0" | |
1225 | (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) 0))) | |
1226 | ||
1227 | (pass-if "n = 1" | |
1228 | (eqv? 1 (gcd (- fixnum-min 1) 1))) | |
1229 | ||
1230 | (pass-if "n = -1" | |
1231 | (eqv? 1 (gcd (- fixnum-min 1) -1))) | |
1232 | ||
1233 | (pass-if "n = fixnum-max" | |
1234 | (eqv? 1 (gcd (- fixnum-min 1) fixnum-max))) | |
1235 | ||
1236 | (pass-if "n = fixnum-max + 1" | |
1237 | (eqv? 1 (gcd (- fixnum-min 1) (+ fixnum-max 1)))) | |
1238 | ||
1239 | (pass-if "n = fixnum-min" | |
1240 | (eqv? 1 (gcd (- fixnum-min 1) fixnum-min))) | |
1241 | ||
1242 | (pass-if "n = fixnum-min - 1" | |
1243 | (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) (- fixnum-min 1))))) | |
de142bea DH |
1244 | |
1245 | ;; Are wrong type arguments detected correctly? | |
1246 | ||
1247 | ) | |
1248 | ||
f29b3454 DH |
1249 | ;;; |
1250 | ;;; lcm | |
1251 | ;;; | |
1252 | ||
7c24e528 RB |
1253 | (with-test-prefix "lcm" |
1254 | ;; FIXME: more tests? | |
1255 | ;; (some of these are already in r4rs.test) | |
d389e966 | 1256 | (pass-if (documented? lcm)) |
7c24e528 RB |
1257 | (pass-if (= (lcm) 1)) |
1258 | (pass-if (= (lcm 32 -36) 288)) | |
1259 | (let ((big-n 115792089237316195423570985008687907853269984665640564039457584007913129639936) ; 2 ^ 256 | |
1260 | (lcm-of-big-n-and-11 1273712981610478149659280835095566986385969831322046204434033424087044426039296)) | |
1261 | (pass-if (= lcm-of-big-n-and-11 (lcm big-n 11))) | |
1262 | (pass-if (= lcm-of-big-n-and-11 (lcm 11 big-n 11))))) | |
1263 | ||
f29b3454 DH |
1264 | ;;; |
1265 | ;;; number->string | |
1266 | ;;; | |
1267 | ||
7c24e528 RB |
1268 | (with-test-prefix "number->string" |
1269 | (let ((num->str->num | |
1270 | (lambda (n radix) | |
1271 | (string->number (number->string n radix) radix)))) | |
1272 | ||
1273 | (pass-if (documented? number->string)) | |
1274 | (pass-if (string=? (number->string 0) "0")) | |
1275 | (pass-if (string=? (number->string 171) "171")) | |
1276 | (pass-if (= (+ fixnum-max 1) (num->str->num (+ fixnum-max 1) 10))) | |
1277 | (pass-if (= (- fixnum-min 1) (num->str->num (- fixnum-min 1) 10))) | |
1278 | (pass-if (= (inf) (num->str->num (inf) 10))) | |
d39a7b58 MV |
1279 | (pass-if (= 1.3 (num->str->num 1.3 10))) |
1280 | ||
eb73f94b MV |
1281 | ;; XXX - some results depend on whether Guile is compiled optimzed |
1282 | ;; or not. It is clearly undesirable to have number->string to be | |
1283 | ;; influenced by this. | |
1284 | ||
3740c788 | 1285 | (pass-if (string=? (number->string 35.25 36) "z.9")) |
eb73f94b MV |
1286 | (pass-if (or (string=? (number->string 0.25 2) "0.01") |
1287 | (string=? (number->string 0.25 2) "0.010"))) | |
3740c788 | 1288 | (pass-if (string=? (number->string 255.0625 16) "ff.1")) |
d39a7b58 | 1289 | (pass-if (string=? (number->string (/ 1 3) 3) "1/10")) |
23f2b9a3 | 1290 | |
a6f3af16 | 1291 | (pass-if (string=? (number->string 10) "10")) |
3740c788 | 1292 | (pass-if (string=? (number->string 10 11) "a")) |
a6f3af16 AW |
1293 | (pass-if (string=? (number->string 36 36) "10")) |
1294 | (pass-if (= (num->str->num 36 36) 36)) | |
1295 | (pass-if (= (string->number "z" 36) 35)) | |
1296 | (pass-if (= (string->number "Z" 36) 35)) | |
1297 | (pass-if (not (string->number "Z" 35))) | |
3740c788 | 1298 | (pass-if (string=? (number->string 35 36) "z")) |
a6f3af16 AW |
1299 | (pass-if (= (num->str->num 35 36) 35)) |
1300 | ||
23f2b9a3 KR |
1301 | ;; Numeric conversion from decimal is not precise, in its current |
1302 | ;; implementation, so 11.333... and 1.324... can't be expected to | |
1303 | ;; reliably come out to precise values. These tests did actually work | |
1304 | ;; for a while, but something in gcc changed, affecting the conversion | |
1305 | ;; code. | |
1306 | ;; | |
1307 | ;; (pass-if (or (string=? (number->string 11.33333333333333333 12) | |
1308 | ;; "B.4") | |
1309 | ;; (string=? (number->string 11.33333333333333333 12) | |
1310 | ;; "B.400000000000009"))) | |
1311 | ;; (pass-if (or (string=? (number->string 1.324e44 16) | |
1312 | ;; "5.EFE0A14FAFEe24") | |
1313 | ;; (string=? (number->string 1.324e44 16) | |
1314 | ;; "5.EFE0A14FAFDF8e24"))) | |
1315 | )) | |
7c24e528 | 1316 | |
f29b3454 DH |
1317 | ;;; |
1318 | ;;; string->number | |
1319 | ;;; | |
1320 | ||
2f4a254a DH |
1321 | (with-test-prefix "string->number" |
1322 | ||
ff758237 | 1323 | (pass-if "documented?" |
2f4a254a DH |
1324 | (documented? string->number)) |
1325 | ||
1326 | (pass-if "non number strings" | |
1327 | (for-each (lambda (x) (if (string->number x) (throw 'fail))) | |
569c483b | 1328 | '("" "q" "1q" "6+7iq" "8+9q" "10+11" "13+" "18@19q" "20@q" "23@" |
2f4a254a | 1329 | "+25iq" "26i" "-q" "-iq" "i" "5#.0" "8/" "10#11" ".#" "." |
569c483b | 1330 | "#o.2" "3.4q" "15.16e17q" "18.19e+q" ".q" ".17#18" "10q" "#b2" |
2f4a254a DH |
1331 | "#b3" "#b4" "#b5" "#b6" "#b7" "#b8" "#b9" "#ba" "#bb" "#bc" |
1332 | "#bd" "#be" "#bf" "#q" "#b#b1" "#o#o1" "#d#d1" "#x#x1" "#e#e1" | |
1333 | "#i#i1" "12@12+0i")) | |
1334 | #t) | |
1335 | ||
b7d9b1cf DH |
1336 | (pass-if "valid number strings" |
1337 | (for-each (lambda (couple) | |
1338 | (apply | |
1339 | (lambda (x y) | |
9dd9857f MV |
1340 | (let ((xx (string->number x))) |
1341 | (if (or (eq? xx #f) (not (eqv? xx y))) | |
ca2b31fe MV |
1342 | (begin |
1343 | (pk x y) | |
1344 | (throw 'fail))))) | |
b7d9b1cf DH |
1345 | couple)) |
1346 | `(;; Radix: | |
1347 | ("#b0" 0) ("#B0" 0) ("#b1" 1) ("#B1" 1) ("#o0" 0) ("#O0" 0) | |
1348 | ("#o1" 1) ("#O1" 1) ("#o2" 2) ("#O2" 2) ("#o3" 3) ("#O3" 3) | |
1349 | ("#o4" 4) ("#O4" 4) ("#o5" 5) ("#O5" 5) ("#o6" 6) ("#O6" 6) | |
1350 | ("#o7" 7) ("#O7" 7) ("#d0" 0) ("#D0" 0) ("#d1" 1) ("#D1" 1) | |
1351 | ("#d2" 2) ("#D2" 2) ("#d3" 3) ("#D3" 3) ("#d4" 4) ("#D4" 4) | |
1352 | ("#d5" 5) ("#D5" 5) ("#d6" 6) ("#D6" 6) ("#d7" 7) ("#D7" 7) | |
1353 | ("#d8" 8) ("#D8" 8) ("#d9" 9) ("#D9" 9) | |
1354 | ("#xa" 10) ("#Xa" 10) ("#xb" 11) ("#Xb" 11) | |
1355 | ("#xc" 12) ("#Xc" 12) ("#xd" 13) ("#Xd" 13) | |
1356 | ("#xe" 14) ("#Xe" 14) ("#xf" 15) ("#Xf" 15) | |
1357 | ("#b1010" 10) | |
1358 | ("#o12345670" 2739128) | |
1359 | ("#d1234567890" 1234567890) | |
1360 | ("#x1234567890abcdef" 1311768467294899695) | |
1361 | ;; Exactness: | |
ca2b31fe | 1362 | ("#e1" 1) ("#e1.2" 12/10) |
9dd9857f | 1363 | ("#i1.1" 1.1) ("#i1" 1.0) |
b7d9b1cf DH |
1364 | ;; Integers: |
1365 | ("1" ,(1+ 0)) ("23" ,(+ 9 9 5)) ("-1" ,(- 0 1)) | |
1366 | ("-45" ,(- 0 45)) ("2#" 20.0) ("2##" 200.0) ("12##" 1200.0) | |
1367 | ("#b#i100" 4.0) | |
9dd9857f MV |
1368 | ;; Fractions: |
1369 | ("1/1" 1) ("1/2" 1/2) ("-1/2" -1/2) ("1#/1" 10.0) | |
1370 | ("10/1#" 1.0) ("1#/1#" 1.0) ("#e9/10" 9/10) ("#e10/1#" 1) | |
b7d9b1cf DH |
1371 | ("#i6/8" 0.75) ("#i1/1" 1.0) |
1372 | ;; Decimal numbers: | |
1373 | ;; * <uinteger 10> <suffix> | |
1374 | ("1e2" 100.0) ("1E2" 100.0) ("1s2" 100.0) ("1S2" 100.0) | |
1375 | ("1f2" 100.0) ("1F2" 100.0) ("1d2" 100.0) ("1D2" 100.0) | |
1376 | ("1l2" 100.0) ("1L2" 100.0) ("1e+2" 100.0) ("1e-2" 0.01) | |
1377 | ;; * . <digit 10>+ #* <suffix> | |
1378 | (".1" .1) (".0123456789" 123456789e-10) (".16#" 0.16) | |
1379 | (".0123456789e10" 123456789.0) (".16#e3" 160.0) ("#d.3" 0.3) | |
1380 | ;; * <digit 10>+ . <digit 10>* #* <suffix> | |
1381 | ("3." ,(exact->inexact 3)) ("3.e0" ,(exact->inexact 3)) | |
1382 | ("3.1" ,(exact->inexact 31/10)) ("3.1e0" 3.1) ("3.1#" 3.1) | |
1383 | ("3.1#e0" 3.1) | |
1384 | ;; * <digit 10>+ #+ . #* <suffix> | |
1385 | ("3#." 30.0) ("3#.e0" 30.0) ("3#.#" 30.0) ("3#.#e0" 30.0) | |
1386 | ;; Complex: | |
1387 | ("1@0" 1.0) ("1@+0" 1.0) ("1@-0" 1.0) | |
1388 | ("2+3i" ,(+ 2 (* 3 +i))) ("4-5i" ,(- 4 (* 5 +i))) | |
1389 | ("1+i" 1+1i) ("1-i" 1-1i) ("+1i" 0+1i) ("-1i" 0-1i) | |
40f89215 NJ |
1390 | ("+i" +1i) ("-i" -1i) |
1391 | ("1.0+.1i" 1.0+0.1i) | |
1392 | ("1.0-.1i" 1.0-0.1i) | |
1393 | (".1+.0i" 0.1) | |
1394 | ("1.+.0i" 1.0) | |
1395 | (".1+.1i" 0.1+0.1i) | |
1396 | ("1e1+.1i" 10+0.1i) | |
1397 | )) | |
b7d9b1cf DH |
1398 | #t) |
1399 | ||
2f4a254a DH |
1400 | (pass-if-exception "exponent too big" |
1401 | exception:out-of-range | |
48e78ba6 KR |
1402 | (string->number "12.13e141414")) |
1403 | ||
1404 | ;; in guile 1.6.7 and earlier, bad polar forms (where the conversion of | |
1405 | ;; the angle gave #f) caused a segv | |
1406 | (pass-if "1@a" | |
1407 | (eq? #f (string->number "1@a")))) | |
2f4a254a | 1408 | |
f29b3454 DH |
1409 | ;;; |
1410 | ;;; number? | |
1411 | ;;; | |
1412 | ||
7c24e528 RB |
1413 | (with-test-prefix "number?" |
1414 | (pass-if (documented? number?)) | |
1415 | (pass-if (number? 0)) | |
1416 | (pass-if (number? 7)) | |
1417 | (pass-if (number? -7)) | |
1418 | (pass-if (number? 1.3)) | |
1419 | (pass-if (number? (+ 1 fixnum-max))) | |
1420 | (pass-if (number? (- 1 fixnum-min))) | |
1421 | (pass-if (number? 3+4i)) | |
4d332f19 DH |
1422 | (pass-if (not (number? #\a))) |
1423 | (pass-if (not (number? "a"))) | |
1424 | (pass-if (not (number? (make-vector 0)))) | |
1425 | (pass-if (not (number? (cons 1 2)))) | |
1426 | (pass-if (not (number? #t))) | |
1427 | (pass-if (not (number? (lambda () #t)))) | |
1428 | (pass-if (not (number? (current-input-port))))) | |
7c24e528 | 1429 | |
f29b3454 DH |
1430 | ;;; |
1431 | ;;; complex? | |
1432 | ;;; | |
1433 | ||
7c24e528 RB |
1434 | (with-test-prefix "complex?" |
1435 | (pass-if (documented? complex?)) | |
1436 | (pass-if (complex? 0)) | |
1437 | (pass-if (complex? 7)) | |
1438 | (pass-if (complex? -7)) | |
1439 | (pass-if (complex? (+ 1 fixnum-max))) | |
1440 | (pass-if (complex? (- 1 fixnum-min))) | |
1441 | (pass-if (complex? 1.3)) | |
1442 | (pass-if (complex? 3+4i)) | |
4d332f19 DH |
1443 | (pass-if (not (complex? #\a))) |
1444 | (pass-if (not (complex? "a"))) | |
1445 | (pass-if (not (complex? (make-vector 0)))) | |
1446 | (pass-if (not (complex? (cons 1 2)))) | |
1447 | (pass-if (not (complex? #t))) | |
1448 | (pass-if (not (complex? (lambda () #t)))) | |
1449 | (pass-if (not (complex? (current-input-port))))) | |
7c24e528 | 1450 | |
f29b3454 DH |
1451 | ;;; |
1452 | ;;; real? | |
1453 | ;;; | |
1454 | ||
7c24e528 RB |
1455 | (with-test-prefix "real?" |
1456 | (pass-if (documented? real?)) | |
1457 | (pass-if (real? 0)) | |
1458 | (pass-if (real? 7)) | |
1459 | (pass-if (real? -7)) | |
1460 | (pass-if (real? (+ 1 fixnum-max))) | |
1461 | (pass-if (real? (- 1 fixnum-min))) | |
1462 | (pass-if (real? 1.3)) | |
4d332f19 DH |
1463 | (pass-if (not (real? 3+4i))) |
1464 | (pass-if (not (real? #\a))) | |
1465 | (pass-if (not (real? "a"))) | |
1466 | (pass-if (not (real? (make-vector 0)))) | |
1467 | (pass-if (not (real? (cons 1 2)))) | |
1468 | (pass-if (not (real? #t))) | |
1469 | (pass-if (not (real? (lambda () #t)))) | |
1470 | (pass-if (not (real? (current-input-port))))) | |
7c24e528 | 1471 | |
f29b3454 | 1472 | ;;; |
7c24e528 | 1473 | ;;; rational? (same as real? right now) |
f29b3454 DH |
1474 | ;;; |
1475 | ||
7c24e528 RB |
1476 | (with-test-prefix "rational?" |
1477 | (pass-if (documented? rational?)) | |
1478 | (pass-if (rational? 0)) | |
1479 | (pass-if (rational? 7)) | |
1480 | (pass-if (rational? -7)) | |
1481 | (pass-if (rational? (+ 1 fixnum-max))) | |
1482 | (pass-if (rational? (- 1 fixnum-min))) | |
1483 | (pass-if (rational? 1.3)) | |
4d332f19 DH |
1484 | (pass-if (not (rational? 3+4i))) |
1485 | (pass-if (not (rational? #\a))) | |
1486 | (pass-if (not (rational? "a"))) | |
1487 | (pass-if (not (rational? (make-vector 0)))) | |
1488 | (pass-if (not (rational? (cons 1 2)))) | |
1489 | (pass-if (not (rational? #t))) | |
1490 | (pass-if (not (rational? (lambda () #t)))) | |
1491 | (pass-if (not (rational? (current-input-port))))) | |
7c24e528 | 1492 | |
f29b3454 DH |
1493 | ;;; |
1494 | ;;; integer? | |
1495 | ;;; | |
1496 | ||
7c24e528 RB |
1497 | (with-test-prefix "integer?" |
1498 | (pass-if (documented? integer?)) | |
1499 | (pass-if (integer? 0)) | |
1500 | (pass-if (integer? 7)) | |
1501 | (pass-if (integer? -7)) | |
1502 | (pass-if (integer? (+ 1 fixnum-max))) | |
1503 | (pass-if (integer? (- 1 fixnum-min))) | |
1504 | (pass-if (and (= 3+0i (round 3+0i)) (integer? 3+0i))) | |
1505 | (pass-if (and (= 1.0 (round 1.0)) (integer? 1.0))) | |
4d332f19 | 1506 | (pass-if (not (integer? 1.3))) |
e456f08e KR |
1507 | (pass-if (integer? +inf.0)) |
1508 | (pass-if (integer? -inf.0)) | |
c1122753 | 1509 | (pass-if (not (integer? +nan.0))) |
4d332f19 DH |
1510 | (pass-if (not (integer? 3+4i))) |
1511 | (pass-if (not (integer? #\a))) | |
1512 | (pass-if (not (integer? "a"))) | |
1513 | (pass-if (not (integer? (make-vector 0)))) | |
1514 | (pass-if (not (integer? (cons 1 2)))) | |
1515 | (pass-if (not (integer? #t))) | |
1516 | (pass-if (not (integer? (lambda () #t)))) | |
1517 | (pass-if (not (integer? (current-input-port))))) | |
7c24e528 | 1518 | |
f29b3454 DH |
1519 | ;;; |
1520 | ;;; inexact? | |
1521 | ;;; | |
1522 | ||
7c24e528 RB |
1523 | (with-test-prefix "inexact?" |
1524 | (pass-if (documented? inexact?)) | |
4d332f19 DH |
1525 | (pass-if (not (inexact? 0))) |
1526 | (pass-if (not (inexact? 7))) | |
1527 | (pass-if (not (inexact? -7))) | |
1528 | (pass-if (not (inexact? (+ 1 fixnum-max)))) | |
1529 | (pass-if (not (inexact? (- 1 fixnum-min)))) | |
7c24e528 RB |
1530 | (pass-if (inexact? 1.3)) |
1531 | (pass-if (inexact? 3.1+4.2i)) | |
ca2b31fe MV |
1532 | (pass-if-exception "char" |
1533 | exception:wrong-type-arg | |
1534 | (not (inexact? #\a))) | |
1535 | (pass-if-exception "string" | |
1536 | exception:wrong-type-arg | |
1537 | (not (inexact? "a"))) | |
1538 | (pass-if-exception "vector" | |
1539 | exception:wrong-type-arg | |
1540 | (not (inexact? (make-vector 0)))) | |
1541 | (pass-if-exception "cons" | |
1542 | exception:wrong-type-arg | |
1543 | (not (inexact? (cons 1 2)))) | |
1544 | (pass-if-exception "bool" | |
1545 | exception:wrong-type-arg | |
1546 | (not (inexact? #t))) | |
1547 | (pass-if-exception "procedure" | |
1548 | exception:wrong-type-arg | |
1549 | (not (inexact? (lambda () #t)))) | |
1550 | (pass-if-exception "port" | |
1551 | exception:wrong-type-arg | |
1552 | (not (inexact? (current-input-port))))) | |
7c24e528 | 1553 | |
47ae1f0e DH |
1554 | ;;; |
1555 | ;;; equal? | |
1556 | ;;; | |
1557 | ||
1558 | (with-test-prefix "equal?" | |
1559 | (pass-if (documented? equal?)) | |
1560 | (pass-if (equal? 0 0)) | |
1561 | (pass-if (equal? 7 7)) | |
1562 | (pass-if (equal? -7 -7)) | |
1563 | (pass-if (equal? (+ 1 fixnum-max) (+ 1 fixnum-max))) | |
1564 | (pass-if (equal? (- fixnum-min 1) (- fixnum-min 1))) | |
1565 | (pass-if (not (equal? 0 1))) | |
1566 | (pass-if (not (equal? fixnum-max (+ 1 fixnum-max)))) | |
1567 | (pass-if (not (equal? (+ 1 fixnum-max) fixnum-max))) | |
1568 | (pass-if (not (equal? (+ 1 fixnum-max) (+ 2 fixnum-max)))) | |
1569 | (pass-if (not (equal? fixnum-min (- fixnum-min 1)))) | |
1570 | (pass-if (not (equal? (- fixnum-min 1) fixnum-min))) | |
1571 | (pass-if (not (equal? (- fixnum-min 1) (- fixnum-min 2)))) | |
1572 | (pass-if (not (equal? (+ fixnum-max 1) (- fixnum-min 1)))) | |
1573 | ||
1574 | (pass-if (not (equal? (ash 1 256) +inf.0))) | |
1575 | (pass-if (not (equal? +inf.0 (ash 1 256)))) | |
1576 | (pass-if (not (equal? (ash 1 256) -inf.0))) | |
1577 | (pass-if (not (equal? -inf.0 (ash 1 256)))) | |
1578 | ||
1579 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
1580 | ;; sure we've avoided that | |
1581 | (pass-if (not (equal? (ash 1 1024) +inf.0))) | |
1582 | (pass-if (not (equal? +inf.0 (ash 1 1024)))) | |
1583 | (pass-if (not (equal? (- (ash 1 1024)) -inf.0))) | |
1584 | (pass-if (not (equal? -inf.0 (- (ash 1 1024))))) | |
1585 | ||
1586 | (pass-if (not (equal? +nan.0 +nan.0))) | |
1587 | (pass-if (not (equal? 0 +nan.0))) | |
1588 | (pass-if (not (equal? +nan.0 0))) | |
1589 | (pass-if (not (equal? 1 +nan.0))) | |
1590 | (pass-if (not (equal? +nan.0 1))) | |
1591 | (pass-if (not (equal? -1 +nan.0))) | |
1592 | (pass-if (not (equal? +nan.0 -1))) | |
1593 | ||
1594 | (pass-if (not (equal? (ash 1 256) +nan.0))) | |
1595 | (pass-if (not (equal? +nan.0 (ash 1 256)))) | |
1596 | (pass-if (not (equal? (- (ash 1 256)) +nan.0))) | |
1597 | (pass-if (not (equal? +nan.0 (- (ash 1 256))))) | |
1598 | ||
1599 | (pass-if (not (equal? (ash 1 8192) +nan.0))) | |
1600 | (pass-if (not (equal? +nan.0 (ash 1 8192)))) | |
1601 | (pass-if (not (equal? (- (ash 1 8192)) +nan.0))) | |
1602 | (pass-if (not (equal? +nan.0 (- (ash 1 8192))))) | |
1603 | ||
1604 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
1605 | ;; sure we've avoided that | |
1606 | (pass-if (not (equal? (ash 3 1023) +nan.0))) | |
1607 | (pass-if (not (equal? +nan.0 (ash 3 1023))))) | |
1608 | ||
f29b3454 DH |
1609 | ;;; |
1610 | ;;; = | |
1611 | ;;; | |
1612 | ||
7c24e528 | 1613 | (with-test-prefix "=" |
8a1f4f98 | 1614 | (pass-if (documented? =)) |
7c24e528 RB |
1615 | (pass-if (= 0 0)) |
1616 | (pass-if (= 7 7)) | |
1617 | (pass-if (= -7 -7)) | |
1618 | (pass-if (= (+ 1 fixnum-max) (+ 1 fixnum-max))) | |
47ae1f0e | 1619 | (pass-if (= (- fixnum-min 1) (- fixnum-min 1))) |
4d332f19 DH |
1620 | (pass-if (not (= 0 1))) |
1621 | (pass-if (not (= fixnum-max (+ 1 fixnum-max)))) | |
1622 | (pass-if (not (= (+ 1 fixnum-max) fixnum-max))) | |
47ae1f0e | 1623 | (pass-if (not (= (+ 1 fixnum-max) (+ 2 fixnum-max)))) |
4d332f19 DH |
1624 | (pass-if (not (= fixnum-min (- fixnum-min 1)))) |
1625 | (pass-if (not (= (- fixnum-min 1) fixnum-min))) | |
47ae1f0e | 1626 | (pass-if (not (= (- fixnum-min 1) (- fixnum-min 2)))) |
4d332f19 | 1627 | (pass-if (not (= (+ fixnum-max 1) (- fixnum-min 1)))) |
2cfcaed5 | 1628 | |
adda36ed KR |
1629 | (pass-if (not (= (ash 1 256) +inf.0))) |
1630 | (pass-if (not (= +inf.0 (ash 1 256)))) | |
1631 | (pass-if (not (= (ash 1 256) -inf.0))) | |
1632 | (pass-if (not (= -inf.0 (ash 1 256)))) | |
1633 | ||
1634 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
1635 | ;; sure we've avoided that | |
1636 | (pass-if (not (= (ash 1 1024) +inf.0))) | |
1637 | (pass-if (not (= +inf.0 (ash 1 1024)))) | |
1638 | (pass-if (not (= (- (ash 1 1024)) -inf.0))) | |
1639 | (pass-if (not (= -inf.0 (- (ash 1 1024))))) | |
1640 | ||
2cfcaed5 KR |
1641 | (pass-if (not (= +nan.0 +nan.0))) |
1642 | (pass-if (not (= 0 +nan.0))) | |
1643 | (pass-if (not (= +nan.0 0))) | |
1644 | (pass-if (not (= 1 +nan.0))) | |
1645 | (pass-if (not (= +nan.0 1))) | |
1646 | (pass-if (not (= -1 +nan.0))) | |
1647 | (pass-if (not (= +nan.0 -1))) | |
1648 | ||
1649 | (pass-if (not (= (ash 1 256) +nan.0))) | |
1650 | (pass-if (not (= +nan.0 (ash 1 256)))) | |
1651 | (pass-if (not (= (- (ash 1 256)) +nan.0))) | |
1652 | (pass-if (not (= +nan.0 (- (ash 1 256))))) | |
1653 | ||
1654 | (pass-if (not (= (ash 1 8192) +nan.0))) | |
1655 | (pass-if (not (= +nan.0 (ash 1 8192)))) | |
1656 | (pass-if (not (= (- (ash 1 8192)) +nan.0))) | |
1657 | (pass-if (not (= +nan.0 (- (ash 1 8192))))) | |
1658 | ||
1659 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
1660 | ;; sure we've avoided that | |
1661 | (pass-if (not (= (ash 3 1023) +nan.0))) | |
2a8b5e04 KR |
1662 | (pass-if (not (= +nan.0 (ash 3 1023)))) |
1663 | ||
1664 | (pass-if (= 1/2 0.5)) | |
1665 | (pass-if (not (= 1/3 0.333333333333333333333333333333333))) | |
1666 | (pass-if (not (= 2/3 0.5))) | |
1667 | (pass-if (not (= 0.5 (+ 1/2 (/ 1 (ash 1 1000)))))) | |
1668 | ||
1669 | (pass-if (= 1/2 0.5+0i)) | |
1670 | (pass-if (not (= 0.333333333333333333333333333333333 1/3))) | |
1671 | (pass-if (not (= 2/3 0.5+0i))) | |
1672 | (pass-if (not (= 1/2 0+0.5i))) | |
1673 | ||
1674 | (pass-if (= 0.5 1/2)) | |
1675 | (pass-if (not (= 0.5 2/3))) | |
1676 | (pass-if (not (= (+ 1/2 (/ 1 (ash 1 1000))) 0.5))) | |
1677 | ||
1678 | (pass-if (= 0.5+0i 1/2)) | |
1679 | (pass-if (not (= 0.5+0i 2/3))) | |
6f6847fa KR |
1680 | (pass-if (not (= 0+0.5i 1/2))) |
1681 | ||
1682 | ;; prior to guile 1.8, inum/flonum comparisons were done just by | |
1683 | ;; converting the inum to a double, which on a 64-bit would round making | |
1684 | ;; say inexact 2^58 appear equal to exact 2^58+1 | |
1685 | (pass-if (= (ash-flo 1.0 58) (ash 1 58))) | |
1686 | (pass-if (not (= (ash-flo 1.0 58) (1+ (ash 1 58))))) | |
1687 | (pass-if (not (= (ash-flo 1.0 58) (1- (ash 1 58))))) | |
1688 | (pass-if (= (ash 1 58) (ash-flo 1.0 58))) | |
1689 | (pass-if (not (= (1+ (ash 1 58)) (ash-flo 1.0 58)))) | |
1690 | (pass-if (not (= (1- (ash 1 58)) (ash-flo 1.0 58))))) | |
7c24e528 | 1691 | |
de142bea DH |
1692 | ;;; |
1693 | ;;; < | |
1694 | ;;; | |
1695 | ||
1696 | (with-test-prefix "<" | |
1697 | ||
8a1f4f98 | 1698 | (pass-if "documented?" |
cb18f2a8 | 1699 | (documented? <)) |
de142bea | 1700 | |
de142bea DH |
1701 | (with-test-prefix "(< 0 n)" |
1702 | ||
1703 | (pass-if "n = 0" | |
1704 | (not (< 0 0))) | |
1705 | ||
1706 | (pass-if "n = 0.0" | |
1707 | (not (< 0 0.0))) | |
1708 | ||
1709 | (pass-if "n = 1" | |
1710 | (< 0 1)) | |
1711 | ||
1712 | (pass-if "n = 1.0" | |
1713 | (< 0 1.0)) | |
1714 | ||
1715 | (pass-if "n = -1" | |
1716 | (not (< 0 -1))) | |
1717 | ||
1718 | (pass-if "n = -1.0" | |
1719 | (not (< 0 -1.0))) | |
1720 | ||
21e39e8f DH |
1721 | (pass-if "n = fixnum-max" |
1722 | (< 0 fixnum-max)) | |
1723 | ||
1724 | (pass-if "n = fixnum-max + 1" | |
1725 | (< 0 (+ fixnum-max 1))) | |
1726 | ||
1727 | (pass-if "n = fixnum-min" | |
1728 | (not (< 0 fixnum-min))) | |
de142bea | 1729 | |
21e39e8f DH |
1730 | (pass-if "n = fixnum-min - 1" |
1731 | (not (< 0 (- fixnum-min 1))))) | |
1732 | ||
de142bea DH |
1733 | (with-test-prefix "(< 0.0 n)" |
1734 | ||
1735 | (pass-if "n = 0" | |
1736 | (not (< 0.0 0))) | |
1737 | ||
1738 | (pass-if "n = 0.0" | |
1739 | (not (< 0.0 0.0))) | |
1740 | ||
1741 | (pass-if "n = 1" | |
1742 | (< 0.0 1)) | |
1743 | ||
1744 | (pass-if "n = 1.0" | |
1745 | (< 0.0 1.0)) | |
1746 | ||
1747 | (pass-if "n = -1" | |
1748 | (not (< 0.0 -1))) | |
1749 | ||
1750 | (pass-if "n = -1.0" | |
1751 | (not (< 0.0 -1.0))) | |
1752 | ||
21e39e8f DH |
1753 | (pass-if "n = fixnum-max" |
1754 | (< 0.0 fixnum-max)) | |
1755 | ||
1756 | (pass-if "n = fixnum-max + 1" | |
1757 | (< 0.0 (+ fixnum-max 1))) | |
de142bea | 1758 | |
21e39e8f DH |
1759 | (pass-if "n = fixnum-min" |
1760 | (not (< 0.0 fixnum-min))) | |
1761 | ||
1762 | (pass-if "n = fixnum-min - 1" | |
1763 | (not (< 0.0 (- fixnum-min 1))))) | |
1764 | ||
1765 | (with-test-prefix "(< 1 n)" | |
de142bea | 1766 | |
21e39e8f | 1767 | (pass-if "n = 0" |
de142bea DH |
1768 | (not (< 1 0))) |
1769 | ||
21e39e8f DH |
1770 | (pass-if "n = 0.0" |
1771 | (not (< 1 0.0))) | |
1772 | ||
1773 | (pass-if "n = 1" | |
1774 | (not (< 1 1))) | |
1775 | ||
de142bea | 1776 | (pass-if "n = 1.0" |
21e39e8f DH |
1777 | (not (< 1 1.0))) |
1778 | ||
1779 | (pass-if "n = -1" | |
1780 | (not (< 1 -1))) | |
1781 | ||
1782 | (pass-if "n = -1.0" | |
1783 | (not (< 1 -1.0))) | |
1784 | ||
1785 | (pass-if "n = fixnum-max" | |
1786 | (< 1 fixnum-max)) | |
1787 | ||
1788 | (pass-if "n = fixnum-max + 1" | |
1789 | (< 1 (+ fixnum-max 1))) | |
1790 | ||
1791 | (pass-if "n = fixnum-min" | |
1792 | (not (< 1 fixnum-min))) | |
1793 | ||
1794 | (pass-if "n = fixnum-min - 1" | |
1795 | (not (< 1 (- fixnum-min 1))))) | |
1796 | ||
1797 | (with-test-prefix "(< 1.0 n)" | |
1798 | ||
1799 | (pass-if "n = 0" | |
de142bea DH |
1800 | (not (< 1.0 0))) |
1801 | ||
21e39e8f DH |
1802 | (pass-if "n = 0.0" |
1803 | (not (< 1.0 0.0))) | |
1804 | ||
1805 | (pass-if "n = 1" | |
1806 | (not (< 1.0 1))) | |
1807 | ||
1808 | (pass-if "n = 1.0" | |
1809 | (not (< 1.0 1.0))) | |
1810 | ||
de142bea | 1811 | (pass-if "n = -1" |
21e39e8f DH |
1812 | (not (< 1.0 -1))) |
1813 | ||
1814 | (pass-if "n = -1.0" | |
1815 | (not (< 1.0 -1.0))) | |
1816 | ||
1817 | (pass-if "n = fixnum-max" | |
1818 | (< 1.0 fixnum-max)) | |
1819 | ||
1820 | (pass-if "n = fixnum-max + 1" | |
1821 | (< 1.0 (+ fixnum-max 1))) | |
1822 | ||
1823 | (pass-if "n = fixnum-min" | |
1824 | (not (< 1.0 fixnum-min))) | |
1825 | ||
1826 | (pass-if "n = fixnum-min - 1" | |
1827 | (not (< 1.0 (- fixnum-min 1))))) | |
1828 | ||
1829 | (with-test-prefix "(< -1 n)" | |
1830 | ||
1831 | (pass-if "n = 0" | |
de142bea DH |
1832 | (< -1 0)) |
1833 | ||
21e39e8f DH |
1834 | (pass-if "n = 0.0" |
1835 | (< -1 0.0)) | |
1836 | ||
1837 | (pass-if "n = 1" | |
1838 | (< -1 1)) | |
1839 | ||
1840 | (pass-if "n = 1.0" | |
1841 | (< -1 1.0)) | |
1842 | ||
1843 | (pass-if "n = -1" | |
1844 | (not (< -1 -1))) | |
1845 | ||
de142bea | 1846 | (pass-if "n = -1.0" |
21e39e8f DH |
1847 | (not (< -1 -1.0))) |
1848 | ||
1849 | (pass-if "n = fixnum-max" | |
1850 | (< -1 fixnum-max)) | |
1851 | ||
1852 | (pass-if "n = fixnum-max + 1" | |
1853 | (< -1 (+ fixnum-max 1))) | |
1854 | ||
1855 | (pass-if "n = fixnum-min" | |
1856 | (not (< -1 fixnum-min))) | |
1857 | ||
1858 | (pass-if "n = fixnum-min - 1" | |
1859 | (not (< -1 (- fixnum-min 1))))) | |
1860 | ||
1861 | (with-test-prefix "(< -1.0 n)" | |
1862 | ||
1863 | (pass-if "n = 0" | |
de142bea DH |
1864 | (< -1.0 0)) |
1865 | ||
21e39e8f DH |
1866 | (pass-if "n = 0.0" |
1867 | (< -1.0 0.0)) | |
1868 | ||
1869 | (pass-if "n = 1" | |
1870 | (< -1.0 1)) | |
1871 | ||
1872 | (pass-if "n = 1.0" | |
1873 | (< -1.0 1.0)) | |
1874 | ||
1875 | (pass-if "n = -1" | |
1876 | (not (< -1.0 -1))) | |
1877 | ||
1878 | (pass-if "n = -1.0" | |
1879 | (not (< -1.0 -1.0))) | |
1880 | ||
1881 | (pass-if "n = fixnum-max" | |
1882 | (< -1.0 fixnum-max)) | |
1883 | ||
1884 | (pass-if "n = fixnum-max + 1" | |
1885 | (< -1.0 (+ fixnum-max 1))) | |
de142bea | 1886 | |
21e39e8f DH |
1887 | (pass-if "n = fixnum-min" |
1888 | (not (< -1.0 fixnum-min))) | |
1889 | ||
1890 | (pass-if "n = fixnum-min - 1" | |
1891 | (not (< -1.0 (- fixnum-min 1))))) | |
1892 | ||
1893 | (with-test-prefix "(< fixnum-max n)" | |
1894 | ||
1895 | (pass-if "n = 0" | |
1896 | (not (< fixnum-max 0))) | |
1897 | ||
1898 | (pass-if "n = 0.0" | |
1899 | (not (< fixnum-max 0.0))) | |
de142bea DH |
1900 | |
1901 | (pass-if "n = 1" | |
21e39e8f | 1902 | (not (< fixnum-max 1))) |
de142bea DH |
1903 | |
1904 | (pass-if "n = 1.0" | |
21e39e8f | 1905 | (not (< fixnum-max 1.0))) |
de142bea DH |
1906 | |
1907 | (pass-if "n = -1" | |
21e39e8f | 1908 | (not (< fixnum-max -1))) |
de142bea DH |
1909 | |
1910 | (pass-if "n = -1.0" | |
21e39e8f | 1911 | (not (< fixnum-max -1.0))) |
de142bea | 1912 | |
21e39e8f DH |
1913 | (pass-if "n = fixnum-max" |
1914 | (not (< fixnum-max fixnum-max))) | |
de142bea | 1915 | |
21e39e8f DH |
1916 | (pass-if "n = fixnum-max + 1" |
1917 | (< fixnum-max (+ fixnum-max 1))) | |
1918 | ||
1919 | (pass-if "n = fixnum-min" | |
1920 | (not (< fixnum-max fixnum-min))) | |
1921 | ||
1922 | (pass-if "n = fixnum-min - 1" | |
1923 | (not (< fixnum-max (- fixnum-min 1))))) | |
1924 | ||
1925 | (with-test-prefix "(< (+ fixnum-max 1) n)" | |
1926 | ||
1927 | (pass-if "n = 0" | |
1928 | (not (< (+ fixnum-max 1) 0))) | |
1929 | ||
1930 | (pass-if "n = 0.0" | |
1931 | (not (< (+ fixnum-max 1) 0.0))) | |
de142bea DH |
1932 | |
1933 | (pass-if "n = 1" | |
21e39e8f | 1934 | (not (< (+ fixnum-max 1) 1))) |
de142bea DH |
1935 | |
1936 | (pass-if "n = 1.0" | |
21e39e8f | 1937 | (not (< (+ fixnum-max 1) 1.0))) |
de142bea DH |
1938 | |
1939 | (pass-if "n = -1" | |
21e39e8f | 1940 | (not (< (+ fixnum-max 1) -1))) |
de142bea DH |
1941 | |
1942 | (pass-if "n = -1.0" | |
21e39e8f | 1943 | (not (< (+ fixnum-max 1) -1.0))) |
de142bea | 1944 | |
21e39e8f DH |
1945 | (pass-if "n = fixnum-max" |
1946 | (not (< (+ fixnum-max 1) fixnum-max))) | |
de142bea | 1947 | |
21e39e8f DH |
1948 | (pass-if "n = fixnum-max + 1" |
1949 | (not (< (+ fixnum-max 1) (+ fixnum-max 1)))) | |
de142bea | 1950 | |
21e39e8f DH |
1951 | (pass-if "n = fixnum-min" |
1952 | (not (< (+ fixnum-max 1) fixnum-min))) | |
1953 | ||
1954 | (pass-if "n = fixnum-min - 1" | |
1955 | (not (< (+ fixnum-max 1) (- fixnum-min 1))))) | |
1956 | ||
1957 | (with-test-prefix "(< fixnum-min n)" | |
1958 | ||
1959 | (pass-if "n = 0" | |
1960 | (< fixnum-min 0)) | |
1961 | ||
1962 | (pass-if "n = 0.0" | |
1963 | (< fixnum-min 0.0)) | |
de142bea DH |
1964 | |
1965 | (pass-if "n = 1" | |
21e39e8f | 1966 | (< fixnum-min 1)) |
de142bea DH |
1967 | |
1968 | (pass-if "n = 1.0" | |
21e39e8f | 1969 | (< fixnum-min 1.0)) |
de142bea DH |
1970 | |
1971 | (pass-if "n = -1" | |
21e39e8f | 1972 | (< fixnum-min -1)) |
de142bea DH |
1973 | |
1974 | (pass-if "n = -1.0" | |
21e39e8f | 1975 | (< fixnum-min -1.0)) |
de142bea | 1976 | |
21e39e8f DH |
1977 | (pass-if "n = fixnum-max" |
1978 | (< fixnum-min fixnum-max)) | |
1979 | ||
1980 | (pass-if "n = fixnum-max + 1" | |
1981 | (< fixnum-min (+ fixnum-max 1))) | |
de142bea | 1982 | |
21e39e8f DH |
1983 | (pass-if "n = fixnum-min" |
1984 | (not (< fixnum-min fixnum-min))) | |
de142bea | 1985 | |
21e39e8f DH |
1986 | (pass-if "n = fixnum-min - 1" |
1987 | (not (< fixnum-min (- fixnum-min 1))))) | |
1988 | ||
1989 | (with-test-prefix "(< (- fixnum-min 1) n)" | |
1990 | ||
1991 | (pass-if "n = 0" | |
1992 | (< (- fixnum-min 1) 0)) | |
1993 | ||
1994 | (pass-if "n = 0.0" | |
1995 | (< (- fixnum-min 1) 0.0)) | |
1996 | ||
1997 | (pass-if "n = 1" | |
1998 | (< (- fixnum-min 1) 1)) | |
1999 | ||
2000 | (pass-if "n = 1.0" | |
2001 | (< (- fixnum-min 1) 1.0)) | |
de142bea DH |
2002 | |
2003 | (pass-if "n = -1" | |
21e39e8f | 2004 | (< (- fixnum-min 1) -1)) |
de142bea DH |
2005 | |
2006 | (pass-if "n = -1.0" | |
21e39e8f DH |
2007 | (< (- fixnum-min 1) -1.0)) |
2008 | ||
2009 | (pass-if "n = fixnum-max" | |
2010 | (< (- fixnum-min 1) fixnum-max)) | |
2011 | ||
2012 | (pass-if "n = fixnum-max + 1" | |
2013 | (< (- fixnum-min 1) (+ fixnum-max 1))) | |
2014 | ||
2015 | (pass-if "n = fixnum-min" | |
2016 | (< (- fixnum-min 1) fixnum-min)) | |
2017 | ||
2018 | (pass-if "n = fixnum-min - 1" | |
2cfcaed5 KR |
2019 | (not (< (- fixnum-min 1) (- fixnum-min 1))))) |
2020 | ||
adda36ed KR |
2021 | (pass-if (< (ash 1 256) +inf.0)) |
2022 | (pass-if (not (< +inf.0 (ash 1 256)))) | |
2023 | (pass-if (not (< (ash 1 256) -inf.0))) | |
2024 | (pass-if (< -inf.0 (ash 1 256))) | |
2025 | ||
2026 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
2027 | ;; sure we've avoided that | |
2028 | (pass-if (< (1- (ash 1 1024)) +inf.0)) | |
2029 | (pass-if (< (ash 1 1024) +inf.0)) | |
2030 | (pass-if (< (1+ (ash 1 1024)) +inf.0)) | |
2031 | (pass-if (not (< +inf.0 (1- (ash 1 1024))))) | |
2032 | (pass-if (not (< +inf.0 (ash 1 1024)))) | |
2033 | (pass-if (not (< +inf.0 (1+ (ash 1 1024))))) | |
2034 | (pass-if (< -inf.0 (- (1- (ash 1 1024))))) | |
2035 | (pass-if (< -inf.0 (- (ash 1 1024)))) | |
2036 | (pass-if (< -inf.0 (- (1+ (ash 1 1024))))) | |
2037 | (pass-if (not (< (- (1- (ash 1 1024))) -inf.0))) | |
2038 | (pass-if (not (< (- (ash 1 1024)) -inf.0))) | |
2039 | (pass-if (not (< (- (1+ (ash 1 1024))) -inf.0))) | |
2040 | ||
2cfcaed5 KR |
2041 | (pass-if (not (< +nan.0 +nan.0))) |
2042 | (pass-if (not (< 0 +nan.0))) | |
2043 | (pass-if (not (< +nan.0 0))) | |
2044 | (pass-if (not (< 1 +nan.0))) | |
2045 | (pass-if (not (< +nan.0 1))) | |
2046 | (pass-if (not (< -1 +nan.0))) | |
2047 | (pass-if (not (< +nan.0 -1))) | |
2048 | ||
2049 | (pass-if (not (< (ash 1 256) +nan.0))) | |
2050 | (pass-if (not (< +nan.0 (ash 1 256)))) | |
2051 | (pass-if (not (< (- (ash 1 256)) +nan.0))) | |
2052 | (pass-if (not (< +nan.0 (- (ash 1 256))))) | |
2053 | ||
2054 | (pass-if (not (< (ash 1 8192) +nan.0))) | |
2055 | (pass-if (not (< +nan.0 (ash 1 8192)))) | |
2056 | (pass-if (not (< (- (ash 1 8192)) +nan.0))) | |
2057 | (pass-if (not (< +nan.0 (- (ash 1 8192))))) | |
2058 | ||
2059 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
2060 | ;; sure we've avoided that | |
2061 | (pass-if (not (< (ash 3 1023) +nan.0))) | |
2062 | (pass-if (not (< (1+ (ash 3 1023)) +nan.0))) | |
2063 | (pass-if (not (< (1- (ash 3 1023)) +nan.0))) | |
2064 | (pass-if (not (< +nan.0 (ash 3 1023)))) | |
2065 | (pass-if (not (< +nan.0 (1+ (ash 3 1023))))) | |
fe89421e KR |
2066 | (pass-if (not (< +nan.0 (1- (ash 3 1023))))) |
2067 | ||
2068 | (with-test-prefix "inum/frac" | |
2069 | (pass-if (< 2 9/4)) | |
2070 | (pass-if (< -2 9/4)) | |
2071 | (pass-if (< -2 7/4)) | |
2072 | (pass-if (< -2 -7/4)) | |
2073 | (pass-if (eq? #f (< 2 7/4))) | |
2074 | (pass-if (eq? #f (< 2 -7/4))) | |
2075 | (pass-if (eq? #f (< 2 -9/4))) | |
2076 | (pass-if (eq? #f (< -2 -9/4)))) | |
2077 | ||
2078 | (with-test-prefix "bignum/frac" | |
2079 | (let ((x (ash 1 2048))) | |
2080 | (pass-if (< x (* 4/3 x))) | |
2081 | (pass-if (< (- x) (* 4/3 x))) | |
2082 | (pass-if (< (- x) (* 2/3 x))) | |
2083 | (pass-if (< (- x) (* -2/3 x))) | |
2084 | (pass-if (eq? #f (< x (* 2/3 x)))) | |
2085 | (pass-if (eq? #f (< x (* -2/3 x)))) | |
2086 | (pass-if (eq? #f (< x (* -4/3 x)))) | |
2087 | (pass-if (eq? #f (< (- x) (* -4/3 x)))))) | |
2088 | ||
2089 | (with-test-prefix "flonum/frac" | |
2090 | (pass-if (< 0.75 4/3)) | |
2091 | (pass-if (< -0.75 4/3)) | |
2092 | (pass-if (< -0.75 2/3)) | |
2093 | (pass-if (< -0.75 -2/3)) | |
2094 | (pass-if (eq? #f (< 0.75 2/3))) | |
2095 | (pass-if (eq? #f (< 0.75 -2/3))) | |
2096 | (pass-if (eq? #f (< 0.75 -4/3))) | |
2097 | (pass-if (eq? #f (< -0.75 -4/3))) | |
2098 | ||
2099 | (pass-if (< -inf.0 4/3)) | |
2100 | (pass-if (< -inf.0 -4/3)) | |
2101 | (pass-if (eq? #f (< +inf.0 4/3))) | |
2102 | (pass-if (eq? #f (< +inf.0 -4/3))) | |
2103 | ||
2104 | (pass-if (eq? #f (< +nan.0 4/3))) | |
2105 | (pass-if (eq? #f (< +nan.0 -4/3)))) | |
2106 | ||
2107 | (with-test-prefix "frac/inum" | |
2108 | (pass-if (< 7/4 2)) | |
2109 | (pass-if (< -7/4 2)) | |
2110 | (pass-if (< -9/4 2)) | |
2111 | (pass-if (< -9/4 -2)) | |
2112 | (pass-if (eq? #f (< 9/4 2))) | |
2113 | (pass-if (eq? #f (< 9/4 -2))) | |
2114 | (pass-if (eq? #f (< 7/4 -2))) | |
2115 | (pass-if (eq? #f (< -7/4 -2)))) | |
2116 | ||
2117 | (with-test-prefix "frac/bignum" | |
2118 | (let ((x (ash 1 2048))) | |
2119 | (pass-if (< (* 2/3 x) x)) | |
2120 | (pass-if (< (* -2/3 x) x)) | |
2121 | (pass-if (< (* -4/3 x) x)) | |
2122 | (pass-if (< (* -4/3 x) (- x))) | |
2123 | (pass-if (eq? #f (< (* 4/3 x) x))) | |
2124 | (pass-if (eq? #f (< (* 4/3 x) (- x)))) | |
2125 | (pass-if (eq? #f (< (* 2/3 x) (- x)))) | |
2126 | (pass-if (eq? #f (< (* -2/3 x) (- x)))))) | |
2127 | ||
2128 | (with-test-prefix "frac/flonum" | |
2129 | (pass-if (< 2/3 0.75)) | |
2130 | (pass-if (< -2/3 0.75)) | |
2131 | (pass-if (< -4/3 0.75)) | |
2132 | (pass-if (< -4/3 -0.75)) | |
2133 | (pass-if (eq? #f (< 4/3 0.75))) | |
2134 | (pass-if (eq? #f (< 4/3 -0.75))) | |
2135 | (pass-if (eq? #f (< 2/3 -0.75))) | |
2136 | (pass-if (eq? #f (< -2/3 -0.75))) | |
2137 | ||
2138 | (pass-if (< 4/3 +inf.0)) | |
2139 | (pass-if (< -4/3 +inf.0)) | |
2140 | (pass-if (eq? #f (< 4/3 -inf.0))) | |
2141 | (pass-if (eq? #f (< -4/3 -inf.0))) | |
2142 | ||
2143 | (pass-if (eq? #f (< 4/3 +nan.0))) | |
2144 | (pass-if (eq? #f (< -4/3 +nan.0)))) | |
2145 | ||
2146 | (with-test-prefix "frac/frac" | |
2147 | (pass-if (< 2/3 6/7)) | |
2148 | (pass-if (< -2/3 6/7)) | |
2149 | (pass-if (< -4/3 6/7)) | |
2150 | (pass-if (< -4/3 -6/7)) | |
2151 | (pass-if (eq? #f (< 4/3 6/7))) | |
2152 | (pass-if (eq? #f (< 4/3 -6/7))) | |
2153 | (pass-if (eq? #f (< 2/3 -6/7))) | |
2154 | (pass-if (eq? #f (< -2/3 -6/7))))) | |
f29b3454 DH |
2155 | |
2156 | ;;; | |
2157 | ;;; > | |
2158 | ;;; | |
2159 | ||
7c24e528 RB |
2160 | ;; currently not tested -- implementation is trivial |
2161 | ;; (> x y) is implemented as (< y x) | |
2162 | ;; FIXME: tests should probably be added in case we change implementation. | |
2163 | ||
f29b3454 DH |
2164 | ;;; |
2165 | ;;; <= | |
2166 | ;;; | |
2167 | ||
7c24e528 RB |
2168 | ;; currently not tested -- implementation is trivial |
2169 | ;; (<= x y) is implemented as (not (< y x)) | |
2170 | ;; FIXME: tests should probably be added in case we change implementation. | |
2171 | ||
f29b3454 DH |
2172 | ;;; |
2173 | ;;; >= | |
2174 | ;;; | |
2175 | ||
7c24e528 RB |
2176 | ;; currently not tested -- implementation is trivial |
2177 | ;; (>= x y) is implemented as (not (< x y)) | |
2178 | ;; FIXME: tests should probably be added in case we change implementation. | |
2179 | ||
f29b3454 DH |
2180 | ;;; |
2181 | ;;; zero? | |
2182 | ;;; | |
2183 | ||
7c24e528 RB |
2184 | (with-test-prefix "zero?" |
2185 | (expect-fail (documented? zero?)) | |
2186 | (pass-if (zero? 0)) | |
4d332f19 DH |
2187 | (pass-if (not (zero? 7))) |
2188 | (pass-if (not (zero? -7))) | |
2189 | (pass-if (not (zero? (+ 1 fixnum-max)))) | |
2190 | (pass-if (not (zero? (- 1 fixnum-min)))) | |
2191 | (pass-if (not (zero? 1.3))) | |
2192 | (pass-if (not (zero? 3.1+4.2i)))) | |
7c24e528 | 2193 | |
f29b3454 DH |
2194 | ;;; |
2195 | ;;; positive? | |
2196 | ;;; | |
2197 | ||
7c24e528 RB |
2198 | (with-test-prefix "positive?" |
2199 | (expect-fail (documented? positive?)) | |
2200 | (pass-if (positive? 1)) | |
2201 | (pass-if (positive? (+ fixnum-max 1))) | |
2202 | (pass-if (positive? 1.3)) | |
4d332f19 DH |
2203 | (pass-if (not (positive? 0))) |
2204 | (pass-if (not (positive? -1))) | |
2205 | (pass-if (not (positive? (- fixnum-min 1)))) | |
2206 | (pass-if (not (positive? -1.3)))) | |
7c24e528 | 2207 | |
f29b3454 DH |
2208 | ;;; |
2209 | ;;; negative? | |
2210 | ;;; | |
2211 | ||
7c24e528 RB |
2212 | (with-test-prefix "negative?" |
2213 | (expect-fail (documented? negative?)) | |
4d332f19 DH |
2214 | (pass-if (not (negative? 1))) |
2215 | (pass-if (not (negative? (+ fixnum-max 1)))) | |
2216 | (pass-if (not (negative? 1.3))) | |
2217 | (pass-if (not (negative? 0))) | |
7c24e528 RB |
2218 | (pass-if (negative? -1)) |
2219 | (pass-if (negative? (- fixnum-min 1))) | |
2220 | (pass-if (negative? -1.3))) | |
2221 | ||
f29b3454 DH |
2222 | ;;; |
2223 | ;;; max | |
2224 | ;;; | |
2225 | ||
adda36ed | 2226 | (with-test-prefix "max" |
593a4c2f KR |
2227 | (pass-if-exception "no args" exception:wrong-num-args |
2228 | (max)) | |
2229 | ||
2230 | (pass-if-exception "one complex" exception:wrong-type-arg | |
2231 | (max 1+i)) | |
2232 | ||
2233 | (pass-if-exception "inum/complex" exception:wrong-type-arg | |
2234 | (max 123 1+i)) | |
2235 | (pass-if-exception "big/complex" exception:wrong-type-arg | |
2236 | (max 9999999999999999999999999999999999999999 1+i)) | |
2237 | (pass-if-exception "real/complex" exception:wrong-type-arg | |
2238 | (max 123.0 1+i)) | |
2239 | (pass-if-exception "frac/complex" exception:wrong-type-arg | |
2240 | (max 123/456 1+i)) | |
2241 | ||
2242 | (pass-if-exception "complex/inum" exception:wrong-type-arg | |
2243 | (max 1+i 123)) | |
2244 | (pass-if-exception "complex/big" exception:wrong-type-arg | |
2245 | (max 1+i 9999999999999999999999999999999999999999)) | |
2246 | (pass-if-exception "complex/real" exception:wrong-type-arg | |
2247 | (max 1+i 123.0)) | |
2248 | (pass-if-exception "complex/frac" exception:wrong-type-arg | |
2249 | (max 1+i 123/456)) | |
2250 | ||
adda36ed KR |
2251 | (let ((big*2 (* fixnum-max 2)) |
2252 | (big*3 (* fixnum-max 3)) | |
2253 | (big*4 (* fixnum-max 4)) | |
2254 | (big*5 (* fixnum-max 5))) | |
501da403 | 2255 | |
2530518e KR |
2256 | (with-test-prefix "inum / frac" |
2257 | (pass-if (= 3 (max 3 5/2))) | |
2258 | (pass-if (= 5/2 (max 2 5/2)))) | |
2259 | ||
2260 | (with-test-prefix "frac / inum" | |
2261 | (pass-if (= 3 (max 5/2 3))) | |
2262 | (pass-if (= 5/2 (max 5/2 2)))) | |
2263 | ||
23d77957 KR |
2264 | (with-test-prefix "inum / real" |
2265 | (pass-if (nan? (max 123 +nan.0)))) | |
2266 | ||
2267 | (with-test-prefix "real / inum" | |
2268 | (pass-if (nan? (max +nan.0 123)))) | |
2269 | ||
2530518e KR |
2270 | (with-test-prefix "big / frac" |
2271 | (pass-if (= big*2 (max big*2 5/2))) | |
2272 | (pass-if (= 5/2 (max (- big*2) 5/2)))) | |
2273 | ||
2274 | (with-test-prefix "frac / big" | |
2275 | (pass-if (= big*2 (max 5/2 big*2))) | |
2276 | (pass-if (= 5/2 (max 5/2 (- big*2))))) | |
2277 | ||
23d77957 KR |
2278 | (with-test-prefix "big / real" |
2279 | (pass-if (nan? (max big*5 +nan.0))) | |
23d72566 KR |
2280 | (pass-if (eqv? (exact->inexact big*5) (max big*5 -inf.0))) |
2281 | (pass-if (eqv? (exact->inexact big*5) (max big*5 1.0))) | |
2282 | (pass-if (eqv? +inf.0 (max big*5 +inf.0))) | |
2283 | (pass-if (eqv? 1.0 (max (- big*5) 1.0)))) | |
23d77957 KR |
2284 | |
2285 | (with-test-prefix "real / big" | |
2286 | (pass-if (nan? (max +nan.0 big*5))) | |
23d72566 KR |
2287 | (pass-if (eqv? (exact->inexact big*5) (max -inf.0 big*5))) |
2288 | (pass-if (eqv? (exact->inexact big*5) (max 1.0 big*5))) | |
2289 | (pass-if (eqv? +inf.0 (max +inf.0 big*5))) | |
2290 | (pass-if (eqv? 1.0 (max 1.0 (- big*5))))) | |
23d77957 | 2291 | |
2530518e KR |
2292 | (with-test-prefix "frac / frac" |
2293 | (pass-if (= 2/3 (max 1/2 2/3))) | |
2294 | (pass-if (= 2/3 (max 2/3 1/2))) | |
2295 | (pass-if (= -1/2 (max -1/2 -2/3))) | |
2296 | (pass-if (= -1/2 (max -2/3 -1/2)))) | |
2297 | ||
23d77957 KR |
2298 | (with-test-prefix "real / real" |
2299 | (pass-if (nan? (max 123.0 +nan.0))) | |
2300 | (pass-if (nan? (max +nan.0 123.0))) | |
2301 | (pass-if (nan? (max +nan.0 +nan.0))) | |
2302 | (pass-if (= 456.0 (max 123.0 456.0))) | |
2303 | (pass-if (= 456.0 (max 456.0 123.0))))) | |
adda36ed KR |
2304 | |
2305 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make | |
2306 | ;; sure we've avoided that | |
2307 | (for-each (lambda (b) | |
2308 | (pass-if (list b +inf.0) | |
2309 | (= +inf.0 (max b +inf.0))) | |
2310 | (pass-if (list +inf.0 b) | |
2311 | (= +inf.0 (max b +inf.0))) | |
2312 | (pass-if (list b -inf.0) | |
23d77957 | 2313 | (= (exact->inexact b) (max b -inf.0))) |
adda36ed | 2314 | (pass-if (list -inf.0 b) |
23d77957 | 2315 | (= (exact->inexact b) (max b -inf.0)))) |
adda36ed KR |
2316 | (list (1- (ash 1 1024)) |
2317 | (ash 1 1024) | |
2318 | (1+ (ash 1 1024)) | |
2319 | (- (1- (ash 1 1024))) | |
2320 | (- (ash 1 1024)) | |
501da403 KR |
2321 | (- (1+ (ash 1 1024))))) |
2322 | ||
2323 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
2324 | ;; sure we've avoided that | |
2325 | (pass-if (nan? (max (ash 1 2048) +nan.0))) | |
2326 | (pass-if (nan? (max +nan.0 (ash 1 2048))))) | |
adda36ed | 2327 | |
f29b3454 DH |
2328 | ;;; |
2329 | ;;; min | |
2330 | ;;; | |
2331 | ||
7c24e528 RB |
2332 | ;; FIXME: unfinished... |
2333 | ||
2334 | (with-test-prefix "min" | |
593a4c2f KR |
2335 | (pass-if-exception "no args" exception:wrong-num-args |
2336 | (min)) | |
2337 | ||
2338 | (pass-if-exception "one complex" exception:wrong-type-arg | |
2339 | (min 1+i)) | |
2340 | ||
2341 | (pass-if-exception "inum/complex" exception:wrong-type-arg | |
2342 | (min 123 1+i)) | |
2343 | (pass-if-exception "big/complex" exception:wrong-type-arg | |
2344 | (min 9999999999999999999999999999999999999999 1+i)) | |
2345 | (pass-if-exception "real/complex" exception:wrong-type-arg | |
2346 | (min 123.0 1+i)) | |
2347 | (pass-if-exception "frac/complex" exception:wrong-type-arg | |
2348 | (min 123/456 1+i)) | |
2349 | ||
2350 | (pass-if-exception "complex/inum" exception:wrong-type-arg | |
2351 | (min 1+i 123)) | |
2352 | (pass-if-exception "complex/big" exception:wrong-type-arg | |
2353 | (min 1+i 9999999999999999999999999999999999999999)) | |
2354 | (pass-if-exception "complex/real" exception:wrong-type-arg | |
2355 | (min 1+i 123.0)) | |
2356 | (pass-if-exception "complex/frac" exception:wrong-type-arg | |
2357 | (min 1+i 123/456)) | |
2358 | ||
7c24e528 RB |
2359 | (let ((big*2 (* fixnum-max 2)) |
2360 | (big*3 (* fixnum-max 3)) | |
2361 | (big*4 (* fixnum-max 4)) | |
2362 | (big*5 (* fixnum-max 5))) | |
23d77957 | 2363 | |
d389e966 | 2364 | (pass-if (documented? min)) |
7c24e528 RB |
2365 | (pass-if (= 1 (min 7 3 1 5))) |
2366 | (pass-if (= 1 (min 1 7 3 5))) | |
2367 | (pass-if (= 1 (min 7 3 5 1))) | |
2368 | (pass-if (= -7 (min 2 3 4 -2 5 -7 1 -1 4 2))) | |
2369 | (pass-if (= -7 (min -7 2 3 4 -2 5 1 -1 4 2))) | |
2370 | (pass-if (= -7 (min 2 3 4 -2 5 1 -1 4 2 -7))) | |
2371 | (pass-if (= big*2 (min big*3 big*5 big*2 big*4))) | |
2372 | (pass-if (= big*2 (min big*2 big*3 big*5 big*4))) | |
2373 | (pass-if (= big*2 (min big*3 big*5 big*4 big*2))) | |
2374 | (pass-if | |
2375 | (= (- fixnum-min 1) (min 2 4 (- fixnum-min 1) 3 (* 2 fixnum-max)))) | |
2376 | (pass-if | |
2377 | (= (- fixnum-min 1) (min (- fixnum-min 1) 2 4 3 (* 2 fixnum-max)))) | |
2378 | (pass-if | |
adda36ed | 2379 | (= (- fixnum-min 1) (min 2 4 3 (* 2 fixnum-max) (- fixnum-min 1)))) |
23d77957 | 2380 | |
2530518e KR |
2381 | (with-test-prefix "inum / frac" |
2382 | (pass-if (= 5/2 (min 3 5/2))) | |
2383 | (pass-if (= 2 (min 2 5/2)))) | |
2384 | ||
2385 | (with-test-prefix "frac / inum" | |
2386 | (pass-if (= 5/2 (min 5/2 3))) | |
2387 | (pass-if (= 2 (min 5/2 2)))) | |
2388 | ||
23d77957 KR |
2389 | (with-test-prefix "inum / real" |
2390 | (pass-if (nan? (min 123 +nan.0)))) | |
2391 | ||
2392 | (with-test-prefix "real / inum" | |
2393 | (pass-if (nan? (min +nan.0 123)))) | |
2394 | ||
2530518e KR |
2395 | (with-test-prefix "big / frac" |
2396 | (pass-if (= 5/2 (min big*2 5/2))) | |
2397 | (pass-if (= (- big*2) (min (- big*2) 5/2)))) | |
2398 | ||
2399 | (with-test-prefix "frac / big" | |
2400 | (pass-if (= 5/2 (min 5/2 big*2))) | |
2401 | (pass-if (= (- big*2) (min 5/2 (- big*2))))) | |
2402 | ||
23d77957 KR |
2403 | (with-test-prefix "big / real" |
2404 | (pass-if (nan? (min big*5 +nan.0))) | |
23d72566 KR |
2405 | (pass-if (eqv? (exact->inexact big*5) (min big*5 +inf.0))) |
2406 | (pass-if (eqv? -inf.0 (min big*5 -inf.0))) | |
2407 | (pass-if (eqv? 1.0 (min big*5 1.0))) | |
2408 | (pass-if (eqv? (exact->inexact (- big*5)) (min (- big*5) 1.0)))) | |
23d77957 KR |
2409 | |
2410 | (with-test-prefix "real / big" | |
2411 | (pass-if (nan? (min +nan.0 big*5))) | |
23d72566 KR |
2412 | (pass-if (eqv? (exact->inexact big*5) (min +inf.0 big*5))) |
2413 | (pass-if (eqv? -inf.0 (min -inf.0 big*5))) | |
2414 | (pass-if (eqv? 1.0 (min 1.0 big*5))) | |
2415 | (pass-if (eqv? (exact->inexact (- big*5)) (min 1.0 (- big*5))))) | |
23d77957 | 2416 | |
2530518e KR |
2417 | (with-test-prefix "frac / frac" |
2418 | (pass-if (= 1/2 (min 1/2 2/3))) | |
2419 | (pass-if (= 1/2 (min 2/3 1/2))) | |
2420 | (pass-if (= -2/3 (min -1/2 -2/3))) | |
2421 | (pass-if (= -2/3 (min -2/3 -1/2)))) | |
2422 | ||
23d77957 KR |
2423 | (with-test-prefix "real / real" |
2424 | (pass-if (nan? (min 123.0 +nan.0))) | |
2425 | (pass-if (nan? (min +nan.0 123.0))) | |
2426 | (pass-if (nan? (min +nan.0 +nan.0))) | |
2427 | (pass-if (= 123.0 (min 123.0 456.0))) | |
2428 | (pass-if (= 123.0 (min 456.0 123.0))))) | |
2429 | ||
2430 | ||
adda36ed KR |
2431 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make |
2432 | ;; sure we've avoided that | |
2433 | (for-each (lambda (b) | |
2434 | (pass-if (list b +inf.0) | |
23d77957 | 2435 | (= (exact->inexact b) (min b +inf.0))) |
adda36ed | 2436 | (pass-if (list +inf.0 b) |
23d77957 | 2437 | (= (exact->inexact b) (min b +inf.0))) |
adda36ed KR |
2438 | (pass-if (list b -inf.0) |
2439 | (= -inf.0 (min b -inf.0))) | |
2440 | (pass-if (list -inf.0 b) | |
2441 | (= -inf.0 (min b -inf.0)))) | |
2442 | (list (1- (ash 1 1024)) | |
2443 | (ash 1 1024) | |
2444 | (1+ (ash 1 1024)) | |
2445 | (- (1- (ash 1 1024))) | |
2446 | (- (ash 1 1024)) | |
501da403 KR |
2447 | (- (1+ (ash 1 1024))))) |
2448 | ||
2449 | ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make | |
2450 | ;; sure we've avoided that | |
2451 | (pass-if (nan? (min (- (ash 1 2048)) (- +nan.0)))) | |
2452 | (pass-if (nan? (min (- +nan.0) (- (ash 1 2048)))))) | |
adda36ed | 2453 | |
f29b3454 DH |
2454 | ;;; |
2455 | ;;; + | |
2456 | ;;; | |
2457 | ||
2458 | (with-test-prefix "+" | |
2459 | ||
d389e966 | 2460 | (pass-if "documented?" |
cedf24d8 | 2461 | (documented? +))) |
f29b3454 | 2462 | |
f29b3454 DH |
2463 | ;;; |
2464 | ;;; - | |
2465 | ;;; | |
2466 | ||
072e6de2 KR |
2467 | (with-test-prefix "-" |
2468 | ||
2469 | (pass-if "-inum - +bignum" | |
2470 | (= #x-100000000000000000000000000000001 | |
ef016629 KR |
2471 | (- -1 #x100000000000000000000000000000000))) |
2472 | ||
2473 | (pass-if "big - inum" | |
2474 | (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF | |
2475 | (- #x100000000000000000000000000000000 1))) | |
2476 | ||
2477 | (pass-if "big - -inum" | |
2478 | (= #x100000000000000000000000000000001 | |
2479 | (- #x100000000000000000000000000000000 -1)))) | |
072e6de2 | 2480 | |
f29b3454 DH |
2481 | ;;; |
2482 | ;;; * | |
2483 | ;;; | |
2484 | ||
65ea251e KR |
2485 | (with-test-prefix "*" |
2486 | ||
23d72566 KR |
2487 | (with-test-prefix "inum * bignum" |
2488 | ||
2489 | (pass-if "0 * 2^256 = 0" | |
2490 | (eqv? 0 (* 0 (ash 1 256))))) | |
2491 | ||
2492 | (with-test-prefix "inum * flonum" | |
2493 | ||
2494 | (pass-if "0 * 1.0 = 0" | |
2495 | (eqv? 0 (* 0 1.0)))) | |
2496 | ||
2497 | (with-test-prefix "inum * complex" | |
2498 | ||
2499 | (pass-if "0 * 1+1i = 0" | |
2500 | (eqv? 0 (* 0 1+1i)))) | |
2501 | ||
2502 | (with-test-prefix "inum * frac" | |
2503 | ||
2504 | (pass-if "0 * 2/3 = 0" | |
2505 | (eqv? 0 (* 0 2/3)))) | |
2506 | ||
2507 | (with-test-prefix "bignum * inum" | |
2508 | ||
2509 | (pass-if "2^256 * 0 = 0" | |
2510 | (eqv? 0 (* (ash 1 256) 0)))) | |
2511 | ||
2512 | (with-test-prefix "flonum * inum" | |
2513 | ||
2514 | ;; in guile 1.6.8 and 1.8.1 and earlier this returned inexact 0.0 | |
2515 | (pass-if "1.0 * 0 = 0" | |
2516 | (eqv? 0 (* 1.0 0)))) | |
2517 | ||
2518 | (with-test-prefix "complex * inum" | |
2519 | ||
2520 | ;; in guile 1.6.8 and 1.8.1 and earlier this returned inexact 0.0 | |
2521 | (pass-if "1+1i * 0 = 0" | |
2522 | (eqv? 0 (* 1+1i 0)))) | |
2523 | ||
65ea251e KR |
2524 | (pass-if "complex * bignum" |
2525 | (let ((big (ash 1 90))) | |
2526 | (= (make-rectangular big big) | |
23d72566 KR |
2527 | (* 1+1i big)))) |
2528 | ||
2529 | (with-test-prefix "frac * inum" | |
2530 | ||
2531 | (pass-if "2/3 * 0 = 0" | |
2532 | (eqv? 0 (* 2/3 0))))) | |
65ea251e | 2533 | |
f29b3454 DH |
2534 | ;;; |
2535 | ;;; / | |
2536 | ;;; | |
2537 | ||
1b3a7932 DH |
2538 | (with-test-prefix "/" |
2539 | ||
d389e966 | 2540 | (pass-if "documented?" |
1b3a7932 DH |
2541 | (documented? /)) |
2542 | ||
2543 | (with-test-prefix "division by zero" | |
2544 | ||
2545 | (pass-if-exception "(/ 0)" | |
2f359170 | 2546 | exception:numerical-overflow |
1b3a7932 DH |
2547 | (/ 0)) |
2548 | ||
cdf52e3d MV |
2549 | (pass-if "(/ 0.0)" |
2550 | (= +inf.0 (/ 0.0))) | |
80074d77 | 2551 | |
1b3a7932 | 2552 | (pass-if-exception "(/ 1 0)" |
2f359170 | 2553 | exception:numerical-overflow |
80074d77 DH |
2554 | (/ 1 0)) |
2555 | ||
cdf52e3d MV |
2556 | (pass-if "(/ 1 0.0)" |
2557 | (= +inf.0 (/ 1 0.0))) | |
80074d77 DH |
2558 | |
2559 | (pass-if-exception "(/ bignum 0)" | |
2f359170 | 2560 | exception:numerical-overflow |
80074d77 DH |
2561 | (/ (+ fixnum-max 1) 0)) |
2562 | ||
cdf52e3d MV |
2563 | (pass-if "(/ bignum 0.0)" |
2564 | (= +inf.0 (/ (+ fixnum-max 1) 0.0))) | |
80074d77 DH |
2565 | |
2566 | (pass-if-exception "(/ 1.0 0)" | |
2f359170 | 2567 | exception:numerical-overflow |
80074d77 DH |
2568 | (/ 1.0 0)) |
2569 | ||
cdf52e3d MV |
2570 | (pass-if "(/ 1.0 0.0)" |
2571 | (= +inf.0 (/ 1.0 0.0))) | |
80074d77 DH |
2572 | |
2573 | (pass-if-exception "(/ +i 0)" | |
2f359170 | 2574 | exception:numerical-overflow |
80074d77 DH |
2575 | (/ +i 0)) |
2576 | ||
cdf52e3d MV |
2577 | (pass-if "(/ +i 0.0)" |
2578 | (= +inf.0 (imag-part (/ +i 0.0))))) | |
469b963c | 2579 | |
2f359170 KR |
2580 | (with-test-prefix "1/complex" |
2581 | ||
2582 | (pass-if "0+1i" | |
2583 | (eqv? 0-1i (/ 0+1i))) | |
2584 | ||
2585 | ;; in guile 1.6 through 1.6.7 this incorrectly resulted in nans | |
2586 | (pass-if "0-1i" | |
2587 | (eqv? 0+1i (/ 0-1i))) | |
2588 | ||
2589 | (pass-if "1+1i" | |
2590 | (eqv? 0.5-0.5i (/ 1+1i))) | |
2591 | ||
2592 | (pass-if "1-1i" | |
2593 | (eqv? 0.5+0.5i (/ 1-1i))) | |
2594 | ||
2595 | (pass-if "-1+1i" | |
2596 | (eqv? -0.5-0.5i (/ -1+1i))) | |
2597 | ||
2598 | (pass-if "-1-1i" | |
2599 | (eqv? -0.5+0.5i (/ -1-1i))) | |
469b963c MV |
2600 | |
2601 | (pass-if "(/ 3+4i)" | |
2602 | (= (/ 3+4i) 0.12-0.16i)) | |
2603 | ||
2604 | (pass-if "(/ 4+3i)" | |
2605 | (= (/ 4+3i) 0.16-0.12i)) | |
2606 | ||
2f359170 KR |
2607 | (pass-if "(/ 1e200+1e200i)" |
2608 | (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i))) | |
469b963c | 2609 | |
2f359170 | 2610 | (with-test-prefix "inum/complex" |
469b963c MV |
2611 | |
2612 | (pass-if "(/ 25 3+4i)" | |
2613 | (= (/ 25 3+4i) 3.0-4.0i)) | |
2614 | ||
2615 | (pass-if "(/ 25 4+3i)" | |
2f359170 | 2616 | (= (/ 25 4+3i) 4.0-3.0i))) |
469b963c | 2617 | |
2f359170 KR |
2618 | (with-test-prefix "complex/complex" |
2619 | ||
2620 | (pass-if "(/ 25+125i 3+4i)" | |
2621 | (= (/ 25+125i 3+4i) 23.0+11.0i)) | |
2622 | ||
2623 | (pass-if "(/ 25+125i 4+3i)" | |
2624 | (= (/ 25+125i 4+3i) 19.0+17.0i)))) | |
1b3a7932 | 2625 | |
f29b3454 DH |
2626 | ;;; |
2627 | ;;; truncate | |
2628 | ;;; | |
2629 | ||
14a6784c KR |
2630 | (with-test-prefix "truncate" |
2631 | (pass-if (= 1 (truncate 1.75))) | |
2632 | (pass-if (= 1 (truncate 1.5))) | |
2633 | (pass-if (= 1 (truncate 1.25))) | |
2634 | (pass-if (= 0 (truncate 0.75))) | |
2635 | (pass-if (= 0 (truncate 0.5))) | |
2636 | (pass-if (= 0 (truncate 0.0))) | |
2637 | (pass-if (= 0 (truncate -0.5))) | |
2638 | (pass-if (= -1 (truncate -1.25))) | |
2639 | (pass-if (= -1 (truncate -1.5)))) | |
2640 | ||
f29b3454 DH |
2641 | ;;; |
2642 | ;;; round | |
2643 | ;;; | |
2644 | ||
14a6784c KR |
2645 | (with-test-prefix "round" |
2646 | (pass-if (= 2 (round 1.75))) | |
2647 | (pass-if (= 2 (round 1.5))) | |
2648 | (pass-if (= 1 (round 1.25))) | |
2649 | (pass-if (= 1 (round 0.75))) | |
2650 | (pass-if (= 0 (round 0.5))) | |
2651 | (pass-if (= 0 (round 0.0))) | |
2652 | (pass-if (= 0 (round -0.5))) | |
2653 | (pass-if (= -1 (round -1.25))) | |
abff733b KR |
2654 | (pass-if (= -2 (round -1.5))) |
2655 | ||
2656 | (with-test-prefix "inum" | |
2657 | (pass-if "0" | |
2658 | (and (= 0 (round 0)) | |
2659 | (exact? (round 0)))) | |
2660 | ||
2661 | (pass-if "1" | |
2662 | (and (= 1 (round 1)) | |
2663 | (exact? (round 1)))) | |
2664 | ||
2665 | (pass-if "-1" | |
2666 | (and (= -1 (round -1)) | |
2667 | (exact? (round -1))))) | |
2668 | ||
2669 | (with-test-prefix "bignum" | |
2670 | (let ((x (1+ most-positive-fixnum))) | |
2671 | (pass-if "(1+ most-positive-fixnum)" | |
2672 | (and (= x (round x)) | |
2673 | (exact? (round x))))) | |
2674 | ||
2675 | (let ((x (1- most-negative-fixnum))) | |
2676 | (pass-if "(1- most-negative-fixnum)" | |
2677 | (and (= x (round x)) | |
2678 | (exact? (round x)))))) | |
2679 | ||
6203b5f5 KR |
2680 | (with-test-prefix "frac" |
2681 | (define (=exact x y) | |
2682 | (and (= x y) | |
2683 | (exact? y))) | |
2684 | ||
2685 | (pass-if (=exact -2 (round -7/3))) | |
2686 | (pass-if (=exact -2 (round -5/3))) | |
2687 | (pass-if (=exact -1 (round -4/3))) | |
2688 | (pass-if (=exact -1 (round -2/3))) | |
2689 | (pass-if (=exact 0 (round -1/3))) | |
2690 | (pass-if (=exact 0 (round 1/3))) | |
2691 | (pass-if (=exact 1 (round 2/3))) | |
2692 | (pass-if (=exact 1 (round 4/3))) | |
2693 | (pass-if (=exact 2 (round 5/3))) | |
2694 | (pass-if (=exact 2 (round 7/3))) | |
2695 | ||
2696 | (pass-if (=exact -3 (round -17/6))) | |
2697 | (pass-if (=exact -3 (round -16/6))) | |
2698 | (pass-if (=exact -2 (round -15/6))) | |
2699 | (pass-if (=exact -2 (round -14/6))) | |
2700 | (pass-if (=exact -2 (round -13/6))) | |
2701 | (pass-if (=exact -2 (round -11/6))) | |
2702 | (pass-if (=exact -2 (round -10/6))) | |
2703 | (pass-if (=exact -2 (round -9/6))) | |
2704 | (pass-if (=exact -1 (round -8/6))) | |
2705 | (pass-if (=exact -1 (round -7/6))) | |
2706 | (pass-if (=exact -1 (round -5/6))) | |
2707 | (pass-if (=exact -1 (round -4/6))) | |
2708 | (pass-if (=exact 0 (round -3/6))) | |
2709 | (pass-if (=exact 0 (round -2/6))) | |
2710 | (pass-if (=exact 0 (round -1/6))) | |
2711 | (pass-if (=exact 0 (round 1/6))) | |
2712 | (pass-if (=exact 0 (round 2/6))) | |
2713 | (pass-if (=exact 0 (round 3/6))) | |
2714 | (pass-if (=exact 1 (round 4/6))) | |
2715 | (pass-if (=exact 1 (round 5/6))) | |
2716 | (pass-if (=exact 1 (round 7/6))) | |
2717 | (pass-if (=exact 1 (round 8/6))) | |
2718 | (pass-if (=exact 2 (round 9/6))) | |
2719 | (pass-if (=exact 2 (round 10/6))) | |
2720 | (pass-if (=exact 2 (round 11/6))) | |
2721 | (pass-if (=exact 2 (round 13/6))) | |
2722 | (pass-if (=exact 2 (round 14/6))) | |
2723 | (pass-if (=exact 2 (round 15/6))) | |
2724 | (pass-if (=exact 3 (round 16/6))) | |
2725 | (pass-if (=exact 3 (round 17/6)))) | |
2726 | ||
abff733b KR |
2727 | (with-test-prefix "real" |
2728 | (pass-if "0.0" | |
2729 | (and (= 0.0 (round 0.0)) | |
2730 | (inexact? (round 0.0)))) | |
2731 | ||
2732 | (pass-if "1.0" | |
2733 | (and (= 1.0 (round 1.0)) | |
2734 | (inexact? (round 1.0)))) | |
2735 | ||
2736 | (pass-if "-1.0" | |
2737 | (and (= -1.0 (round -1.0)) | |
2738 | (inexact? (round -1.0)))) | |
2739 | ||
2740 | (pass-if "-3.1" | |
2741 | (and (= -3.0 (round -3.1)) | |
2742 | (inexact? (round -3.1)))) | |
2743 | ||
2744 | (pass-if "3.1" | |
2745 | (and (= 3.0 (round 3.1)) | |
2746 | (inexact? (round 3.1)))) | |
2747 | ||
2748 | (pass-if "3.9" | |
2749 | (and (= 4.0 (round 3.9)) | |
2750 | (inexact? (round 3.9)))) | |
2751 | ||
2752 | (pass-if "-3.9" | |
2753 | (and (= -4.0 (round -3.9)) | |
2754 | (inexact? (round -3.9)))) | |
2755 | ||
2756 | (pass-if "1.5" | |
2757 | (and (= 2.0 (round 1.5)) | |
2758 | (inexact? (round 1.5)))) | |
2759 | ||
2760 | (pass-if "2.5" | |
2761 | (and (= 2.0 (round 2.5)) | |
2762 | (inexact? (round 2.5)))) | |
2763 | ||
2764 | (pass-if "3.5" | |
2765 | (and (= 4.0 (round 3.5)) | |
2766 | (inexact? (round 3.5)))) | |
2767 | ||
2768 | (pass-if "-1.5" | |
2769 | (and (= -2.0 (round -1.5)) | |
2770 | (inexact? (round -1.5)))) | |
2771 | ||
2772 | (pass-if "-2.5" | |
2773 | (and (= -2.0 (round -2.5)) | |
2774 | (inexact? (round -2.5)))) | |
2775 | ||
2776 | (pass-if "-3.5" | |
2777 | (and (= -4.0 (round -3.5)) | |
2778 | (inexact? (round -3.5)))) | |
2779 | ||
2780 | ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a | |
2781 | ;; float with mantissa all ones) came out as 2^53 from `round' (except | |
2782 | ;; on i386 and m68k systems using the coprocessor and optimizing, where | |
2783 | ;; extra precision hid the problem) | |
2784 | (pass-if "2^53-1" | |
2785 | (let ((x (exact->inexact (1- (ash 1 53))))) | |
2786 | (and (= x (round x)) | |
2787 | (inexact? (round x))))) | |
2788 | (pass-if "-(2^53-1)" | |
2789 | (let ((x (exact->inexact (- (1- (ash 1 53)))))) | |
2790 | (and (= x (round x)) | |
2791 | (inexact? (round x))))))) | |
14a6784c | 2792 | |
f29b3454 DH |
2793 | ;;; |
2794 | ;;; exact->inexact | |
2795 | ;;; | |
2796 | ||
a1fb3b1c KR |
2797 | (with-test-prefix "exact->inexact" |
2798 | ||
2799 | ;; Test "(exact->inexact n)", expect "want". | |
2800 | ;; "i" is a index, for diagnostic purposes. | |
2801 | (define (try-i i n want) | |
2802 | (with-test-prefix (list i n want) | |
2803 | (with-test-prefix "pos" | |
2804 | (let ((got (exact->inexact n))) | |
2805 | (pass-if "inexact?" (inexact? got)) | |
2806 | (pass-if (list "=" got) (= want got)))) | |
2807 | (set! n (- n)) | |
2808 | (set! want (- want)) | |
2809 | (with-test-prefix "neg" | |
2810 | (let ((got (exact->inexact n))) | |
2811 | (pass-if "inexact?" (inexact? got)) | |
2812 | (pass-if (list "=" got) (= want got)))))) | |
2813 | ||
2814 | (with-test-prefix "2^i, no round" | |
2815 | (do ((i 0 (1+ i)) | |
2816 | (n 1 (* 2 n)) | |
2817 | (want 1.0 (* 2.0 want))) | |
2818 | ((> i 100)) | |
2819 | (try-i i n want))) | |
2820 | ||
2821 | (with-test-prefix "2^i+1, no round" | |
2822 | (do ((i 1 (1+ i)) | |
2823 | (n 3 (1- (* 2 n))) | |
2824 | (want 3.0 (- (* 2.0 want) 1.0))) | |
2825 | ((>= i dbl-mant-dig)) | |
2826 | (try-i i n want))) | |
2827 | ||
2828 | (with-test-prefix "(2^i+1)*2^100, no round" | |
2829 | (do ((i 1 (1+ i)) | |
2830 | (n 3 (1- (* 2 n))) | |
2831 | (want 3.0 (- (* 2.0 want) 1.0))) | |
2832 | ((>= i dbl-mant-dig)) | |
2833 | (try-i i (ash n 100) (ash-flo want 100)))) | |
2834 | ||
2835 | ;; bit pattern: 1111....11100.00 | |
2836 | ;; <-mantdig-><-i-> | |
2837 | ;; | |
2838 | (with-test-prefix "mantdig ones then zeros, no rounding" | |
2839 | (do ((i 0 (1+ i)) | |
2840 | (n (- (ash 1 dbl-mant-dig) 1) (* 2 n)) | |
2841 | (want (- (ash-flo 1.0 dbl-mant-dig) 1.0) (* 2.0 want))) | |
2842 | ((> i 100)) | |
2843 | (try-i i n want))) | |
2844 | ||
2845 | ;; bit pattern: 1111....111011..1 | |
2846 | ;; <-mantdig-> <-i-> | |
2847 | ;; This sort of value was incorrectly rounded upwards in Guile 1.6.4 when | |
2848 | ;; i >= 11 (that's when the total is 65 or more bits). | |
2849 | ;; | |
2850 | (with-test-prefix "mantdig ones then 011..11, round down" | |
2851 | (do ((i 0 (1+ i)) | |
2852 | (n (- (ash 1 (+ 1 dbl-mant-dig)) 2) (+ 1 (* 2 n))) | |
2853 | (want (- (ash-flo 1.0 (+ 1 dbl-mant-dig)) 2.0) (* 2.0 want))) | |
2854 | ((> i 100)) | |
2855 | (try-i i n want))) | |
2856 | ||
2857 | ;; bit pattern: 1111....111100..001 | |
2858 | ;; <-mantdig-> <--i-> | |
2859 | ;; | |
2860 | (with-test-prefix "mantdig ones then 100..001, round up" | |
2861 | (do ((i 0 (1+ i)) | |
2862 | (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n))) | |
2863 | (want (ash-flo 1.0 (+ 2 dbl-mant-dig)) (* 2.0 want))) | |
2864 | ((> i 100)) | |
2865 | (try-i i n want))) | |
2866 | ||
2867 | ;; bit pattern: 1000....000100..001 | |
2868 | ;; <-mantdig-> <--i-> | |
2869 | ;; | |
2870 | (with-test-prefix "2^mantdig then 100..001, round up" | |
2871 | (do ((i 0 (1+ i)) | |
2872 | (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n))) | |
2873 | (want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want))) | |
2874 | ((> i 100)) | |
23f2b9a3 KR |
2875 | (try-i i n want))) |
2876 | ||
2877 | (pass-if "frac big/big" | |
2878 | (let ((big (ash 1 256))) | |
2879 | (= 1.0 (exact->inexact (/ (1+ big) big))))) | |
2880 | ||
2881 | ;; In guile 1.8.0 this failed, giving back "nan" because it tried to | |
2882 | ;; convert the num and den to doubles, resulting in infs. | |
2883 | (pass-if "frac big/big, exceeding double" | |
2884 | (let ((big (ash 1 4096))) | |
2885 | (= 1.0 (exact->inexact (/ (1+ big) big)))))) | |
a1fb3b1c | 2886 | |
f29b3454 DH |
2887 | ;;; |
2888 | ;;; floor | |
2889 | ;;; | |
2890 | ||
2891 | ;;; | |
2892 | ;;; ceiling | |
2893 | ;;; | |
2894 | ||
46f2c0f1 RB |
2895 | ;;; |
2896 | ;;; expt | |
2897 | ;;; | |
2898 | ||
2899 | (with-test-prefix "expt" | |
a4082ab5 AW |
2900 | (pass-if-exception "non-numeric base" exception:wrong-type-arg |
2901 | (expt #t 0)) | |
46f2c0f1 RB |
2902 | (pass-if "(= 1 (expt 0 0))" (= 1 (expt 0 0))) |
2903 | (pass-if "(= 1 (expt 0 0.0))" (= 1 (expt 0 0.0))) | |
2904 | (pass-if "(= 1 (expt 0.0 0))" (= 1 (expt 0.0 0))) | |
2905 | (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0)))) | |
2906 | ||
14a6784c KR |
2907 | ;;; |
2908 | ;;; asinh | |
2909 | ;;; | |
2910 | ||
2911 | (with-test-prefix "asinh" | |
2912 | (pass-if (= 0 (asinh 0)))) | |
2913 | ||
2914 | ;;; | |
2915 | ;;; acosh | |
2916 | ;;; | |
2917 | ||
2918 | (with-test-prefix "acosh" | |
2919 | (pass-if (= 0 (acosh 1)))) | |
2920 | ||
2921 | ;;; | |
2922 | ;;; atanh | |
2923 | ;;; | |
2924 | ||
2925 | (with-test-prefix "atanh" | |
2926 | (pass-if (= 0 (atanh 0)))) | |
2927 | ||
f29b3454 DH |
2928 | ;;; |
2929 | ;;; make-rectangular | |
2930 | ;;; | |
2931 | ||
2932 | ;;; | |
2933 | ;;; make-polar | |
2934 | ;;; | |
2935 | ||
d40681ec KR |
2936 | (with-test-prefix "make-polar" |
2937 | (define pi 3.14159265358979323846) | |
2938 | (define (almost= x y) | |
2939 | (> 0.01 (magnitude (- x y)))) | |
2940 | ||
2941 | (pass-if (= 0 (make-polar 0 0))) | |
2942 | (pass-if (= 0 (make-polar 0 123.456))) | |
2943 | (pass-if (= 1 (make-polar 1 0))) | |
2944 | (pass-if (= -1 (make-polar -1 0))) | |
2945 | ||
2946 | (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi)))) | |
2947 | (pass-if (almost= -1 (make-polar 1 (* 1.0 pi)))) | |
2948 | (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi)))) | |
2949 | (pass-if (almost= 1 (make-polar 1 (* 2.0 pi))))) | |
2950 | ||
f29b3454 DH |
2951 | ;;; |
2952 | ;;; real-part | |
2953 | ;;; | |
2954 | ||
2955 | ;;; | |
2956 | ;;; imag-part | |
2957 | ;;; | |
2958 | ||
2959 | ;;; | |
2960 | ;;; magnitude | |
2961 | ;;; | |
2962 | ||
d40681ec KR |
2963 | (with-test-prefix "magnitude" |
2964 | (pass-if (= 0 (magnitude 0))) | |
2965 | (pass-if (= 1 (magnitude 1))) | |
2966 | (pass-if (= 1 (magnitude -1))) | |
2967 | (pass-if (= 1 (magnitude 0+i))) | |
2968 | (pass-if (= 1 (magnitude 0-i))) | |
2969 | (pass-if (= 5 (magnitude 3+4i))) | |
2970 | (pass-if (= 5 (magnitude 3-4i))) | |
2971 | (pass-if (= 5 (magnitude -3+4i))) | |
2972 | (pass-if (= 5 (magnitude -3-4i)))) | |
2973 | ||
f29b3454 DH |
2974 | ;;; |
2975 | ;;; angle | |
2976 | ;;; | |
2977 | ||
cfc9fc1c KR |
2978 | (with-test-prefix "angle" |
2979 | (define pi 3.14159265358979323846) | |
2980 | (define (almost= x y) | |
2981 | (> 0.01 (magnitude (- x y)))) | |
2982 | ||
2983 | (pass-if "inum +ve" (= 0 (angle 1))) | |
2984 | (pass-if "inum -ve" (almost= pi (angle -1))) | |
2985 | ||
2986 | (pass-if "bignum +ve" (= 0 (angle (1+ fixnum-max)))) | |
2987 | (pass-if "bignum -ve" (almost= pi (angle (1- fixnum-min)))) | |
2988 | ||
2989 | (pass-if "flonum +ve" (= 0 (angle 1.5))) | |
2990 | (pass-if "flonum -ve" (almost= pi (angle -1.5)))) | |
2991 | ||
f29b3454 DH |
2992 | ;;; |
2993 | ;;; inexact->exact | |
2994 | ;;; | |
300c6a76 | 2995 | |
1259cb26 KR |
2996 | (with-test-prefix "inexact->exact" |
2997 | ||
9dd9857f | 2998 | (pass-if-exception "+inf" exception:out-of-range |
a409f865 | 2999 | (inexact->exact +inf.0)) |
1259cb26 | 3000 | |
9dd9857f | 3001 | (pass-if-exception "-inf" exception:out-of-range |
a409f865 | 3002 | (inexact->exact -inf.0)) |
1259cb26 | 3003 | |
9dd9857f | 3004 | (pass-if-exception "nan" exception:out-of-range |
a409f865 | 3005 | (inexact->exact +nan.0)) |
1259cb26 KR |
3006 | |
3007 | (with-test-prefix "2.0**i to exact and back" | |
3008 | (do ((i 0 (1+ i)) | |
3009 | (n 1.0 (* 2.0 n))) | |
3010 | ((> i 100)) | |
3011 | (pass-if (list i n) | |
3012 | (= n (inexact->exact (exact->inexact n))))))) | |
3013 | ||
c1122753 KR |
3014 | ;;; |
3015 | ;;; integer-expt | |
3016 | ;;; | |
3017 | ||
3018 | (with-test-prefix "integer-expt" | |
3019 | ||
5a8fc758 AW |
3020 | (pass-if-exception "non-numeric base" exception:wrong-type-arg |
3021 | (integer-expt #t 0)) | |
c1122753 KR |
3022 | (pass-if-exception "2^+inf" exception:wrong-type-arg |
3023 | (integer-expt 2 +inf.0)) | |
3024 | (pass-if-exception "2^-inf" exception:wrong-type-arg | |
3025 | (integer-expt 2 -inf.0)) | |
3026 | (pass-if-exception "2^nan" exception:wrong-type-arg | |
3027 | (integer-expt 2 +nan.0))) | |
3028 | ||
a04a3604 KR |
3029 | ;;; |
3030 | ;;; integer-length | |
3031 | ;;; | |
3032 | ||
3033 | (with-test-prefix "integer-length" | |
3034 | ||
3035 | (with-test-prefix "-2^i, ...11100..00" | |
3036 | (do ((n -1 (ash n 1)) | |
3037 | (i 0 (1+ i))) | |
3038 | ((> i 256)) | |
3039 | (pass-if (list n "expect" i) | |
3040 | (= i (integer-length n))))) | |
3041 | ||
3042 | (with-test-prefix "-2^i+1 ...11100..01" | |
3043 | (do ((n -3 (logxor 3 (ash n 1))) | |
3044 | (i 2 (1+ i))) | |
3045 | ((> i 256)) | |
3046 | (pass-if n | |
3047 | (= i (integer-length n))))) | |
3048 | ||
3049 | (with-test-prefix "-2^i-1 ...111011..11" | |
3050 | (do ((n -2 (1+ (ash n 1))) | |
3051 | (i 1 (1+ i))) | |
3052 | ((> i 256)) | |
3053 | (pass-if n | |
3054 | (= i (integer-length n)))))) | |
3055 | ||
8ab3d8a0 KR |
3056 | ;;; |
3057 | ;;; log | |
3058 | ;;; | |
3059 | ||
3060 | (with-test-prefix "log" | |
3061 | (pass-if "documented?" | |
3062 | (documented? log)) | |
3063 | ||
3064 | (pass-if-exception "no args" exception:wrong-num-args | |
3065 | (log)) | |
3066 | (pass-if-exception "two args" exception:wrong-num-args | |
3067 | (log 123 456)) | |
3068 | ||
3069 | (pass-if (negative-infinity? (log 0))) | |
3070 | (pass-if (negative-infinity? (log 0.0))) | |
3071 | (pass-if (eqv? 0.0 (log 1))) | |
3072 | (pass-if (eqv? 0.0 (log 1.0))) | |
3073 | (pass-if (eqv-loosely? 1.0 (log const-e))) | |
3074 | (pass-if (eqv-loosely? 2.0 (log const-e^2))) | |
3075 | (pass-if (eqv-loosely? -1.0 (log const-1/e))) | |
3076 | ||
3077 | (pass-if (eqv-loosely? 1.0+1.57079i (log 0+2.71828i))) | |
3078 | (pass-if (eqv-loosely? 1.0-1.57079i (log 0-2.71828i))) | |
3079 | ||
3080 | (pass-if (eqv-loosely? 0.0+3.14159i (log -1.0))) | |
3081 | (pass-if (eqv-loosely? 1.0+3.14159i (log -2.71828))) | |
3082 | (pass-if (eqv-loosely? 2.0+3.14159i (log (* -2.71828 2.71828))))) | |
3083 | ||
3084 | ;;; | |
3085 | ;;; log10 | |
3086 | ;;; | |
3087 | ||
3088 | (with-test-prefix "log10" | |
3089 | (pass-if "documented?" | |
3090 | (documented? log10)) | |
3091 | ||
3092 | (pass-if-exception "no args" exception:wrong-num-args | |
3093 | (log10)) | |
3094 | (pass-if-exception "two args" exception:wrong-num-args | |
3095 | (log10 123 456)) | |
3096 | ||
3097 | (pass-if (negative-infinity? (log10 0))) | |
3098 | (pass-if (negative-infinity? (log10 0.0))) | |
3099 | (pass-if (eqv? 0.0 (log10 1))) | |
3100 | (pass-if (eqv? 0.0 (log10 1.0))) | |
3101 | (pass-if (eqv-loosely? 1.0 (log10 10.0))) | |
3102 | (pass-if (eqv-loosely? 2.0 (log10 100.0))) | |
3103 | (pass-if (eqv-loosely? -1.0 (log10 0.1))) | |
3104 | ||
3105 | (pass-if (eqv-loosely? 1.0+0.68218i (log10 0+10.0i))) | |
3106 | (pass-if (eqv-loosely? 1.0-0.68218i (log10 0-10.0i))) | |
3107 | ||
3108 | (pass-if (eqv-loosely? 0.0+1.36437i (log10 -1))) | |
3109 | (pass-if (eqv-loosely? 1.0+1.36437i (log10 -10))) | |
3110 | (pass-if (eqv-loosely? 2.0+1.36437i (log10 -100)))) | |
3111 | ||
abff733b KR |
3112 | ;;; |
3113 | ;;; logbit? | |
3114 | ;;; | |
3115 | ||
3116 | (with-test-prefix "logbit?" | |
3117 | (pass-if (eq? #f (logbit? 0 0))) | |
3118 | (pass-if (eq? #f (logbit? 1 0))) | |
3119 | (pass-if (eq? #f (logbit? 31 0))) | |
3120 | (pass-if (eq? #f (logbit? 32 0))) | |
3121 | (pass-if (eq? #f (logbit? 33 0))) | |
3122 | (pass-if (eq? #f (logbit? 63 0))) | |
3123 | (pass-if (eq? #f (logbit? 64 0))) | |
3124 | (pass-if (eq? #f (logbit? 65 0))) | |
3125 | ||
3126 | ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap | |
3127 | ;; around and return #t where it ought to be #f | |
3128 | (pass-if (eq? #t (logbit? 0 1))) | |
3129 | (pass-if (eq? #f (logbit? 1 1))) | |
3130 | (pass-if (eq? #f (logbit? 31 1))) | |
3131 | (pass-if (eq? #f (logbit? 32 1))) | |
3132 | (pass-if (eq? #f (logbit? 33 1))) | |
3133 | (pass-if (eq? #f (logbit? 63 1))) | |
3134 | (pass-if (eq? #f (logbit? 64 1))) | |
3135 | (pass-if (eq? #f (logbit? 65 1))) | |
3136 | (pass-if (eq? #f (logbit? 128 1))) | |
3137 | ||
3138 | (pass-if (eq? #t (logbit? 0 -1))) | |
3139 | (pass-if (eq? #t (logbit? 1 -1))) | |
3140 | (pass-if (eq? #t (logbit? 31 -1))) | |
3141 | (pass-if (eq? #t (logbit? 32 -1))) | |
3142 | (pass-if (eq? #t (logbit? 33 -1))) | |
3143 | (pass-if (eq? #t (logbit? 63 -1))) | |
3144 | (pass-if (eq? #t (logbit? 64 -1))) | |
3145 | (pass-if (eq? #t (logbit? 65 -1)))) | |
3146 | ||
300c6a76 KR |
3147 | ;;; |
3148 | ;;; logcount | |
3149 | ;;; | |
3150 | ||
3151 | (with-test-prefix "logcount" | |
3152 | ||
3153 | (with-test-prefix "-2^i, meaning ...11100..00" | |
3154 | (do ((n -1 (ash n 1)) | |
3155 | (i 0 (1+ i))) | |
3156 | ((> i 256)) | |
795c0bae KR |
3157 | (pass-if n |
3158 | (= i (logcount n))))) | |
3159 | ||
3160 | (with-test-prefix "2^i" | |
3161 | (do ((n 1 (ash n 1)) | |
3162 | (i 0 (1+ i))) | |
3163 | ((> i 256)) | |
3164 | (pass-if n | |
3165 | (= 1 (logcount n))))) | |
3166 | ||
3167 | (with-test-prefix "2^i-1" | |
3168 | (do ((n 0 (1+ (ash n 1))) | |
3169 | (i 0 (1+ i))) | |
3170 | ((> i 256)) | |
300c6a76 KR |
3171 | (pass-if n |
3172 | (= i (logcount n)))))) | |
795c0bae | 3173 | |
afd09cfb KR |
3174 | ;;; |
3175 | ;;; logior | |
3176 | ;;; | |
3177 | ||
3178 | (with-test-prefix "logior" | |
3179 | (pass-if (eqv? -1 (logior (ash -1 1) 1))) | |
3180 | ||
3181 | ;; check that bignum or bignum+inum args will reduce to an inum | |
3182 | (let () | |
3183 | (define (test x y) | |
3184 | (pass-if (list x y '=> -1) | |
3185 | (eqv? -1 (logior x y))) | |
3186 | (pass-if (list y x '=> -1) | |
3187 | (eqv? -1 (logior y x)))) | |
3188 | (test (ash -1 8) #xFF) | |
3189 | (test (ash -1 28) #x0FFFFFFF) | |
3190 | (test (ash -1 29) #x1FFFFFFF) | |
3191 | (test (ash -1 30) #x3FFFFFFF) | |
3192 | (test (ash -1 31) #x7FFFFFFF) | |
3193 | (test (ash -1 32) #xFFFFFFFF) | |
3194 | (test (ash -1 33) #x1FFFFFFFF) | |
3195 | (test (ash -1 60) #x0FFFFFFFFFFFFFFF) | |
3196 | (test (ash -1 61) #x1FFFFFFFFFFFFFFF) | |
3197 | (test (ash -1 62) #x3FFFFFFFFFFFFFFF) | |
3198 | (test (ash -1 63) #x7FFFFFFFFFFFFFFF) | |
3199 | (test (ash -1 64) #xFFFFFFFFFFFFFFFF) | |
3200 | (test (ash -1 65) #x1FFFFFFFFFFFFFFFF) | |
3201 | (test (ash -1 128) #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))) | |
3202 | ||
1ec2dd6f KR |
3203 | ;;; |
3204 | ;;; lognot | |
3205 | ;;; | |
3206 | ||
3207 | (with-test-prefix "lognot" | |
3208 | (pass-if (= -1 (lognot 0))) | |
3209 | (pass-if (= 0 (lognot -1))) | |
3210 | (pass-if (= -2 (lognot 1))) | |
3211 | (pass-if (= 1 (lognot -2))) | |
3212 | ||
3213 | (pass-if (= #x-100000000000000000000000000000000 | |
3214 | (lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))) | |
3215 | (pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF | |
3216 | (lognot #x-100000000000000000000000000000000)))) | |
8ab3d8a0 KR |
3217 | |
3218 | ;;; | |
3219 | ;;; sqrt | |
3220 | ;;; | |
3221 | ||
3222 | (with-test-prefix "sqrt" | |
3223 | (pass-if "documented?" | |
3224 | (documented? sqrt)) | |
3225 | ||
3226 | (pass-if-exception "no args" exception:wrong-num-args | |
3227 | (sqrt)) | |
3228 | (pass-if-exception "two args" exception:wrong-num-args | |
3229 | (sqrt 123 456)) | |
3230 | ||
3231 | (pass-if (eqv? 0.0 (sqrt 0))) | |
3232 | (pass-if (eqv? 0.0 (sqrt 0.0))) | |
3233 | (pass-if (eqv? 1.0 (sqrt 1.0))) | |
3234 | (pass-if (eqv-loosely? 2.0 (sqrt 4.0))) | |
3235 | (pass-if (eqv-loosely? 31.62 (sqrt 1000.0))) | |
3236 | ||
3237 | (pass-if (eqv? +1.0i (sqrt -1.0))) | |
3238 | (pass-if (eqv-loosely? +2.0i (sqrt -4.0))) | |
3239 | (pass-if (eqv-loosely? +31.62i (sqrt -1000.0))) | |
3240 | ||
3241 | (pass-if "+i swings back to 45deg angle" | |
3242 | (eqv-loosely? +0.7071+0.7071i (sqrt +1.0i))) | |
3243 | ||
3244 | ;; Note: glibc 2.3 csqrt() had a bug affecting this test case, so if it | |
3245 | ;; fails check whether that's the cause (there's a configure test to | |
3246 | ;; reject it, but when cross-compiling we assume the C library is ok). | |
3247 | (pass-if "-100i swings back to 45deg down" | |
3248 | (eqv-loosely? +7.071-7.071i (sqrt -100.0i)))) | |
3249 | ||
c6a576f7 HWN |
3250 | |
3251 | ;; | |
3252 | ;; equal? | |
3253 | ;; | |
3254 | ||
3255 | ||
3256 | (with-test-prefix "equal?" | |
3257 | (pass-if | |
3258 | ||
3259 | ;; lazy reduction bit for rationals should not affect equal? | |
3260 | (equal? 1/2 ((lambda (x) (denominator x) x) 1/2)))) | |
3261 |