aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index 2f02595..bff0ba9 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -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