C/C++: 1.00000 <= 1.0f = False
c++, floating-accuracy, for-loop
Solution
As correctly pointed out in the comments, the value of `t` is not actually the same `1.00000` that you are defining in the line below.
Printing t with higher precision with `std::setprecision(20)` will reveal its actual value: `1.0000001192092895508`.
The common way to avoid these kinds of issues is to compare not with `1`, but with `1 + epsilon`, where epsilon is a very small number, that is maybe one or two magnitudes greater than your floating point precision.
So you would write your for loop condition as
for(t = 0; t <= 1.000001f; t += step)
Note that in your case, epsilon should be atleast ten times greater than the maximum possible floating point error, as the float is added ten times.
As pointed out by Muepe and Alain, the reason for `t != 1.0f` is that `1/10` can not be precisely represented in binary floating point numbers.
Problem
Can someone explain why 1.000000 <= 1.0f is false? The code: ``` #include <iostream> #include <stdio.h> using namespace std; int main(int argc, char **argv) { float step = 1.0f / 10; float t; for(t = 0; t <= 1.0f; t += step) { printf("t = %f\n", t); cout << "t = " << t << "\n"; cout << "(t <= 1.0f) = " << (t <= 1.0f) << "\n"; } printf("t = %f\n", t ); cout << "t = " << t << "\n"; cout << "(t <= 1.0f) = " << (t <= 1.0f) << "\n"; cout << "\n(1.000000 <= 1.0f) = " << (1.000000 <= 1.0f) << "\n"; } ``` The result: ``` t = 0.000000 t = 0 (t <= 1.0f) = 1 t = 0.100000 t = 0.1 (t <= 1.0f) = 1 t = 0.200000 t = 0.2 (t <= 1.0f) = 1 t = 0.300000 t = 0.3 (t <= 1.0f) = 1 t = 0.400000 t = 0.4 (t <= 1.0f) = 1 t = 0.500000 t = 0.5 (t <= 1.0f) = 1 t = 0.600000 t = 0.6 (t <= 1.0f) = 1 t = 0.700000 t = 0.7 (t <= 1.0f) = 1 t = 0.800000 t = 0.8 (t <= 1.0f) = 1 t = 0.900000 t = 0.9 (t <= 1.0f) = 1 t = 1.000000 t = 1 (t <= 1.0f) = 0 (1.000000 <= 1.0f) = 1 ```