Float precision and float equality
dickinsm at gmail.com
Sat Dec 5 21:56:38 CET 2009
On Dec 5, 8:25 pm, Raymond Hettinger <pyt... at rcn.com> wrote:
> On Dec 5, 7:37 am, Anton81 <gerenu... at googlemail.com> wrote:
> > I'd like to do calculations with floats and at some point equality of
> > two number will be checked.
> > What is the best way to make sure that equality of floats will be
> > detected, where I assume that mismatches beyond a certain point are
> > due to truncation errors?
> Short answer: use round().
Can you explain how this would work? I'm imagining a test
if round(x, 6) == round(y, 6): ...
but that still would end up missing some cases where x and y
are equal to within 1ulp, which presumably isn't what's wanted:
>>> x, y = 0.1234565, 0.123456500000000004
>>> round(x, 6) == round(y, 6)
More information about the Python-list