bug in displaying fixed numbers
Lukasz Pankowski
lupan@zamek.gda.pl
25 Oct 2001 20:13:51 +0200
I was very surprised when I discovered (writing a bit more complicated
program) that a program like
> import Numeric ( floatToDigits )
> print $ showFFloat Nothing 0.01 ""
enters in to infinite loop. I found that (formatRealFloat FFFixed
Nothing) do not like displaying numbers less than 0.1.
For x = 0.01 we have e=-1 and ds="1". Function f loops down to 0,
which will never be reached.
I changed the function in hugs for my use. But since the problem is in
a standard library I will have to make a workaround for may code to
work elsewhere.
Original and modified parts below.
Original:
FFFixed ->
case decs of
Nothing ->
let f 0 s ds = mk0 s ++ "." ++ mk0 ds
f n s "" = f (n-1) (s++"0") ""
f n s (d:ds) = f (n-1) (s++[d]) ds
mk0 "" = "0"
mk0 s = s
in f e "" ds
Wish:
FFFixed ->
case decs of
Nothing ->
let f 0 s ds = mk0 s ++ "." ++ mk0 ds
f n s "" = f (n-1) (s++"0") ""
f n s (d:ds) = f (n-1) (s++[d]) ds
g n ds = "0." ++ (replicate n '0') ++ ds
mk0 "" = "0"
mk0 s = s
in if e >= 0
then f e "" ds
else g (-e) ds
I am rather new to Haskell so I do not know if the problem have been
discussed already.
--
=*= Lukasz Pankowski =*=