How to check if a value is infinity or NaN in Python

2 Answers

0 votes
import math

x = float('inf')
y = float('nan')

print(math.isinf(x))  
print(math.isnan(y))  



'''
run:

True
True

'''

 



answered Jan 12 by avibootz
0 votes
import math

def is_inf_or_nan(value):
    return math.isinf(value) or math.isnan(value)

x = float('inf') 
y = float('nan') 

print(is_inf_or_nan(x))
print(is_inf_or_nan(y))



'''
run:

True
True

'''

 



answered Jan 12 by avibootz

Related questions

...