Keil Logo

GENERAL: ROUNDING PROBLEMS WITH FLOATING-POINT NUMBERS


Information in this article applies to:

  • C166 All Versions
  • C251 All Versions
  • C51 All Versions

QUESTION

I 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:

float fp1;
int   bad;

void main (void) {
  fp1 = 2.34;
  fp1 *= 100;
  bad = fp1;  // bad is 233 but should be 234

  fp1 = 4.68;
  fp1 *= 100;
  bad = fp1;  // bad is 467 but should be 468

  fp1 = 1.17;
  fp1 *= 100;
  bad = fp1;  // bad is 116 but should be 117
}

In these cases, I have noticed that the floating-point number ends with .9999999. Is there a solution to this problem?

ANSWER

Floating-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:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero).

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:

float fp1;
int   good;

void main (void) {
  fp1 = 2.34;
  fp1 *= 100;
  good = fp1 + 0.5;  // gives correct result: 234
}

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Sunday, March 20, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

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.