[Python-ideas] math.inf and math.nan constants
Alexander Heger
python at 2sn.net
Fri Jan 9 14:03:47 CET 2015
> There's an inherent conflict between the mathematical notion of NaN
> and the software engineering notion of object identity. For containers
> in Python, it's the latter that prevails, in order to preserve the
> invariant that "x is y" implies "x in (y,)" for arbitrary containers
> and objects, even if x and y happen to be distinct references to the
> exact same NaN object.
I suppose in the end it is not advisable to use "is" to compare
numerical variables in the first place as you can run into the cached
values,
>>> x = 256
>>> y = 256
>>> x is y
True
>>> x = 257
>>> y = 257
>>> x is y
False
In most cases of numerical applications people will use numpy and that
behaves the expected way, the issue is if a python list behaves
different from a numpy array.
>>> import numpy as np
>>> x = np.tile(np.nan, 3)
>>> x == x
array([False, False, False], dtype=bool)
>>> x is x
True
>>> x = [float('nan')] * 3
>>> x == x
True
>>> x is x
True
-Alexander
More information about the Python-ideas
mailing list