why the following snippet
int i = 30;
int p = 0.7f * i;
printf("p is %d\n", p);
gives 20 instead of 21 on vc++ 2008 express ? I know it's an old compiler!
why the following snippet
int i = 30;
int p = 0.7f * i;
printf("p is %d\n", p);
gives 20 instead of 21 on vc++ 2008 express ? I know it's an old compiler!
The number 0.7 is not representable in the binary-based floating-point format your compiler uses for float
. When the decimal numeral “0.7” is converted to float
, the result is the nearest representable value. That is a number slightly below 0.7.
Multiplying that by 30 produces a number slightly below 21, and converting it to int
with truncation produces 20.