[commit: ghc] master: Make Prelude.abs handle -0.0 correctly (#7858) (6f6ee6e)
git at git.haskell.org
git at git.haskell.org
Tue Aug 19 04:34:21 UTC 2014
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/6f6ee6eaa348b1a4815190c4d526d5c81c264fa7/ghc
>---------------------------------------------------------------
commit 6f6ee6eaa348b1a4815190c4d526d5c81c264fa7
Author: Alexander Berntsen <alexander at plaimi.net>
Date: Mon Aug 18 21:42:12 2014 -0500
Make Prelude.abs handle -0.0 correctly (#7858)
Summary:
Make the `Float` and `Double` implementations of `abs` handle -0.0
correctly per IEEE-754.
abs (-0.0::Float) and abs (-0.0::Double) previously returned -0.0, when
they should return 0.0. This patch fixes this.
Signed-off-by: Alexander Berntsen <alexander at plaimi.net>
Test Plan: abs (-0.0::Double) should = 0.0 instead of (-0.0)
Reviewers: ekmett, hvr, austin, rwbarton
Reviewed By: austin, rwbarton
Subscribers: phaskell, trofi, simonmar, relrod, ezyang, carter
Differential Revision: https://phabricator.haskell.org/D145
GHC Trac Issues: #7858
>---------------------------------------------------------------
6f6ee6eaa348b1a4815190c4d526d5c81c264fa7
libraries/base/GHC/Float.lhs | 10 ++++++----
libraries/base/changelog.md | 2 ++
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/libraries/base/GHC/Float.lhs b/libraries/base/GHC/Float.lhs
index e0c4f4a..52fc9a9 100644
--- a/libraries/base/GHC/Float.lhs
+++ b/libraries/base/GHC/Float.lhs
@@ -205,8 +205,9 @@ 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 | 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
@@ -370,8 +371,9 @@ 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 | 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
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 06c9fa5..251ca88 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -18,6 +18,8 @@
enabled, so that the `Monoid` instance for `Proxy` are polykinded
like `Proxy` itself is.
+ * Make `abs` handle (-0.0) correctly per IEEE-754.
+
## 4.7.0.1 *Jul 2014*
* Bundled with GHC 7.8.3
More information about the ghc-commits
mailing list