I thought this message from Joe should have wider circulation.
Someone out there must have good ideas about this!
Simon
------- Forwarded Message
Date: Thu, 27 Feb 92 01:38:44 -0700
From: [EMAIL PROTECTED] (Joe Fasel)
To: [EMAIL PROTECTED], [EMAIL PROTECTED], kh,
[EMAIL PROTECTED], simonpj
Subject: The last bug in the Prelude
Folks,
The Prelude I have sent to Kevin, John, and Lennart has one known
bug: rationalToRealFloat in PreludeCore loses accuracy (one bit
sometimes). This is a problem by itself, but also means that
read(show x) == x fails about half the time on floating-point
types. I can think of a couple of ways to fix this, but they're
kind of gross, and Lennart has reminded me that, for people like
him who actually use the Prelude, fromRational needs to be efficient,
since it's used for overloaded numeric constants.
It seems what you want to do is find the appropriate power k of the
fp radix b and evaluate
encodeFloat (round (x * (1 % b::k))) k
Any way I can think of to compute k correctly involves something like
using a trial value and then checking to see if it missed (which
involves comparisons on rationals). One way to get the trial
exponent is with logarithms of the rational numerator and denominator.
Another way is to do what the current, inaccurate conversion does:
convert the numerator and denominator separately to the floating-point
type and do a fp division.
Anyway, if anyone can think of a good way to do this, I'd certainly
like to know.
- --Joe
------- End of Forwarded Message