views:

282

answers:

4

Hello everyone,

Here is a problem that has had me completely baffled for the past few hours...

I have an equation hard coded in my program:

double s2;

s2 = -(0*13)/84+6/42-0/84+24/12+(6*13)/42;

Every time i run the program, the computer spits out 3 as the answer, however doing the math by hand, i get 4. Even further, after inputting the equation into Matlab, I also get the answer 4. Whats going on here?

The only thing i can think of that is going wrong here would be round off error. However with a maximum of 5 rounding errors, coupled with using double precision math, my maximum error would be very very small so i doubt that is the problem.

Anyone able to offer any solutions?

Thanks in advance,

-Faken

+7  A: 

You're confusing integer division with floating point division. 3 is the correct answer with integer division. You'll get 4 if you convert those values to floating point numbers.

duffymo
+13  A: 

You're not actually doing floating point math there, you're doing integer math, which will floor the results of divisions.

In C++, 5/4 = 1, not 1.25 - because 5 and 4 are both integers, so the result will be an integer, and thus the fractional part of the result is thrown away.

On the other hand, 5.0/4.0 will equal approx. 1.25 because at least one of 5.0 and 4.0 is a floating-point number so the result will also be floating point.

Amber
Hmm, thanks for your help. Should have come here earlier, oh well.
Faken
1.25 is representable in base 2 as are 4.0 and 5.0. It should be exact.
dmckee
Since it was an arbitrary example dmckee, I decided to go with the safer statement. :)
Amber
+3  A: 

Some of this is being evaluated using integer arithmetic. Try adding a decimal place to your numbers, e.g. 6.0 instead 6 to tell the compiler that you don't want integer arithmetic.

ChrisW
affixing a `f` to them should do the trick as well...
Shog9
Actually, that would be a `.f`, not just `f`.
GMan
+3  A: 
s2 = -(0*13)/84+6/42-0/84+24/12+(6*13)/42;

yields 3

s2 = -(0.*13.)/84.+6./42.-0./84.+24./12.+(6.*13.)/42.;

does what you are expecting.

ezpz