5

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

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Eimantas
  • 48,927
  • 17
  • 132
  • 168

3 Answers3

18

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

ismail
  • 46,010
  • 9
  • 86
  • 95
2
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;
}
Cesar A. Rivas
  • 1,355
  • 1
  • 10
  • 13
0

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

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48