Skip to content

Instantly share code, notes, and snippets.

@marii-moe
Created October 12, 2023 03:24
Show Gist options
  • Save marii-moe/c168f287b75438c468c7da4c9d653b18 to your computer and use it in GitHub Desktop.
Save marii-moe/c168f287b75438c468c7da4c9d653b18 to your computer and use it in GitHub Desktop.
def kahan_sum(xs):
sum=np.array(0.,dtype=np.float16)
prev_sum=np.array(0.,dtype=np.float16)
ε=np.array(0.,dtype=np.float16)
for x in xs:
v=x-ε
prev_sum=sum
sum+=v #prev_sum+x-ε+new_ε
ε=sum-prev_sum-v #(prev_sum+x-ε+new_ε)-(prev_sum)-(x-ε)=new_ε
return sum
@marii-moe
Copy link
Author

marii-moe commented Oct 12, 2023

new errors are generated on adding sum and a new x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment