;;;; numbers.test --- tests guile's numbers -*- scheme -*-
-;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005 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
;;;; License as published by the Free Software Foundation; either
-;;;; version 2.1 of the License, or (at your option) any later version.
+;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
-;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(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
(quotient (- n d -1) d) ;; neg/pos
(quotient n d))) ;; pos/pos
+;; return true of X is in the range LO to HI, inclusive
+(define (within-range? lo hi x)
+ (and (>= x (min lo hi))
+ (<= x (max lo hi))))
+
+;; return true if GOT is within +/- 0.01 of GOT
+;; for a complex number both real and imaginary parts must be in that range
+(define (eqv-loosely? want got)
+ (and (within-range? (- (real-part want) 0.01)
+ (+ (real-part want) 0.01)
+ (real-part got))
+ (within-range? (- (imag-part want) 0.01)
+ (+ (imag-part want) 0.01)
+ (imag-part got))))
+
+;; return true if OBJ is negative infinity
+(define (negative-infinity? obj)
+ (and (real? obj)
+ (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? exp))
+
+ (pass-if-exception "no args" exception:wrong-num-args
+ (exp))
+ (pass-if-exception "two args" exception:wrong-num-args
+ (exp 123 456))
+
+ (pass-if (eqv? 0.0 (exp -inf.0)))
+ (pass-if (eqv-loosely? 1.0 (exp 0)))
+ (pass-if (eqv-loosely? 1.0 (exp 0.0)))
+ (pass-if (eqv-loosely? const-e (exp 1.0)))
+ (pass-if (eqv-loosely? const-e^2 (exp 2.0)))
+ (pass-if (eqv-loosely? const-1/e (exp -1)))
+
+ (pass-if "exp(pi*i) = -1"
+ (eqv-loosely? -1.0 (exp 0+3.14159i)))
+ (pass-if "exp(-pi*i) = -1"
+ (eqv-loosely? -1.0 (exp 0-3.14159i)))
+ (pass-if "exp(2*pi*i) = +1"
+ (eqv-loosely? 1.0 (exp 0+6.28318i)))
+
+ (pass-if "exp(2-pi*i) = -e^2"
+ (eqv-loosely? (- const-e^2) (exp 2.0-3.14159i))))
;;;
;;; odd?
(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"
exception:wrong-type-arg
(modulo-expt 17 23 'Ethel)))
+;;;
+;;; numerator
+;;;
+
+(with-test-prefix "numerator"
+ (pass-if "0"
+ (eqv? 0 (numerator 0)))
+ (pass-if "1"
+ (eqv? 1 (numerator 1)))
+ (pass-if "2"
+ (eqv? 2 (numerator 2)))
+ (pass-if "-1"
+ (eqv? -1 (numerator -1)))
+ (pass-if "-2"
+ (eqv? -2 (numerator -2)))
+
+ (pass-if "0.0"
+ (eqv? 0.0 (numerator 0.0)))
+ (pass-if "1.0"
+ (eqv? 1.0 (numerator 1.0)))
+ (pass-if "2.0"
+ (eqv? 2.0 (numerator 2.0)))
+ (pass-if "-1.0"
+ (eqv? -1.0 (numerator -1.0)))
+ (pass-if "-2.0"
+ (eqv? -2.0 (numerator -2.0)))
+
+ (pass-if "0.5"
+ (eqv? 1.0 (numerator 0.5)))
+ (pass-if "0.25"
+ (eqv? 1.0 (numerator 0.25)))
+ (pass-if "0.75"
+ (eqv? 3.0 (numerator 0.75))))
+
+;;;
+;;; denominator
+;;;
+
+(with-test-prefix "denominator"
+ (pass-if "0"
+ (eqv? 1 (denominator 0)))
+ (pass-if "1"
+ (eqv? 1 (denominator 1)))
+ (pass-if "2"
+ (eqv? 1 (denominator 2)))
+ (pass-if "-1"
+ (eqv? 1 (denominator -1)))
+ (pass-if "-2"
+ (eqv? 1 (denominator -2)))
+
+ (pass-if "0.0"
+ (eqv? 1.0 (denominator 0.0)))
+ (pass-if "1.0"
+ (eqv? 1.0 (denominator 1.0)))
+ (pass-if "2.0"
+ (eqv? 1.0 (denominator 2.0)))
+ (pass-if "-1.0"
+ (eqv? 1.0 (denominator -1.0)))
+ (pass-if "-2.0"
+ (eqv? 1.0 (denominator -2.0)))
+
+ (pass-if "0.5"
+ (eqv? 2.0 (denominator 0.5)))
+ (pass-if "0.25"
+ (eqv? 4.0 (denominator 0.25)))
+ (pass-if "0.75"
+ (eqv? 4.0 (denominator 0.75))))
+
;;;
;;; gcd
;;;
(with-test-prefix "gcd"
- (expect-fail "documented?"
+ (pass-if "documented?"
(documented? gcd))
+ (with-test-prefix "(n)"
+
+ (pass-if "n = -2"
+ (eqv? 2 (gcd -2))))
+
(with-test-prefix "(0 n)"
(pass-if "n = 0"
(with-test-prefix "lcm"
;; FIXME: more tests?
;; (some of these are already in r4rs.test)
- (expect-fail (documented? lcm))
+ (pass-if (documented? lcm))
(pass-if (= (lcm) 1))
(pass-if (= (lcm 32 -36) 288))
(let ((big-n 115792089237316195423570985008687907853269984665640564039457584007913129639936) ; 2 ^ 256
;; 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 (or (string=? (number->string 11.33333333333333333 12)
- "B.4")
- (string=? (number->string 11.33333333333333333 12)
- "B.400000000000009")))
- (pass-if (or (string=? (number->string 1.324e44 16)
- "5.EFE0A14FAFEe24")
- (string=? (number->string 1.324e44 16)
- "5.EFE0A14FAFDF8e24")))))
+
+ (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
+ ;; for a while, but something in gcc changed, affecting the conversion
+ ;; code.
+ ;;
+ ;; (pass-if (or (string=? (number->string 11.33333333333333333 12)
+ ;; "B.4")
+ ;; (string=? (number->string 11.33333333333333333 12)
+ ;; "B.400000000000009")))
+ ;; (pass-if (or (string=? (number->string 1.324e44 16)
+ ;; "5.EFE0A14FAFEe24")
+ ;; (string=? (number->string 1.324e44 16)
+ ;; "5.EFE0A14FAFDF8e24")))
+ ))
;;;
;;; string->number
("1@0" 1.0) ("1@+0" 1.0) ("1@-0" 1.0)
("2+3i" ,(+ 2 (* 3 +i))) ("4-5i" ,(- 4 (* 5 +i)))
("1+i" 1+1i) ("1-i" 1-1i) ("+1i" 0+1i) ("-1i" 0-1i)
- ("+i" +1i) ("-i" -1i)))
+ ("+i" +1i) ("-i" -1i)
+ ("1.0+.1i" 1.0+0.1i)
+ ("1.0-.1i" 1.0-0.1i)
+ (".1+.0i" 0.1)
+ ("1.+.0i" 1.0)
+ (".1+.1i" 0.1+0.1i)
+ ("1e1+.1i" 10+0.1i)
+ ))
#t)
(pass-if-exception "exponent too big"
(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 "="
- (expect-fail (documented? =))
+ (pass-if (documented? =))
(pass-if (= 0 0))
(pass-if (= 7 7))
(pass-if (= -7 -7))
(with-test-prefix "<"
- (expect-fail "documented?"
+ (pass-if "documented?"
(documented? <))
(with-test-prefix "(< 0 n)"
;;;
(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 "big / real"
(pass-if (nan? (max big*5 +nan.0)))
- (pass-if (= big*5 (max big*5 -inf.0)))
- (pass-if (= +inf.0 (max big*5 +inf.0)))
- (pass-if (= 1.0 (max (- big*5) 1.0)))
- (pass-if (inexact? (max big*5 1.0)))
- (pass-if (= (exact->inexact big*5) (max big*5 1.0))))
+ (pass-if (eqv? (exact->inexact big*5) (max big*5 -inf.0)))
+ (pass-if (eqv? (exact->inexact big*5) (max big*5 1.0)))
+ (pass-if (eqv? +inf.0 (max big*5 +inf.0)))
+ (pass-if (eqv? 1.0 (max (- big*5) 1.0))))
(with-test-prefix "real / big"
(pass-if (nan? (max +nan.0 big*5)))
- (pass-if (= +inf.0 (max +inf.0 big*5)))
- (pass-if (= big*5 (max -inf.0 big*5)))
- (pass-if (= 1.0 (max 1.0 (- big*5))))
- (pass-if (inexact? (max 1.0 big*5)))
- (pass-if (= (exact->inexact big*5) (max 1.0 big*5))))
+ (pass-if (eqv? (exact->inexact big*5) (max -inf.0 big*5)))
+ (pass-if (eqv? (exact->inexact big*5) (max 1.0 big*5)))
+ (pass-if (eqv? +inf.0 (max +inf.0 big*5)))
+ (pass-if (eqv? 1.0 (max 1.0 (- big*5)))))
(with-test-prefix "frac / frac"
(pass-if (= 2/3 (max 1/2 2/3)))
(big*4 (* fixnum-max 4))
(big*5 (* fixnum-max 5)))
- (expect-fail (documented? min))
+ (pass-if (documented? min))
(pass-if (= 1 (min 7 3 1 5)))
(pass-if (= 1 (min 1 7 3 5)))
(pass-if (= 1 (min 7 3 5 1)))
(with-test-prefix "big / real"
(pass-if (nan? (min big*5 +nan.0)))
- (pass-if (= big*5 (min big*5 +inf.0)))
- (pass-if (= -inf.0 (min big*5 -inf.0)))
- (pass-if (= 1.0 (min big*5 1.0)))
- (pass-if (inexact? (min (- big*5) 1.0)))
- (pass-if (= (exact->inexact (- big*5)) (min (- big*5) 1.0))))
+ (pass-if (eqv? (exact->inexact big*5) (min big*5 +inf.0)))
+ (pass-if (eqv? -inf.0 (min big*5 -inf.0)))
+ (pass-if (eqv? 1.0 (min big*5 1.0)))
+ (pass-if (eqv? (exact->inexact (- big*5)) (min (- big*5) 1.0))))
(with-test-prefix "real / big"
(pass-if (nan? (min +nan.0 big*5)))
- (pass-if (= big*5 (min +inf.0 big*5)))
- (pass-if (= -inf.0 (min -inf.0 big*5)))
- (pass-if (= 1.0 (min 1.0 big*5)))
- (pass-if (inexact? (min 1.0 (- big*5))))
- (pass-if (= (exact->inexact (- big*5)) (min 1.0 (- big*5)))))
+ (pass-if (eqv? (exact->inexact big*5) (min +inf.0 big*5)))
+ (pass-if (eqv? -inf.0 (min -inf.0 big*5)))
+ (pass-if (eqv? 1.0 (min 1.0 big*5)))
+ (pass-if (eqv? (exact->inexact (- big*5)) (min 1.0 (- big*5)))))
(with-test-prefix "frac / frac"
(pass-if (= 1/2 (min 1/2 2/3)))
;;; +
;;;
-(with-test-prefix "+"
+(with-test-prefix/c&e "+"
- (expect-fail "documented?"
+ (pass-if "documented?"
(documented? +))
- (with-test-prefix "wrong type argument"
+ ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1.
+ (pass-if "fixnum + fixnum = bignum (32-bit)"
+ (eqv? 536870912 (+ 536870910 2)))
- (pass-if-exception "1st argument string"
- exception:wrong-type-arg
- (+ "1" 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))))
- (pass-if-exception "2nd argument bool"
- exception:wrong-type-arg
- (+ 1 #f))))
;;;
;;; -
;;;
-(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"
+ (eqv? 0 (* 0 (ash 1 256)))))
+
+ (with-test-prefix "inum * flonum"
+
+ (pass-if "0 * 1.0 = 0"
+ (eqv? 0 (* 0 1.0))))
+
+ (with-test-prefix "inum * complex"
+
+ (pass-if "0 * 1+1i = 0"
+ (eqv? 0 (* 0 1+1i))))
+
+ (with-test-prefix "inum * frac"
+
+ (pass-if "0 * 2/3 = 0"
+ (eqv? 0 (* 0 2/3))))
+
+ (with-test-prefix "bignum * inum"
+
+ (pass-if "2^256 * 0 = 0"
+ (eqv? 0 (* (ash 1 256) 0))))
+
+ (with-test-prefix "flonum * inum"
+
+ ;; in guile 1.6.8 and 1.8.1 and earlier this returned inexact 0.0
+ (pass-if "1.0 * 0 = 0"
+ (eqv? 0 (* 1.0 0))))
+
+ (with-test-prefix "complex * inum"
+
+ ;; in guile 1.6.8 and 1.8.1 and earlier this returned inexact 0.0
+ (pass-if "1+1i * 0 = 0"
+ (eqv? 0 (* 1+1i 0))))
+
(pass-if "complex * bignum"
(let ((big (ash 1 90)))
(= (make-rectangular big big)
- (* 1+1i big)))))
+ (* 1+1i big))))
+
+ (with-test-prefix "frac * inum"
+
+ (pass-if "2/3 * 0 = 0"
+ (eqv? 0 (* 2/3 0)))))
;;;
;;; /
(with-test-prefix "/"
- (expect-fail "documented?"
+ (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 "division by zero"
(pass-if-exception "(/ 0)"
- exception:numerical-overflow
+ exception:numerical-overflow
(/ 0))
(pass-if "(/ 0.0)"
(= +inf.0 (/ 0.0)))
(pass-if-exception "(/ 1 0)"
- exception:numerical-overflow
+ exception:numerical-overflow
(/ 1 0))
(pass-if "(/ 1 0.0)"
(= +inf.0 (/ 1 0.0)))
(pass-if-exception "(/ bignum 0)"
- exception:numerical-overflow
+ exception:numerical-overflow
(/ (+ fixnum-max 1) 0))
(pass-if "(/ bignum 0.0)"
(= +inf.0 (/ (+ fixnum-max 1) 0.0)))
(pass-if-exception "(/ 1.0 0)"
- exception:numerical-overflow
+ exception:numerical-overflow
(/ 1.0 0))
(pass-if "(/ 1.0 0.0)"
(= +inf.0 (/ 1.0 0.0)))
(pass-if-exception "(/ +i 0)"
- exception:numerical-overflow
+ exception:numerical-overflow
(/ +i 0))
(pass-if "(/ +i 0.0)"
(= +inf.0 (imag-part (/ +i 0.0)))))
- (with-test-prefix "complex division"
+ (with-test-prefix "1/complex"
+
+ (pass-if "0+1i"
+ (eqv? 0-1i (/ 0+1i)))
+
+ ;; in guile 1.6 through 1.6.7 this incorrectly resulted in nans
+ (pass-if "0-1i"
+ (eqv? 0+1i (/ 0-1i)))
+
+ (pass-if "1+1i"
+ (eqv? 0.5-0.5i (/ 1+1i)))
+
+ (pass-if "1-1i"
+ (eqv? 0.5+0.5i (/ 1-1i)))
+
+ (pass-if "-1+1i"
+ (eqv? -0.5-0.5i (/ -1+1i)))
+
+ (pass-if "-1-1i"
+ (eqv? -0.5+0.5i (/ -1-1i)))
(pass-if "(/ 3+4i)"
(= (/ 3+4i) 0.12-0.16i))
(pass-if "(/ 4+3i)"
(= (/ 4+3i) 0.16-0.12i))
- (pass-if "(/ 25+125i 3+4i)"
- (= (/ 25+125i 3+4i) 23.0+11.0i))
+ (pass-if "(/ 1e200+1e200i)"
+ (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i)))
- (pass-if "(/ 25+125i 4+3i)"
- (= (/ 25+125i 4+3i) 19.0+17.0i))
+ (with-test-prefix "inum/complex"
(pass-if "(/ 25 3+4i)"
(= (/ 25 3+4i) 3.0-4.0i))
(pass-if "(/ 25 4+3i)"
- (= (/ 25 4+3i) 4.0-3.0i))
+ (= (/ 25 4+3i) 4.0-3.0i)))
- (pass-if "(/ 1e200+1e200i)"
- (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i))))
+ (with-test-prefix "complex/complex"
+
+ (pass-if "(/ 25+125i 3+4i)"
+ (= (/ 25+125i 3+4i) 23.0+11.0i))
+
+ (pass-if "(/ 25+125i 4+3i)"
+ (= (/ 25+125i 4+3i) 19.0+17.0i))))
;;;
;;; truncate
(n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
(want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want)))
((> i 100))
- (try-i i n want))))
+ (try-i i n want)))
+
+ (pass-if "frac big/big"
+ (let ((big (ash 1 256)))
+ (= 1.0 (exact->inexact (/ (1+ big) big)))))
+
+ ;; In guile 1.8.0 this failed, giving back "nan" because it tried to
+ ;; convert the num and den to doubles, resulting in infs.
+ (pass-if "frac big/big, exceeding double"
+ (let ((big (ash 1 4096)))
+ (= 1.0 (exact->inexact (/ (1+ big) big))))))
;;;
;;; floor
;;;
(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))
(pass-if n
(= i (integer-length n))))))
+;;;
+;;; log
+;;;
+
+(with-test-prefix "log"
+ (pass-if (documented? log))
+
+ (pass-if-exception "no args" exception:wrong-num-args
+ (log))
+ (pass-if-exception "two args" exception:wrong-num-args
+ (log 123 456))
+
+ (pass-if (negative-infinity? (log 0)))
+ (pass-if (negative-infinity? (log 0.0)))
+ (pass-if (eqv? 0.0 (log 1)))
+ (pass-if (eqv? 0.0 (log 1.0)))
+ (pass-if (eqv-loosely? 1.0 (log const-e)))
+ (pass-if (eqv-loosely? 2.0 (log const-e^2)))
+ (pass-if (eqv-loosely? -1.0 (log const-1/e)))
+
+ (pass-if (eqv-loosely? 1.0+1.57079i (log 0+2.71828i)))
+ (pass-if (eqv-loosely? 1.0-1.57079i (log 0-2.71828i)))
+
+ (pass-if (eqv-loosely? 0.0+3.14159i (log -1.0)))
+ (pass-if (eqv-loosely? 1.0+3.14159i (log -2.71828)))
+ (pass-if (eqv-loosely? 2.0+3.14159i (log (* -2.71828 2.71828)))))
+
+;;;
+;;; log10
+;;;
+
+(with-test-prefix "log10"
+ (pass-if (documented? log10))
+
+ (pass-if-exception "no args" exception:wrong-num-args
+ (log10))
+ (pass-if-exception "two args" exception:wrong-num-args
+ (log10 123 456))
+
+ (pass-if (negative-infinity? (log10 0)))
+ (pass-if (negative-infinity? (log10 0.0)))
+ (pass-if (eqv? 0.0 (log10 1)))
+ (pass-if (eqv? 0.0 (log10 1.0)))
+ (pass-if (eqv-loosely? 1.0 (log10 10.0)))
+ (pass-if (eqv-loosely? 2.0 (log10 100.0)))
+ (pass-if (eqv-loosely? -1.0 (log10 0.1)))
+
+ (pass-if (eqv-loosely? 1.0+0.68218i (log10 0+10.0i)))
+ (pass-if (eqv-loosely? 1.0-0.68218i (log10 0-10.0i)))
+
+ (pass-if (eqv-loosely? 0.0+1.36437i (log10 -1)))
+ (pass-if (eqv-loosely? 1.0+1.36437i (log10 -10)))
+ (pass-if (eqv-loosely? 2.0+1.36437i (log10 -100))))
+
;;;
;;; logbit?
;;;
(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)))
(lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
(pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
(lognot #x-100000000000000000000000000000000))))
+
+;;;
+;;; sqrt
+;;;
+
+(with-test-prefix "sqrt"
+ (pass-if (documented? sqrt))
+
+ (pass-if-exception "no args" exception:wrong-num-args
+ (sqrt))
+ (pass-if-exception "two args" exception:wrong-num-args
+ (sqrt 123 456))
+
+ (pass-if (eqv? 0.0 (sqrt 0)))
+ (pass-if (eqv? 0.0 (sqrt 0.0)))
+ (pass-if (eqv? 1.0 (sqrt 1.0)))
+ (pass-if (eqv-loosely? 2.0 (sqrt 4.0)))
+ (pass-if (eqv-loosely? 31.62 (sqrt 1000.0)))
+
+ (pass-if (eqv? +1.0i (sqrt -1.0)))
+ (pass-if (eqv-loosely? +2.0i (sqrt -4.0)))
+ (pass-if (eqv-loosely? +31.62i (sqrt -1000.0)))
+
+ (pass-if "+i swings back to 45deg angle"
+ (eqv-loosely? +0.7071+0.7071i (sqrt +1.0i)))
+
+ ;; Note: glibc 2.3 csqrt() had a bug affecting this test case, so if it
+ ;; fails check whether that's the cause (there's a configure test to
+ ;; reject it, but when cross-compiling we assume the C library is ok).
+ (pass-if "-100i swings back to 45deg down"
+ (eqv-loosely? +7.071-7.071i (sqrt -100.0i))))
+
+;;;
+;;; 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/)))