odd math in actionscript 3? [duplicate]

梦想的初衷 提交于 2020-01-17 16:38:45

问题


Duplicate:

  • Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
  • Is JavaScript’s math broken?
  • many, many other questions, for every language and platform, all with the same answer.

trace( ">> " + (399.6-(Math.floor(399.6))) );

prints out

>> 0.6000000000000227

why?


回答1:


Floating Point rounding errors. Floating point numbers can't represent some values exactly, so what you are seeing is the limitations of using FP numbers. The mantissa (the part of the FP num that gives the accuracy) is only a certain number of bits, and when something can't be exactly represented, you get results like the above. I know for a fact that 0.1 can't be represented exactly, so it makes sense that 0.6 can't as well.




回答2:


Essentially, the decimal part of a floating point number is a summation:

1/2 + 1/4 + 1/8 + ...

where each fraction is one bit of your floating point representation. As paintballbob mentioned, this means there are some numbers that can't be represented (such at 0.1).

As far as I know, there is no fixed-point decimal built in to AS3 (though there are third-party libraries).



来源:https://stackoverflow.com/questions/784594/odd-math-in-actionscript-3

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