GY-63_MS5611/libraries/FLE/FLE.cpp

182 lines
2.8 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: FLE.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-21
2021-06-16 07:32:34 -04:00
// VERSION: 0.1.2
2021-01-29 06:31:58 -05:00
// PURPOSE: Arduino library for float with error datatype
// URL: https://github.com/RobTillaart/FLE
//
// HISTORY
// 0.0.1 2020-07-21 initial version
// 0.1.0 2020-12-23 arduino-CI + unit tests
2021-05-28 07:27:47 -04:00
// 0.1.1 2021-05-27 fix arduino-lint
2021-06-16 07:32:34 -04:00
// 0.1.2 2021-06-15 add negation + unit tests
// add first comparisons + some experimental
//
2021-01-29 06:31:58 -05:00
#include "FLE.h"
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
FLE::FLE(float val, float err)
{
_v = val;
_e = abs(err);
};
// PRINTABLE
size_t FLE::printTo(Print& p) const
{
size_t n = 0;
n += p.print(_v, _decimals);
n += p.print(' ');
n += p.print(_sep);
n += p.print(' ');
n += p.print(_e, _decimals);
return n;
}
2021-06-16 07:32:34 -04:00
/////////////////////////////////////////////////
//
// MATH OPERATORS
//
// NEGATION
FLE FLE::operator - ()
{
return FLE(-_v, _e);
}
2021-01-29 06:31:58 -05:00
FLE FLE::operator + (const FLE &in)
{
return FLE(_v + in._v, _e + in._e);
}
2021-05-28 07:27:47 -04:00
2021-06-16 07:32:34 -04:00
// SUBTRACT
2021-01-29 06:31:58 -05:00
FLE FLE::operator - (const FLE &in)
{
return FLE(_v - in._v, _e + in._e);
}
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
FLE FLE::operator * (const FLE &in)
{
float val = _v * in._v;
float err = (relError() + in.relError()) * val;
return FLE(val, err);
}
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
FLE FLE::operator / (const FLE &in)
{
float val = _v / in._v;
float err = (relError() + in.relError()) * val;
return FLE(val, err);
}
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
FLE FLE::operator += (const FLE &in)
{
_v += in._v;
_e += in._e;
return *this;
}
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
FLE FLE::operator -= (const FLE &in)
{
_v -= in._v;
_e += in._e;
return *this;
}
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
FLE FLE::operator *= (const FLE &in)
{
float sum = relError() + in.relError();
_v *= in._v;
_e = sum * _v;
return *this;
}
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
FLE FLE::operator /= (const FLE &in)
{
float sum = relError() + in.relError();
_v /= in._v;
_e = sum * _v;
return *this;
}
2021-06-16 07:32:34 -04:00
/////////////////////////////////////////////////
//
// BOOL OPERATORS
//
bool FLE::operator == (const FLE &in)
{
return ((_v == in._v) && (_e == in._e));
}
bool FLE::operator != (const FLE &in)
{
return ((_v != in._v) || (_e != in._e));
}
// bool FLE::operator >= (const FLE &in)
// {
// return ((*this == in) || (low() >= in.high()));
// }
bool FLE::operator > (const FLE &in)
{
return low() > in.high();
}
// bool FLE::operator <= (const FLE &in)
// {
// return ((*this == in) || (high() <= in.low()) );
// }
bool FLE::operator < (const FLE &in)
{
return high() < in.low();
}
/////////////////////////////////////////////////
//
// MISC OPERATORS
//
bool FLE::in(FLE a)
{
return ( a.low() <= low() && high() <= a.high());
}
/////////////////////////////////////////////////
//
// EXPERIMENTAL
//
bool FLE::peq(const FLE &in)
{
if (in.low() <= low() && in.high() >= low() ) return true;
if (low() <= in.low() && high() >= in.low() ) return true;
return false;
}
bool FLE::pne(const FLE &in)
{
return !(*this == in);
}
2021-01-29 06:31:58 -05:00
// -- END OF FILE --