Floating point conversion bug

Andreas Gruenbacher gruenbacher-lists@geoinfo.tuwien.ac.at
Thu, 19 Apr 2001 11:56:27 +0200 (CEST)


Hello,

I recently ran into a problem using numbers that require Double precision
floating point. Hugs seems to always convert to single precision Float.

Here is an example:

> f :: Float
> d, d2 :: Double

> f = 3.141592654
> d = 3.141592654
> d2 = fromRational (3141592654 % 1000000000)

> main = do
>     putStrLn (show (round (f * 1000000000)))
>     putStrLn (show (round (d * 1000000000)))
>     putStrLn (show (round (d2 * 1000000000)))

Surprisingly, this gives:

3141592832
3141592832
3141592832

Here is a similar C program:

> int main(void)
> {
>         float f = 3.141592654;
>         double d = 3.141592654;
>
>         printf("%.9f\n", f);
>         printf("%.9f\n", d);
> }

3.141592741
3.141592654

Also, I would expect that at least the single precision case is identical
in both examples, but it's not. Weird...

Besides, how can I influence how floating point numbers are converted to
strings (precision, format, etc.)?


Thanks,
Andreas.

------------------------------------------------------------------------
 Andreas Gruenbacher                  gruenbacher@geoinfo.tuwien.ac.at
 Research Assistant                       Phone      +43(1)58801-12723
 Institute for Geoinformation             Fax        +43(1)58801-12799
 Technical University of Vienna           Cell phone   +43(664)4064789