[PATCH 2/2] Make Prelude.signum handle -0.0 correctly (#7858)

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


Make the `Float` and `Double` implementations of `signum` handle -0.0
correctly per IEEE-754.
---
 libraries/base/GHC/Float.lhs | 16 ++++++++++------
 libraries/base/changelog.md  |  2 +-
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/libraries/base/GHC/Float.lhs b/libraries/base/GHC/Float.lhs
index 442e13c..294ebd5 100644
--- a/libraries/base/GHC/Float.lhs
+++ b/libraries/base/GHC/Float.lhs
@@ -209,9 +209,11 @@ instance  Num Float  where
         | isNegativeZero x = 0
         | x >= 0.0         = x
         | otherwise        = negateFloat x
-    signum x | x == 0.0  = 0
-             | x > 0.0   = 1
-             | otherwise = negate 1
+    signum x
+        | x == 0.0  = x
+        | isNaN x   = x
+        | x > 0.0   = 1
+        | otherwise = negate 1
 
     {-# INLINE fromInteger #-}
     fromInteger i = F# (floatFromInteger i)
@@ -376,9 +378,11 @@ instance  Num Double  where
         | isNegativeZero x = 0
         | x >= 0.0         = x
         | otherwise        = negateDouble x
-    signum x | x == 0.0  = 0
-             | x > 0.0   = 1
-             | otherwise = negate 1
+    signum x
+        | x == 0.0  = x
+        | isNaN x   = x
+        | x > 0.0   = 1
+        | otherwise = negate 1
 
     {-# INLINE fromInteger #-}
     fromInteger i = D# (doubleFromInteger i)
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 9eb279e..bc52660 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -10,7 +10,7 @@
 
   * Weaken RealFloat constraints on some `Data.Complex` functions
 
-  * Make `abs` handle -0.0 correctly for `Float` and `Double`.
+  * Make `abs` and `signum` handle -0.0 correctly for `Float` and `Double`.
 
 ## 4.7.0.0  *Apr 2014*
 
-- 
1.8.3.2



More information about the ghc-devs mailing list