2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: infiniteAverage.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-11-07 08:08:50 -05:00
|
|
|
// VERSION: 0.1.7
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: Calculate the average of a very large number of values.
|
2022-11-14 13:12:40 -05:00
|
|
|
// URL: https://github.com/RobTillaart/infiniteAverage
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2023-11-07 08:08:50 -05:00
|
|
|
#define IAVG_LIB_VERSION (F("0.1.7"))
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
class IAVG
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IAVG()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
_sum = 0;
|
|
|
|
_overflow = 0;
|
|
|
|
_count = 0;
|
2023-11-07 08:08:50 -05:00
|
|
|
// cannot reset _minimum or maximum;
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void add(float value)
|
|
|
|
{
|
|
|
|
if (_count == 0)
|
|
|
|
{
|
|
|
|
_minimum = value;
|
|
|
|
_maximum = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value < _minimum) _minimum = value;
|
|
|
|
if (value > _maximum) _maximum = value;
|
|
|
|
}
|
|
|
|
_count++;
|
|
|
|
long ov = value;
|
|
|
|
_overflow += ov;
|
|
|
|
value -= ov;
|
|
|
|
_sum += value;
|
|
|
|
if (_sum > 1)
|
|
|
|
{
|
|
|
|
_overflow++;
|
|
|
|
_sum -= 1;
|
|
|
|
}
|
2021-12-20 10:20:02 -05:00
|
|
|
// scale back factor 2 when overflow comes near
|
|
|
|
// TODO abs(_overflow)
|
|
|
|
if ( (_overflow >= _threshold) || (_count >= 4294967000 ) )
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2023-11-07 08:08:50 -05:00
|
|
|
if (_overflow & 1) _sum += 1.0; // fix rounding error.
|
2021-01-29 06:31:58 -05:00
|
|
|
_count /= 2;
|
|
|
|
_overflow /= 2;
|
|
|
|
_sum /= 2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float average()
|
|
|
|
{
|
|
|
|
if (_count == 0) return NAN;
|
|
|
|
return _sum / _count + (1.0 * _overflow) / _count;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float maximum()
|
|
|
|
{
|
|
|
|
if (_count == 0) return NAN;
|
|
|
|
return _maximum;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float minimum()
|
|
|
|
{
|
|
|
|
if (_count == 0) return NAN;
|
|
|
|
return _minimum;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t count()
|
|
|
|
{
|
|
|
|
return _count;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t whole()
|
|
|
|
{
|
|
|
|
return _overflow;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float decimals()
|
|
|
|
{
|
|
|
|
return _sum;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-11-05 15:16:15 -04:00
|
|
|
void setDivideThreshold(uint32_t threshold)
|
|
|
|
{
|
|
|
|
_threshold = threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t getDivideThreshold()
|
|
|
|
{
|
|
|
|
return _threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
private:
|
|
|
|
float _sum = 0;
|
|
|
|
float _minimum = 0;
|
|
|
|
float _maximum = 0;
|
|
|
|
uint32_t _overflow = 0;
|
|
|
|
uint32_t _count = 0;
|
2021-12-20 10:20:02 -05:00
|
|
|
uint32_t _threshold = (1UL << 30);
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
2021-05-28 07:34:28 -04:00
|
|
|
|
2023-11-07 08:08:50 -05:00
|
|
|
// -- END OF FILE --
|
2021-11-05 15:16:15 -04:00
|
|
|
|