valderman / haste-compiler

A GHC-based Haskell to JavaScript compiler
http://haste-lang.org
BSD 3-Clause "New" or "Revised" License
1.45k stars 115 forks source link

read enters an infinite loop for certain Doubles #392

Open valderman opened 7 years ago

valderman commented 7 years ago

As the topic says, certain valid floating point numbers (Double as well as Float) will cause read to loop indefinitely. One such number is 11111111111 (eleven ones), and another is 2222222222 (ten twos; eleven twos work as expected however). Integral types don't seem to be affected, and reading the numbers to an Integer and then converting them to a Double using fromInteger does not trigger the bug.

The following code exhibits the bug:

import Haste

main = do
  let s = "11111111111"
      d = read s :: Double
  alert $ toJSString d
blynn commented 7 years ago

A related issue: trying to parse a Double >= 2^32 seems to wrap around, e.g. "9999999999" (ten 9s) gives 1410065407.0, which is (10^10 - 1) mod 2^32.

import Haste main = writeLog $ show $ (read $ show $ 10^10 - 1 :: Double)

In contrast, ghc prints "9.999999999e9" for the following program:

main = print (read $ show $ 10^10 - 1 :: Double)