diff --git a/typed-racket-lib/typed-racket/optimizer/float-complex.rkt b/typed-racket-lib/typed-racket/optimizer/float-complex.rkt index 68d84521..8a670e31 100644 --- a/typed-racket-lib/typed-racket/optimizer/float-complex.rkt +++ b/typed-racket-lib/typed-racket/optimizer/float-complex.rkt @@ -565,9 +565,11 @@ [(#%plain-app op:magnitude^ c:unboxed-float-complex-opt-expr) (log-unboxing-opt "unboxed unary float complex") #`(let*-values (c.bindings ...) - (unsafe-flsqrt - (unsafe-fl+ (unsafe-fl* c.real-binding c.real-binding) - (unsafe-fl* c.imag-binding c.imag-binding))))]))) + ;; reuses the algorithm used by the Racket runtime + (let-values ([(q) (unsafe-fl/ c.real-binding c.imag-binding)]) + (unsafe-fl* c.imag-binding + (unsafe-flsqrt (unsafe-fl+ 1.0 + (unsafe-fl* q q))))))]))) (pattern (#%plain-app op:float-complex-op e:expr ...) diff --git a/typed-racket-test/optimizer/known-bugs.rkt b/typed-racket-test/optimizer/known-bugs.rkt index d85c35cb..5919c734 100644 --- a/typed-racket-test/optimizer/known-bugs.rkt +++ b/typed-racket-test/optimizer/known-bugs.rkt @@ -85,7 +85,7 @@ (good-opt (+ (expt 10 501) (expt -10 501) 1.0+1.0i)) ;; Magnitude should not overflow unless necessary - (bad-opt (magnitude 3.0e300+4.0e300i)) + (good-opt (magnitude 3.0e300+4.0e300i)) ;; Negation should correctly compute sign of 0.0 (good-opt (- 0.0+0.0i))