1

Possible Duplicate:
Is JavaScript’s Floating-Point Math Broken?

This is going to be a very rudimentary comp-sci question. Consider the following C# (the same holds true for JS, I assume it's how math works with doubles in general).

var i = .01;
i+=.01; //i=.02
i+=.01; //i=.03
i+=.01; //i=.04
i+=.01; //i=.05
i+=.01; //i=.0600000000005 (I may have added/missed a few 0s in there)
i+=.01; //i=.07
i+=.01; //i=.08
i+=.01; //i=.09
i+=.01; //i=.0999999999992 (I may have added/missed a few 9s in there)

So, what's happening and how can I accurately predict the sum of i+.01?

4

1 回答 1

0

双精度数不仅仅是为您的数字获取小数点的一种方法。它使用浮点数学。Pete Becker的这篇文章很好地解释了它是如何工作的。

于 2012-12-25T15:25:27.413 回答