我需要帮助来理解 ruby 的浮点精度

I need help understanding ruby's floating point precision

前几天我遇到了这个,并确定它不会给我带来任何麻烦,但我只是想知道为什么会这样?

1.9.2p320 :001 > 0.39-0.09
 => 0.30000000000000004

这是因为 Ruby 默认使用 Double-precision floating-point format. You can read about issues related to it here。然而,这里有一个简短明了的答案:

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

来源:http://floating-point-gui.de/

浮点数不能精确表示所有的实数,浮点运算也不能精确表示真正的算术运算,这就导致了很多令人吃惊的情况。

我建议阅读:https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

您可能希望使用 BigDecimal 来避免此类问题。