Incorrect floating point math?

Here's a problem that over the past few hours has completely worried ...

I have an equation hardcoded in my program:

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

Each time I run the program, the computer spills out 3 as an answer, but doing the math manually, I get 4. Even further, after entering the equation in Matlab, I also get the answer 4. What happens here?

The only thing I can think of is what is going wrong is the rounding error. However, with a maximum of 5 rounding errors, combined with double precision math, my maximum error will be very small, so I doubt it is a problem.

Anyone who can offer any solutions?

Thanks in advance,

-Faken

+2
source share
4 answers

In fact, you are not doing math with floating point, you are doing integer math that fills in the results of the divisions.

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

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

+17
source

You are misleading integer division with floating point division. 3 - the correct answer with the whole division. You will get 4 if you convert these values ​​to floating point numbers.

+7
source

Some of them are evaluated using integer arithmetic. Try adding a decimal number to your numbers, for example. 6.0 instead of 6 tell the compiler that you do not want integer arithmetic.

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

gives 3

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

does what you expect.

+3
source

Source: https://habr.com/ru/post/1315202/


All Articles