Jude said:
here is the source code:
#include<stdio.h>
int main()
{
float f;
scanf("%d%f",&f);
printf("The float is %10.5f\n",f);
return 0;
}
when I input 12345.11111,the output is 12345.11133.
what wrong with it?
I compile it with vc++6.0.
Is that really the source code you compiled? The format string for
scanf() expects a pointer to int ("%d") and a pointer to float ("%f"),
but you only give it a pointer to float.
Assuming you drop the "%d", the behavior you're seeing is
unsurprising. The value 12345.11111 cannot be represented exactly in
binary floating-point, and type float in particular is only guaranteed
to support 6 decimal digits (double supports at least 10). The
closest approximation in type float to 12345.11111 is probably
something like 12345.111328125.
See section 14 of the comp.lang.c FAQ, <
http://www.c-faq.com/>.
For a more advanced treatment, see David Goldberg's paper "What Every
Computer Scientist Should Know About Floating-Point Arithmetic".