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



上一篇:Serial,Parallel,CMS,G1四大GC收集器特点小结
下一篇:laydate 时间框弹窗点击弹出后立马消失的问题解决

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