The information in this article applies to:
SYMPTOMSYou might get incorrect rounding results when you use the printf() and _fcvt() functions. CAUSEIn the 16-bit compiler, the floating-point representation for a double data type is in 80 bits. The 32-bit compiler uses the Institute of Electrical and Electronics Engineers, Inc. (IEEE) floating-point specification of 64 bits. Because you cannot always get an exact representation of decimal floating-point numbers in binary form, the reduction in the number of bits affects the rounding result for some numbers. RESOLUTIONThe following code demonstrates this behavior. The results are shown for both Visual C++ 5.0 (32-bit compiler) and Visual C++ 1.52 (16-bit compiler). Sample Code
VC++ 1.52c (16-bit compiler) results:
VC++ 5.0 (32-bit compiler) results:
With Visual C++ 5.0, test cases 2, 5, and 6 are correct, while 1, 3, and 4
do not round as expected.
To work around this behavior, add a very small number to the variable used. In the example above, add 1e-10 to Value. Modify each assignment, as shown in the following example:
STATUSThis behavior is by design. MORE INFORMATIONBy adding the small number, you offset the rounding error that is caused by inexact representation of some decimal floating-point numbers in binary. You can make this number even smaller, such as equal to or greater than 1e- 15. REFERENCESFor more information on using floating-point numbers, please see the following article in the Microsoft Knowledge Base: Q145889 INFO: Why Floating Point Numbers May Lose Precision Additional query words:
Keywords : kbcode kbCompiler kbVC200 kbVC210 kbVC220 kbVC400 kbVC410 kbVC420 kbVC500 kbVC600 |
Last Reviewed: July 24, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |