开发者

0.1 + 0.7 != 0.8 [duplicate]

开发者_JAVA技巧This question already has answers here: Is floating point math broken? (31 answers) Closed 11 months ago.

Why in some programming languages the expression in title evaluates to true? I've tried it in php, ruby and python.


Please read What Every Programmer Should Know About Floating-Point Arithmetic .


double TOLERANCE < 1.0E-10;
if(fabs(0.1+0.7-0.8)< TOLERANCE)
{
    std::cout << "0.1 + 0.7 == 0.8" << std::endl;
}
else
{
    std::cout << "0.1 + 0.7 != 0.8" << std::endl;
}


Because of internal interpretation of floating-point numbers, they do not exactly equal to what you have wrote.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜