Format double precision
-
I had follow code :
double dbl = 24.000560700; CString sTemp,sDecimal; sTemp.Format(\_T("%0.16f"),dbl); SetWindowText(sTemp);
and result is 24.0005607000000010 ... not 24.000560700, why ?
because the number cannot be precisely represented in binary, and when it gets printed out, the value will be displayed with that "error". See this : http://support.microsoft.com/kb/42980[^]
Watched code never compiles.
-
I had follow code :
double dbl = 24.000560700; CString sTemp,sDecimal; sTemp.Format(\_T("%0.16f"),dbl); SetWindowText(sTemp);
and result is 24.0005607000000010 ... not 24.000560700, why ?
Why does this question keep coming up? ...please do a search before posting questions... I'm sure we've answered this about 10-20 times in a year's time frame...
-
I had follow code :
double dbl = 24.000560700; CString sTemp,sDecimal; sTemp.Format(\_T("%0.16f"),dbl); SetWindowText(sTemp);
and result is 24.0005607000000010 ... not 24.000560700, why ?
Because you asked for 16 digits, it gave you 16 digits. 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. 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. You have also made a basic assumption that is also flawed. You assume this program statement produces a "precise" value:
double dbl = 24.000560700;
when in fact, the compiler has to take this character representation of a number, convert it from base 10 to the binary representation of a floating point number, base 2, and put it into the object file to be included in the program. Because of the what I said above, conversion from text to binary form (compilation) or from binary form to text (printf) each provide some level of "imprecision" or "approximation" that varies in the least significant digits, as you've discovered.
-
I had follow code :
double dbl = 24.000560700; CString sTemp,sDecimal; sTemp.Format(\_T("%0.16f"),dbl); SetWindowText(sTemp);
and result is 24.0005607000000010 ... not 24.000560700, why ?
"%0.16f" means there will be 16 digits after the "." for result. "%0.9f" is what you need!
-
I had follow code :
double dbl = 24.000560700; CString sTemp,sDecimal; sTemp.Format(\_T("%0.16f"),dbl); SetWindowText(sTemp);
and result is 24.0005607000000010 ... not 24.000560700, why ?
Because the
double
data type is a convenient approximate representation (see here, it is quite formative)[^]) of real numbers. Computers memory is finite, it cannot map infinite sets like real numbers (or even natural numbers).Veni, vidi, vici.
-
I had follow code :
double dbl = 24.000560700; CString sTemp,sDecimal; sTemp.Format(\_T("%0.16f"),dbl); SetWindowText(sTemp);
and result is 24.0005607000000010 ... not 24.000560700, why ?
Hi, try doing sTemp.Format(_T("%0.08f"),dbl); regards, vatsa www.objectiveprogramming.com
-
Hi, try doing sTemp.Format(_T("%0.08f"),dbl); regards, vatsa www.objectiveprogramming.com
-
You might as well ask "How many angels can dance on the head of a pin?". You really need to read the article already linked http://support.microsoft.com/kb/42980[^] and embrace the idea that a "Binary Computer" cannot precisely represent every possible "Decimal Digit" that exists for floating point numbers. It is a fact of life. Rather than try to figure out how many "decimals exist", you just need to determine "how many you care about". 6, 12, 18 are a pretty common need, unless you are doing planetary orbital calculations or high energy physics.