`
gao_20022002
  • 浏览: 160498 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

0.4 - 0.3 == 0.1是true??false

    博客分类:
  • ror
阅读更多

你注意过了么?

 

此处的运算结果是false。

 

这个是ruby上给的解释。哈哈,见识了。

 

Float objects have plenty of precision and can approximate 0.1 very well, 
but the fact that this number cannot be represented exactly leads to problems. 
Consider the following simple Ruby expression:

0.4 - 0.3 == 0.1    # Evaluates to false in most implementations

Because of rounding error, the difference between the approximations of 0.4 and 0.3 is 
not quite the same as the approximation of 0.1. This problem is not specific to Ruby: C, 
Java,JavaScript, and all languages that use IEEE-754 floating-point numbers suffer 
from it as well.
 
3
0
分享到:
评论
2 楼 surpass 2008-12-11  
java里面也是这样的啊
1 楼 fins 2008-12-03  
我印象中 任何语言在处理浮点数的时候 都会类似的问题.
例如 10个0.1相加不等于1 之类的

相关推荐

Global site tag (gtag.js) - Google Analytics