Why is math.nan
different than numpy.nan
?
>>> import math
>>> import numpy as np
>>> math.nan is math.nan
True
>>> np.nan is np.nan
True
>>> np.nan is math.nan
False
What is the point of having two different NaNs?
Why is math.nan
different than numpy.nan
?
>>> import math
>>> import numpy as np
>>> math.nan is math.nan
True
>>> np.nan is np.nan
True
>>> np.nan is math.nan
False
What is the point of having two different NaNs?
What is the point of having two different NaNs?
Disclaimer: I assume you want to know why there is math.nan
and numpy.nan
, if this does not hold true ignore this answer entirely.
This is due to historical reasons, math docs inform that math.nan
is
New in version 3.5.
NumPy is older than that, PyPI NumPy release history shows that version 1.0 was released in October 2006, whilst according to PEP 478 first final version for Python 3.5.0 was scheduled for September 2015, therefore creators of NumPy have not possibility of usage of math.nan
. This lead to question why nan
was added to math
if it is already in numpy
. This might be explained simply: math
is built-in module, whilst numpy
is not.
As side note you might also craft NaN
without any import
s in python, just do float("NaN")
.
© 2022 - 2024 — McMap. All rights reserved.
np.nan == np.nan
returnsFalse
. – Pristinex = 1.0; y = 1.0; print(x is y)
. The output is False. – Amphibolousmath.nan == math.nan
also returnsFalse
. – Franconianfloat('NaN') == float('NaN')
also returnsFalse
, although I don't know if that differs frommath.nan
is implemented. – Franconianisnan()
tests and compare those results if you want to see if two values are both NaNs. – Cadence