Possible Duplicate:
Most effective way for float and double comparison
strange output in comparison of float with float literal
int main()
{
float a = 0.8;
if (a == 0.8)
printf("x\n");
else
printf("y\n");
return 0;
}
Though a is equal to 0.8, it outputs y.
0.8 cannot be represented accurately in binary floating-point. Your code
if (a == 0.8)basically compares single-precision 0.8 with double-precision 0.8, which are not equal.To see this for yourself, try the following code:
It outputs: