Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Stats.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_STATS_HXX_
36#define _UTILS_STATS_HXX_
37
38#include <math.h>
39#include <stdint.h>
40#include <string>
41#include <limits>
42
43class Stats
44{
45public:
46 using FloatType = double;
47 using ValueType = int32_t;
48
49 Stats()
50 {
51 clear();
52 }
53
54 // Clear the statistics (erase all data points).
55 void clear()
56 {
57 sum_ = 0;
58 count_ = 0;
59 qsum_ = 0;
60 max_ = std::numeric_limits<ValueType>::min();
61 }
62
65 void add(ValueType value)
66 {
67 ++count_;
68 sum_ += value;
69 FloatType fval = value;
70 qsum_ += fval * fval;
71 if (value > max_)
72 {
73 max_ = value;
74 }
75 }
76
78 FloatType favg()
79 {
80 return FloatType(sum_) / count_;
81 }
82
84 int32_t avg()
85 {
86 return sum_ / count_;
87 }
88
90 FloatType stddev()
91 {
92 // Formula: sqrt(N*qsum - sum^2) / N
93 FloatType sum(sum_);
94 return sqrt(qsum_ * count_ - sum * sum) / count_;
95 }
96
98 std::string debug_string();
99
100private:
102 uint32_t count_;
104 ValueType max_;
106 int64_t sum_;
108 FloatType qsum_;
109};
110
111#endif // _UTILS_STATS_HXX_
void add(ValueType value)
Appends a data point to the statistics.
Definition Stats.hxx:65
FloatType favg()
Definition Stats.hxx:78
uint32_t count_
Number of samples added.
Definition Stats.hxx:102
FloatType qsum_
Sum of squares of sample values added.
Definition Stats.hxx:108
FloatType stddev()
Definition Stats.hxx:90
int32_t avg()
Definition Stats.hxx:84
ValueType max_
Maximum value found since the last clear.
Definition Stats.hxx:104
std::string debug_string()
Creates a half-a-line printout of this stats object for debug purposes.
Definition Stats.cxx:39
int64_t sum_
Sum of sample values added.
Definition Stats.hxx:106