473,513 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2298
"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
3784
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
7223
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
1376
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
3776
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
5434
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
12
43400
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
8756
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
2829
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
29
11052
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;
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.