0

First Situation

for (int i = 0 ; i <=2 ; i++)
{
    cout << i << endl ;
}

output:

1
2

Second Situation

for (float i = 0 ; i <= 2 ; i+=.2)
{
    cout << i << endl;
}

output

1
1.2
1.4
1.6
1.8

The question is why in the second situation he didn't take the 2 even i said ( <= ) and the funny thing if i remove the = the output will be even the same ?

Constrains i have to use the float DataType and i want to use the <= Operator

dragosht
  • 3,237
  • 2
  • 23
  • 32
Hesham Adel
  • 367
  • 4
  • 16
  • 3
    `2.0` is not equal to `2`! – haccks Jul 03 '14 at 09:09
  • 1
    @haccks: 2.0/2.0f *is* equal to 2, the problem is 0.2f != 0.2 (mathematically), and `0.2f + 0.2f + 0.2f + 0.2f + 0.2f != 1`. @H.Scrope: floating point representation is not perfectly precise, so after your additions you end up with a number that's ever-so-slightly less than 2. You could say `< 2.1` to solve your problem, as the accumulated error will be far less than that (around the 15th digit). – Tony Delroy Jul 03 '14 at 09:13
  • In fact, `float` is precise. But you can cast `float` to `int` to compare. – YaleCheung Jul 03 '14 at 09:15
  • 1
    Not sure why the down-vote. It seems like a perfectly valid question from someone who doesn't understand the many _many_ (_many!_) intricacies of playing with floats. I would add an answer referencing Bruce Dawson's [excellent posts](http://randomascii.wordpress.com/category/floating-point/) about the subject, but I fear that would just cause confusion at this point. – icabod Jul 03 '14 at 09:17
  • 1
    possible duplicate of [For loop with float as counter, increasing by 0.01, does not print expected float values](http://stackoverflow.com/questions/16124040/for-loop-with-float-as-counter-increasing-by-0-01-does-not-print-expected-floa) – Pascal Cuoq Jul 03 '14 at 12:25

2 Answers2

1

Because 0.2 doesn't fit exactly in a float and you accumulate floating point errors in your loop. On my computer accumulating 10 times 0.2 is 2.38419e-07 above 2.0f

fjardon
  • 7,921
  • 22
  • 31
0

You cannot compare float or double variables using == because of possible arithmetic rounding errors. You should use epsilon.

const float EPSILON = 0.00001f;
for (float f = 0.0f; EPSILON > std::fabs(f - 2.0f); f += 0.2f)
{
std::cout << f << std::endl;
}

Also try use lireral f when you are using float type (float my_float = 12.4f;).

Dakorn
  • 883
  • 6
  • 11
  • 2
    I disagree that using epsilon is the way forward. It is fine for some circumstances (such as the example given), but can be inappropriate for either very large or very small (<1.0) numbers. Again, I reference [Bruce Dawson](http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) who covers comparison quite nicely, along with explanation. – icabod Jul 03 '14 at 09:28
  • 1) the condition is iverted: The condition ought to be true to continue the loop. What you wrote is the condition when to stop the loop. 2) It would fail if you wanted to go to 100.0f instead of 2.0f – fjardon Jul 03 '14 at 09:44