Skip to content

Instantly share code, notes, and snippets.

@zethon
Created December 19, 2020 02:47
Show Gist options
  • Save zethon/661da71b6d066439c525e16e4f89ddb6 to your computer and use it in GitHub Desktop.
Save zethon/661da71b6d066439c525e16e4f89ddb6 to your computer and use it in GitHub Desktop.
C++ Cumulative Moving Average Class
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