Re: C sucks at math? (help)



I said I wouldn't try doubles in the real project, but it didn't prove
that difficult and I couldn't let it rest. Anyway, it didn't work as
well as I hoped. This is some of the output I got:
x = 64.200000 / (107.000000 / 5) = 64.200000 / 21.400000 = 3.000000 = 2

Notice the 3.000000 = 2? That's what's screwing my program over.

Anyway, as I understand from you guys, I need infinite bits to get 100%
accuracy. That's probably not going to happen anytime soon, so I need
another solution. Is there a way to make the program use the calculated
3.000000 instead of the 2.9999999999999999999999999 it actually
calculated? Is there some way to specify how accurate I need things?

Thanks for any help!



Ian Collins wrote:
Jordi wrote:
I have made a little C program that generates the following output:
227.000000 / 5 = 45.400002

Here's the code:
int main (int argc, char* argv[]) {
float var = 227;
printf("%f / 5 = %f\n", var, var / 5);
return 0;
}

Obviously, the output should be 45.4 (or 45.400000), but for some
mysterious reason C decided to add 0.000002* to the result. The error
might seem minor and this program is obviously trivial, but I need to
do similar calculations in a much bigger project and the problem I'm
encountering is caused by exactly this error.

* It seems that it is not .000002 is added, but rather
..0000015258789076710855... (which I found out after subtracting 45.4
from the result and then multiplying it with 10,000,000,000,000,000)

Why does C do this and what can I do about it?

It doesn't, the floating point hardware or emulation does.

Floating point isn't precise. Avoid floats and use doubles, even then,
you have to understand the limitations of floating point math.

You must have a dodgy FPU, the result is 45.400000 on my system :)

--
Ian Collins.

.



Relevant Pages

  • Re: Arrays. Beginners question!
    ... If you are using Doubles, and if you are using the same base for the array and not accidentally failing to account for an element, then the results should be approximately the same, because the Array function uses Doubles in the Variant array, although with floating point data you always need to be prepared to accept the fact that decimal values (and calculations performed on them) are almost never held in any floating point variable with absolute accuracy because the binary number system simply cannot do so, especially with the limited number of bits which the IEEE floating point data has to work with and the vast range of values that it needs to encompass. ...
    (microsoft.public.vb.general.discussion)
  • Re: int64 or double
    ... To address the question of why one would use a Floating Point value... ... Doubles make sense. ... -4.94E-3 equates to -0.00494 ... you can see that we have ranges of: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Floating point issue in sbcl 1.0.10?
    ... If you ensure that your floating point numbers are ... doubles, either by explicitly writing them that way, or by altering ... CL-USER> ... question leads off to all kinds of tangents. ...
    (comp.lang.lisp)
  • Re: C sucks at math? (help)
    ... I had just figured out that the emulation/casting was where it went ... It seems that doubles work for the toy-program, ... the floating point hardware or emulation does. ...
    (comp.lang.c)
  • Re: Java rounding with big Decimal.
    ... That is, more or less, what I meant by "inherently inaccurate". ... less error in calculations involving them than you might with ... I still say BigDecimals are better than doubles, ...
    (comp.lang.java.programmer)