[Python-ideas] math.inf and math.nan constants
Chris Angelico
rosuav at gmail.com
Wed Jan 7 23:13:20 CET 2015
On Thu, Jan 8, 2015 at 6:02 AM, Bruce Leban <bruce at leban.us> wrote:
> There is a problem here but that's not it. The problem is that
>
> float("nan") is float("nan") => False
> math.nan is math.nan => True
>
>
> So is this an attractive nuisance that will make people think they can
> should use x is math.nan instead of math.isnan(x)? Hopefully not but
> something to consider. Pylint should flag this usage.
I don't think that's a problem. Using 'is' to test floating-point
values is already broken:
>>> math.acos(-1) is math.pi
False
>>> math.atan(1)*4 is math.pi
False
>>> 1.0 + 2.0 is 3.0
False
There are plenty of calculations that will result in infinity or NaN,
and the correct thing to do will be either an equality check or the
math.is* functions. Or, as everyone keeps spouting, some kind of
epsilon-based "almost equal" check. Or something. But not object
identity.
ChrisA
More information about the Python-ideas
mailing list