这将是一个非常基本的综合问题。考虑下面的C#(对于JS来说也是如此,我认为数学通常用双精度数来表示。)
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)
那么,发生了什么,我怎样才能准确预测i + .01的总和?