[Haskell-cafe] instance Enum Double considerednotentirelygreat?

Chris Smith cdsmith at gmail.com
Tue Sep 27 21:13:39 CEST 2011

On Tue, 2011-09-27 at 12:36 -0400, Steve Schafer wrote:
> [0.1,0.2..0.5] isn't the problem. The problem is coming up with
> something that not only works for [0.1,0.2..0.5], but also works for
> [0.1,0.2..1234567890.5].
> A good rule of thumb: For every proposal that purports to eliminate
> having to explicitly take into consideration the limited precision of
> floating-point representations, there exists a trivial example that
> breaks that proposal.

If by "trivial" you mean easy to construct, sure.  But if you mean
typical, that's overstating the case by quite a bit.

There are plenty of perfectly good uses for floating point numbers, as
long as you keep in mind a few simple rules:

1. Don't expect any exact answers.

2. Don't add or subtract values of vastly different magnitudes if you
expect any kind of accuracy in the results.

3. When you do depend on discrete answers (like with the Ord functions)
you assume an obligation to check that the function you're computing is
continuous around the boundary.

If you can't follow these rules, you probably should find a different
type.  But there's a very large class of computing tasks where these
rules are not a problem at all.  In your example, you're breaking rule
#2.  It's certainly not a typical case to be adding numbers like 0.1 to
numbers in the billions, and if you're doing that, you should know in
advance that an approximate type is risky.


More information about the Haskell-Cafe mailing list