   For-loops with floats? (how I hate thee...)
  I'm trying to step through a parametric curve that goes from values 0..1 - So i thought i'd just set up a simple for-loop:for ( float x = 0.0f; x <= 1.00f; x += 0.05f ) {std::cout << x << "\n";}output:00.050.10.150.20.250.30.350.40.450.50.550.60.650.70.750.80.850.90.95As you can see, it never actually reaches 1.00, even though i specifically told it "less than or equal to 1.00". I know this is some kind of floating point fudginess, but can anyone explain why it won't ever print "1.0" ? I still need to iterate from 0..1 for my program without a bunch of goofy hacks.PS - i know i can increment with INTs and then cast to floats, as in float(x)*increment, but i'm trying to avoid unnecessary casting.Thanks for the insights!gcc version 4.2.4
  Here are my 2 ideas. First is, floating point comparisons with the normal comparison operator's is generally not going to produce the desired result reliably.It's a rather complicated topic, but luckily Boost has a solution as always :)http://www.boost.org/doc/libs/1_34_1/boost/test/floating_point_comparison.hppMy other thought is, you can simplify the problem, the loop needs to run N number of times, it doesn't care about the floating point value associated with each pass, so base the loop off of ints and usual, and use a simple algorithm to compute N.For instance:int n = 1.0f / 0.05f;float x = 0.0f;for ( int i = 0; i <= n; ++i, x += 0.05f ) {std::cout << x << "\n";}*edit* the post below is a fantastic explanation of why standard comparison operators will not function as expected, ++Brother Bob :)   ;  The floating point values you're trying to use cannot be represented exactly, and the numbers you print are not exactly the correct values of x. For example, 0.05 cannot be stored exactly, so the actual value is the closest approximation that can actually be stored.Since the value of 0.05 is not exactly 0.05, the loop variable is not hitting exactly 1.0 in the last iteration. It is most likely something just above 1.0, and this fails the loop condition. Print the value of x with much higher precision and you will see this difference. At the moment, the formatting rounds to fewer decimals and you cannot see the result.Keep in mind that floats works with a binary number base, and some numbers that can be expressed exactly in base 10 cannot be represented exactly in base 2. One such number that doesn't have an exact representation is 0.1, which require an infinite expansion in base 2.I suggest you use an integer loop instead to control the exact number of iterations you want, and convert to float for calculation of actual interpolation factor.   ;  Let it count from 0.0f to 1.0f+epsilon. More correctly, one generally writes something like abs(A-B) <= epsilon, but counting to 1.0f+epsilon will work in your case and is simpler.For floats smaller than +/- 10.0, 0.000001 is a good epsilon (has to be bigger if your number ranges are bigger, for example 0.000061 for numbers up to 1000.0).In your particular case it does not matter much since you're only counting in increments of 0.05 - so you could as well use 0.01 or 0.001 or pretty much any other small number, just exactly 1.0 won't work.   ;  If you're summing floats, you can also improve precision using the Kahan summation algorithm, but it only improves precision, not completely solves it, so you will always need to deal with some degree if imprecision/epsilon.   ;int n = 1.0f / 0.05f;Wouldn't this also behave in unexpected ways? ;)One could also trydouble d = 0.0;for (int i = 0; i <= 100; i += 5, d += 0.5)    std::cout << d << '\n';;Quote:Original post by visitorOne could also try*** Source Snippet Removed ***But there you have float addition again. Yes, it works with 0.5, but it can still be imprecise with other values. Check out this code:#include <iostream>int main(void){float x = 0.0;float y = 0.0;for(; x < 1.0; )x += 0.05;if(x == 1.0)std::cout << "x = " << x << " equal 1" << std::endl;elsestd::cout << "x = " << x << " *not* equal 1" << std::endl;for(int i=0; y < 1.0; i++)y = i * 0.05;if(y == 1.0)std::cout << "y = " << y << " equal 1" << std::endl;elsestd::cout << "y = " << y << " *not* equal 1" << std::endl;}Output of the program:x = 1 *not* equal 1y = 1 equal 1So it's probably better to multiply your step size with the loop iterations.;float x = 0.0f;for ( ; x < 1.00f; x += 0.05f ) { // strictly lessstd::cout << x << "\n";}// whatever x is, it's first value not strictly less than 1.0std::cout << x << "\n"; ;  I don't understand the last post. What do you mean by "first value"? Its first value is 0.0f, which is definitely less than 1.0.   ;  After the for loop has ended, x will be whatever is the first value that didn't satisfy the loop condition was.   