[commit: ghc] master: Fixup c1035d51e to behave more like in GHC 7.6 (ec550e8)

git at git.haskell.org git at git.haskell.org
Tue Jun 24 21:31:14 UTC 2014


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

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

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

commit ec550e8f951e50fb91c89389e2e77a3358079c3a
Author: Herbert Valerio Riedel <hvr at gnu.org>
Date:   Tue Jun 24 22:58:41 2014 +0200

    Fixup c1035d51e to behave more like in GHC 7.6
    
    The fix in c1035d51e (addressing #9231) was done under the assumption
    that `Data.Fixed` is used only with power-of-10 `resolution`. This
    follow-up fixup changes the behavior to be more consistent with the
    previous behavior in GHC 7.6
    
    For instance, for the following `B7` resolution
    
       > data B7
       > instance HasResolution B7 where resolution _ = 128
    
    After this patch, the following behavior is now observable:
    
       > 1.070 :: Fixed B7
       1.062
       > 1.062 :: Fixed B7
       1.054
    
       > read "1.070" :: Fixed B7
       1.062
       > read "1.062" :: Fixed B7
       1.054
    
    This doesn't provide the desirable "read . show == id" property
    yet (which didn't hold in GHC 7.6 either), but at least `fromRational`
    and `read` are consistent.
    
    Signed-off-by: Herbert Valerio Riedel <hvr at gnu.org>


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

ec550e8f951e50fb91c89389e2e77a3358079c3a
 libraries/base/Data/Fixed.hs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/libraries/base/Data/Fixed.hs b/libraries/base/Data/Fixed.hs
index b3af208..cadbb61 100644
--- a/libraries/base/Data/Fixed.hs
+++ b/libraries/base/Data/Fixed.hs
@@ -159,9 +159,12 @@ instance (HasResolution a) => Read (Fixed a) where
 convertFixed :: forall a . HasResolution a => Lexeme -> ReadPrec (Fixed a)
 convertFixed (Number n)
  | Just (i, f) <- numberToFixed e n =
-    return (fromInteger i + (fromInteger f / fromInteger r))
-    where r = resolution (undefined :: Fixed a) -- = 10^e
-          e = round (logBase 10 (fromInteger r) :: Double)
+    return (fromInteger i + (fromInteger f / (10 ^ e)))
+    where r = resolution (undefined :: Fixed a)
+          -- round 'e' up to help make the 'read . show == id' property
+          -- possible also for cases where 'resolution' is not a
+          -- power-of-10, such as e.g. when 'resolution = 128'
+          e = ceiling (logBase 10 (fromInteger r) :: Double)
 convertFixed _ = pfail
 
 data E0 = E0



More information about the ghc-commits mailing list