#include
int main()
{
double fract=0;
int tmp;
//scanf(\"%lf\",&fract);
fract=0.312;
printf(\"%lf\",fract);
printf(\"\\n
Kernighan & Plauger say, in their old but classic book "The Elements of Programming Style", that:
They also say:
Both sayings point out that floating point arithmetic is not precise.
Note that some modern CPUs (IBM PPC) have IEEE 754:2008 decimal floating point arithmetic built-in. If using the correct types, then your calculation will be exact.
So you multiplied 0.3119999999999999999895916591441391574335284531116485595703125 by 1000 and truncated it and got 311? I don't see where the problem is.
Floating-point arithmetic is confusing, and not guaranteed to behave intuitively.
Here's a good reference document: What Every Computer Scientist Should Know About Floating-Point Arithmetic. It's a long document, because it's a complicated problem.
In summary: Don't use floating-point values if you are relying on exact values.