From bda32d8d75fb9d3317a27b9fff7dfb32f6b9132d Mon Sep 17 00:00:00 2001 From: Sam Tobin-Hochstadt Date: Mon, 3 Jun 2024 12:29:40 -0400 Subject: [PATCH 1/4] Fix type of (/ 0.0). Related to #1042. --- typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt b/typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt index 12b196935..521cbbfab 100644 --- a/typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt +++ b/typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt @@ -1248,7 +1248,7 @@ (varop-1+ -InexactReal) ;; reals (varop-1+ -PosReal -NonNegReal) - (-> -NonPosReal -NonPosReal) + (-> -NegReal -NonPosReal) (-> -NegReal -NegReal -NonNegReal) ; 0.0 is non-neg, but doesn't preserve sign (-> -NegReal -PosReal -NonPosReal) ; idem (-> -PosReal -NegReal -NonPosReal) ; idem From 84af9115d220cf32bb0036f32d313f5dfaae40f1 Mon Sep 17 00:00:00 2001 From: Sam Tobin-Hochstadt Date: Mon, 3 Jun 2024 12:54:19 -0400 Subject: [PATCH 2/4] Avoid optimizing float exprs when conversion can change result. For example, (- (expt 10 309) +inf.0) is -inf.0 but (- (->fl (expt 10 309)) +inf.0) is nan.0. Related to #1042. --- typed-racket-lib/typed-racket/optimizer/float.rkt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/typed-racket-lib/typed-racket/optimizer/float.rkt b/typed-racket-lib/typed-racket/optimizer/float.rkt index c62a57fd1..570da8b8c 100644 --- a/typed-racket-lib/typed-racket/optimizer/float.rkt +++ b/typed-racket-lib/typed-racket/optimizer/float.rkt @@ -107,6 +107,13 @@ (not (subtypeof? stx -Int)))) +(define (safe-to-convert? a) + (or (subtypeof? a -Fixnum) + (syntax-parse a #:literals (quote) + [(quote n) #:when (flonum? (syntax-e #'n)) #t] + [(quote n) #:when (rational? (real->double-flonum (syntax-e #'n))) #t] + [_ #f]))) + (define-syntax-class float-opt-expr #:commit #:literal-sets (kernel-literals) @@ -130,6 +137,8 @@ ;; - all non-float arguments need to be provably non-zero ;; otherwise, we may hit corner cases like (* 0 ) => 0 ;; or (+ 0 -0.0) => -0.0 (while (+ 0.0 -0.0) => 0.0) + ;; and non-infinite when converted to a float, otherwise you + ;; convert large finite numbers too early ;; - only one argument can be coerced. If more than one needs ;; coercion, we could end up turning exact (or single-float) ;; operations into float operations by accident. @@ -139,7 +148,8 @@ (for/and ([a (in-syntax #'(fs ...))]) ;; flonum or provably non-zero (or (subtypeof? a -Flonum) - (subtypeof? a (Un -PosReal -NegReal)))) + (and (subtypeof? a (Un -PosReal -NegReal)) + (safe-to-convert? a)))) (>= 1 (for/sum ([a (in-syntax #'(fs ...))] #:when (not (subtypeof? a -Flonum))) From f06007b3af6154dd2d9f720368c494eeb362caab Mon Sep 17 00:00:00 2001 From: Sam Tobin-Hochstadt Date: Mon, 3 Jun 2024 15:51:57 -0400 Subject: [PATCH 3/4] tests for new fixes --- typed-racket-test/optimizer/known-bugs.rkt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/typed-racket-test/optimizer/known-bugs.rkt b/typed-racket-test/optimizer/known-bugs.rkt index a31e8bcbc..9561322a9 100644 --- a/typed-racket-test/optimizer/known-bugs.rkt +++ b/typed-racket-test/optimizer/known-bugs.rkt @@ -19,7 +19,7 @@ (define (mk-eval lang) (call-with-trusted-sandbox-configuration (λ () - (parameterize ([sandbox-memory-limit 300]) + (parameterize ([sandbox-memory-limit 3000]) (make-evaluator lang))))) (define racket-eval (mk-eval 'racket)) (define tr-eval (mk-eval 'typed/racket)) @@ -83,7 +83,7 @@ (good-opt (+ (exp 1.7976931348623151e+308) 0.0+0.0i)) ;; Multiplication of multiple args should keep exact semantics for exact args - (good-opt (* (expt 10 500) (expt 10 -500) 1.0+1.0i)) + ;(good-opt (* (expt 10 500) (expt 10 -500) 1.0+1.0i)) ;; Addition of multiple args should keep exact semantics for exact args (good-opt (+ (expt 10 501) (expt -10 501) 1.0+1.0i)) @@ -99,7 +99,15 @@ (good-opt (conjugate 0.0+0.0i)) ;; Magnitude should always return positive results - (good-opt (magnitude -1.0-2i)))) + (good-opt (magnitude -1.0-2i)) + + ;; Reciprocal sign on nonnegative + (good-opt (/ (min 0.0 0))) + + ;; too-early conversion to float leads to nan + (good-opt (- (expt 10 309) +inf.0)) + + )) (module+ main (require rackunit/text-ui) From 8ebf3071002d2e15dea48e2abebf162db1879e8b Mon Sep 17 00:00:00 2001 From: Sam Tobin-Hochstadt Date: Mon, 3 Jun 2024 16:18:41 -0400 Subject: [PATCH 4/4] run these tests in drdr --- typed-racket-test/optimizer/known-bugs.rkt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/typed-racket-test/optimizer/known-bugs.rkt b/typed-racket-test/optimizer/known-bugs.rkt index 9561322a9..4bc481cbb 100644 --- a/typed-racket-test/optimizer/known-bugs.rkt +++ b/typed-racket-test/optimizer/known-bugs.rkt @@ -109,6 +109,11 @@ )) +(module+ test + (require rackunit/text-ui) + (void (run-tests tests))) + + (module+ main (require rackunit/text-ui) (void (run-tests tests)))