Description of why floating point numbers may lose precision in Visual C++
This article was previously published under Q145889
The information in this article is included in the documentation starting
with Visual C++ 5.0. Look there for future revisions.
On This PageSUMMARY
Floating point decimal values generally do not have an exact binary
representation. This is a side effect of how the CPU represents floating
point data. For this reason, you may experience some loss of precision, and
some floating point operations may produce unexpected results. This behavior is the end result of one of the following:
MORE INFORMATION
Microsoft uses the Institute of Electrical and Electronics Engineers, Inc.
(IEEE) floating point format for floating point number representation. For
information about the actual binary representation of floating point values
in a CPU and how precision and accuracy are affected in a floating point
calculation, please see the following articles in the Microsoft Knowledge
Base:
36068 (http://support.microsoft.com/kb/36068/EN-US/) INFO: IEEE Floating-Point Representation and MS Languages
125056 (http://support.microsoft.com/kb/125056/EN-US/) INFO: Precision and Accuracy in Floating-Point Calculations
Sample CodeThe Output ResultThey are not equal. The value of c is 2.4679999352 or 2.468000.For EPSILON, you may use the constants FLT_EPSILON defined for float as 1.192092896e-07F or DBL_EPSILON defined for double as 2.2204460492503131 e-016. You need to include float.h for these constants. These constants are defined as the smallest positive number x, such that x+1.0 is not equal to 1.0. Because this is a very small number it is advisable that you employ user-defined tolerance for calculations involving very large numbers. REFERENCES
For information on other predefined constants, please see:
Microsoft Developer Network: C Floating-Point Constants Microsoft
APPLIES TO
| Article Translations
|
Back to the top
