Created
December 19, 2020 02:47
-
-
Save zethon/661da71b6d066439c525e16e4f89ddb6 to your computer and use it in GitHub Desktop.
C++ Cumulative Moving Average Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename NumberT> | |
class CumulativeMovingAverage | |
{ | |
NumberT _total = 0; | |
std::size_t _count = 0; | |
public: | |
float value() const | |
{ | |
// TODO: pretty sure only one cast is need, but not 100% sure | |
return (static_cast<float>(_total) / static_cast<float>(_count)); | |
} | |
void addValue(NumberT v) | |
{ | |
_total += v; | |
_count++; | |
} | |
void reset() | |
{ | |
_total = 0; | |
_count = 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment