Thanks for a fine report, Tim!

> I set USE_DOUBLE_PRECISION in options.h.in (manually, since I can't see
>   an appropriate configure switch).

We used to have a switch - but decided to hide it because
GreenCard couldn't cope with the change very well.


> I don't expect 
>     $ make check
> to pass now, but expect diffs to be confined to less significant digits
> in results. Instead I see many other errors.
> 
> Small example:
>     Prelude> 1.0 :: Ratio Int
> elicits
>     Program error: PreludeRatio.% zero denominator
> instead of
>     1 % 1

Yoiks!  I was mightily surprised by this report but, after a bit of 
chasing around, I decided that it's more or less the right answer.

The short explanation is that with Ints being 32 bits wide and
Float/Double being 64 bits wide, overflow and underflow are 
almost bound to happen when we convert between them.

In this case, what's happening is something like this:

  1.0
= "desugaring"
  fromDouble 1.0
= 
  let (m,n) = decodeFloat 1.0 in fromInteger m % (fromInteger 2 ^ (-n))
=
  let (m,n) = (4503599627370496,-52) in fromInteger m % (fromInteger 2 ^ (-n))
=
  fromInteger 4503599627370496 % (2 ^ 52)
=
  <4503599627370496 modulo 2^32>  %  <2^52 modulo 2^32>
=
  <4503599627370496 modulo 2^32>  %  0
=
  <division by zero>

Possible fixes:

1) Hugs really shouldn't be desugaring 1.0 as "fromDouble 1.0".
   It should be desugaring it as "fromRational (1 % 1)".

   With this fix, you'd have to work a lot harder before you got an
   example which failed as obviously as the above - but you'd still
   have problems.

2) We could rewrite doubleToRatio to avoid depending on the size of Int.
   The most obvious version is this:

     doubleToRatio :: Integral a => Double -> Ratio a
     doubleToRatio = fromRational . toRational

   This is probably a bit slower - but at least it gives the
   right answer.

3) Discourage the use of "Ratio Int"


[This seems like an argument in favour of Jon Fairbairn's proposal that
Standard Haskell should use Integer whenever possible.  Hmmm.]

>  What's the recommended way to search the archive when accessed via
>  web?

I don't think we ever thought of that.
We turned the hugs-bugs mail into a web page because it seemed easy...
but, as always, it's harder than we thought.

Alastair



Reply via email to