problem about converting "float" to "int"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I write below code in my program:
float f=0.371f;
int i=(int)(f*1000.0f);

I think the result of "i" should be "371",but in fact it is "370",why?How to solve it?
 
pango said:
I write below code in my program:
float f=0.371f;
int i=(int)(f*1000.0f);

I think the result of "i" should be "371",but in fact it is "370",why?How to solve it?

If you look at f in your debugger you will see it is actually
0.37099999. This is the nature of floating point representation. The
other key thing to note is that float to integer conversions don't
round. If you want rounding, you write your own.


And, as an exercise in confusion, inspect this with your debugger:

int i2 = (0.371 * 1000.0);


doug.
 
Search for "what every programmer should know about floating point" using
your favorite search engine for the classic (and well written) article
describing this and other relevant issues.

Ronald Laeremans
Visual C++ team
 
Back
Top