Click here to Skip to main content
11,647,748 members (65,081 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
In this program float x value is 1.234567890000 but output is 1.234567880630. I did't understand this output anybody explain
#include<stdio.h>
#include<conio.h>

void main()
{
float x;
x = 1.234567890000;

printf("%.12lf\n",x);

getch();
}
Posted 3-Feb-12 15:42pm
Comments
Amir Mahfoozi at 4-Feb-12 0:59am
   
Change float to double and it produces what you wished :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please do some reading on the limitations of floating point values. Here's a starter[^]
  Permalink  
Comments
SAKryukov at 4-Feb-12 2:30am
   
Right, a 5. --SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

File this away for future reference: The "imprecision" is in producing the OUTPUT, the conversion of the binary / computer representation of the number into the string of characters that you display. This is true regardless of whether it is you printing the value or the debugger displaying it for you. Both processes need to take the binary value and convert it to a string of characters for your eyes. If you a computing a value and wish to use it in other compututations then always carry the binary value around, don't convert it to a string and then reconvert it to binary. The binary value is as precise as you are going to get, converting it back and forth only adds "imprecision" Computer binary representations (Base 2) and printed represetnations (in Base 10) are inherently incompatible and can only be approximated. You control the approximation with the format specifier for how many digits you want to see. PS - when I was in college, I had a Comp Sci instructor who told us that "Floating Point Numbers have a precision of about 6 significant digits" (single precision back then). Concerning format statements, I asked "What happens when I ask it to print 10 digits after the decimal point? How does it get the extra 4 digits?" His response - "It makes them up." That's as true today as it was 45 years ago. You asked a single precision floating point number to print 12 digits of precision. It made some of them up Smile | :)
  Permalink  
v2
Comments
SAKryukov at 4-Feb-12 2:31am
   
Nice explanation, a 5. --SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 jyo.net 500
1 F-ES Sitecore 410
2 OriginalGriff 322
3 DamithSL 315
4 CPallini 290
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795


Advertise | Privacy | Mobile
Web01 | 2.8.150804.3 | Last Updated 3 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100