Files
Python_CookBook_repo/3.数字日期和时间/7.处理无穷大和NAN.py
2025-09-10 16:12:45 +08:00

20 lines
626 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import math
if __name__ == '__main__':
# python没有原生的东西来表示这些值但他们可以被创建
a = float('inf')
b = float('-inf')
c = float('nan')
print(a, b, c)
# 如果想要感知这些值请使用isnan或isinf函数
print(math.isnan(c))
print(math.isinf(a))
# 要尤其注意inf在计算中会被传播
print(a+45)
# 但是一些神奇操作会产生nan比如
print(a + b)
print(a / a)
# 尤其注意nan会在所有的操作中传播且不会引起任何报错请务必在计算前进行使用isnan函数进行安全检测