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

Serhiy Storchaka storchaka at gmail.com
Tue Jan 13 08:57:30 CET 2015


On 13.01.15 02:52, Alexander Belopolsky wrote:
> 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.

Note that x + y*1j is not equivalent to complex(x, y).

 >>> complex(0, math.inf)
infj
 >>> math.inf*1j
(nan+infj)

It's because 1j == 0 + 1j and 0*inf is nan.

> For example
>
>  >>> cmath.exp(inf*1j)
> (nan+nanj)

 >>> cmath.exp(complex(0, math.inf))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: math domain error




More information about the Python-ideas mailing list