|
|||||||||||
Technical Support Support Resources
Product Information |
GENERAL: Rounding Problems with Floating-point NumbersInformation in this article applies to:
QUESTIONI have seen a strange problem with floating-point to integer conversion. After multiplying the floating-point number by 100, the wrong value is copied into the integer variable. For example:
In these cases, I have noticed that the floating-point number ends with .9999999. Is there a solution to this problem? ANSWERFloating-point operations are not 100% accurate because some numbers cannot be represented in the IEEE format without the loss of precision. Therefore, the results of multiplications can end with .999999. The ANSI Standard specifies the following for floating-point to integer cast operations:
This implies that the result of the conversion of 1.999999 to int will result in the value 1. If you need rounding you may simply add the value 0.5 to the result before converting it to an integer value. For example:
SEE ALSOLast Reviewed: Thursday, February 25, 2021 | ||||||||||
|
Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.