Convert float to int implicit vs explicit (cast) discrepancy

Viewed 5027
int i1 = 0, i2 = 0;
float f = 2.8f;
i1 += f;
i2 += (int)f;
f += 0.1;
i1 += f;
i2 += (int)f;
f += 0.1;
i1 += f;
i2 += (int)f;
printf("%d %d\n", i1, i2);

Output:

7 6
  1. Why would implicit and explicit conversion results be different?
  2. I would like results to be like the ones for implicit conversion, but without compilation warning. Is it possible?

Platform is Windows7, VS2010 or 2013.

3 Answers
Related