[commit: ghc] master: Make Prelude.signum handle -0.0 correctly (#7858) (d9a2057)

git at git.haskell.org git at git.haskell.org
Tue Aug 19 04:34:19 UTC 2014


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/d9a20573f473cc7389004470999b8a318aa6b3f2/ghc

>---------------------------------------------------------------

commit d9a20573f473cc7389004470999b8a318aa6b3f2
Author: Alexander Berntsen <alexander at plaimi.net>
Date:   Mon Aug 18 21:43:33 2014 -0500

    Make Prelude.signum handle -0.0 correctly (#7858)
    
    Summary:
    Make the `Float` and `Double` implementations of `signum` handle -0.0
    correctly per IEEE-754.
    
    This, together with "Make Prelude.abs handle -0.0 correctly (#7858)",
    fixes Trac #7858.
    
    Depends on D145
    
    Signed-off-by: Alexander Berntsen <alexander at plaimi.net>
    
    Test Plan:
    signum of (-0.0) should be (-0.0) not 0.0.
    
    Test program:
    
      main =
        putStrLn $ p ++ " " ++ n
        where
          f = show . signum
          p = f (-0.0 :: Double)
        n = f (0.0 :: Double)
    
    Reviewers: ekmett, hvr, rwbarton, austin
    
    Reviewed By: austin
    
    Subscribers: phaskell, simonmar, relrod, ezyang, carter
    
    Differential Revision: https://phabricator.haskell.org/D148
    
    GHC Trac Issues: #7858


>---------------------------------------------------------------

d9a20573f473cc7389004470999b8a318aa6b3f2
 libraries/base/GHC/Float.lhs | 13 +++++++------
 libraries/base/changelog.md  |  2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/libraries/base/GHC/Float.lhs b/libraries/base/GHC/Float.lhs
index 52fc9a9..fcb9c16 100644
--- a/libraries/base/GHC/Float.lhs
+++ b/libraries/base/GHC/Float.lhs
@@ -208,9 +208,9 @@ instance  Num Float  where
     abs x    | x == 0    = 0 -- handles (-0.0)
              | x >  0    = x
              | otherwise = negateFloat x
-    signum x | x == 0.0  = 0
-             | x > 0.0   = 1
-             | otherwise = negate 1
+    signum x | x > 0     = 1
+             | x < 0     = negateFloat 1
+             | otherwise = x -- handles 0.0, (-0.0), and NaN
 
     {-# INLINE fromInteger #-}
     fromInteger i = F# (floatFromInteger i)
@@ -374,9 +374,10 @@ instance  Num Double  where
     abs x    | x == 0    = 0 -- handles (-0.0)
              | x >  0    = x
              | otherwise = negateDouble x
-    signum x | x == 0.0  = 0
-             | x > 0.0   = 1
-             | otherwise = negate 1
+    signum x | x > 0     = 1
+             | x < 0     = negateDouble 1
+             | otherwise = x -- handles 0.0, (-0.0), and NaN
+
 
     {-# INLINE fromInteger #-}
     fromInteger i = D# (doubleFromInteger i)
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 251ca88..28005f8 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -18,7 +18,7 @@
     enabled, so that the `Monoid` instance for `Proxy` are polykinded
     like `Proxy` itself is.
 
-  * Make `abs` handle (-0.0) correctly per IEEE-754.
+  * Make `abs` and `signum` handle (-0.0) correctly per IEEE-754.
 
 ## 4.7.0.1  *Jul 2014*
 



More information about the ghc-commits mailing list