From be1ec6742c6ed6f2c1cc550f87e009be3ab45f78 Mon Sep 17 00:00:00 2001 From: simonmar Date: Wed, 29 Aug 2001 14:32:49 +0000 Subject: [PATCH] [project @ 2001-08-29 14:32:49 by simonmar] Fix *two* bugs in formatRealFloat: The first one is in the Haskell 98 errata, namely that negative exponents cause an infinite loop in the FFFixed case when no precision is specified. eg. `Numeric.showFFloat Nothing 0.02 ""' I've modified the code in the errata to properly handle the e == 0 case and to be slightly more efficient when e > 0. The second bug is this: Prelude> Numeric.showFFloat (Just 0) 0.02 "" "0." --- ghc/lib/std/PrelFloat.lhs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ghc/lib/std/PrelFloat.lhs b/ghc/lib/std/PrelFloat.lhs index 67eb2a7..ef065a4 100644 --- a/ghc/lib/std/PrelFloat.lhs +++ b/ghc/lib/std/PrelFloat.lhs @@ -1,5 +1,5 @@ % ------------------------------------------------------------------------------ -% $Id: PrelFloat.lhs,v 1.11 2001/02/28 00:01:03 qrczak Exp $ +% $Id: PrelFloat.lhs,v 1.12 2001/08/29 14:32:49 simonmar Exp $ % % (c) The University of Glasgow, 1994-2000 % @@ -496,13 +496,15 @@ formatRealFloat fmt decs x mk0 ls = case ls of { "" -> "0" ; _ -> ls} in case decs of - Nothing -> - let - f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs - f n s "" = f (n-1) ('0':s) "" - f n s (r:rs) = f (n-1) (r:s) rs - in - f e "" ds + Nothing + | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds + | otherwise -> + let + f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs + f n s "" = f (n-1) ('0':s) "" + f n s (r:rs) = f (n-1) (r:s) rs + in + f e "" ds Just dec -> let dec' = max dec 0 in if e >= 0 then @@ -516,8 +518,8 @@ formatRealFloat fmt decs x (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is) d:ds' = map intToDigit (if ei > 0 then is' else 0:is') in - d : '.' : ds' - + d : (if null ds' then "" else '.':ds') + roundTo :: Int -> Int -> [Int] -> (Int,[Int]) roundTo base d is = -- 1.7.10.4