471,585 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

a simple problem about printf("%f",f)

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.

Feb 17 '06 #1
2 2131
"Jude" <ja*******@lianluo.com> writes:
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".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '06 #2
Jude wrote:
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.

All floating point types have limits on their precision. Interpreting
digits beyond that is pointless. Run the following with your
implementation and see what happens:

#include <stdio.h>
#include <float.h>

int main()
{
float xf;
double xd;
long double xld;
char *src[] = { "12345.11111", "12345.11133" };
size_t i, n = sizeof src / sizeof *src;
for (i = 0; i < n; i++) {
printf("input string is \"%s\"\n", src[i]);
sscanf(src[i], "%f", &xf);
printf("Read into a float: %.*g\n", FLT_DIG, xf);
sscanf(src[i], "%lf", &xd);
printf("Read into a double: %.*g\n", DBL_DIG, xd);
sscanf(src[i], "%Lf", &xld);
printf("Read into a long double: %.*Lg\n\n", LDBL_DIG, xld);
}
return 0;
}

input string is "12345.11111"
Read into a float: 12345.1
Read into a double: 12345.11111
Read into a long double: 12345.11111

input string is "12345.11133"
Read into a float: 12345.1
Read into a double: 12345.11133
Read into a long double: 12345.11133

Feb 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by sugaray | last post: by
8 posts views Thread by fxc123 | last post: by
10 posts views Thread by Jude | last post: by
19 posts views Thread by v4vijayakumar | last post: by
12 posts views Thread by Zero | last post: by
1 post views Thread by manchin2 | last post: by
29 posts views Thread by candy_init | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.