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
All programming languages lose precision when converting floating point numbers from decimal representation to binary representation. This results in inaccurate calculations (at least from a base 10 perspective, since the math is actually being done on floating point values represented in binary), including cases where order of operations changes the result. Most languages provide a datastructure to maintain base 10 precision, at the cost of performance. Look at Decimal in Python.
Edit:
In answer to your update, not exactly. Computers do things in order, so when you provide them a sequence of operations, they proceed 1 by 1 through the sequence. There's no explicit order of operations thing going on beyond sequential command processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With