detect add() bug (#4)
This commit is contained in:
parent
5cbc9c0651
commit
a751d44368
6 changed files with 134 additions and 66 deletions
|
@ -21,7 +21,8 @@ The stability of the formulas is improved by the help of Gil Ross (Thanks!)
|
||||||
- **Statistic(bool useStdDev = true)** Constructor, default use the standard deviation
|
- **Statistic(bool useStdDev = true)** Constructor, default use the standard deviation
|
||||||
functions. Setting this flag to **false** reduces math so slight increase of performance.
|
functions. Setting this flag to **false** reduces math so slight increase of performance.
|
||||||
- **void clear(bool useStdDev = true)** resets all variables.
|
- **void clear(bool useStdDev = true)** resets all variables.
|
||||||
- **void add(float value)**
|
- **float add(float value)** (since 0.4.3) returns value actually added to internal sum.
|
||||||
|
If this is (much) different from what should be added it becomes time to call **clear()**
|
||||||
- **uint32_t count()** returns zero if count == zero (of course)
|
- **uint32_t count()** returns zero if count == zero (of course)
|
||||||
- **float sum()** returns zero if count == zero
|
- **float sum()** returns zero if count == zero
|
||||||
- **float minimum()** returns zero if count == zero
|
- **float minimum()** returns zero if count == zero
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// FILE: Statistic.cpp
|
// FILE: Statistic.cpp
|
||||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||||
// modified at 0.3 by Gil Ross at physics dot org
|
// modified at 0.3 by Gil Ross at physics dot org
|
||||||
// VERSION: 0.4.2
|
// VERSION: 0.4.3
|
||||||
// PURPOSE: Recursive statistical library for Arduino
|
// PURPOSE: Recursive statistical library for Arduino
|
||||||
//
|
//
|
||||||
// NOTE: 2011-01-07 Gill Ross
|
// NOTE: 2011-01-07 Gill Ross
|
||||||
|
@ -54,6 +54,7 @@
|
||||||
// Added flag to switch on the use of stdDev runtime. [idea marc.recksiedl]
|
// Added flag to switch on the use of stdDev runtime. [idea marc.recksiedl]
|
||||||
// 0.4.1 2020-06-19 fix library.json
|
// 0.4.1 2020-06-19 fix library.json
|
||||||
// 0.4.2 2021-01-08 add Arduino-CI + unit tests
|
// 0.4.2 2021-01-08 add Arduino-CI + unit tests
|
||||||
|
// 0.4.3 2021-01-20 add() returns how much was actually added.
|
||||||
|
|
||||||
|
|
||||||
#include "Statistic.h"
|
#include "Statistic.h"
|
||||||
|
@ -79,8 +80,9 @@ void Statistic::clear(bool useStdDev) // useStdDev default true.
|
||||||
|
|
||||||
|
|
||||||
// adds a new value to the data-set
|
// adds a new value to the data-set
|
||||||
void Statistic::add(const float value)
|
float Statistic::add(const float value)
|
||||||
{
|
{
|
||||||
|
float previousSum = _sum;
|
||||||
if (_cnt == 0)
|
if (_cnt == 0)
|
||||||
{
|
{
|
||||||
_min = value;
|
_min = value;
|
||||||
|
@ -104,6 +106,7 @@ void Statistic::add(const float value)
|
||||||
// solution: TODO verify
|
// solution: TODO verify
|
||||||
// _ssqdif = _ssqdif + (_store * _store / _cnt) / (_cnt - 1);
|
// _ssqdif = _ssqdif + (_store * _store / _cnt) / (_cnt - 1);
|
||||||
}
|
}
|
||||||
|
return _sum - previousSum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// FILE: Statistic.h
|
// FILE: Statistic.h
|
||||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||||
// modified at 0.3 by Gil Ross at physics dot org
|
// modified at 0.3 by Gil Ross at physics dot org
|
||||||
// VERSION: 0.4.2
|
// VERSION: 0.4.3
|
||||||
// PURPOSE: Recursive Statistical library for Arduino
|
// PURPOSE: Recursive Statistical library for Arduino
|
||||||
// HISTORY: See Statistic.cpp
|
// HISTORY: See Statistic.cpp
|
||||||
//
|
//
|
||||||
|
@ -13,14 +13,15 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
#define STATISTIC_LIB_VERSION (F("0.4.2"))
|
#define STATISTIC_LIB_VERSION (F("0.4.3"))
|
||||||
|
|
||||||
class Statistic
|
class Statistic
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Statistic(bool useStdDev = true); // "switches on/off" stdev run time
|
Statistic(bool useStdDev = true); // "switches on/off" stdev run time
|
||||||
void clear(bool useStdDev = true); // "switches on/off" stdev run time
|
void clear(bool useStdDev = true); // "switches on/off" stdev run time
|
||||||
void add(const float);
|
float add(const float); // returns value actually added
|
||||||
|
|
||||||
|
|
||||||
// returns the number of values added
|
// returns the number of values added
|
||||||
uint32_t count() const { return _cnt; }; // zero if count == zero
|
uint32_t count() const { return _cnt; }; // zero if count == zero
|
||||||
|
@ -29,11 +30,13 @@ public:
|
||||||
float maximum() const { return _max; }; // zero if count == zero
|
float maximum() const { return _max; }; // zero if count == zero
|
||||||
float average() const; // NAN if count == zero
|
float average() const; // NAN if count == zero
|
||||||
|
|
||||||
|
|
||||||
// useStdDev must be true to use next three
|
// useStdDev must be true to use next three
|
||||||
float variance() const; // NAN if count == zero
|
float variance() const; // NAN if count == zero
|
||||||
float pop_stdev() const; // population stdev // NAN if count == zero
|
float pop_stdev() const; // population stdev // NAN if count == zero
|
||||||
float unbiased_stdev() const; // NAN if count == zero
|
float unbiased_stdev() const; // NAN if count == zero
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint32_t _cnt;
|
uint32_t _cnt;
|
||||||
float _sum;
|
float _sum;
|
||||||
|
|
61
examples/statistic_add_overflow/statistic_add_overflow.ino
Normal file
61
examples/statistic_add_overflow/statistic_add_overflow.ino
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
//
|
||||||
|
// FILE: TimingTest.ino
|
||||||
|
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||||
|
// VERSION: 0.2.0
|
||||||
|
// PURPOSE: this sketch shows a known problem when
|
||||||
|
// internal sum is orders of magnitude larger
|
||||||
|
// than the added value.
|
||||||
|
|
||||||
|
#include "Statistic.h"
|
||||||
|
|
||||||
|
Statistic myStats;
|
||||||
|
|
||||||
|
void setup(void)
|
||||||
|
{
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.println(__FILE__);
|
||||||
|
Serial.print("STATISTIC_LIB_VERSION: ");
|
||||||
|
Serial.println(STATISTIC_LIB_VERSION);
|
||||||
|
|
||||||
|
myStats.clear();
|
||||||
|
|
||||||
|
Serial.println("\nCOUNT\tVALUE\tACTUAL\tRATIO");
|
||||||
|
for (float value = 1e8; value > 1; value *= 0.1)
|
||||||
|
{
|
||||||
|
float actual = myStats.add(value);
|
||||||
|
float ratio = actual / value;
|
||||||
|
|
||||||
|
Serial.print(myStats.count());
|
||||||
|
Serial.print('\t');
|
||||||
|
Serial.print(value);
|
||||||
|
Serial.print('\t');
|
||||||
|
Serial.print(actual);
|
||||||
|
Serial.print('\t');
|
||||||
|
Serial.print(ratio);
|
||||||
|
Serial.print('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
for (float value = 10; value > 0.1; value -= 1)
|
||||||
|
{
|
||||||
|
float actual = myStats.add(value);
|
||||||
|
float ratio = actual / value;
|
||||||
|
|
||||||
|
Serial.print(myStats.count());
|
||||||
|
Serial.print('\t');
|
||||||
|
Serial.print(value);
|
||||||
|
Serial.print('\t');
|
||||||
|
Serial.print(actual);
|
||||||
|
Serial.print('\t');
|
||||||
|
Serial.print(ratio);
|
||||||
|
Serial.print('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("\nQED...");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- END OF FILE --
|
|
@ -18,7 +18,7 @@
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/RobTillaart/Statistic.git"
|
"url": "https://github.com/RobTillaart/Statistic.git"
|
||||||
},
|
},
|
||||||
"version":"0.4.2",
|
"version":"0.4.3",
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "*"
|
"platforms": "*"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name=Statistic
|
name=Statistic
|
||||||
version=0.4.2
|
version=0.4.3
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
sentence=Library with basic statistical functions for Arduino.
|
sentence=Library with basic statistical functions for Arduino.
|
||||||
|
|
Loading…
Reference in a new issue