How to check if a variable is NaN (not a number) in Python

4 Answers

0 votes
import math

n = (100.0 ** 100) * (100.0 ** 100)

print(n)

# NaN = "not a number"

print('isnan(n) =', math.isnan(n))


'''
run:

inf
isnan(n) = False

'''

 



answered Oct 16, 2017 by avibootz
0 votes
import math

n = 100

print(n)

# NaN = "not a number"

print('isnan(n) =', math.isnan(n))


'''
run:

100
isnan(n) = False

'''

 



answered Oct 16, 2017 by avibootz
0 votes
import math

n = math.pi

print(n)

# NaN = "not a number"

print('isnan(n) =', math.isnan(n))


'''
run:

3.141592653589793
isnan(n) = False

'''

 



answered Oct 16, 2017 by avibootz
0 votes
import math

n = (100.0 ** 100) * (100.0 ** 100)
x = n / n

print('n / n =', n / n)

# NaN = "not a number"

print('isnan(x) =', math.isnan(x))


'''
run:

n / n = nan
isnan(x) = True

'''

 



answered Oct 16, 2017 by avibootz
...