Precision for equality of two floats?

Alex Martelli aleax at mail.comcast.net
Mon Nov 28 10:58:37 EST 2005


Anton81 <berrybear at gmx.net> wrote:

> Hi!
> 
> When I do simple calculation with float values, they are rarely exactly
> equal even if they should be. What is the threshold and how can I change
> it?

Python's builtin floats compare for exact bit-by-bit equality -- no
"threshold".  You may want to look at the allclose function in the
Numeric extension package, at least for its specs (it consider both
absolute and relative difference).  Extension module gmpy might also
help, since its mpf floating numbers implement a fast reldiff (relative
difference) method.


Alex



More information about the Python-list mailing list