Strange behaviour in my signum

Alexander Berntsen alexander at plaimi.net
Sat Apr 19 18:02:23 UTC 2014


I started preparing patches for #7858. My abs patch works well, my
signum patch not at all.

The gist of the bug is that abs and signum return erroneous values for
for the input -0.0. abs (-0.0 :: Float) should return 0.0, not -0.0. My
patch fixes this. For signum the situation is reverse. My patch does
*not* fix this.

It does not work using GHC nor GHCi (GHCi -ddump-simpl demonstrated
below). The code for my signum is correct, as may be demonstrated by
putting

signumFix x
    | x == 0.0  = x
    | isNaN x   = x
    | x > 0.0   = 1
    | otherwise = negate 1

in a file signumfix.hs and :l signumfix.hs into GHCi. signumFix (-0.0 ::
Float) should now return -0.0, where signum returns 0.0 with the same
argument.

Following this email come the patches. Please review the second patch.
If you can spot what I am doing wrong, I would be grateful.




Prelude> abs (-0.0 :: Float)

==================== Simplified expression ====================
let {
  it_aqt :: GHC.Types.Float
  [LclId,
   Str=DmdType,
   Unf=Unf{Src=<vanilla>, TopLvl=False, Arity=0, Value=False,
           ConLike=False, WorkFree=False, Expandable=False,
           Guidance=IF_ARGS [] 70 0}]
  it_aqt =
    GHC.Num.abs
      @ GHC.Types.Float
      GHC.Float.$fNumFloat
      (GHC.Num.negate
         @ GHC.Types.Float
         GHC.Float.$fNumFloat
         (GHC.Types.F# (__float 0.0))) } in
GHC.Base.thenIO
  @ ()
  @ [()]
  (System.IO.print @ GHC.Types.Float GHC.Float.$fShowFloat it_aqt)
  (GHC.Base.returnIO
     @ [()]
     (GHC.Types.:
        @ ()
        (it_aqt
         `cast` (UnivCo representational GHC.Types.Float ()
                 :: GHC.Types.Float ~# ()))
        (GHC.Types.[] @ ())))


0.0




Prelude> signum (-0.0 :: Float)

==================== Simplified expression ====================
let {
  it_aKQ :: GHC.Types.Float
  [LclId,
   Str=DmdType,
   Unf=Unf{Src=<vanilla>, TopLvl=False, Arity=0, Value=False,
           ConLike=False, WorkFree=False, Expandable=False,
           Guidance=IF_ARGS [] 70 0}]
  it_aKQ =
    GHC.Num.signum
      @ GHC.Types.Float
      GHC.Float.$fNumFloat
      (GHC.Num.negate
         @ GHC.Types.Float
         GHC.Float.$fNumFloat
         (GHC.Types.F# (__float 0.0))) } in
GHC.Base.thenIO
  @ ()
  @ [()]
  (System.IO.print @ GHC.Types.Float GHC.Float.$fShowFloat it_aKQ)
  (GHC.Base.returnIO
     @ [()]
     (GHC.Types.:
        @ ()
        (it_aKQ
         `cast` (UnivCo representational GHC.Types.Float ()
                 :: GHC.Types.Float ~# ()))
        (GHC.Types.[] @ ())))


0.0

Alexander Berntsen (2):
  Make Prelude.abs handle -0.0 correctly (#7858)
  Make Prelude.signum handle -0.0 correctly (#7858)

 libraries/base/GHC/Float.lhs | 28 ++++++++++++++++++----------
 libraries/base/changelog.md  |  2 ++
 2 files changed, 20 insertions(+), 10 deletions(-)

-- 
1.8.3.2



More information about the ghc-devs mailing list