;;;; numbers.test --- tests guile's numbers -*- scheme -*-
-;;;; Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
+;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006 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
;;;;
;;;; 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)
(set! x (* 0.5 x))
(set! n (1+ n)))
x)
-
+
+;; `quotient' but rounded towards -infinity, like `modulo' or `ash' do
+;; note only positive D supported (that's all that's currently required)
+(define-public (quotient-floor n d)
+ (if (negative? n)
+ (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)))
+
+(define const-e 2.7182818284590452354)
+(define const-e^2 7.3890560989306502274)
+(define const-1/e 0.3678794411714423215)
+
+
+;;;
+;;; 1+
+;;;
+
+(with-test-prefix "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))))
+
+;;;
+;;; 1-
+;;;
+
+(with-test-prefix "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))))
+
+;;;
+;;; ash
+;;;
+
+(with-test-prefix "ash"
+
+ (pass-if "documented?"
+ (documented? ash))
+
+ (pass-if (eqv? 0 (ash 0 0)))
+ (pass-if (eqv? 0 (ash 0 1)))
+ (pass-if (eqv? 0 (ash 0 1000)))
+ (pass-if (eqv? 0 (ash 0 -1)))
+ (pass-if (eqv? 0 (ash 0 -1000)))
+
+ (pass-if (eqv? 1 (ash 1 0)))
+ (pass-if (eqv? 2 (ash 1 1)))
+ (pass-if (eqv? 340282366920938463463374607431768211456 (ash 1 128)))
+ (pass-if (eqv? 0 (ash 1 -1)))
+ (pass-if (eqv? 0 (ash 1 -1000)))
+
+ (pass-if (eqv? -1 (ash -1 0)))
+ (pass-if (eqv? -2 (ash -1 1)))
+ (pass-if (eqv? -340282366920938463463374607431768211456 (ash -1 128)))
+ (pass-if (eqv? -1 (ash -1 -1)))
+ (pass-if (eqv? -1 (ash -1 -1000)))
+
+ (pass-if (eqv? -3 (ash -3 0)))
+ (pass-if (eqv? -6 (ash -3 1)))
+ (pass-if (eqv? -1020847100762815390390123822295304634368 (ash -3 128)))
+ (pass-if (eqv? -2 (ash -3 -1)))
+ (pass-if (eqv? -1 (ash -3 -1000)))
+
+ (pass-if (eqv? -6 (ash -23 -2)))
+
+ (pass-if (eqv? most-positive-fixnum (ash most-positive-fixnum 0)))
+ (pass-if (eqv? (* 2 most-positive-fixnum) (ash most-positive-fixnum 1)))
+ (pass-if (eqv? (* 4 most-positive-fixnum) (ash most-positive-fixnum 2)))
+ (pass-if
+ (eqv? (* most-positive-fixnum 340282366920938463463374607431768211456)
+ (ash most-positive-fixnum 128)))
+ (pass-if (eqv? (quotient most-positive-fixnum 2)
+ (ash most-positive-fixnum -1)))
+ (pass-if (eqv? 0 (ash most-positive-fixnum -1000)))
+
+ (let ((mpf4 (quotient most-positive-fixnum 4)))
+ (pass-if (eqv? (* 2 mpf4) (ash mpf4 1)))
+ (pass-if (eqv? (* 4 mpf4) (ash mpf4 2)))
+ (pass-if (eqv? (* 8 mpf4) (ash mpf4 3))))
+
+ (pass-if (eqv? most-negative-fixnum (ash most-negative-fixnum 0)))
+ (pass-if (eqv? (* 2 most-negative-fixnum) (ash most-negative-fixnum 1)))
+ (pass-if (eqv? (* 4 most-negative-fixnum) (ash most-negative-fixnum 2)))
+ (pass-if
+ (eqv? (* most-negative-fixnum 340282366920938463463374607431768211456)
+ (ash most-negative-fixnum 128)))
+ (pass-if (eqv? (quotient-floor most-negative-fixnum 2)
+ (ash most-negative-fixnum -1)))
+ (pass-if (eqv? -1 (ash most-negative-fixnum -1000)))
+
+ (let ((mnf4 (quotient-floor most-negative-fixnum 4)))
+ (pass-if (eqv? (* 2 mnf4) (ash mnf4 1)))
+ (pass-if (eqv? (* 4 mnf4) (ash mnf4 2)))
+ (pass-if (eqv? (* 8 mnf4) (ash mnf4 3)))))
;;;
;;; exact?
(pass-if "sqrt ((fixnum-max+1)^2 - 1)"
(eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1)))))))
+;;;
+;;; exp
+;;;
+
+(with-test-prefix "exp"
+ (pass-if "documented?"
+ (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-exception
"Proper exception with 0 modulus"
- (cons 'numerical-overflow "")
+ exception:numerical-overflow
(modulo-expt 17 23 0))
(pass-if-exception
"Proper exception when result not invertible"
- (cons 'numerical-overflow "")
+ exception:numerical-overflow
(modulo-expt 10 -1 48))
(pass-if-exception
"Proper exception with wrong type argument"
- (cons 'wrong-type-arg "")
+ exception:wrong-type-arg
(modulo-expt "Sam" 23 10))
(pass-if-exception
"Proper exception with wrong type argument"
- (cons 'wrong-type-arg "")
+ exception:wrong-type-arg
(modulo-expt 17 9.9 10))
(pass-if-exception
"Proper exception with wrong type argument"
- (cons 'wrong-type-arg "")
+ 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
;;;
(pass-if (= (+ fixnum-max 1) (num->str->num (+ fixnum-max 1) 10)))
(pass-if (= (- fixnum-min 1) (num->str->num (- fixnum-min 1) 10)))
(pass-if (= (inf) (num->str->num (inf) 10)))
- (pass-if (= 1.3 (num->str->num 1.3 10)))))
+ (pass-if (= 1.3 (num->str->num 1.3 10)))
+
+ ;; XXX - some results depend on whether Guile is compiled optimzed
+ ;; 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 (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 (/ 1 3) 3) "1/10"))
+
+ ;; 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
(with-test-prefix "string->number"
- (pass-if "string->number"
+ (pass-if "documented?"
(documented? string->number))
(pass-if "non number strings"
(pass-if-exception "exponent too big"
exception:out-of-range
- (string->number "12.13e141414")))
+ (string->number "12.13e141414"))
+
+ ;; in guile 1.6.7 and earlier, bad polar forms (where the conversion of
+ ;; the angle gave #f) caused a segv
+ (pass-if "1@a"
+ (eq? #f (string->number "1@a"))))
;;;
;;; number?
(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? +nan.0)))
(pass-if (not (integer? 3+4i)))
(pass-if (not (integer? #\a)))
(pass-if (not (integer? "a")))
(pass-if (= 0.5+0i 1/2))
(pass-if (not (= 0.5+0i 2/3)))
- (pass-if (not (= 0+0.5i 1/2))))
+ (pass-if (not (= 0+0.5i 1/2)))
+
+ ;; prior to guile 1.8, inum/flonum comparisons were done just by
+ ;; converting the inum to a double, which on a 64-bit would round making
+ ;; say inexact 2^58 appear equal to exact 2^58+1
+ (pass-if (= (ash-flo 1.0 58) (ash 1 58)))
+ (pass-if (not (= (ash-flo 1.0 58) (1+ (ash 1 58)))))
+ (pass-if (not (= (ash-flo 1.0 58) (1- (ash 1 58)))))
+ (pass-if (= (ash 1 58) (ash-flo 1.0 58)))
+ (pass-if (not (= (1+ (ash 1 58)) (ash-flo 1.0 58))))
+ (pass-if (not (= (1- (ash 1 58)) (ash-flo 1.0 58)))))
;;;
;;; <
(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)))
(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 "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 "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
(pass-if (= 0 (round 0.0)))
(pass-if (= 0 (round -0.5)))
(pass-if (= -1 (round -1.25)))
- (pass-if (= -2 (round -1.5))))
+ (pass-if (= -2 (round -1.5)))
+
+ (with-test-prefix "inum"
+ (pass-if "0"
+ (and (= 0 (round 0))
+ (exact? (round 0))))
+
+ (pass-if "1"
+ (and (= 1 (round 1))
+ (exact? (round 1))))
+
+ (pass-if "-1"
+ (and (= -1 (round -1))
+ (exact? (round -1)))))
+
+ (with-test-prefix "bignum"
+ (let ((x (1+ most-positive-fixnum)))
+ (pass-if "(1+ most-positive-fixnum)"
+ (and (= x (round x))
+ (exact? (round x)))))
+
+ (let ((x (1- most-negative-fixnum)))
+ (pass-if "(1- most-negative-fixnum)"
+ (and (= x (round x))
+ (exact? (round x))))))
+
+ (with-test-prefix "frac"
+ (define (=exact x y)
+ (and (= x y)
+ (exact? y)))
+
+ (pass-if (=exact -2 (round -7/3)))
+ (pass-if (=exact -2 (round -5/3)))
+ (pass-if (=exact -1 (round -4/3)))
+ (pass-if (=exact -1 (round -2/3)))
+ (pass-if (=exact 0 (round -1/3)))
+ (pass-if (=exact 0 (round 1/3)))
+ (pass-if (=exact 1 (round 2/3)))
+ (pass-if (=exact 1 (round 4/3)))
+ (pass-if (=exact 2 (round 5/3)))
+ (pass-if (=exact 2 (round 7/3)))
+
+ (pass-if (=exact -3 (round -17/6)))
+ (pass-if (=exact -3 (round -16/6)))
+ (pass-if (=exact -2 (round -15/6)))
+ (pass-if (=exact -2 (round -14/6)))
+ (pass-if (=exact -2 (round -13/6)))
+ (pass-if (=exact -2 (round -11/6)))
+ (pass-if (=exact -2 (round -10/6)))
+ (pass-if (=exact -2 (round -9/6)))
+ (pass-if (=exact -1 (round -8/6)))
+ (pass-if (=exact -1 (round -7/6)))
+ (pass-if (=exact -1 (round -5/6)))
+ (pass-if (=exact -1 (round -4/6)))
+ (pass-if (=exact 0 (round -3/6)))
+ (pass-if (=exact 0 (round -2/6)))
+ (pass-if (=exact 0 (round -1/6)))
+ (pass-if (=exact 0 (round 1/6)))
+ (pass-if (=exact 0 (round 2/6)))
+ (pass-if (=exact 0 (round 3/6)))
+ (pass-if (=exact 1 (round 4/6)))
+ (pass-if (=exact 1 (round 5/6)))
+ (pass-if (=exact 1 (round 7/6)))
+ (pass-if (=exact 1 (round 8/6)))
+ (pass-if (=exact 2 (round 9/6)))
+ (pass-if (=exact 2 (round 10/6)))
+ (pass-if (=exact 2 (round 11/6)))
+ (pass-if (=exact 2 (round 13/6)))
+ (pass-if (=exact 2 (round 14/6)))
+ (pass-if (=exact 2 (round 15/6)))
+ (pass-if (=exact 3 (round 16/6)))
+ (pass-if (=exact 3 (round 17/6))))
+
+ (with-test-prefix "real"
+ (pass-if "0.0"
+ (and (= 0.0 (round 0.0))
+ (inexact? (round 0.0))))
+
+ (pass-if "1.0"
+ (and (= 1.0 (round 1.0))
+ (inexact? (round 1.0))))
+
+ (pass-if "-1.0"
+ (and (= -1.0 (round -1.0))
+ (inexact? (round -1.0))))
+
+ (pass-if "-3.1"
+ (and (= -3.0 (round -3.1))
+ (inexact? (round -3.1))))
+
+ (pass-if "3.1"
+ (and (= 3.0 (round 3.1))
+ (inexact? (round 3.1))))
+
+ (pass-if "3.9"
+ (and (= 4.0 (round 3.9))
+ (inexact? (round 3.9))))
+
+ (pass-if "-3.9"
+ (and (= -4.0 (round -3.9))
+ (inexact? (round -3.9))))
+
+ (pass-if "1.5"
+ (and (= 2.0 (round 1.5))
+ (inexact? (round 1.5))))
+
+ (pass-if "2.5"
+ (and (= 2.0 (round 2.5))
+ (inexact? (round 2.5))))
+
+ (pass-if "3.5"
+ (and (= 4.0 (round 3.5))
+ (inexact? (round 3.5))))
+
+ (pass-if "-1.5"
+ (and (= -2.0 (round -1.5))
+ (inexact? (round -1.5))))
+
+ (pass-if "-2.5"
+ (and (= -2.0 (round -2.5))
+ (inexact? (round -2.5))))
+
+ (pass-if "-3.5"
+ (and (= -4.0 (round -3.5))
+ (inexact? (round -3.5))))
+
+ ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a
+ ;; float with mantissa all ones) came out as 2^53 from `round' (except
+ ;; on i386 and m68k systems using the coprocessor and optimizing, where
+ ;; extra precision hid the problem)
+ (pass-if "2^53-1"
+ (let ((x (exact->inexact (1- (ash 1 53)))))
+ (and (= x (round x))
+ (inexact? (round x)))))
+ (pass-if "-(2^53-1)"
+ (let ((x (exact->inexact (- (1- (ash 1 53))))))
+ (and (= x (round x))
+ (inexact? (round x)))))))
;;;
;;; exact->inexact
(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
(pass-if (list i n)
(= n (inexact->exact (exact->inexact n)))))))
+;;;
+;;; integer-expt
+;;;
+
+(with-test-prefix "integer-expt"
+
+ (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-length
;;;
(pass-if n
(= i (integer-length n))))))
+;;;
+;;; log
+;;;
+
+(with-test-prefix "log"
+ (pass-if "documented?"
+ (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?"
+ (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 (eq? #f (logbit? 0 0)))
+ (pass-if (eq? #f (logbit? 1 0)))
+ (pass-if (eq? #f (logbit? 31 0)))
+ (pass-if (eq? #f (logbit? 32 0)))
+ (pass-if (eq? #f (logbit? 33 0)))
+ (pass-if (eq? #f (logbit? 63 0)))
+ (pass-if (eq? #f (logbit? 64 0)))
+ (pass-if (eq? #f (logbit? 65 0)))
+
+ ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap
+ ;; around and return #t where it ought to be #f
+ (pass-if (eq? #t (logbit? 0 1)))
+ (pass-if (eq? #f (logbit? 1 1)))
+ (pass-if (eq? #f (logbit? 31 1)))
+ (pass-if (eq? #f (logbit? 32 1)))
+ (pass-if (eq? #f (logbit? 33 1)))
+ (pass-if (eq? #f (logbit? 63 1)))
+ (pass-if (eq? #f (logbit? 64 1)))
+ (pass-if (eq? #f (logbit? 65 1)))
+ (pass-if (eq? #f (logbit? 128 1)))
+
+ (pass-if (eq? #t (logbit? 0 -1)))
+ (pass-if (eq? #t (logbit? 1 -1)))
+ (pass-if (eq? #t (logbit? 31 -1)))
+ (pass-if (eq? #t (logbit? 32 -1)))
+ (pass-if (eq? #t (logbit? 33 -1)))
+ (pass-if (eq? #t (logbit? 63 -1)))
+ (pass-if (eq? #t (logbit? 64 -1)))
+ (pass-if (eq? #t (logbit? 65 -1))))
+
;;;
;;; logcount
;;;
(pass-if n
(= i (logcount n))))))
+;;;
+;;; logior
+;;;
+
+(with-test-prefix "logior"
+ (pass-if (eqv? -1 (logior (ash -1 1) 1)))
+
+ ;; check that bignum or bignum+inum args will reduce to an inum
+ (let ()
+ (define (test x y)
+ (pass-if (list x y '=> -1)
+ (eqv? -1 (logior x y)))
+ (pass-if (list y x '=> -1)
+ (eqv? -1 (logior y x))))
+ (test (ash -1 8) #xFF)
+ (test (ash -1 28) #x0FFFFFFF)
+ (test (ash -1 29) #x1FFFFFFF)
+ (test (ash -1 30) #x3FFFFFFF)
+ (test (ash -1 31) #x7FFFFFFF)
+ (test (ash -1 32) #xFFFFFFFF)
+ (test (ash -1 33) #x1FFFFFFFF)
+ (test (ash -1 60) #x0FFFFFFFFFFFFFFF)
+ (test (ash -1 61) #x1FFFFFFFFFFFFFFF)
+ (test (ash -1 62) #x3FFFFFFFFFFFFFFF)
+ (test (ash -1 63) #x7FFFFFFFFFFFFFFF)
+ (test (ash -1 64) #xFFFFFFFFFFFFFFFF)
+ (test (ash -1 65) #x1FFFFFFFFFFFFFFFF)
+ (test (ash -1 128) #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
+
;;;
;;; lognot
;;;
(lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
(pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
(lognot #x-100000000000000000000000000000000))))
+
+;;;
+;;; sqrt
+;;;
+
+(with-test-prefix "sqrt"
+ (pass-if "documented?"
+ (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))))
+
+
+;;
+;; 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))))
+