Skip to content

realToFrac between Float and Double gives different results depending on compile flags

The following simple program gives different results depending on optimization level with GHC 8.6.4

moe, larry, curly :: Double
moe = 0/0
larry = 1/0
curly = -1/0

dancingLady = [moe,larry,curly]
helloPop = map realToFrac dancingLady :: [Float]
planeNuts = map realToFrac helloPop :: [Double]

main = do
  print dancingLady
  print helloPop
  print planeNuts

With -O1 and -O2 it prints the expected

[NaN,Infinity,-Infinity]
[NaN,Infinity,-Infinity]
[NaN,Infinity,-Infinity]

but with -O0 or when interpreted via ghci it prints

[NaN,Infinity,-Infinity]
[-Infinity,Infinity,-Infinity]
[-3.402823669209385e38,3.402823669209385e38,-3.402823669209385e38]

:-(

I hope you agree this is a serious bug. At the very least I would expect the conversion to be consistent, i.e. not a function of the optimization level.

Edited by SDNW
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information