Commit be1ec674 authored by simonmar's avatar simonmar
Browse files

[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."
parent c31a55d1
% ------------------------------------------------------------------------------ % ------------------------------------------------------------------------------
% $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
% %
...@@ -496,13 +496,15 @@ formatRealFloat fmt decs x ...@@ -496,13 +496,15 @@ formatRealFloat fmt decs x
mk0 ls = case ls of { "" -> "0" ; _ -> ls} mk0 ls = case ls of { "" -> "0" ; _ -> ls}
in in
case decs of case decs of
Nothing -> Nothing
let | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs | otherwise ->
f n s "" = f (n-1) ('0':s) "" let
f n s (r:rs) = f (n-1) (r:s) rs f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
in f n s "" = f (n-1) ('0':s) ""
f e "" ds f n s (r:rs) = f (n-1) (r:s) rs
in
f e "" ds
Just dec -> Just dec ->
let dec' = max dec 0 in let dec' = max dec 0 in
if e >= 0 then if e >= 0 then
...@@ -516,8 +518,8 @@ formatRealFloat fmt decs x ...@@ -516,8 +518,8 @@ formatRealFloat fmt decs x
(ei,is') = roundTo base dec' (replicate (-e) 0 ++ is) (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
d:ds' = map intToDigit (if ei > 0 then is' else 0:is') d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
in in
d : '.' : ds' d : (if null ds' then "" else '.':ds')
roundTo :: Int -> Int -> [Int] -> (Int,[Int]) roundTo :: Int -> Int -> [Int] -> (Int,[Int])
roundTo base d is = roundTo base d is =
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment