Skip to content

Incorrect optimizations involving negative zero

The program

main = print $ if negativeZero == 0.0 then 1 / negativeZero else 0.0
  where
    negativeZero = negate 0.0

outputs -Infinity when compiled with GHC 7.6.3 and Infinity when compiled with GHC 7.8.3 and the optimization level -O2. The relevant pieces of Core:

-- GHC 7.6.3
Main.main2 =
  case GHC.Prim.==## (GHC.Prim.negateDouble# 0.0) 0.0 of _ {
    GHC.Types.False -> Main.main3 (GHC.Types.[] @ GHC.Types.Char);
    GHC.Types.True ->
      case GHC.Prim./## 1.0 (GHC.Prim.negateDouble# 0.0)
      of wild2_aK6 { __DEFAULT ->
      GHC.Float.$w$sshowSignedFloat
        GHC.Float.$fShowDouble_$sshowFloat
        GHC.Show.shows26
        wild2_aK6
        (GHC.Types.[] @ GHC.Types.Char)
      }
  }
-- GHC 7.8.3
Main.main2 =
  case GHC.Prim.negateDouble# 0.0 of _ [Occ=Dead] {
    __DEFAULT -> Main.main3 (GHC.Types.[] @ GHC.Types.Char);
    0.0 ->
      case GHC.Prim./## 1.0 0.0 of wild2_a1NU { __DEFAULT ->
      GHC.Float.$w$sshowSignedFloat
        GHC.Float.$fShowDouble_$sshowFloat
        GHC.Show.shows27
        wild2_a1NU
        (GHC.Types.[] @ GHC.Types.Char)
      }
  }

I don't think this problem is operating system/architecture dependent, but for the sake of completeness I'm on a 32-bit Linux, and this issue was first discovered on 64-bit Windows running in a virtual machine.

Trac metadata
Trac field Value
Version 7.8.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information