Is it possible to set a number to NaN or infinity?

Bob picture Bob · Mar 25, 2011 · Viewed 322.8k times · Source

Is it possible to set an element of an array to NaN in Python?

Additionally, is it possible to set a variable to +/- infinity? If so, is there any function to check whether a number is infinity or not?

Answer

moinudin picture moinudin · Mar 25, 2011

Cast from string using float():

>>> float('NaN')
nan
>>> float('Inf')
inf
>>> -float('Inf')
-inf
>>> float('Inf') == float('Inf')
True
>>> float('Inf') == 1
False