Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0.1 + 0.7 != 0.8 [duplicate]

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

like image 733
Eimantas Avatar asked Dec 31 '10 16:12

Eimantas


2 Answers

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

like image 126
ismail Avatar answered Sep 25 '22 23:09

ismail


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;
}
like image 35
Cesar A. Rivas Avatar answered Sep 24 '22 23:09

Cesar A. Rivas