在ActionScript 3奇怪的数学?奇怪、数学、ActionScript

2023-09-08 12:00:03 作者:最霸气的长名字,霸气长一点

Why我看到初始化为一些价值像21.4作为21.399999618530273双变量?    是JavaScript的数学坏了吗?   在其他许多问题,对于每一种语言和平台,都用同样的答案。    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))) );

打印出

>> 0.6000000000000227

为什么?

推荐答案

浮点舍入误差。浮点数不能再present一些值完全相同,所以你看到的是使用FP数的限制。尾数(对FP民,使该精度的一部分)是只有一定的比特数,而当某物也不可能完全重新presented,则得到像上述结果。我知道一个事实,即0.1不能重新presented完全,所以它是有道理的,0.6不能为好。

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.