better stats functions

This commit is contained in:
2021-11-12 21:23:50 +01:00
parent edcffa34c3
commit 0e7e75d3f5
3 changed files with 37 additions and 85 deletions

View File

@ -73,4 +73,32 @@ constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
}
template <typename ForwardIt>
constexpr typename ForwardIt::value_type average(const ForwardIt first,
const ForwardIt last) {
typedef typename ForwardIt::value_type value_type;
if (first == last) return 0;
value_type average = 0;
for (ForwardIt it = first; it != last; ++it) {
average += *it;
}
return average / (last - first);
}
template <typename ForwardIt>
constexpr typename ForwardIt::value_type
standard_deviation(const ForwardIt first,
const ForwardIt last,
typename ForwardIt::value_type average) {
typedef typename ForwardIt::value_type value_type;
if (first == last) return 0;
value_type accumulator = 0;
for (ForwardIt it = first; it != last; ++it) {
accumulator += (*it - average) * (*it - average);
}
return sqrt(accumulator / (last - first));
}
#endif