Created
September 25, 2014 07:46
-
-
Save jamboree/0bed60f1fe5915d40253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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