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."
% ------------------------------------------------------------------------------
% ------------------------------------------------------------------------------
-% $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
%
%
% (c) The University of Glasgow, 1994-2000
%
mk0 ls = case ls of { "" -> "0" ; _ -> ls}
in
case decs of
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
Just dec ->
let dec' = max dec 0 in
if e >= 0 then
(ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
in
(ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
in
+ d : (if null ds' then "" else '.':ds')
+
roundTo :: Int -> Int -> [Int] -> (Int,[Int])
roundTo base d is =
roundTo :: Int -> Int -> [Int] -> (Int,[Int])
roundTo base d is =