nan = float('nan')
And now you have the constant, nan
.
You can similarly create NaN values for decimal.Decimal.:
dnan = Decimal('nan')
Use float("nan")
:
>>> float("nan")
nan
You can do float('nan')
to get NaN.
You can get NaN from "inf - inf", and you can get "inf" from a number greater than 2e308, so, I generally used:
>>> inf = 9e999
>>> inf
inf
>>> inf - inf
nan
A more consistent (and less opaque) way to generate inf and -inf is to again use float():
>> positive_inf = float('inf')
>> positive_inf
inf
>> negative_inf = float('-inf')
>> negative_inf
-inf
Note that the size of a float varies depending on the architecture, so it probably best to avoid using magic numbers like 9e999, even if that is likely to work.
import sys
sys.float_info
sys.float_info(max=1.7976931348623157e+308,
max_exp=1024, max_10_exp=308,
min=2.2250738585072014e-308, min_exp=-1021,
min_10_exp=-307, dig=15, mant_dig=53,
epsilon=2.220446049250313e-16, radix=2, rounds=1)
Source: Stackoverflow.com