可能重複:
Why is floating point arithmetic in C# imprecise?
Why does ghci say that 1.1 + 1.1 + 1.1 > 3.3 is True?爲什麼我在Perl中從自身中減去相同的浮點數時得不到零?
#!/usr/bin/perl
$l1 = "0+0.590580+0.583742+0.579787+0.564928+0.504538+0.459805+0.433273+0.384211+0.3035810";
$l2 = "0+0.590580+0.583742+0.579788+0.564928+0.504538+0.459805+0.433272+0.384211+0.3035810";
$val1 = eval ($l1);
$val2 = eval ($l2);
$diff = (($val1 - $val2)/$val1)*100;
print " (($val1 - $val2)/$val1)*100 ==> $diff\n";
令人驚訝的輸出最終是
((4.404445 - 4.404445)/4.404445)*100 ==> -2.01655014354845e-14.
難道不應該是零???? 請問任何人都可以這樣解釋......
什麼,你不覺得0.000000000000201655014354845是接近零? – Ether 2010-01-17 17:50:38
這個問題並不完全是引用的非perl問題的重複,因爲問題很複雜,因爲perl提供的默認字符串化精度略低於可用的數字精度。 – ysth 2010-01-18 06:39:24