Modular Megafauna Model 1.1.5
A physiological, dynamic herbivore simulator in C++.
Loading...
Searching...
No Matches
Fauna::ForageValues< tag > Class Template Reference

#include <forage_values.h>

Public Member Functions

 ForageValues (const double init_value=0.0)
 Constructor with initializing value. More...
 
ForageValues< tag > divide_safely (const ForageValues< tag > &divisor, const double na_value) const
 Divide safely also by zero values. More...
 
double get (const ForageType ft) const
 Get a value (read-only). More...
 
ForageValues< tag > & merge (const ForageValues< tag > &other, const double this_weight=1.0, const double other_weight=1.0)
 Merge this object with another one by building (weighted) means. More...
 
ForageValues< tag > & max (const ForageValues< tag > &other)
 For each forage type, take the maximum value. More...
 
ForageValues< tag > & min (const ForageValues< tag > &other)
 For each forage type, take the minimum value. More...
 
double operator[] (const ForageType ft) const
 Read-only value access. More...
 
double & operator[] (const ForageType ft)
 Write access to values. More...
 
void set (const ForageType forage_type, double value)
 Set a value, only finite values are allowed. More...
 
void set (double value)
 Set all forage types to one value. More...
 
double sum () const
 Sum of all values. More...
 
Operator overload.
ForageValues< tag > & operator+= (const double rhs)
 
ForageValues< tag > & operator-= (const double rhs)
 
ForageValues< tag > & operator*= (const double rhs)
 
ForageValues< tag > & operator/= (const double rhs)
 
ForageValues< tag > operator+ (const double rhs) const
 
ForageValues< tag > operator- (const double rhs) const
 
ForageValues< tag > operator* (const double rhs) const
 
ForageValues< tag > operator/ (const double rhs) const
 
ForageValues< tag > operator+ (const ForageValues< tag > &rhs) const
 
ForageValues< tag > operator- (const ForageValues< tag > &rhs) const
 
ForageValues< tag > operator* (const ForageValues< tag > &rhs) const
 
ForageValues< tag > operator/ (const ForageValues< tag > &rhs) const
 
ForageValues< tag > & operator+= (const ForageValues< tag > &rhs)
 
ForageValues< tag > & operator-= (const ForageValues< tag > &rhs)
 
ForageValues< tag > & operator*= (const ForageValues< tag > &rhs)
 
ForageValues< tag > & operator/= (const ForageValues< tag > &rhs)
 
bool operator== (const ForageValues< tag > &rhs) const
 
bool operator!= (const ForageValues< tag > &rhs) const
 
bool operator< (const ForageValues< tag > &rhs) const
 
bool operator<= (const ForageValues< tag > &rhs) const
 
bool operator> (const ForageValues< tag > &rhs) const
 
bool operator>= (const ForageValues< tag > &rhs) const
 

Static Public Attributes

static constexpr const double IMPRECISION_TOLERANCE = 1e-3
 Tolerance range for imprecise floating point results. More...
 

Private Member Functions

void check_value (double &value) const
 Helper function to throw exceptions in the set() functions. More...
 

Private Attributes

std::array< double, 1 > array
 Forage values for all but ForageType::Inedible. More...
 

Detailed Description

template<ForageValueTag tag>
class Fauna::ForageValues< tag >

Multi-purpose template class for double values mapped by edible(!) forage type. The forage type ForageType::Inedible is excluded from all operations.

Template Parameters
tagDefines the allowed data range.
Note
Operators that take a number as argument will interpret that as a ForageValues object where all forage type values are that number.
Warning
It is important to understand and use the binary comparison operators correctly. Be F1 and F2 ForageValues objects. F1>F2 then means that each value in F1 (one for each forage type) is greater than the corresponding value in F2. In the same way, F1==F2 means that all corresponding values are identical. F1!=F2 means then that each pair of values is not equal. If one value pair, is identical, the result is false! In the same logic, F1==d is true if all values in F1 are equal to the double value d. Now, F1!=d is true only if all values in F1 are not equal to d. If only one value of F1 matches d, the result will be false! To check if not all values in F1 are equal to d, you would use !(F1==d).

Constructor & Destructor Documentation

◆ ForageValues()

template<ForageValueTag tag>
Fauna::ForageValues< tag >::ForageValues ( const double  init_value = 0.0)
inline

Constructor with initializing value.

Exceptions
std::invalid_argumentIf init_value is not allowed by given tag.
std::logic_errorIf tag is not implemented.

Member Function Documentation

◆ check_value()

template<ForageValueTag tag>
void Fauna::ForageValues< tag >::check_value ( double &  value) const
inlineprivate

Helper function to throw exceptions in the set() functions.

Parameters
valueThe value to check. It is passed as reference so that it can be corrected with IMPRECISION_TOLERANCE.

◆ divide_safely()

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::divide_safely ( const ForageValues< tag > &  divisor,
const double  na_value 
) const
inline

Divide safely also by zero values.

Parameters
divisorNumbers to divide by; can contain zeros.
na_valueValue that shall be set as a result for division by zero.
Returns
New object with the division result.
See also
operator/()

◆ get()

template<ForageValueTag tag>
double Fauna::ForageValues< tag >::get ( const ForageType  ft) const
inline

Get a value (read-only).

Exceptions
std::invalid_argumentIf ForageType::Inedible is passed.

◆ max()

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::max ( const ForageValues< tag > &  other)
inline

For each forage type, take the maximum value.

Parameters
otherThe object to compare this object with.
Returns
This object.

◆ merge()

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::merge ( const ForageValues< tag > &  other,
const double  this_weight = 1.0,
const double  other_weight = 1.0 
)
inline

Merge this object with another one by building (weighted) means.

Parameters
otherOther object to merge into this one.
this_weightWeight of this object’s values.
other_weightWeight of the other object’s values.
Returns
This object.
Exceptions
std::invalid_argumentThe same as average().
See also
Fauna::average().

◆ min()

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::min ( const ForageValues< tag > &  other)
inline

For each forage type, take the minimum value.

Parameters
otherThe object to compare this object with.
Returns
This object.

◆ operator!=()

template<ForageValueTag tag>
bool Fauna::ForageValues< tag >::operator!= ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator*() [1/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator* ( const double  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator*() [2/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator* ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator*=() [1/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator*= ( const double  rhs)
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator*=() [2/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator*= ( const ForageValues< tag > &  rhs)
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator+() [1/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator+ ( const double  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator+() [2/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator+ ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator+=() [1/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator+= ( const double  rhs)
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator+=() [2/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator+= ( const ForageValues< tag > &  rhs)
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator-() [1/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator- ( const double  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator-() [2/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator- ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator-=() [1/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator-= ( const double  rhs)
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator-=() [2/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator-= ( const ForageValues< tag > &  rhs)
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator/() [1/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator/ ( const double  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator/() [2/2]

template<ForageValueTag tag>
ForageValues< tag > Fauna::ForageValues< tag >::operator/ ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorOn division by zero.

◆ operator/=() [1/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator/= ( const double  rhs)
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator/=() [2/2]

template<ForageValueTag tag>
ForageValues< tag > & Fauna::ForageValues< tag >::operator/= ( const ForageValues< tag > &  rhs)
inline
Exceptions
std::domain_errorOn division by zero.

◆ operator<()

template<ForageValueTag tag>
bool Fauna::ForageValues< tag >::operator< ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator<=()

template<ForageValueTag tag>
bool Fauna::ForageValues< tag >::operator<= ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator==()

template<ForageValueTag tag>
bool Fauna::ForageValues< tag >::operator== ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator>()

template<ForageValueTag tag>
bool Fauna::ForageValues< tag >::operator> ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator>=()

template<ForageValueTag tag>
bool Fauna::ForageValues< tag >::operator>= ( const ForageValues< tag > &  rhs) const
inline
Exceptions
std::domain_errorIf rhs==0.0.

◆ operator[]() [1/2]

template<ForageValueTag tag>
double & Fauna::ForageValues< tag >::operator[] ( const ForageType  ft)
inline

Write access to values.

◆ operator[]() [2/2]

template<ForageValueTag tag>
double Fauna::ForageValues< tag >::operator[] ( const ForageType  ft) const
inline

Read-only value access.

◆ set() [1/2]

template<ForageValueTag tag>
void Fauna::ForageValues< tag >::set ( const ForageType  forage_type,
double  value 
)
inline

Set a value, only finite values are allowed.

Exceptions
std::invalid_argumentIf value is not allowed by given tag, is NAN or is INFINITY.
std::invalid_argumentIf forage_type==ForageType::Inedible.
std::logic_errorIf tag is not implemented.

◆ set() [2/2]

template<ForageValueTag tag>
void Fauna::ForageValues< tag >::set ( double  value)
inline

Set all forage types to one value.

Exceptions
std::invalid_argumentIf value is not allowed by given tag, is NAN or is INFINITY.
std::invalid_argumentIf forage_type==ForageType::Inedible.
std::logic_errorIf tag is not implemented.

◆ sum()

template<ForageValueTag tag>
double Fauna::ForageValues< tag >::sum ( ) const
inline

Sum of all values.

Member Data Documentation

◆ array

template<ForageValueTag tag>
std::array<double, 1> Fauna::ForageValues< tag >::array
private

Forage values for all but ForageType::Inedible.

◆ IMPRECISION_TOLERANCE

template<ForageValueTag tag>
constexpr const double Fauna::ForageValues< tag >::IMPRECISION_TOLERANCE = 1e-3
staticconstexpr

Tolerance range for imprecise floating point results.

For example, if no negative values are allowed, a value only slightly below zero could result from an imprecise floating point calculation. This must be corrected. So a value barely below zero will be corrected to actual zero.

Setting the tolerance is an arbitrary decision. In general, ecologically significant numbers throughout the program should be above 1.0. Therefore it is important to choose your units carefully. If you encounter errors that are certainly coming from rounding imprecision, you may try to increase this tolerance value.

See also
check_value()

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