summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHerbert Valerio Riedel <hvr@gnu.org>2014-06-24 22:58:41 +0200
committerHerbert Valerio Riedel <hvr@gnu.org>2014-06-24 23:30:12 +0200
commitec550e8f951e50fb91c89389e2e77a3358079c3a (patch)
tree50295797463e2fa4bcb9ecef53ce652925b58344
parentd8abf85f8ca176854e9d5d0b12371c4bc402aac3 (diff)
downloadhaskell-ec550e8f951e50fb91c89389e2e77a3358079c3a.tar.gz
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@gnu.org>
-rw-r--r--libraries/base/Data/Fixed.hs9
1 files changed, 6 insertions, 3 deletions
diff --git a/libraries/base/Data/Fixed.hs b/libraries/base/Data/Fixed.hs
index b3af208469..cadbb61ac1 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