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