Skip to content

Instantly share code, notes, and snippets.

@shane5ul
Created May 12, 2014 19:19
Show Gist options
  • Save shane5ul/8d4dac90d4d81b50ee43 to your computer and use it in GitHub Desktop.
Save shane5ul/8d4dac90d4d81b50ee43 to your computer and use it in GitHub Desktop.
An Octave program to compute a compensated sum.
function s = KahanSum(x)
s = 0; % the sum
r = 0; % the error or remainder
for i = 1:length(x)
tmp = s;
y = x(i) + r;
s = tmp + y; % s = s + x_i
r = (tmp - s) + y;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment