Float precision and float equality

Carl Banks pavlovevidence at gmail.com
Sun Dec 6 17:54:37 EST 2009


On Dec 6, 11:34 am, Anton81 <gerenu... at googlemail.com> wrote:
> I do some linear algebra and whenever the prefactor of a vector turns
> out to be zero, I want to remove it.
>
> I'd like to keep the system comfortable. So basically I should write a
> new class for numbers that has it's own __eq__ operator?
> Is there an existing module for that?

I highly recommend against it; among other things it invalidates the
transitive property of equality:

"If a == b and b == c, then a == c."

It will also make the number non-hashable, and have several other
negative consequences.  Plus, it's not something that's never
foolproof.  What numbers are close enought to be condidered "equal"
depends on the calculations.

(I remember once struggling in a homework assignment over seemingly
large discrepancies in a calculation I was doing, until i realized
that the actual numbers were on the scale of 10**11, and the
difference was around 10**1, so it really didn't matter.)



Carl Banks



More information about the Python-list mailing list