Float precision and float equality

Dave Angel davea at ieee.org
Sun Dec 6 17:21:46 EST 2009



Anton81 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?
>
>
>   
You have to define your own "comfortable."  But if it's zero you're 
checking for, then I most certainly wouldn't try to hide it inside a 
"number class."  Most such formulas go ballistic when you get near zero.

The definition of 'close enough" is very context dependent, and 
shouldn't be hidden at too low a level.  But your mileage may vary.

For example, in your case, you might want to check that the prefactor is 
much smaller than the average (of the abs values) of the vector 
elements.  Enough orders of magnitude smaller, and you call it equal to 
zero.

DaveA




More information about the Python-list mailing list