python 3.44 float addition bug?
Steven D'Aprano
steve at pearwood.info
Tue Jun 24 02:34:11 EDT 2014
On Mon, 23 Jun 2014 17:55:50 -0700, buck wrote:
> It used to be that the best way to compare floating point numbers while
> disregarding the inherent epsilon was to use `str(x) == str(y)`. It
> looks like that workaround doesn't work anymore in 3.4.
What inherent epsilon? Can you explain what you mean?
> What's the recommended way to do this now?
>
>>>> format(.01 + .01 + .01 + .01 + .01 + .01, 'g') == format(.06, 'g')
> True
That's equivalent to doing an implicit round. If all you want to do is
round your calculations, then explicitly round them:
py> x = sum([0.01]*6)
py> y = 0.06
py> round(x, 12) == round(y, 12)
True
Not that I'm recommending that you do it this way, but an explicit round
is better than using string formatting.
See also this:
http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
--
Steven
More information about the Python-list
mailing list