Microsoft KB Archive/59407

= PRB: Inaccurate Representation of Large Double Values =

Article ID: 59407

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft C Professional Development System 6.0a
 * Microsoft C Professional Development System 6.0a
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5

-



This article was previously published under Q59407





SYMPTOMS
In Microsoft C, subtracting double values greater than or equal to 1.0E+025 may return inaccurate results.



CAUSE
This is expected behavior and is due to the imprecise nature of floating-point math. Anytime floating-point math uses large numbers, there will be rounding/truncation errors and errors introduced due to imprecise representation of a result in binary format.



MORE INFORMATION
Because double values are only 15-digit precise, simple subtraction of two large numbers can give unexpected results. The following sample code demonstrates this behavior.

Double values less than 1.0E+25 may not experience the same problem.

Sample Code

 * 1) include 

double a = 1E+28, tmp = 9E+28;

void main (void) {

printf (&quot;a = %le   tmp = %le\n&quot;, a, tmp);

while (tmp >= 1E+25) { tmp -= a;     printf (&quot;a = %le    tmp = %le\n&quot;, a, tmp); } } The above sample code produces the following output: a = 1.000000e+028   tmp = 9.000000e+028 a = 1.000000e+028   tmp = 8.000000e+028 a = 1.000000e+028   tmp = 7.000000e+028 a = 1.000000e+028   tmp = 6.000000e+028 a = 1.000000e+028   tmp = 5.000000e+028 a = 1.000000e+028   tmp = 4.000000e+028 a = 1.000000e+028   tmp = 3.000000e+028 a = 1.000000e+028   tmp = 2.000000e+028 a = 1.000000e+028   tmp = 1.000000e+028 a = 1.000000e+028   tmp = 1.319414e+013

Additional query words: 8.00 8.00c 9.00

Keywords: kbprb kbcompiler KB59407

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.