i have written complex function and i have used the float number and multiplied of of the numbers, but the float number use only the 1st 2 fraction, so how can i use all fraction of float number.
i have simple program and the result, i hope this to be clear to my question:
this is as example, but the actual thing is why the float ignore the other fraction??
when float f=10.123456;
why the result of int i= (int) f * 1000; is not 10123 ???
Looks like we double-posted. To answer your question about why the expression
int i= (int) f * 1000;
doesn't work, the order of precedence of operators means that f is cast to an int before it's multiplied. You need to force the multiplication with parentheses first:
@robtillaart: I'm not a big fan of expressions like:
int i= f * 1000;
int i= round(f * 1000.0);
because both rely on "silent casts" to work correctly. While the code compiles without error, it's dangerous because you're trying to pour 4 bytes of data into a 2-byte bucket. The compiler my company produced had a "picky flag" that could be set from 1 to 10. Level 10 was as fussy as the Lint utility on Unix and the code above would not pass without error. Using an explicit cast at least better documents what you are doing:
int i= (int) (f * 1000.0);
int i= (int) round(f * 1000.0);