X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/blobdiff_plain/825abbb3bde6c35ab1d1cdfba25fba4be5dcea2f..62556b444506565f9a55889f482d8758ca50c3a6:/statistics.h?ds=inline diff --git a/statistics.h b/statistics.h index 2978747..7e3fb2e 100644 --- a/statistics.h +++ b/statistics.h @@ -1,29 +1,37 @@ #ifndef STATISTICS_H #define STATISTICS_H +#include #include class statistics { public: - statistics(); - - void reset(); - void push(double value); - - size_t get_count(); - double get_sum(); - double get_avg(); - double get_stddev(); + statistics() + : count(0) + , sum(0.0) + , mean(0.0) + , sqdiff_sum(0.0) + { } + + void push(double x) { + double delta = x - mean; + ++count; + sum += x; + mean = sum / count; + sqdiff_sum += delta * (x - mean); + } + + unsigned get_count() const { return count; } + double get_sum() const { return sum; } + double get_mean() const { return mean; } + double get_variance() const { return sqdiff_sum / count; } + double get_stddev() const { return sqrt(get_variance()); } private: - bool up2date; - - std::vector values; - double sum; - double avg; - double stddev; - - void update(); + int count; + double sum; // sum of x_i + double mean; // mean of x_i + double sqdiff_sum; // sum of (x_i - mean)^2 }; #endif // !STATISTICS_H