One - One Code All

Blog Content

python里对浮点数变量判断是否相等equal,不可以直接用==

Python   2017-01-15 17:10:26

python里对浮点数变量判断是否相等equal,不可以直接用==

a = 1.7
b = 0.9 + 0.8  # This should be 1.7
 
if a == b:
    print("a and b are equal.")
else:
    print("a and b are different !")
 
#返回结果是:a and b are different !
 
if abs(a-b) < 0.00001:
    print("a and b are equal.")
else:
    print("a and b are different !")
 
#返回结果:a and b are equal



上一篇:python中str函数isdigit、isdecimal、isnumeric的区别
下一篇:Python中字典合并的多种方法

The minute you think of giving up, think of the reason why you held on so long.