1. float number division

Hi guys,

does anyone know how to lose the small noise getting added to float numbers?

here is the code:

Code:
```typedef unsigned long u32;

int main()
{
float width,left;
float temp1, temp2;
u32 x;

temp1 = 640.0;
temp2 = 800.0;

width = (temp1 / temp2);

left = -(width / 2);

x = (left + 0.5f) * 800;

printf("width = %f\n",width);
printf("left = %f\n",left);

printf("x = %d\n", x);

return 0;
}```
if you punch the numbers on your calculator, the answer is 80 which is correct.
This piece of code however, shows 79.

This is because the var "width" contains very small noise after the first division. It prints out 0.8 but the actual value is 0.800000012.

Is there any way to get rid of the 12 at the end?

2. If you wanted to "lose the noise" like your calculator does, you would probably have to do it the way your calculator does it: keep track of everything digit by digit, and perform all calculations digit by digit (as opposed to using binary).

Or, if you just want an integer back (since you are storing in an integer), you could just round.

3. You could just ask printf() to only print a few significant digits.
Code:
`printf("%.4f", width);`
I'm not sure if that's what you're asking.

4. Thanks for reply dwks but how it prints out was not my concern. I was working with pixel coords and the result was sometimes 1 pixel off just like in the example I posted.
I guess the best option is what tabstop suggested(rounding). Thanks to tabstop.
But was this always an underlying problem in float/double number operations? I never realised.

5. Originally Posted by tabstop
If you wanted to "lose the noise" like your calculator does, you would probably have to do it the way your calculator does it: keep track of everything digit by digit, and perform all calculations digit by digit (as opposed to using binary).

6. Originally Posted by brewbuck

Okay I should have specified base-10 digits.

7. First of all, for both calculators and computers, most floating point calculation are not EXACT. Calculators often "cheat" and calculate one more digit than they show, which is used to round off the number - you can prove this by typing in 0.333... with as many 3's as the calculator can use, and multiply by 3. Then try the same thing by calculating 1/3 and then multiply by 3. Some calculators are "more precise" in the second case and come up with 1.0, rather than 0.99999...

One solution for your calculation here would be to round the final number, e.g:
Code:
`x = ((left + 0.5f) * 800.0 + 0.5);`
Alternatively, you may get better results using "double" - float is about half the precision, and for most things, the calculation time for float or double is identical [at least in x86 processors, where the all intermediate calculations are done to 80-bit precision, and the result is only rounded down to 32 bits when it is actually stored as a float - when that happens depends on the optimization level, the complexity of the calculation and the compiler itself].

And finally, the 0.800000012 is probably more of a calculation artifact when translating the number into decimal, rather than the fact that it is "slightly larger than it should be".

--
Mats