summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <bgamari.foss@gmail.com>2018-05-13 11:40:18 -0400
committerBen Gamari <ben@smart-cactus.org>2018-05-13 18:30:43 -0400
commit9039f847a568ac69436d449b9fe090ecd03b9e06 (patch)
tree91ad1495e541e7025c7a1501097c5e114d15153d
parent6ab7cf995dafcc9196e87bbde76b4f6937507592 (diff)
downloadhaskell-9039f847a568ac69436d449b9fe090ecd03b9e06.tar.gz
base: Fix handling of showEFloat (Just 0)
Previously `showEFloat (Just 0) pi ""` would produce `3.0e0`. Of course, this blatantly disrespects the user's request to print with zero digits of precision. Fix this. This is tested by base's `num008` testcase. Test Plan: Validate Reviewers: hvr Subscribers: rwbarton, thomie, carter GHC Trac Issues: #15115 Differential Revision: https://phabricator.haskell.org/D4665
-rw-r--r--libraries/base/GHC/Float.hs10
-rw-r--r--libraries/base/changelog.md3
-rw-r--r--libraries/base/tests/Numeric/num008.stdout42
3 files changed, 34 insertions, 21 deletions
diff --git a/libraries/base/GHC/Float.hs b/libraries/base/GHC/Float.hs
index d60c660bd0..db4200d874 100644
--- a/libraries/base/GHC/Float.hs
+++ b/libraries/base/GHC/Float.hs
@@ -690,6 +690,16 @@ formatRealFloatAlt fmt decs alt x
[d] -> d : ".0e" ++ show_e'
(d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
[] -> errorWithoutStackTrace "formatRealFloat/doFmt/FFExponent: []"
+ Just 0 ->
+ -- handle this case specifically since we need to omit the
+ -- decimal point as well (#15115)
+ case is of
+ [0] -> "0e0"
+ _ ->
+ let
+ (ei,is') = roundTo base 1 is
+ d:_ = map intToDigit (if ei > 0 then init is' else is')
+ in d : 'e' : show (e-1+ei)
Just dec ->
let dec' = max dec 1 in
case is of
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index d4b9efa2ea..f239073b60 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -17,6 +17,9 @@
* `asinh` for `Float` and `Double` is now numerical stable in the face of
large negative arguments (#14927)
+ * `Numeric.showEFloat (Just 0)` now respects the user's requested precision.
+ (#15115)
+
## 4.11.1.0 *TBA*
* Bundled with GHC 8.4.2
diff --git a/libraries/base/tests/Numeric/num008.stdout b/libraries/base/tests/Numeric/num008.stdout
index 5086442f0f..116998cc80 100644
--- a/libraries/base/tests/Numeric/num008.stdout
+++ b/libraries/base/tests/Numeric/num008.stdout
@@ -6,14 +6,14 @@
4.2000000e-2
1.8217369e0
1.8217369e-300
-0.0e0
-4.2e2
-4.2e1
-4.2e0
-4.2e-1
-4.2e-2
-1.8e0
-1.8e-300
+0e0
+4e2
+4e1
+4e0
+4e-1
+4e-2
+2e0
+2e-300
0.0e0
4.2e2
4.2e1
@@ -59,9 +59,9 @@
42
4
0
-4.2e-2
+4e-2
2
-1.8e-300
+2e-300
0.0
420.0
42.0
@@ -79,14 +79,14 @@
4.2000000e-2
1.8217369e0
0.0000000e0
-0.0e0
-4.2e2
-4.2e1
-4.2e0
-4.2e-1
-4.2e-2
-1.8e0
-0.0e0
+0e0
+4e2
+4e1
+4e0
+4e-1
+4e-2
+2e0
+0e0
0.0e0
4.2e2
4.2e1
@@ -132,7 +132,7 @@
42
4
0
-4.2e-2
+4e-2
2
0
0.0
@@ -144,5 +144,5 @@
1.8217369
0.0
-[0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,1.8217369e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8,1.8e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369128763983,1.821736912876398e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.0,0.0,0.0,2.0,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369128763983,1.821736912876398e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,1.8217369e-300,0.0,420.0,42.0,4.0,0.0,4.2e-2,2.0,1.8e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369128763983,1.821736912876398e-300]
-[0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.0,0.0,0.0,2.0,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.0,0.0,4.2e-2,2.0,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0]
+[0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,1.8217369e-300,0.0,400.0,40.0,4.0,0.4,4.0e-2,2.0,2.0e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369128763983,1.821736912876398e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.0,0.0,0.0,2.0,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369128763983,1.821736912876398e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,1.8217369e-300,0.0,420.0,42.0,4.0,0.0,4.0e-2,2.0,2.0e-300,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369128763983,1.821736912876398e-300]
+[0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,400.0,40.0,4.0,0.4,4.0e-2,2.0,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.0,0.0,0.0,2.0,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0,0.0,420.0,42.0,4.0,0.0,4.0e-2,2.0,0.0,0.0,420.0,42.0,4.2,0.42,4.2e-2,1.8217369,0.0]