diff options
Diffstat (limited to 'src/misc.c')
-rw-r--r-- | src/misc.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -2,6 +2,26 @@ #include <math.h> #include <stdlib.h> /* for size_t */ +double kahan_sum(double *x, size_t n) +{ + /* Returns the sum of the elements of `x` using the Kahan summation algorithm. + * + * See https://en.wikipedia.org/wiki/Kahan_summation_algorithm. */ + size_t i; + double sum, c, y, t; + + sum = 0.0; + c = 0.0; + for (i = 0; i < n; i++) { + y = x[i] - c; + t = sum + y; + c = (t - sum) - y; + sum = t; + } + + return sum; +} + double interp1d(double x, double *xp, double *yp, size_t n) { /* A fast interpolation routine which assumes that the values in `xp` are |