Modular Megafauna Model 1.1.5
A physiological, dynamic herbivore simulator in C++.
Loading...
Searching...
No Matches
Fauna::PeriodAverage Class Reference

Average of a double value over a given time period. More...

#include <average.h>

Public Member Functions

 PeriodAverage (const int count)
 Constructor. More...
 
void add_value (const double)
 Add a value to the record. More...
 
double get_average () const
 Get arithmetic mean over all so-far recorded values. More...
 
double get_first () const
 Get first (oldest) value in the record. More...
 

Private Attributes

std::vector< double > values
 
unsigned int count
 
unsigned int current_index = 0
 

Detailed Description

Average of a double value over a given time period.

This helper class successively takes double values and stores/records them up to a given count. At any time, the average (arithmetic mean) over the stored values can be queried with get_average().

Use this to keep track of for instance the average body condition of the last month or the average phenology of the last year. In the first case, you would create the object with count==30 and call add_value() exactly once every day. In the second scenario, count would equal 365.

Constructor & Destructor Documentation

◆ PeriodAverage()

PeriodAverage::PeriodAverage ( const int  count)

Constructor.

Parameters
countNumber of values to remember and use for average.
Exceptions
std::invalid_argumentIf count<=0.

Member Function Documentation

◆ add_value()

void PeriodAverage::add_value ( const double  v)

Add a value to the record.

◆ get_average()

double PeriodAverage::get_average ( ) const

Get arithmetic mean over all so-far recorded values.

Exceptions
std::logic_errorIf no values were added yet.

◆ get_first()

double PeriodAverage::get_first ( ) const

Get first (oldest) value in the record.

Exceptions
std::logic_errorIf no values were added yet.

Member Data Documentation

◆ count

unsigned int Fauna::PeriodAverage::count
private

◆ current_index

unsigned int Fauna::PeriodAverage::current_index = 0
private

◆ values

std::vector<double> Fauna::PeriodAverage::values
private

The documentation for this class was generated from the following files: