Why do "Not a Number" values equal True when cast as boolean in Python/Numpy?

math, numpy, python

Solution

This is in no way NumPy-specific, but is consistent with how Python treats NaNs:

In [1]: bool(float('nan'))
Out[1]: True

The rules are spelled out in the documentation.

I think it could be reasonably argued that the truth value of NaN should be False. However, this is not how the language works right now.

Problem

When casting a NumPy Not-a-Number value as a boolean, it becomes True, e.g. as follows. ``` >>> import numpy as np >>> bool(np.nan) True ``` This is the exact opposite to what I would intuitively expect. Is there a sound principle underlying this behaviour? (I suspect there might be as the same behaviour seems to occur in Octave.)

Original source