From 2588094087f613bf5937b2ce99b57fa939098f76 Mon Sep 17 00:00:00 2001 From: Neil Toronto Date: Tue, 5 Jun 2012 22:40:44 -0600 Subject: [PATCH] Added nan?, infinite?, degrees->radians, radians->degrees, exact-round, exact-floor, exact-ceiling, exact-truncate to racket/math Altered TR's random arithmetic testing to generate single-flonums and very small flonums; fails now because of erroneous types Fixes to sgn, sinh, cosh, and tanh: * preserve single-flonum-ness * correct zero sign (-0.0) for negative return values that are smaller than epsilon * correct behavior with NaN and infinite inputs original commit: a713ca8a8b6c7aed987e80d0621484e68bc3c6f5 --- .../tests/typed-racket/tr-random-testing.rkt | 29 +++++++++++++++++-- .../base-env/base-env-numeric.rkt | 3 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/collects/tests/typed-racket/tr-random-testing.rkt b/collects/tests/typed-racket/tr-random-testing.rkt index cd0a4431..f124ab78 100644 --- a/collects/tests/typed-racket/tr-random-testing.rkt +++ b/collects/tests/typed-racket/tr-random-testing.rkt @@ -3,7 +3,7 @@ ;; Random testing of type preservation for floats. (require redex - racket/flonum racket/unsafe/ops + racket/flonum racket/unsafe/ops unstable/flonum racket/sandbox) (require (except-in typed-racket/utils/utils infer) @@ -18,6 +18,8 @@ (b:init) (n:init) (define-namespace-anchor anch) +todo: exact numbers + (define-language tr-arith ; to stay within floats, removed some numeric ops [n real] [E n @@ -99,10 +101,33 @@ (define subtype? (subtype type-after type-before)) subtype?) +(define (random-integer->random-float E) + (define r (random)) + (cond + ;; probability 1/4: noisify and convert to single flonum + [(r . < . 0.25) + (real->single-flonum (* (random) E))] + ;; probability 1/4: noisify and convert to double flonum + [(r . < . 0.5) + (real->double-flonum (* (random) E))] + ;; probability 1/4: convert to very small double flonum + [(r . < . 0.75) + (define x (ordinal->flonum E)) + (cond [(= x 0.0) (if ((random) . < . 0.5) 0.0 -0.0)] + [else x])] + ;; probability 1/20: +nan.0 + [(r . < . 0.8) + +nan.0] + ;; remaining probability: convert to very large double flonum + [else + (if ((random) . < . 0.5) + (flstep -inf.0 E) + (flstep +inf.0 (- E)))])) + ;; Redex can't generate floats, so we convert ints to floats. (define (exp->float-exp E) ; numbers or symbols or lists (cond [(number? E) - (exact->inexact (* (random) E))] ; add noise + (random-integer->random-float E)] [(list? E) (map exp->float-exp E)] [else diff --git a/collects/typed-racket/base-env/base-env-numeric.rkt b/collects/typed-racket/base-env/base-env-numeric.rkt index 2ad97857..8ebffb83 100644 --- a/collects/typed-racket/base-env/base-env-numeric.rkt +++ b/collects/typed-racket/base-env/base-env-numeric.rkt @@ -1808,7 +1808,8 @@ (-NegRat . -> . -NegFixnum) ; -1 (-NonPosRat . -> . -NonPosFixnum) ; 0 or -1 (-Rat . -> . -Fixnum) - (-InexactReal . -> . -Flonum) ; single-flonums give a flonum result + (-Flonum . -> . -Flonum) + (-SingleFlonum . -> . -SingleFlonum) (-Real . -> . -Real))] [pi -PosFlonum]