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