为什么6.84 - 3.6 == 3.2399999999999998 [英] Why 6.84 - 3.6 == 3.2399999999999998

查看:166
本文介绍了为什么6.84 - 3.6 == 3.2399999999999998的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以解释为什么在Ruby中:

  6.84  -  3.6 == 3.2399999999999998 

而不只是3.24?我知道它与这些数字的二进制表示有关,并且这个数字和
$ b $ pre $ (十进制('6.84') - 十进制('3.6 '))

会返回预期的结果。我只是很好奇Float行为的详细解释。

请参阅(以及许多其他地方)关于浮点数学。

为什么100 * 0.07等于6.9999 ....?


I just encountered this and can't figure out why exactly Ruby behaves this way.

Could someone explain why in Ruby:

6.84 - 3.6 == 3.2399999999999998

and not just 3.24? I know that it's related to the binary representation of those number and that

(Decimal('6.84') - Decimal('3.6'))

would return the expected results. I'm just curious about the detailed explanation of the Float behavior.

解决方案

See the answers in "Why is the 100*0.07 equal to 6.9999....?" (and many other places) about floating-point math.

Why is the 100*0.07 equal to 6.9999....?

这篇关于为什么6.84 - 3.6 == 3.2399999999999998的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆