(define-module (test-suite test-numbers)
#:use-module (test-suite lib)
- #:use-module (ice-9 documentation))
+ #:use-module (ice-9 documentation)
+ #:use-module (srfi srfi-11)) ; let-values
;;;
;;; miscellaneous
(negative? obj)
(inf? obj)))
+;;
+;; Tolerance used by test-eqv? for inexact numbers.
+;;
+(define test-epsilon 1e-10)
+
+;;
+;; Like eqv?, except that inexact finite numbers need only be within
+;; test-epsilon (1e-10) to be considered equal. An exception is made
+;; for zeroes, however. If X is zero, then it is tested using eqv?
+;; without any allowance for imprecision. In particular, 0.0 is
+;; considered distinct from -0.0. For non-real complex numbers,
+;; each component is tested according to these rules. The intent
+;; is that the known-correct value will be the first parameter.
+;;
+(define (test-eqv? x y)
+ (cond ((real? x)
+ (and (real? y) (test-real-eqv? x y)))
+ ((complex? x)
+ (and (not (real? y))
+ (test-real-eqv? (real-part x) (real-part y))
+ (test-real-eqv? (imag-part x) (imag-part y))))
+ (else (eqv? x y))))
+
+;; Auxiliary predicate used by test-eqv?
+(define (test-real-eqv? x y)
+ (cond ((or (exact? x) (zero? x) (nan? x) (inf? x))
+ (eqv? x y))
+ (else (and (inexact? y) (> test-epsilon (abs (- x y)))))))
+
(define const-e 2.7182818284590452354)
(define const-e^2 7.3890560989306502274)
(define const-1/e 0.3678794411714423215)
(eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1)))))
(pass-if "sqrt ((fixnum-max+1)^2 - 1)"
- (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1)))))))
+ (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1)))))
+
+ (pass-if (not (exact? +inf.0)))
+ (pass-if (not (exact? -inf.0)))
+ (pass-if (not (exact? +nan.0)))))
;;;
;;; exp
(pass-if (not (even? (- (* 2 fixnum-min) 1))))
(pass-if (even? (* 2 fixnum-min))))
+;;;
+;;; finite?
+;;;
+
+(with-test-prefix "finite?"
+ (pass-if (documented? finite?))
+ (pass-if (not (finite? (inf))))
+ (pass-if (not (finite? +inf.0)))
+ (pass-if (not (finite? -inf.0)))
+ (pass-if-exception
+ "complex numbers not in domain of finite?"
+ exception:wrong-type-arg
+ (finite? +inf.0+1i))
+ (pass-if-exception
+ "complex numbers not in domain of finite? (2)"
+ exception:wrong-type-arg
+ (finite? +1+inf.0i))
+ (pass-if-exception
+ "complex numbers not in domain of finite? (3)"
+ exception:wrong-type-arg
+ (finite? +1+1i))
+ (pass-if (finite? 3+0i))
+ (pass-if (not (finite? (nan))))
+ (pass-if (not (finite? +nan.0)))
+ (pass-if (finite? 0))
+ (pass-if (finite? 0.0))
+ (pass-if (finite? -0.0))
+ (pass-if (finite? 42.0))
+ (pass-if (finite? 1/2))
+ (pass-if (finite? (+ fixnum-max 1)))
+ (pass-if (finite? (- fixnum-min 1))))
+
;;;
;;; inf? and inf
;;;
;; FIXME: what are the expected behaviors?
;; (pass-if (inf? (/ 1.0 0.0))
;; (pass-if (inf? (/ 1 0.0))
+ (pass-if-exception
+ "complex numbers not in domain of inf?"
+ exception:wrong-type-arg
+ (inf? +1+inf.0i))
+ (pass-if (inf? +inf.0+0i))
(pass-if (not (inf? 0)))
(pass-if (not (inf? 42.0)))
(pass-if (not (inf? (+ fixnum-max 1))))
(pass-if (real? (+ 1 fixnum-max)))
(pass-if (real? (- 1 fixnum-min)))
(pass-if (real? 1.3))
+ (pass-if (real? +inf.0))
+ (pass-if (real? -inf.0))
+ (pass-if (real? +nan.0))
+ (pass-if (not (real? +inf.0-inf.0i)))
+ (pass-if (not (real? +nan.0+nan.0i)))
(pass-if (not (real? 3+4i)))
(pass-if (not (real? #\a)))
(pass-if (not (real? "a")))
(pass-if (not (real? (current-input-port)))))
;;;
-;;; rational? (same as real? right now)
+;;; rational?
;;;
(with-test-prefix "rational?"
(pass-if (rational? (+ 1 fixnum-max)))
(pass-if (rational? (- 1 fixnum-min)))
(pass-if (rational? 1.3))
+ (pass-if (not (rational? +inf.0)))
+ (pass-if (not (rational? -inf.0)))
+ (pass-if (not (rational? +nan.0)))
+ (pass-if (not (rational? +inf.0-inf.0i)))
+ (pass-if (not (rational? +nan.0+nan.0i)))
(pass-if (not (rational? 3+4i)))
(pass-if (not (rational? #\a)))
(pass-if (not (rational? "a")))
(pass-if (not (inexact? (- 1 fixnum-min))))
(pass-if (inexact? 1.3))
(pass-if (inexact? 3.1+4.2i))
+ (pass-if (inexact? +inf.0))
+ (pass-if (inexact? -inf.0))
+ (pass-if (inexact? +nan.0))
(pass-if-exception "char"
exception:wrong-type-arg
(not (inexact? #\a)))
(with-test-prefix "equal?"
(pass-if (documented? equal?))
+
+ ;; The following test will fail on platforms
+ ;; without distinct signed zeroes 0.0 and -0.0.
+ (pass-if (not (equal? 0.0 -0.0)))
+
(pass-if (equal? 0 0))
(pass-if (equal? 7 7))
(pass-if (equal? -7 -7))
(pass-if (equal? (+ 1 fixnum-max) (+ 1 fixnum-max)))
(pass-if (equal? (- fixnum-min 1) (- fixnum-min 1)))
+ (pass-if (equal? 0.0 0.0))
+ (pass-if (equal? -0.0 -0.0))
(pass-if (not (equal? 0 1)))
+ (pass-if (not (equal? 0 0.0)))
+ (pass-if (not (equal? 1 1.0)))
+ (pass-if (not (equal? 0.0 0)))
+ (pass-if (not (equal? 1.0 1)))
+ (pass-if (not (equal? -1.0 -1)))
(pass-if (not (equal? fixnum-max (+ 1 fixnum-max))))
(pass-if (not (equal? (+ 1 fixnum-max) fixnum-max)))
(pass-if (not (equal? (+ 1 fixnum-max) (+ 2 fixnum-max))))
(pass-if (not (equal? (- (ash 1 1024)) -inf.0)))
(pass-if (not (equal? -inf.0 (- (ash 1 1024)))))
- (pass-if (not (equal? +nan.0 +nan.0)))
+ (pass-if (equal? +nan.0 +nan.0))
+ (pass-if (equal? +nan.0 +nan.0))
+ (pass-if (not (equal? +nan.0 0.0+nan.0i)))
+
(pass-if (not (equal? 0 +nan.0)))
(pass-if (not (equal? +nan.0 0)))
(pass-if (not (equal? 1 +nan.0)))
(pass-if (not (equal? (ash 3 1023) +nan.0)))
(pass-if (not (equal? +nan.0 (ash 3 1023)))))
+;;;
+;;; eqv?
+;;;
+
+(with-test-prefix "eqv?"
+ (pass-if (documented? eqv?))
+
+ ;; The following test will fail on platforms
+ ;; without distinct signed zeroes 0.0 and -0.0.
+ (pass-if (not (eqv? 0.0 -0.0)))
+
+ (pass-if (eqv? 0 0))
+ (pass-if (eqv? 7 7))
+ (pass-if (eqv? -7 -7))
+ (pass-if (eqv? (+ 1 fixnum-max) (+ 1 fixnum-max)))
+ (pass-if (eqv? (- fixnum-min 1) (- fixnum-min 1)))
+ (pass-if (eqv? 0.0 0.0))
+ (pass-if (eqv? -0.0 -0.0))
+ (pass-if (not (eqv? 0 1)))
+ (pass-if (not (eqv? 0 0.0)))
+ (pass-if (not (eqv? 1 1.0)))
+ (pass-if (not (eqv? 0.0 0)))
+ (pass-if (not (eqv? 1.0 1)))
+ (pass-if (not (eqv? -1.0 -1)))
+ (pass-if (not (eqv? fixnum-max (+ 1 fixnum-max))))
+ (pass-if (not (eqv? (+ 1 fixnum-max) fixnum-max)))
+ (pass-if (not (eqv? (+ 1 fixnum-max) (+ 2 fixnum-max))))
+ (pass-if (not (eqv? fixnum-min (- fixnum-min 1))))
+ (pass-if (not (eqv? (- fixnum-min 1) fixnum-min)))
+ (pass-if (not (eqv? (- fixnum-min 1) (- fixnum-min 2))))
+ (pass-if (not (eqv? (+ fixnum-max 1) (- fixnum-min 1))))
+
+ (pass-if (not (eqv? (ash 1 256) +inf.0)))
+ (pass-if (not (eqv? +inf.0 (ash 1 256))))
+ (pass-if (not (eqv? (ash 1 256) -inf.0)))
+ (pass-if (not (eqv? -inf.0 (ash 1 256))))
+
+ ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
+ ;; sure we've avoided that
+ (pass-if (not (eqv? (ash 1 1024) +inf.0)))
+ (pass-if (not (eqv? +inf.0 (ash 1 1024))))
+ (pass-if (not (eqv? (- (ash 1 1024)) -inf.0)))
+ (pass-if (not (eqv? -inf.0 (- (ash 1 1024)))))
+
+ (pass-if (eqv? +nan.0 +nan.0))
+ (pass-if (not (eqv? +nan.0 0.0+nan.0i)))
+
+ (pass-if (not (eqv? 0 +nan.0)))
+ (pass-if (not (eqv? +nan.0 0)))
+ (pass-if (not (eqv? 1 +nan.0)))
+ (pass-if (not (eqv? +nan.0 1)))
+ (pass-if (not (eqv? -1 +nan.0)))
+ (pass-if (not (eqv? +nan.0 -1)))
+
+ (pass-if (not (eqv? (ash 1 256) +nan.0)))
+ (pass-if (not (eqv? +nan.0 (ash 1 256))))
+ (pass-if (not (eqv? (- (ash 1 256)) +nan.0)))
+ (pass-if (not (eqv? +nan.0 (- (ash 1 256)))))
+
+ (pass-if (not (eqv? (ash 1 8192) +nan.0)))
+ (pass-if (not (eqv? +nan.0 (ash 1 8192))))
+ (pass-if (not (eqv? (- (ash 1 8192)) +nan.0)))
+ (pass-if (not (eqv? +nan.0 (- (ash 1 8192)))))
+
+ ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
+ ;; sure we've avoided that
+ (pass-if (not (eqv? (ash 3 1023) +nan.0)))
+ (pass-if (not (eqv? +nan.0 (ash 3 1023)))))
+
;;;
;;; =
;;;
(with-test-prefix/c&e "-"
+ (pass-if "double-negation of fixnum-min: ="
+ (= fixnum-min (- (- fixnum-min))))
+ (pass-if "double-negation of fixnum-min: eqv?"
+ (eqv? fixnum-min (- (- fixnum-min))))
+ (pass-if "double-negation of fixnum-min: equal?"
+ (equal? fixnum-min (- (- fixnum-min))))
+
+ (pass-if "binary double-negation of fixnum-min: ="
+ (= fixnum-min (- 0 (- 0 fixnum-min))))
+ (pass-if "binary double-negation of fixnum-min: eqv?"
+ (eqv? fixnum-min (- 0 (- 0 fixnum-min))))
+ (pass-if "binary double-negation of fixnum-min: equal?"
+ (equal? fixnum-min (- 0 (- 0 fixnum-min))))
+
(pass-if "-inum - +bignum"
(= #x-100000000000000000000000000000001
(- -1 #x100000000000000000000000000000000)))
(with-test-prefix "*"
+ (with-test-prefix "double-negation of fixnum-min"
+ (pass-if (= fixnum-min (* -1 (* -1 fixnum-min))))
+ (pass-if (eqv? fixnum-min (* -1 (* -1 fixnum-min))))
+ (pass-if (equal? fixnum-min (* -1 (* -1 fixnum-min))))
+ (pass-if (= fixnum-min (* (* fixnum-min -1) -1)))
+ (pass-if (eqv? fixnum-min (* (* fixnum-min -1) -1)))
+ (pass-if (equal? fixnum-min (* (* fixnum-min -1) -1))))
+
(with-test-prefix "inum * bignum"
(pass-if "0 * 2^256 = 0"
(with-test-prefix "/"
+ (with-test-prefix "double-negation of fixnum-min"
+ (pass-if (= fixnum-min (/ (/ fixnum-min -1) -1)))
+ (pass-if (eqv? fixnum-min (/ (/ fixnum-min -1) -1)))
+ (pass-if (equal? fixnum-min (/ (/ fixnum-min -1) -1))))
+
(pass-if "documented?"
(documented? /))
(pass-if "-100i swings back to 45deg down"
(eqv-loosely? +7.071-7.071i (sqrt -100.0i))))
-
-;;
-;; equal?
-;;
-
-
-(with-test-prefix "equal?"
- (pass-if
-
- ;; lazy reduction bit for rationals should not affect equal?
- (equal? 1/2 ((lambda (x) (denominator x) x) 1/2))))
-
+;;;
+;;; euclidean/
+;;; euclidean-quotient
+;;; euclidean-remainder
+;;; centered/
+;;; centered-quotient
+;;; centered-remainder
+;;;
+
+(with-test-prefix "Number-theoretic division"
+
+ ;; Tests that (lo <= x < hi),
+ ;; but allowing for imprecision
+ ;; if x is inexact.
+ (define (test-within-range? lo hi x)
+ (if (exact? x)
+ (and (<= lo x) (< x hi))
+ (let ((lo (- lo test-epsilon))
+ (hi (+ hi test-epsilon)))
+ (<= lo x hi))))
+
+ (define (safe-euclidean-quotient x y)
+ (cond ((not (and (real? x) (real? y))) (throw 'wrong-type-arg))
+ ((zero? y) (throw 'divide-by-zero))
+ ((nan? y) (nan))
+ ((positive? y) (floor (/ x y)))
+ ((negative? y) (ceiling (/ x y)))
+ (else (throw 'unknown-problem))))
+
+ (define (safe-euclidean-remainder x y)
+ (- x (* y (safe-euclidean-quotient x y))))
+
+ (define (safe-euclidean/ x y)
+ (let ((q (safe-euclidean-quotient x y))
+ (r (safe-euclidean-remainder x y)))
+ (if (not (and (eq? (exact? q) (exact? r))
+ (eq? (exact? q) (and (exact? x) (exact? y)))
+ (test-real-eqv? r (- x (* q y)))
+ (or (and (integer? q)
+ (test-within-range? 0 (abs y) r))
+ (not (finite? x))
+ (not (finite? y)))))
+ (throw 'safe-euclidean/-is-broken (list x y q r))
+ (values q r))))
+
+ (define (safe-centered-quotient x y)
+ (cond ((not (and (real? x) (real? y))) (throw 'wrong-type-arg))
+ ((zero? y) (throw 'divide-by-zero))
+ ((nan? y) (nan))
+ ((positive? y) (floor (+ 1/2 (/ x y))))
+ ((negative? y) (ceiling (+ -1/2 (/ x y))))
+ (else (throw 'unknown-problem))))
+
+ (define (safe-centered-remainder x y)
+ (- x (* y (safe-centered-quotient x y))))
+
+ (define (safe-centered/ x y)
+ (let ((q (safe-centered-quotient x y))
+ (r (safe-centered-remainder x y)))
+ (if (not (and (eq? (exact? q) (exact? r))
+ (eq? (exact? q) (and (exact? x) (exact? y)))
+ (test-real-eqv? r (- x (* q y)))
+ (or (and (integer? q)
+ (test-within-range? (* -1/2 (abs y))
+ (* +1/2 (abs y))
+ r))
+ (not (finite? x))
+ (not (finite? y)))))
+ (throw 'safe-centered/-is-broken (list x y q r))
+ (values q r))))
+
+ (define test-numerators
+ (append
+ (list 123 125 127 130 3 5 10 123.2 125.0
+ -123 -125 -127 -130 -3 -5 -10 -123.2 -125.0
+ 127.2 130.0 123/7 125/7 127/7 130/7
+ -127.2 -130.0 -123/7 -125/7 -127/7 -130/7
+ 0 +0.0 -0.0 +inf.0 -inf.0 +nan.0
+ most-negative-fixnum (1+ most-positive-fixnum)
+ (1- most-negative-fixnum))
+ (apply append
+ (map (lambda (x) (list (* x (+ 1 most-positive-fixnum))
+ (* x (+ 2 most-positive-fixnum))))
+ '( 123 125 127 130 3 5 10
+ -123 -125 -127 -130 -3 -5 -10)))))
+
+ (define test-denominators
+ (list 10 5 10/7 127/2 10.0 63.5
+ -10 -5 -10/7 -127/2 -10.0 -63.5
+ +inf.0 -inf.0 +nan.0 most-negative-fixnum
+ (+ 1 most-positive-fixnum) (+ -1 most-negative-fixnum)
+ (+ 2 most-positive-fixnum) (+ -2 most-negative-fixnum)))
+
+ (define (do-tests-1 op-name real-op safe-op)
+ (for-each (lambda (d)
+ (for-each (lambda (n)
+ (run-test (list op-name n d) #t
+ (lambda ()
+ (test-eqv? (real-op n d)
+ (safe-op n d)))))
+ test-numerators))
+ test-denominators))
+
+ (define (do-tests-2 op-name real-op safe-op)
+ (for-each (lambda (d)
+ (for-each (lambda (n)
+ (run-test (list op-name n d) #t
+ (lambda ()
+ (let-values
+ (((q r) (safe-op n d))
+ ((q1 r1) (real-op n d)))
+ (and (test-eqv? q q1)
+ (test-eqv? r r1))))))
+ test-numerators))
+ test-denominators))
+
+ (with-test-prefix "euclidean-quotient"
+ (do-tests-1 'euclidean-quotient
+ euclidean-quotient
+ safe-euclidean-quotient))
+ (with-test-prefix "euclidean-remainder"
+ (do-tests-1 'euclidean-remainder
+ euclidean-remainder
+ safe-euclidean-remainder))
+ (with-test-prefix "euclidean/"
+ (do-tests-2 'euclidean/
+ euclidean/
+ safe-euclidean/))
+
+ (with-test-prefix "centered-quotient"
+ (do-tests-1 'centered-quotient
+ centered-quotient
+ safe-centered-quotient))
+ (with-test-prefix "centered-remainder"
+ (do-tests-1 'centered-remainder
+ centered-remainder
+ safe-centered-remainder))
+ (with-test-prefix "centered/"
+ (do-tests-2 'centered/
+ centered/
+ safe-centered/)))