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