Reputation: 832
I am trying to understand tthe associativity of operations when it comes to floating points. In the lecture notes i have, the following is stated:
"suppose floating-point values store seven digit of accuracy. Considee the problem of adding 11 numbers together, where one of the numbers is 10^7 and the other ten are 1.
If the small numbers (the 1s) are each added to the large number, one at a time, there is no effect on that number, because the small numbers occur in the eighth digit of the large number ". So here I understood that the result is 1,000,001.
"however, if the small numbers are first added together and the result is added to the large number, the result is a seven-digit accurancy 1.000001 * 10^7"
But both approaches seemed the same to me: we are adding the 10 numbers to the larger number. Can someone please clarify this problem ? Thank you
Upvotes: 1
Views: 35
Reputation: 91
Let's go over the first method first. When the small numbers are added one by one to the large number, the following will happen ten times:
10,000,000 + 1 = 10,000,001
However since the floating-point values store only seven digits of accuracy this last digit, the eight digit, will be rounded in the seventh digit to zero. This operation will happen 10 times, and so the value will remain 10,000,000.
Next let's go over the second method. The 10 number 1's are added together first and so this will sum up to 10. When this is added to 10^7 the following will happen:
10,000,000 + 10 = 10,000,010
Since the floating-point values store seven digits of accuracy this number will remain!
Upvotes: 3