The information in this article applies to:
SYMPTOMSType conversion functions, such as CInt, may sometimes return unexpected results. For example, CInt(2.5) should return 2 according to the documentation. However, CInt(25.0 * 0.1) will sometimes result in 3. CAUSEThis is caused by the way floating point values are handled in computers. Because most floating point values cannot be accurately represented with fixed length binary values, the internal result of the floating point calculation may differ slightly. For example, 25.0 * 0.1 may be 02.5000... 001 or 02.4999...999 depending on many factors. This rounding error causes the CInt function to return 2 or 3 in different situations. RESOLUTIONThis is expected behavior and there is no fix for this. However, you can make these functions return the expected values more often by using wrapping functions. For example, you can use the following function instead of using CInt:
This works by forcing the expression to be evaluated and stored into an
intermediate variable. This forces rounding, which often discards the small
difference between the correct value and the real value in the computer.
This method can also be used for CLng, etc.
STATUSThis behavior is by design. MORE INFORMATIONSteps to Reproduce Behavior
REFERENCESFor more information regarding precision and accuracy in floating point calculations, please see the following articles in the Microsoft Knowledge Base: Q125056 INFO: Precision and Accuracy in Floating-Point Calculations Q145889 INFO: Why Floating Point Numbers May Lose Precision Additional query words:
Keywords : kbFloatPoint kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpVB kbCodeSam |
Last Reviewed: January 5, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |