How can I check for NaN values?

Jack Ha picture Jack Ha · Jun 3, 2009 · Viewed 1.6M times · Source

float('nan') results in Nan (not a number). But how do I check for it? Should be very easy, but I cannot find it.

Answer

gimel picture gimel · Jun 3, 2009

math.isnan(x)

Return True if x is a NaN (not a number), and False otherwise.

>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True