Error in Flash addition

谁说胖子不能爱 提交于 2019-12-24 06:45:04

问题


Make a new AS3 Document in Flash, paste in the following code and run it:

var a:Number=0;
trace(a)  //  0
a+=0.3;
trace(a)  //  0.3
a+=0.3;
trace(a)  //  0.6
a+=0.3;

trace(a)  //  0.8999999999999999
a+=0.3;
trace(a)  //  1.2
a+=0.3;
trace(a)  //  1.5
a+=0.3;
trace(a)  //  1.8
a+=0.3;
trace(a)  //  2.1
a+=0.3;
          //  ^ This is the output. Notice the inaccuracy starting from 0.9 / 0.89

Why the error? I'm just doing an ordinary hi resolution addition.

Any workarounds?


回答1:


This is a standard floating point problem. Binary floating point numbers do not represent the full range of decimal numbers with perfect accuracy, you need to do something along the lines of

trace (round (a, 1))

or, better yet, turn it into a string and strip off everything past the first fractional digit (since the result of round may also not be easily representable in binary).

although I don't know ActionScript. Still it's a very well known issue and not limited to AS3.

See for example Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273? or Strange floating-point behaviour in a Java program or What is a simple example of floating point/rounding error?.




回答2:


One workaround is to use a scaled integer type. For this example, you could increment by 3, and then divide by 10 just before output. That avoids all the rounding errors inherent in floating point arithmetic.



来源:https://stackoverflow.com/questions/366104/error-in-flash-addition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!