编程语言
首页 > 编程语言> > Python浮动平等怪异

Python浮动平等怪异

作者:互联网

今晚看到Python的一些意外行为.为什么以下打印出“不相等”?!

num = 1.00
num -= .95
nickel = .05

if nickel != num:
    print 'not equal'
else:
    print 'equal' 

解决方法:

What every computer scientist should know about floating point arithmetic.

>>> num = 1.00
>>> num
1.0
>>> num -= 0.95
>>> num
0.050000000000000044
>>> nickel = .05
>>> nickel
0.05

标签:python,floating-point,equality
来源: https://codeday.me/bug/20191003/1850093.html