[commit: ghc] master: base: Fix handling of showEFloat (Just 0) (9039f84)

git at git.haskell.org git at git.haskell.org
Sun May 13 22:31:15 UTC 2018


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/9039f847a568ac69436d449b9fe090ecd03b9e06/ghc

>---------------------------------------------------------------

commit 9039f847a568ac69436d449b9fe090ecd03b9e06
Author: Ben Gamari <bgamari.foss at gmail.com>
Date:   Sun May 13 11:40:18 2018 -0400

    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


>---------------------------------------------------------------

9039f847a568ac69436d449b9fe090ecd03b9e06
 libraries/base/GHC/Float.hs                | 10 +++++++
 libraries/base/changelog.md                |  3 +++
 libraries/base/tests/Numeric/num008.stdout | 42 +++++++++++++++---------------
 3 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/libraries/base/GHC/Float.hs b/libraries/base/GHC/Float.hs
index d60c660..db4200d 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 d4b9efa..f239073 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 5086442..116998c 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]



More information about the ghc-commits mailing list