# GENERAL: ROUNDING PROBLEMS WITH FLOATING-POINT NUMBERS

• 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;

void main (void) {
fp1 = 2.34;
fp1 *= 100;

fp1 = 4.68;
fp1 *= 100;

fp1 = 1.17;
fp1 *= 100;
}
```

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

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
}
```

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

Last Reviewed: Sunday, March 20, 2005

Yes
No
Not Sure

##### Important information

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