// // Math.hpp // amoeba // // Created by Timothy Prepscius on 12/30/16. // Copyright © 2016 Timothy Prepscius. All rights reserved. // #pragma once namespace tjp::core::math { template struct Mean_SumDivide { T t = zero(); size_t count = 0; void add(const T &v) { t = t + v; count++; } T mean() const { return t / count; } }; // https://www.geeksforgeeks.org/program-for-average-of-an-array-without-running-into-overflow/ template struct Mean_AddDivide { T t = zero(); size_t count = 0; void add(const T &v) { ++count; t = t + (v - t) / (count); } T mean() const { return t / count; } } ; // https://www.geeksforgeeks.org/compute-average-two-numbers-without-overflow/ template struct Mean_AddDivideMod { T t = zero(); size_t count = 0; void add(const T &v) { ++count; t = t + (v - t) / (count); } T mean() const { return t / count; } } ; template auto mean_(const U &u) { T t; for (auto &v: u) t.add(v); return t.mean(); } template auto mean(const U &u) { return mean_>(u); } } // namespace