Talk:Kahan summation: Difference between revisions

→‎So far, in J and R: bigger changes
(→‎So far, in J and R: bigger changes)
Line 60:
 
Any ideas for a save? --[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 06:48, 13 December 2014 (UTC)
 
:I have the same concerns. How about something like this,
<lang python>def kahan(vals, start = 0):
tot = start
c = 0
for x in vals:
y = x - c
t = tot + y
c = (t - tot) - y
tot = t
return tot
 
def triangle(n):
return n * (n + 1) / 2
 
bigVal = 1e20
numSmall = 54321
 
print("Sequential: ", sum(range(numSmall), bigVal))
print("Kahan ", kahan(range(numSmall), bigVal))
print("Triangle: ", triangle(numSmall))</lang>
{{out}}
<pre>
Sequential: 1.0000000000146198e+20
Kahan 1.0000000000147536e+20
Triangle: 1475358360.0
</pre>
:The idea is to sum enough numbers to skip over the fine-point-erry, while still picking numbers where it's easy to see the correct answer. Adding just three numers is always going rely on doing stuff to the last bit. If you add lots of numbers you can get the result we're interested in to span multiple decimal places. Most people will accept that the triangle fomula should give a good anwer, triangle(54321) fits in a 32 bit int. For types other than 754 binary64, it should be easy to pick other parameters bigVal and numSmall that will still show the loss of precision in the sequential sum. &mdash;[[User:Sonia|Sonia]] ([[User talk:Sonia|talk]]) 03:26, 14 December 2014 (UTC)
1,707

edits