;;;; fp-utils-test -*- Scheme -*- ;;;; Kon Lovett, Sep '18 (import test) (test-begin "FP Utils") ;;; (import fp-inlines fp-utils) ;; (import (chicken base) (chicken flonum)) (test-group "FP Inlines" (test-assert (fpzero? 0.0)) (test-assert (not (fpzero? 1.0))) (test-assert (not (fpzero? maximum-flonum))) (test-assert (not (fpzero? minimum-flonum))) (test-assert (not (fppositive? 0.0))) (test-assert (not (fppositive? (fpneg minimum-flonum)))) (test-assert (fppositive? maximum-flonum)) (test-assert (fpnatural? 0.0)) (test-assert (not (fpnatural? (fpneg minimum-flonum)))) (test-assert (fpnatural? maximum-flonum)) (test-assert (not (fpnegative? 0.0))) (test-assert (fpnegative? (fpneg minimum-flonum))) (test-assert (not (fpnegative? maximum-flonum))) (test-assert (not (fpeven? 7.0))) (test-assert (fpeven? 6.0)) (test-assert (not (fpodd? 6.0))) (test-assert (fpodd? 7.0)) (test 4.0 (fpadd1 3.0)) (test 2.0 (fpsub1 3.0)) (test 9.0 (fpsqr 3.0)) (test 27.0 (fpcub 3.0)) (test 0.477121254719662 (fplog10 3.0)) (test 1.5 (fp% 3.0 50)) (test 4.5 (fpavg 3.0 6.0)) ) ;; (test-group "FP precision" (test 10.0 (fpprecision-factor 1)) (test 10000000000.0 (fpprecision-factor 10)) (test 15.8489319246111 (fpprecision-factor 1.2)) (test 0.1 (fpprecision-epsilon 1)) (test 0.0000000001 (fpprecision-epsilon 10)) (test 0.0630957344480193 (fpprecision-epsilon 1.2)) ) ;; (test-group "FP Approximate =" ;sweet, sweet repeating expansion (let ((x (fp+ 0.15 0.15)) (y (fp+ 0.1 0.2))) (test-assert ".15+.15 <> .1+.2" (not (fp= x y))) (test-assert ".15+.15 ~= .1+.2" (fp~= x y)) ) ;own epsilon (let ((eps5 (fpprecision-epsilon 5))) (test-assert (fp~= 0.123456 0.123457 eps5)) (test-assert (fp~<= 0.123456 0.123457 eps5)) (test-assert (fp~>= 0.123456 0.123457 eps5)) (test-assert (fp~<= 0.123456 0.12346 eps5)) (test-assert (fp~>= 0.123456 0.12344 eps5)) ) ) ;; (define-constant 4EPS (fp/ 9.0 1e05)) (test-group "FP Utils" (test 1.0 (fpmodulo 5.0 2.0)) (test 0.0 (fpmodulo 0.0 1.0)) (test 2.0 (fpquotient 5.0 2.0)) (test 1.0 (fpremainder 5.0 2.0)) (test-assert (flonum? (fprandom))) (test-assert (flonum? (fprandom 2456))) (parameterize ((current-test-epsilon 4EPS)) (test 5.6568 (fpdistance 1.0 1.0 5.0 5.0)) ) (receive (mx mn) (fpmax-and-min 1.0 -1.0 -16.0 13.0 2.0 16.0 7.0 -8.0) (test "fpmax-and-min max" 16.0 mx) (test "fpmax-and-min min" -16.0 mn) ) (test 1.0 (fpround/to 0.5555 0)) (test 0.6 (fpround/to 0.5555 1)) (test 0.56 (fpround/to 0.5555 2)) (test 0.556 (fpround/to 0.5555 3)) (test 0.5555 (fpround/to 0.5555 4)) (test 6765.0 (fpfib* 20.)) ) #; (do ((i 0 (add1 i)) ) ((>= i 100000) ) (let ((n (fprandom 0.05 0.002))) (print n) (assert (<= 0.002 n 0.05)) ) ) (import (srfi 4)) (test-group "sum/prod" (test 2.0 (fpsum (list 1. (fpexpt 10. 100.) 1. (fpneg (fpexpt 10. 100.))))) (test 2.0 (fpsum (vector 1. (fpexpt 10. 100.) 1. (fpneg (fpexpt 10. 100.))))) (test 2.0 (fpsum (f32vector 1. (fpexpt 10. 20.) 1. (fpneg (fpexpt 10. 20.))))) (test 2.0 (fpsum (f64vector 1. (fpexpt 10. 100.) 1. (fpneg (fpexpt 10. 100.))))) (test -1e+200 (fpprod (list 1. (fpexpt 10. 100.) 1. (fpneg (fpexpt 10. 100.))))) (test -1e+200 (fpprod (vector 1. (fpexpt 10. 100.) 1. (fpneg (fpexpt 10. 100.))))) (test -1e+40 (fpprod (f32vector 1. (fpexpt 10. 20.) 1. (fpneg (fpexpt 10. 20.))))) (test -1e+200 (fpprod (f64vector 1. (fpexpt 10. 100.) 1. (fpneg (fpexpt 10. 100.))))) ) (test-group "math-utils" (test 1938.84 (fpcompound-interest 0.043 4.0 6.0 1500.0)) ) ;;; (test-end "FP Utils") (test-exit)