472,951 Members | 2,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 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 2255
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: sugaray | last post by:
does the expression int a=printf("%d\n",a); implementation dependent ? I supposed it would produced the result 2, but i got 4206596 (result may vary on your machine), i wonder if the value produced...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
8
by: fxc123 | last post by:
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); }
10
by: Jude | last post by:
here is the source code: #include<stdio.h> int main() { float f; scanf("%f",&f); printf("The float is %10.5f\n",f); return 0; }
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
6
by: WeiWangJi | last post by:
int i=8; printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--); printf("%d\n", i); why the results is: 8 8 7 8 8 -8 7 How to explain?
1
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.