robertspierre
robertspierre

Reputation: 4331

Why numpy.nan is different than math.nan?

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?

Upvotes: 8

Views: 1795

Answers (1)

Daweo
Daweo

Reputation: 36360

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 imports in python, just do float("NaN").

Upvotes: 10

Related Questions