;;;; numbers.test --- tests guile's numbers -*- scheme -*-
-;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
+;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
(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)
;;; 1+
;;;
-(with-test-prefix "1+"
+(with-test-prefix/c&e "1+"
(pass-if "documented?"
(documented? 1+))
- (pass-if (eqv? 1 (1+ 0)))
- (pass-if (eqv? 0 (1+ -1)))
- (pass-if (eqv? 101 (1+ 100)))
- (pass-if (eqv? -99 (1+ -100))))
+ (pass-if "0" (eqv? 1 (1+ 0)))
+ (pass-if "-1" (eqv? 0 (1+ -1)))
+ (pass-if "100" (eqv? 101 (1+ 100)))
+ (pass-if "-100" (eqv? -99 (1+ -100)))
+
+ ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1.
+ (pass-if "1+ fixnum = bignum (32-bit)"
+ (eqv? 536870912 (1+ 536870911)))
+
+ ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1.
+ (pass-if "1+ fixnum = bignum (64-bit)"
+ (eqv? 2305843009213693952 (1+ 2305843009213693951))))
;;;
;;; 1-
;;;
-(with-test-prefix "1-"
+(with-test-prefix/c&e "1-"
(pass-if "documented?"
(documented? 1-))
- (pass-if (eqv? -1 (1- 0)))
- (pass-if (eqv? 0 (1- 1)))
- (pass-if (eqv? 99 (1- 100)))
- (pass-if (eqv? -101 (1- -100))))
+ (pass-if "0" (eqv? -1 (1- 0)))
+ (pass-if "1" (eqv? 0 (1- 1)))
+ (pass-if "100" (eqv? 99 (1- 100)))
+ (pass-if "-100" (eqv? -101 (1- -100)))
+
+ ;; The minimum fixnum on a 32-bit architecture: -2^29.
+ (pass-if "1- fixnum = bignum (32-bit)"
+ (eqv? -536870913 (1- -536870912)))
+
+ ;; The minimum fixnum on a 64-bit architecture: -2^61.
+ (pass-if "1- fixnum = bignum (64-bit)"
+ (eqv? -2305843009213693953 (1- -2305843009213693952))))
;;;
;;; ash
(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
;;;
(with-test-prefix "exp"
- (pass-if "documented?"
- (documented? exp))
+ (pass-if (documented? exp))
(pass-if-exception "no args" exception:wrong-num-args
(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))))
;;;
(with-test-prefix "quotient"
-
- (expect-fail "documented?"
- (documented? quotient))
+ (pass-if (documented? quotient))
(with-test-prefix "0 / n"
;;;
(with-test-prefix "remainder"
-
- (expect-fail "documented?"
- (documented? remainder))
+ (pass-if (documented? remainder))
(with-test-prefix "0 / n"
;;;
(with-test-prefix "modulo"
-
- (expect-fail "documented?"
- (documented? modulo))
+ (pass-if (documented? modulo))
(with-test-prefix "0 % n"
;; or not. It is clearly undesirable to have number->string to be
;; influenced by this.
- (pass-if (string=? (number->string 35.25 36) "Z.9"))
+ (pass-if (string=? (number->string 35.25 36) "z.9"))
(pass-if (or (string=? (number->string 0.25 2) "0.01")
(string=? (number->string 0.25 2) "0.010")))
- (pass-if (string=? (number->string 255.0625 16) "FF.1"))
+ (pass-if (string=? (number->string 255.0625 16) "ff.1"))
(pass-if (string=? (number->string (/ 1 3) 3) "1/10"))
+ (pass-if (string=? (number->string 10) "10"))
+ (pass-if (string=? (number->string 10 11) "a"))
+ (pass-if (string=? (number->string 36 36) "10"))
+ (pass-if (= (num->str->num 36 36) 36))
+ (pass-if (= (string->number "z" 36) 35))
+ (pass-if (= (string->number "Z" 36) 35))
+ (pass-if (not (string->number "Z" 35)))
+ (pass-if (string=? (number->string 35 36) "z"))
+ (pass-if (= (num->str->num 35 36) 35))
+
;; Numeric conversion from decimal is not precise, in its current
;; implementation, so 11.333... and 1.324... can't be expected to
;; reliably come out to precise values. These tests did actually work
(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 (and (= 3+0i (round 3+0i)) (integer? 3+0i)))
(pass-if (and (= 1.0 (round 1.0)) (integer? 1.0)))
(pass-if (not (integer? 1.3)))
- (pass-if (integer? +inf.0))
- (pass-if (integer? -inf.0))
+ (pass-if (not (integer? +inf.0)))
+ (pass-if (not (integer? -inf.0)))
(pass-if (not (integer? +nan.0)))
(pass-if (not (integer? 3+4i)))
(pass-if (not (integer? #\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 "zero?"
- (expect-fail (documented? zero?))
+ (pass-if (documented? zero?))
(pass-if (zero? 0))
(pass-if (not (zero? 7)))
(pass-if (not (zero? -7)))
;;;
(with-test-prefix "positive?"
- (expect-fail (documented? positive?))
+ (pass-if (documented? positive?))
(pass-if (positive? 1))
(pass-if (positive? (+ fixnum-max 1)))
(pass-if (positive? 1.3))
;;;
(with-test-prefix "negative?"
- (expect-fail (documented? negative?))
+ (pass-if (documented? negative?))
(pass-if (not (negative? 1)))
(pass-if (not (negative? (+ fixnum-max 1))))
(pass-if (not (negative? 1.3)))
;;; +
;;;
-(with-test-prefix "+"
+(with-test-prefix/c&e "+"
(pass-if "documented?"
- (documented? +)))
+ (documented? +))
+
+ ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1.
+ (pass-if "fixnum + fixnum = bignum (32-bit)"
+ (eqv? 536870912 (+ 536870910 2)))
+
+ ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1.
+ (pass-if "fixnum + fixnum = bignum (64-bit)"
+ (eqv? 2305843009213693952 (+ 2305843009213693950 2)))
+
+ (pass-if "bignum + fixnum = fixnum"
+ (eqv? 0 (+ (1+ most-positive-fixnum) most-negative-fixnum))))
;;;
;;; -
;;;
-(with-test-prefix "-"
+(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
(pass-if "big - -inum"
(= #x100000000000000000000000000000001
- (- #x100000000000000000000000000000000 -1))))
+ (- #x100000000000000000000000000000000 -1)))
+
+ ;; The mininum fixnum on a 32-bit architecture: -2^29.
+ (pass-if "fixnum - fixnum = bignum (32-bit)"
+ (eqv? -536870912 (- -536870910 2)))
+
+ ;; The minimum fixnum on a 64-bit architecture: -2^61.
+ (pass-if "fixnum - fixnum = bignum (64-bit)"
+ (eqv? -2305843009213693952 (- -2305843009213693950 2)))
+
+ (pass-if "bignum - fixnum = fixnum"
+ (eqv? most-positive-fixnum (- (1+ most-positive-fixnum) 1))))
;;;
;;; *
(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? /))
;;;
(with-test-prefix "expt"
- (pass-if "(= 1 (expt 0 0))" (= 1 (expt 0 0)))
- (pass-if "(= 1 (expt 0 0.0))" (= 1 (expt 0 0.0)))
- (pass-if "(= 1 (expt 0.0 0))" (= 1 (expt 0.0 0)))
- (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0))))
+ (pass-if (documented? expt))
+ (pass-if-exception "non-numeric base" exception:wrong-type-arg
+ (expt #t 0))
+ (pass-if (eqv? 1 (expt 0 0)))
+ (pass-if (eqv? 1 (expt 0.0 0)))
+ (pass-if (eqv? 1.0 (expt 0 0.0)))
+ (pass-if (eqv? 1.0 (expt 0.0 0.0)))
+ (pass-if (nan? (expt 0 -1)))
+ (pass-if (nan? (expt 0 -1.0)))
+ (pass-if (nan? (expt 0.0 -1)))
+ (pass-if (nan? (expt 0.0 -1.0)))
+ (pass-if (eqv? 0 (expt 0 3)))
+ (pass-if (= 0 (expt 0 4.0)))
+ (pass-if (eqv? 0.0 (expt 0.0 5)))
+ (pass-if (eqv? 0.0 (expt 0.0 6.0)))
+ (pass-if (eqv? -2742638075.5 (expt -2742638075.5 1)))
+ (pass-if (eqv? (* -2742638075.5 -2742638075.5)
+ (expt -2742638075.5 2)))
+ (pass-if (eqv? 4.0 (expt -2.0 2.0)))
+ (pass-if (eqv? -1/8 (expt -2 -3)))
+ (pass-if (eqv? -0.125 (expt -2.0 -3)))
+ (pass-if (eqv? -0.125 (expt -2 -3.0)))
+ (pass-if (eqv? -0.125 (expt -2.0 -3.0)))
+ (pass-if (eqv? 0.25 (expt 2.0 -2.0)))
+ (pass-if (eqv? (* -1.0 12398 12398) (expt +12398i 2.0)))
+ (pass-if (eqv-loosely? +i (expt -1 0.5)))
+ (pass-if (eqv-loosely? +i (expt -1 1/2)))
+ (pass-if (eqv-loosely? 1.0+1.7320508075688i (expt -8 1/3)))
+ (pass-if (eqv? +inf.0 (expt 2 +inf.0)))
+ (pass-if (eqv? +inf.0 (expt 2.0 +inf.0)))
+ (pass-if (eqv? 0.0 (expt 2 -inf.0)))
+ (pass-if (eqv? 0.0 (expt 2.0 -inf.0))))
+
;;;
;;; asinh
;;; real-part
;;;
+(with-test-prefix "real-part"
+ (pass-if (documented? real-part))
+ (pass-if (eqv? 5.0 (real-part 5.0)))
+ (pass-if (eqv? 0.0 (real-part +5.0i)))
+ (pass-if (eqv? 5 (real-part 5)))
+ (pass-if (eqv? 1/5 (real-part 1/5)))
+ (pass-if (eqv? (1+ fixnum-max) (real-part (1+ fixnum-max)))))
+
;;;
;;; imag-part
;;;
+(with-test-prefix "imag-part"
+ (pass-if (documented? imag-part))
+ (pass-if (eqv? 0.0 (imag-part 5.0)))
+ (pass-if (eqv? 5.0 (imag-part +5.0i)))
+ (pass-if (eqv? 0 (imag-part 5)))
+ (pass-if (eqv? 0 (imag-part 1/5)))
+ (pass-if (eqv? 0 (imag-part (1+ fixnum-max)))))
+
;;;
;;; magnitude
;;;
(with-test-prefix "magnitude"
+ (pass-if (documented? magnitude))
(pass-if (= 0 (magnitude 0)))
(pass-if (= 1 (magnitude 1)))
(pass-if (= 1 (magnitude -1)))
(define (almost= x y)
(> 0.01 (magnitude (- x y))))
+ (pass-if (documented? angle))
+
(pass-if "inum +ve" (= 0 (angle 1)))
(pass-if "inum -ve" (almost= pi (angle -1)))
;;;
(with-test-prefix "inexact->exact"
-
+ (pass-if (documented? inexact->exact))
+
(pass-if-exception "+inf" exception:out-of-range
(inexact->exact +inf.0))
;;;
(with-test-prefix "integer-expt"
+ (pass-if (documented? integer-expt))
+ (pass-if-exception "non-numeric base" exception:wrong-type-arg
+ (integer-expt #t 0))
(pass-if-exception "2^+inf" exception:wrong-type-arg
(integer-expt 2 +inf.0))
(pass-if-exception "2^-inf" exception:wrong-type-arg
(integer-expt 2 -inf.0))
(pass-if-exception "2^nan" exception:wrong-type-arg
- (integer-expt 2 +nan.0)))
+ (integer-expt 2 +nan.0))
+
+ (pass-if (eqv? 1 (integer-expt 0 0)))
+ (pass-if (eqv? 1 (integer-expt 0.0 0)))
+ (pass-if (nan? (integer-expt 0 -1)))
+ (pass-if (nan? (integer-expt 0.0 -1)))
+ (pass-if (eqv? 0 (integer-expt 0 3)))
+ (pass-if (eqv? 0.0 (integer-expt 0.0 5)))
+ (pass-if (eqv? -2742638075.5 (integer-expt -2742638075.5 1)))
+ (pass-if (eqv? (* -2742638075.5 -2742638075.5)
+ (integer-expt -2742638075.5 2)))
+ (pass-if (eqv? 4.0 (integer-expt -2.0 2)))
+ (pass-if (eqv? -1/8 (integer-expt -2 -3)))
+ (pass-if (eqv? -0.125 (integer-expt -2.0 -3)))
+ (pass-if (eqv? 0.25 (integer-expt 2.0 -2)))
+ (pass-if (eqv? (* -1.0 12398 12398) (integer-expt +12398.0i 2))))
+
;;;
;;; integer-length
;;;
(with-test-prefix "integer-length"
+ (pass-if (documented? integer-length))
(with-test-prefix "-2^i, ...11100..00"
(do ((n -1 (ash n 1))
;;;
(with-test-prefix "log"
- (pass-if "documented?"
- (documented? log))
+ (pass-if (documented? log))
(pass-if-exception "no args" exception:wrong-num-args
(log))
;;;
(with-test-prefix "log10"
- (pass-if "documented?"
- (documented? log10))
+ (pass-if (documented? log10))
(pass-if-exception "no args" exception:wrong-num-args
(log10))
;;;
(with-test-prefix "logbit?"
+ (pass-if (documented? logbit?))
+
(pass-if (eq? #f (logbit? 0 0)))
(pass-if (eq? #f (logbit? 1 0)))
(pass-if (eq? #f (logbit? 31 0)))
;;;
(with-test-prefix "logcount"
+ (pass-if (documented? logcount))
(with-test-prefix "-2^i, meaning ...11100..00"
(do ((n -1 (ash n 1))
;;;
(with-test-prefix "logior"
+ (pass-if (documented? logior))
+
(pass-if (eqv? -1 (logior (ash -1 1) 1)))
;; check that bignum or bignum+inum args will reduce to an inum
;;;
(with-test-prefix "lognot"
+ (pass-if (documented? lognot))
+
(pass-if (= -1 (lognot 0)))
(pass-if (= 0 (lognot -1)))
(pass-if (= -2 (lognot 1)))
;;;
(with-test-prefix "sqrt"
- (pass-if "documented?"
- (documented? sqrt))
+ (pass-if (documented? sqrt))
(pass-if-exception "no args" exception:wrong-num-args
(sqrt))
(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))
+
+ (pass-if (documented? euclidean/))
+ (pass-if (documented? euclidean-quotient))
+ (pass-if (documented? euclidean-remainder))
+ (pass-if (documented? centered/))
+ (pass-if (documented? centered-quotient))
+ (pass-if (documented? centered-remainder))
+
+ (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/)))