1
我不明白爲什麼這個代碼返回False:爲什麼這個返回False
list_of_disks = [[170, 158, 470, 0.1], [135, 176, 410, 0.2], [100, 193, 350, 0.3], [170, 458, 470, 1.1]]
def f1(pos):
for x in range(0, len(list_of_disks)):
if list_of_disks[x][3] == pos:
print list_of_disks[x+1][3] - 0.1, list_of_disks[x][3]
print list_of_disks[x+1][3] - 0.1 == list_of_disks[x][3] # Why is this False..?
break
f1(0.2)
當我打印出來的2個值,他們似乎是一樣的..? 希望有人能幫助我,謝謝!
浮點不能被依賴那麼精確。 – 2014-10-16 14:39:57
請參閱Python教程的[「浮點:問題和限制」](https://docs.python.org/2/tutorial/floatingpoint.html)。另請參閱[「每位計算機科學家應瞭解的浮點算術知識」](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)。 – 2014-10-16 14:44:03
我認爲精度是這裏的問題。 – 2014-10-16 14:44:19