
On Thu, 12 Jul 2007, Jon Fairbairn wrote:
Now, a proper exact real type is doubtless very inefficient, but wouldn't it be possible to define something that had a fairly efficient head, and a lazy tail? So you'd have, say
data Real = R {big::(Ratio !Int !Int), small:: More_Precision}
for some exact real representation More_Precision such that R a b represents the number (a+b) (It might be better to use something shorter than Int for the Ratio so that it takes less space). For any rational arithmetic that fits in the big part, the small part would be zero (and therefore small!).
This would give exact answers for the sort of arithmetic you list above, but could still be an instance of Floating etc.
Interesting approach. Somehow similar to making Integer a sum of Int and BigInt. Indeed, I have used transcendent arithmetic on Doubles to speedup computations for real numbers. However real numbers cannot be checked for equality. This can be also considered an advantage, because using (==) for floating point numbers is most oftenly a bug. I think that this hybrid type is nice and could be used by default. But it should not replace native floating point types, since they have guaranteed speed in favor of not guaranteed precision. And we need a correct implementation.