From 2ca04fbe7985ee944f3fa6302886a252a51add0c Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Sun, 24 Apr 2016 23:52:45 +0200 Subject: initial commit --- stats.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 stats.h (limited to 'stats.h') diff --git a/stats.h b/stats.h new file mode 100644 index 0000000..8796161 --- /dev/null +++ b/stats.h @@ -0,0 +1,35 @@ +#pragma once +#ifndef STATS_H +#define STATS_H + +#include + +struct stats_running { + unsigned n; + double S; + double m; +}; +#define STATS_RUNNING_INIT {0, NAN, NAN} + +void stats_running_reset(struct stats_running *s); +void stats_running_push(struct stats_running *s, double value); + +static inline +unsigned stats_running_N(struct stats_running const *s) +{ + return s ? s->n : 0; +} + +static inline +double stats_running_mean(struct stats_running const *s) +{ + return s && (0 < s->n) ? s->m : NAN; +} + +static inline +double stats_running_variance(struct stats_running const *s) +{ + return s && (1 < s->n) ? s->S/s->n : NAN; +} + +#endif/*STATS_H*/ -- cgit v1.2.3