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

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Jan 13 01:52:31 CET 2015


On Mon, Jan 12, 2015 at 7:26 PM, Andrew Barnert <
abarnert at yahoo.com.dmarc.invalid> wrote:

> But I think the unavoidable ambiguity of cmath.inf argues against having
> cmath.nan.


I think nan and inf constants should follow the precedent established by e
and pi and be float in both math and cmath:

>>> type(cmath.pi)
<type 'float'>
>>> type(cmath.e)
<type 'float'>

Once you import float nan or inf, it is obvious how to build an complex
value containing them using x + y*1j expression, but having to import nan
and inf separately when you import everything else from cmath is an
annoyance.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150112/09837605/attachment.html>


More information about the Python-ideas mailing list