A little houskeeping.
Increasing counter sizes from int to long for averages. Rename random functions to end with _co instead of _exclusive to indicate range [close, open[, and to allow future suffixes oc, cc and oo.
This commit is contained in:
@ -34,7 +34,7 @@ public:
|
||||
* @param ave The average value
|
||||
* @param num_samples How many samples were taken to construct it
|
||||
*/
|
||||
void add(double ave, int num_samples = 1);
|
||||
void add(double ave, long num_samples = 1);
|
||||
|
||||
/**
|
||||
* Get the average value
|
||||
@ -48,7 +48,7 @@ public:
|
||||
*
|
||||
* @return Number of collected samples
|
||||
*/
|
||||
int num_samples() const;
|
||||
long num_samples() const;
|
||||
|
||||
/**
|
||||
* Reset the average value
|
||||
@ -60,8 +60,8 @@ public:
|
||||
CumulativeAverage& operator+=(const CumulativeAverage& rhs);
|
||||
private:
|
||||
double m_ave = 0;
|
||||
int m_num_samples = 0;
|
||||
int m_num_last_added = 0;
|
||||
long m_num_samples = 0;
|
||||
long m_num_last_added = 0;
|
||||
};
|
||||
|
||||
CumulativeAverage operator+(const CumulativeAverage& rhs, const CumulativeAverage& lhs);
|
||||
@ -81,7 +81,7 @@ public:
|
||||
* @param max_alpha The extra alpha value
|
||||
* @param sample_max Maximum number of samples to use
|
||||
*/
|
||||
EMAverage(double min_alpha, double max_alpha, int sample_max);
|
||||
EMAverage(double min_alpha, double max_alpha, long sample_max);
|
||||
|
||||
/**
|
||||
* Add a new value to the average made of `num_samples` samples
|
||||
@ -101,7 +101,7 @@ public:
|
||||
* @param ave Value to add
|
||||
* @param num_samples Number of samples the value consists of
|
||||
*/
|
||||
void add(double ave, int num_samples = 1);
|
||||
void add(double ave, long num_samples = 1);
|
||||
|
||||
/**
|
||||
* Add a CumulativeAverage
|
||||
@ -124,21 +124,21 @@ public:
|
||||
*
|
||||
* @return The number of samples
|
||||
*/
|
||||
int num_samples() const;
|
||||
long num_samples() const;
|
||||
|
||||
/**
|
||||
* Set maximum sample size
|
||||
*
|
||||
* @param sample_max The new sample max size
|
||||
*/
|
||||
void set_sample_max(int sample_max);
|
||||
void set_sample_max(long sample_max);
|
||||
|
||||
/**
|
||||
* Get maximum sample size
|
||||
*
|
||||
* @return The maximum sample size
|
||||
*/
|
||||
int sample_max() const;
|
||||
long sample_max() const;
|
||||
|
||||
/**
|
||||
* Reset the average
|
||||
@ -150,8 +150,8 @@ public:
|
||||
private:
|
||||
const double m_min_alpha;
|
||||
const double m_max_alpha;
|
||||
int m_sample_max;
|
||||
int m_num_samples = 0;
|
||||
long m_sample_max;
|
||||
long m_num_samples = 0;
|
||||
double m_ave = 0;
|
||||
};
|
||||
|
||||
|
@ -37,8 +37,8 @@ public:
|
||||
uint64_t rand();
|
||||
uint32_t rand32();
|
||||
bool rand_bool();
|
||||
int64_t b_to_e_exclusive(int64_t b, int64_t e);
|
||||
double zero_to_one_exclusive();
|
||||
int64_t b_to_e_co(int64_t b, int64_t e);
|
||||
double zero_to_one_co();
|
||||
private:
|
||||
uint64_t rotl(const uint64_t x, int k);
|
||||
std::array<uint64_t, 4> m_state;
|
||||
@ -57,8 +57,8 @@ public:
|
||||
uint64_t rand();
|
||||
uint32_t rand32();
|
||||
bool rand_bool();
|
||||
int64_t b_to_e_exclusive(int64_t b, int64_t e);
|
||||
double zero_to_one_exclusive();
|
||||
int64_t b_to_e_co(int64_t b, int64_t e);
|
||||
double zero_to_one_co();
|
||||
|
||||
// Borrow the mt19937_64 engine for other distributions.
|
||||
std::mt19937_64& rnd_engine();
|
||||
@ -104,7 +104,7 @@ inline bool XorShiftRandom::rand_bool()
|
||||
return std::signbit(int64_t(rand()));
|
||||
}
|
||||
|
||||
inline double XorShiftRandom::zero_to_one_exclusive()
|
||||
inline double XorShiftRandom::zero_to_one_co()
|
||||
{
|
||||
uint64_t x = rand();
|
||||
|
||||
@ -118,7 +118,7 @@ inline double XorShiftRandom::zero_to_one_exclusive()
|
||||
return u.d - 1.0;
|
||||
}
|
||||
|
||||
inline int64_t XorShiftRandom::b_to_e_exclusive(int64_t b, int64_t e)
|
||||
inline int64_t XorShiftRandom::b_to_e_co(int64_t b, int64_t e)
|
||||
{
|
||||
// With 64 bits mod bias does not happen in practise (a very, very large e-b would be needed).
|
||||
// alternative: return b + int64_t(zero_to_one_exclusive()*(e-b));
|
||||
@ -140,13 +140,13 @@ inline bool StdTwisterRandom::rand_bool()
|
||||
return std::signbit(int32_t(rand32()));
|
||||
}
|
||||
|
||||
inline double StdTwisterRandom::zero_to_one_exclusive()
|
||||
inline double StdTwisterRandom::zero_to_one_co()
|
||||
{
|
||||
std::uniform_real_distribution<double> zero_to_one {0, 1};
|
||||
return zero_to_one(m_twister_engine_64);
|
||||
}
|
||||
|
||||
inline int64_t StdTwisterRandom::b_to_e_exclusive(int64_t b, int64_t e)
|
||||
inline int64_t StdTwisterRandom::b_to_e_co(int64_t b, int64_t e)
|
||||
{
|
||||
std::uniform_int_distribution<int64_t> dist {b, e - 1};
|
||||
return dist(m_twister_engine_64);
|
||||
|
@ -17,7 +17,7 @@
|
||||
namespace maxbase
|
||||
{
|
||||
|
||||
void CumulativeAverage::add(double ave, int num_samples)
|
||||
void CumulativeAverage::add(double ave, long num_samples)
|
||||
{
|
||||
m_num_samples += num_samples;
|
||||
|
||||
@ -38,7 +38,7 @@ double CumulativeAverage::average() const
|
||||
return m_ave;
|
||||
}
|
||||
|
||||
int CumulativeAverage::num_samples() const
|
||||
long CumulativeAverage::num_samples() const
|
||||
{
|
||||
return m_num_samples;
|
||||
}
|
||||
@ -61,17 +61,17 @@ void CumulativeAverage::reset()
|
||||
m_num_last_added = 0;
|
||||
}
|
||||
|
||||
EMAverage::EMAverage(double min_alpha, double max_alpha, int sample_max)
|
||||
EMAverage::EMAverage(double min_alpha, double max_alpha, long sample_max)
|
||||
: m_min_alpha{min_alpha}
|
||||
, m_max_alpha{max_alpha}
|
||||
, m_sample_max{sample_max}
|
||||
{
|
||||
}
|
||||
|
||||
void EMAverage::add(double ave, int num_samples)
|
||||
void EMAverage::add(double ave, long num_samples)
|
||||
{
|
||||
// Give more weight to initial samples.
|
||||
int sample_max = std::min(m_num_samples ? m_num_samples : 1, m_sample_max);
|
||||
long sample_max = std::min(m_num_samples ? m_num_samples : 1, m_sample_max);
|
||||
|
||||
double alpha = m_min_alpha + m_max_alpha
|
||||
* std::min(double(num_samples) / sample_max, 1.0);
|
||||
@ -97,17 +97,17 @@ double EMAverage::average() const
|
||||
return m_ave;
|
||||
}
|
||||
|
||||
int EMAverage::num_samples() const
|
||||
long EMAverage::num_samples() const
|
||||
{
|
||||
return m_num_samples;
|
||||
}
|
||||
|
||||
void EMAverage::set_sample_max(int sample_max)
|
||||
void EMAverage::set_sample_max(long sample_max)
|
||||
{
|
||||
m_sample_max = sample_max;
|
||||
}
|
||||
|
||||
int EMAverage::sample_max() const
|
||||
long EMAverage::sample_max() const
|
||||
{
|
||||
return m_sample_max;
|
||||
}
|
||||
|
Reference in New Issue
Block a user