summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Berntsen <alexander@plaimi.net>2014-08-18 21:42:12 -0500
committerAustin Seipp <austin@well-typed.com>2014-08-18 23:26:19 -0500
commit6f6ee6eaa348b1a4815190c4d526d5c81c264fa7 (patch)
tree607f3b4db3f206397193fd518f4b889482705d44
parent88b1f99d473e5588378699abeb203c39ea554334 (diff)
downloadhaskell-6f6ee6eaa348b1a4815190c4d526d5c81c264fa7.tar.gz
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@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
-rw-r--r--libraries/base/GHC/Float.lhs10
-rw-r--r--libraries/base/changelog.md2
2 files changed, 8 insertions, 4 deletions
diff --git a/libraries/base/GHC/Float.lhs b/libraries/base/GHC/Float.lhs
index e0c4f4ae95..52fc9a99ee 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 06c9fa5a97..251ca88cbf 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