5、处理无穷大和NaN
5.1 对浮点数的无穷大、负无穷大或NaN(Not a Number)进行测试
5.2 解决方案:
# python中没有特殊的语法用来表示这些特殊的浮点数值,但是可以通过float()来创建
a = float(‘inf’)
b = float(’-inf’)
c = float(‘nan’)
print(a,b,c)
#要检测是否出现了这些值,可以使用isinf() 和 isnan()
import math
print(math.isinf(a)) # True
print(math.isnan©) # True
5.3 inf 和 nan 都会进行传播
print(a+1) # inf
print(1/a) # 0.0
print(a/a) # nan
print(c+1) # nan