1
1
//
2
2
// FILE: RunningAverage.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.2.04
4
+ // VERSION: 0.2.05
5
+ // DATE: 2014-dec-16
5
6
// PURPOSE: RunningAverage library for Arduino
6
7
//
7
8
// The library stores the last N individual values in a circular buffer,
16
17
// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue
17
18
// 0.2.03 - 2013-11-31 getElement
18
19
// 0.2.04 - 2014-07-03 added memory protection
20
+ // 0.2.05 - 2014-12-16 changed float -> double
19
21
//
20
22
// Released to the public domain
21
23
//
26
28
RunningAverage::RunningAverage (int n)
27
29
{
28
30
_size = n;
29
- _ar = (float *) malloc (_size * sizeof (float ));
31
+ _ar = (double *) malloc (_size * sizeof (double ));
30
32
if (_ar == NULL ) _size = 0 ;
31
33
clear ();
32
34
}
@@ -46,7 +48,7 @@ void RunningAverage::clear()
46
48
}
47
49
48
50
// adds a new value to the data-set
49
- void RunningAverage::addValue (float f)
51
+ void RunningAverage::addValue (double f)
50
52
{
51
53
if (_ar == NULL ) return ;
52
54
_sum -= _ar[_idx];
@@ -58,14 +60,14 @@ void RunningAverage::addValue(float f)
58
60
}
59
61
60
62
// returns the average of the data-set added sofar
61
- float RunningAverage::getAverage ()
63
+ double RunningAverage::getAverage ()
62
64
{
63
65
if (_cnt == 0 ) return NAN;
64
66
return _sum / _cnt;
65
67
}
66
68
67
69
// returns the value of an element if exist, 0 otherwise
68
- float RunningAverage::getElement (uint8_t idx)
70
+ double RunningAverage::getElement (uint8_t idx)
69
71
{
70
72
if (idx >=_cnt ) return NAN;
71
73
return _ar[idx];
@@ -74,7 +76,7 @@ float RunningAverage::getElement(uint8_t idx)
74
76
// fill the average with a value
75
77
// the param number determines how often value is added (weight)
76
78
// number should preferably be between 1 and size
77
- void RunningAverage::fillValue (float value, int number)
79
+ void RunningAverage::fillValue (double value, int number)
78
80
{
79
81
clear ();
80
82
for (int i = 0 ; i < number; i++)
0 commit comments