summaryrefslogtreecommitdiffstats
path: root/src/stats.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stats.h')
-rw-r--r--src/stats.h47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/stats.h b/src/stats.h
new file mode 100644
index 0000000..f9e5d7a
--- /dev/null
+++ b/src/stats.h
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright © 2010, Zed A. Shaw.
+ * Copyright © 2020 rsiddharth <s@ricketyspace.net>
+ */
+
+#ifndef stats_h
+#define stats_h
+
+#include <math.h>
+#include <stdlib.h>
+#include <bstrlib.h>
+#include <dbg.h>
+
+typedef struct Stats {
+ double sum;
+ double sumsq;
+ unsigned long n;
+ double min;
+ double max;
+} Stats;
+
+Stats *Stats_recreate(double sum, double sumsq, unsigned long n,
+ double min, double max);
+
+Stats *Stats_create();
+
+void Stats_sample(Stats *st, double s);
+
+char *Stats_dump(Stats *st);
+
+char *Stats_stringify(Stats *st);
+
+Stats *Stats_unstringify(char *st_str);
+
+static inline double Stats_mean(Stats *st)
+{
+ return st->sum / st->n;
+}
+
+static inline double Stats_stddev(Stats *st)
+{
+ return sqrt((st->sumsq - (st->sum * st->sum / st->n)) /
+ (st->n - 1));
+}
+
+#endif