[Python-ideas] Way to check for floating point "closeness"?

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Jan 13 03:52:28 CET 2015


On Mon, Jan 12, 2015 at 9:32 PM, Nathaniel Smith <njs at pobox.com> wrote:

> I don't want to step on your positive let's-fix-it attitude, I think
> that's great, but... I do think you are really underestimating the
> complexity of the problem.


Didn't IEEE 754 solve this problem for us?  In IEEE 754 the bijection
between floats and integers defined by reading float bits as a 64-bit
unsigned integer preserves the total ordering and therefore the
"closeness".  For finite floats that are not too far apart the difference
between the corresponding ints is the same as the difference in ulps.

I think something like abs(x.view(int) - y.view(int)) < ulps would be a
reasonable implementation for is_close(x, y).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150112/03d58ff9/attachment-0001.html>


More information about the Python-ideas mailing list