,我看到這個Python的舍入誤差
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 62 * 41.2 * 2/250
20.435200000000002
>>> 62 * 4.12 * 2/25
20.4352
>>>
只是可以肯定我解僱下面的代碼通過GCC但沒有告訴我上述行爲。我知道這可能是四捨五入的,但它應該是統一的。爲什麼Python在數學上加起來的00000000002不應該在那裏。
C/C++代碼
#include <stdio.h>
int main() {
printf ("%lf %lf \n", 62 * 41.2 * 2/250, 62 * 4.12 * 2/25);
}
結果
20.435200 20.435200
如果有人好奇,他們屬於某個發動機排量計算比較:)
浮點運算,它的工作原理是這樣的大多數語言:Python的http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html應用一些四捨五入到打印輸出你可以在這裏看到http://docs.python.org/2/tutorial/floatingpoint.html,但是在兩種情況下存儲的數字都只有浮點允許的數量。 – 2013-04-10 15:41:20
這是浮點差異。由於內部表示,「41.2/10!= 4.12」。 – mgilson 2013-04-10 15:44:19