> Date: Mon, 12 Jun 2000 15:52:20 +0200
> From: Sven Panne <[EMAIL PROTECTED]>

> Is the following behaviour of GHC allowed by the H98 report?
> 
> ---------- Foo.hs --------------------------------
> main :: IO ()
> main = print (0.1234567891234567891 :: Float,
>               0.1234567891234567891 :: Double)
> --------------------------------------------------
> panne@jeanluc:~ > ghc Foo.hs && ./a.out
> (0.12345679,0.12345678912345678)
> --------------------------------------------------
> 
> Note that the Float gets rounded, but the Double is truncated.

The Double is rounded too --- remember that what you're seeing is the
decimal representation of a binary floating-point value. With a few
more decimal digits shown, the closest double to 0.1234567891234567891
is 0.1234567891234567838 and the next higher is 0.1234567891234567977.

BTW, eight significant decimal digits aren't quite enough for Float.
The local ghc is broken right now so I can't check, but I think this
will print (0.12345678,0.12345678,False):

> x :: Float
> x = 0.1234567835
> y :: Float
> y = 0.1234567761
> main = print (x, y, x == y)

Hugs 1.4 prints (0.123457, 0.123457, False), but at least it is
obvious that those are rounded values. I'm not saying that ghc is
wrong; but when the decimal precision is _almost_ good enough to
distinguish all binary values, you can potentially get badly surprised
by the cases where it happens not to be.

Lars Mathiesen (U of Copenhagen CS Dep) <[EMAIL PROTECTED]> (Humour NOT marked)

Reply via email to