[Haskell-cafe] Mystery of an Eq instance

Sven Panne svenpanne at gmail.com
Tue Sep 24 17:39:40 CEST 2013


2013/9/22 Mike Meyer <mwm at mired.org>:
> On Sat, Sep 21, 2013 at 5:28 PM, Bardur Arantsson <spam at scientician.net>
> wrote:
> Trying to make something whose name is "Not A Number" act like a
> number sounds broken from the start.

The point here is that IEEE floats are actually more something like a
"Maybe Float", with various "Nothing"s, i.e. the infinities and NaNs,
which all propagate in a well-defined way. Basically a monad built
into your CPU's FP unit. ;-)

> I just went back through the thread, and the only examples I could
> find where that happened (as opposed to where floating point
> calculations or literals resulted in unexpected values) was with
> NaNs. Just out of curiosity, do you know of any that don't involve
> NaNs?

Well, with IEEE arithmetic almost nothing you learned in school about
math holds anymore. Apart from rounding errors, NaNs and infinities,
-0 is another "fun" part:

   x * (-1)

is not the same as

   0 - x

(Hint: Try with x == 0 and use recip on the result.)

> Float violates the expected behavior of instances of - well, pretty
> much everything it's an instance of. Even if you restrict yourself to
> working with integer values that can be represented as floats.  If
> we're going to start removing it as an instance for violating instance
> expectations, we might as well take it out of the numeric stack (or
> the language) completely.

Exactly, and I am sure 99.999% of all people wouldn't like that
removal. Learn IEEE arithmetic, hate it, and deal with it. Or use
something different, which is probably several magnitudes slower. :-/



More information about the Haskell-Cafe mailing list