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

Mark Dickinson dickinsm at gmail.com
Tue Jan 13 10:54:04 CET 2015


[Responding to everyone this time; apologies to Alexander.]

On Tue, Jan 13, 2015 at 12:52 AM, Alexander Belopolsky <
alexander.belopolsky at gmail.com> wrote:

> BTW, as we are discussing math, cmath and non-finite floats, maybe we
> could clean up some inconsistencies.
>
> For example
>
> >>> cmath.exp(inf*1j)
> (nan+nanj)
>
> but
>
> >>> cmath.sin(inf)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: math domain error
> >>> cmath.cos(inf)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: math domain error
>

There's no inconsistency here: those are all correct. Note that "inf * 1j"
is *not* complex(0, inf): it's a complex nan (complex(nan, inf), to be
precise), and exp applied to that correctly gives another complex nan, with
no exception.  You'd see the same with cmath.sin(inf*1j).  Note that
cmath.exp(complex(0, float('inf'))) *does* give a ValueError.

-- 
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150113/0f313ab3/attachment.html>


More information about the Python-ideas mailing list