Your code is equivalent to:
double x1 = 0.9;
float xf = (float)x1; // discard some precision
double x2 = (double)xf;
if (x2 >= x1) {
// ...
}
The problem is that, after you discarded some precision from x1
by converting it to float
, it can never be recovered; casting it back to double
doesn’t regain that precision. So x2
is still not the same as 0.9
, and due to the rounding direction, it happens to be a bit less. (Note: if you try the same thing with other values, you will sometimes find that it’s a bit more.)
solved Why does >= operator work as > operator in C when compared with 0.9 [duplicate]