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

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


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

diff --git a/libraries/base/GHC/Float.lhs b/libraries/base/GHC/Float.lhs
index e0c4f4a..442e13c 100644
--- a/libraries/base/GHC/Float.lhs
+++ b/libraries/base/GHC/Float.lhs
@@ -205,8 +205,10 @@ instance  Num Float  where
     (-)         x y     =  minusFloat x y
     negate      x       =  negateFloat x
     (*)         x y     =  timesFloat x y
-    abs x | x >= 0.0    =  x
-          | otherwise   =  negateFloat x
+    abs x
+        | isNegativeZero x = 0
+        | x >= 0.0         = x
+        | otherwise        = negateFloat x
     signum x | x == 0.0  = 0
              | x > 0.0   = 1
              | otherwise = negate 1
@@ -370,8 +372,10 @@ instance  Num Double  where
     (-)         x y     =  minusDouble x y
     negate      x       =  negateDouble x
     (*)         x y     =  timesDouble x y
-    abs x | x >= 0.0    =  x
-          | otherwise   =  negateDouble x
+    abs x
+        | isNegativeZero x = 0
+        | x >= 0.0         = x
+        | otherwise        = negateDouble x
     signum x | x == 0.0  = 0
              | x > 0.0   = 1
              | otherwise = negate 1
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index a72e4e6..9eb279e 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -10,6 +10,8 @@
 
   * Weaken RealFloat constraints on some `Data.Complex` functions
 
+  * Make `abs` handle -0.0 correctly for `Float` and `Double`.
+
 ## 4.7.0.0  *Apr 2014*
 
   * Bundled with GHC 7.8.1
-- 
1.8.3.2



More information about the ghc-devs mailing list