pythonroundingnumerical-computing

Rounding errors in python


why the order of multiplications can impact the results? Consider the following code

a=47.215419672114173
b=-0.45000000000000007
c=-0.91006620964286644
result1=a*b*c
temp=b*c
result2=a*temp
result1==result2

We all know that result1 should be equal to result2, however we get:

result1==result2 #FALSE!

the difference is minimal

result1-result2 #3.552713678800501e-15

However, for particular applications this error can amplify so that the output of two programs doing the same computations (one using result1 and the other result2) can be completely different.

Why is this so and what can be done to address such issues in heavily numerical/scientific applications?

Thanks!

UPDATE

Good answers, but I still miss the reason why the order of multiplication matters, e.g.

temp2=a*b
result3=temp2*c
result1==result3 #True

So it seems that the compiler/interpreter sees a*b*c as (a*b)*c


Solution

  • Each multiplication results in twice as many digits (or bits) as the original numbers and needs to be rounded so that it will fit back into the space allocated for a floating point number. This rounding can potentially change the results when you rearrange the order.