racket / typed-racket

Typed Racket
Other
516 stars 102 forks source link

fix inconsistencies between optimizer and racketcs #868

Open samth opened 4 years ago

samth commented 4 years ago

Currently, Typed Racket's optimizer changes some expressions to behave differently on RacketCS. In particular, these are expressions where Racket and RacketCS differ, and the optimizer causes TR to follow Racket, meaning that it changes behavior between typed and untyped.

There are a few possible solutions here:

  1. Change Racket and RacketCS to agree on these computations, and then change TR to match.
  2. Accept this divergence, since we're accepting that Racket and RacketCS diverge.
  3. Remove these optimizations (it's primarily float-complex division that's the issue).

I know @stamourv thinks 3 is the right answer. @mflatt is (1) likely? Does anyone else have an opinion on this?

not same result untyped: +nan.0+nan.0i typed: +nan.0+0.0i
(/ 2.3454025 (flmin (real->double-flonum 1.797693134862315e+308) (real->double-flonum -1.2848677e+32)) (make-rectangular +nan.0 0.0))

not same result untyped: 0.0-0.0i typed: 0.0+nan.0i
(/ (+ (exact-round 1.8655746e+35) (exact-round 1)) 2.0324421e-21 (make-rectangular 4 1.7976931348623157e+308))

not same result untyped: 0.0-0.0i typed: +nan.0-0.0i
(/ 1.7976931348623141e+308 (make-rectangular (min 1.5e-323 +inf.0 -1.7976931348623147e+308) -0.766481613292698) (unsafe-fl+ (sub1 (real->double-flonum 0.6712944)) (real->double-flonum 0.0)))

not same result untyped: -7.976850792053209e+307+6.995069365946365e-16i typed: -inf.0+6.995069365946365e-16i
(/ -1.797693134862314e+308 (flexp (real->double-flonum 0)) (make-rectangular (sqrt 5.0788827) (min 2e-323)))

not same result untyped: +nan.0+nan.0i typed: +inf.0+0.0i
(/ (make-polar 0.0 (max (real->double-flonum 2) (real->double-flonum 30.317604))))

not same result untyped: +nan.0+nan.0i typed: -inf.0+nan.0i
(/ -8.302019e-6 (make-polar 0.0 5.560084769914483) (abs (real->double-flonum 0)))

not same result untyped: +nan.0+nan.0i typed: +inf.0+0.0i
(/ (* 0.0 (make-rectangular -7 6e-323) -1.7976931348623155e+308))

not same result untyped: +nan.0+nan.0i typed: -inf.0+0.0i
(/ -5 2/7 (make-polar -0.0 (fltan (real->double-flonum -3.833043e+21))))

not same result untyped: -9.0651197858688e-54+9.080760665255492e+288i typed: -9.0651197858688e-54+inf.0i
(/ (min (real->double-flonum 3) (real->double-flonum -1.7976931348623157e+308) (real->double-flonum 3/2)) (make-rectangular 2e-323 1.9796724097581277e+19))

not same result untyped: +nan.0+nan.0i typed: +inf.0-0.0i
(/ (/ 1e-323 (make-rectangular 4 -1.7976931348623145e+308) (add1 (exact-round -0.0))))

not same result untyped: +nan.0+nan.0i typed: -inf.0-0.0i
(/ (make-polar -0.0 -0.0))

not same result untyped: +nan.0+nan.0i typed: +inf.0+0.0i
(/ (make-rectangular (floor 5e-324) -0.0))

not same result untyped: +nan.0+nan.0i typed: -inf.0-0.0i
(/ (make-polar (max (real->double-flonum 0.0) (real->double-flonum -2.400264341701695e+14)) (- (real->double-flonum 3) (real->double-flonum 1/2))))

not same result untyped: 0.0-0.0i typed: +nan.0-0.0i
(/ (sqr 3) (make-rectangular 1.7976931348623151e+308 (cos 1.7976931348623147e+308)))

not same result untyped: +nan.0+inf.0i typed: -0.0+inf.0i
(/ (sub1 0.8550388) (+ (+ 5.5293337e-15 1.6585703248349453) (gcd (exact-round 0) (exact-round 1)) (+ (real->double-flonum 2) (real->double-flonum -1/2))) (sin (make-rectangular 0.0 8.4e-323)))

not same result untyped: -0.0+0.0i typed: +nan.0+0.0i
(/ 2 (make-rectangular 1.7976931348623155e+308 1) (sub1 (real->double-flonum 0.034460585393126276)))

not same result untyped: 4.849907969653664+3.1e-322i typed: 4.849907969653664+3.16e-322i
(- (cos (min (exact-round -9/5) (exact-round 7))) (/ 3.9495411046506046 (flceiling (real->double-flonum 3)) (make-rectangular -1/4 1.5e-323)))

not same result untyped: +nan.0+nan.0i typed: -inf.0-0.0i
(/ (unsafe-flsqrt (real->double-flonum 7)) (make-polar (unsafe-fl* (real->double-flonum 0) (real->double-flonum 1.3238051e-30)) (ceiling (real->double-flonum 3))))

not same result untyped: +nan.0+nan.0i typed: +inf.0-0.0i
(/ (max (exact-round -3) (exact-round 1)) 2 (make-polar 0.0 (fltan (real->double-flonum 0))))
mflatt commented 4 years ago

It looks like there's more room for Racket and Racket CS to match. For example, I see no reason that they have to differ for (/ 0.0+0.0i).

mflatt commented 4 years ago

Two changes can bring all but one result in line for Racket and Racket CS:

After fixing those, the one difference that remains involves sin. Probably cos and tan would also differ. Probably it would be ideal for Racket to use the same algorithm as Chez Scheme for those functions, but maybe those functions are a good place to go with option 3 (if that's possible per-function).