I have a basic understanding in float-point number and was reading this article which says:
0.1 + 0.2: This equals 0.3, but in floating-point:
(0.1 + 0.2) == 0.3
is false. This is because 0.1, 0.2 and 0.3 cannot be precisely represented in base 2 floating-point.
Well, that's true according to the nature of floating point number, but I wrote a simple program to test:
float a = 0.1;
float b = 0.2;
if(a+b == 0.3)
{
printf("true");
} else
{
printf("false");
}
// result is true
but the output is actually true
. Here is my two questions:
I think what happens is, because C uses the round-to-even rounding mode, so after rounding, it happens to be true, is my understanding correct?
If my understanding is correct, then there must be some specified float-point number won't be true in this case, because there is still a small chance that rounding might fail. So that must be some combination as
float a = ...; float b = ...; if(a+b == XXX) // where XXX is the "intuitive" sum of a and b { printf("true"); } else { printf("false"); } //result is false now
Is my understanding correct?