Skip to content

Instantly share code, notes, and snippets.

@jamboree
Created September 25, 2014 07:46
Show Gist options
  • Save jamboree/0bed60f1fe5915d40253 to your computer and use it in GitHub Desktop.
Save jamboree/0bed60f1fe5915d40253 to your computer and use it in GitHub Desktop.
#include <iostream>
// Kahan summation algorithm
template<class T>
struct accum
{
T t, y;
accum(T val = 0): t(val), y() {}
void operator()(T val)
{
y -= val;
val = t - y;
y += (val - t);
t = val;
}
};
int main(int argc, char** argv)
{
accum<float> sum(6);
sum(1);
sum(2);
sum(3);
std::cout << sum.t;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment