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

243 lines
3.6 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: FLE.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-21
2023-10-30 10:37:30 -04:00
// VERSION: 0.1.4
// PURPOSE: Arduino library for float with error data type
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/FLE
#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
/////////////////////////////////////////////////
//
2023-10-30 10:37:30 -04:00
// MATH OPERATORS
2021-06-16 07:32:34 -04:00
//
2023-10-30 10:37:30 -04:00
// NEGATION
2021-06-16 07:32:34 -04:00
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
2023-10-30 10:37:30 -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
/////////////////////////////////////////////////
//
2023-10-30 10:37:30 -04:00
// BOOL OPERATORS
2021-06-16 07:32:34 -04:00
//
2023-10-30 10:37:30 -04:00
// bool FLE::operator == (const FLE in)
// {
// return ((_v == in._v) && (_e == in._e));
// }
2022-12-08 11:29:26 -05:00
2021-06-16 07:32:34 -04:00
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));
}
2023-10-30 10:37:30 -04:00
// bool FLE::operator >= (const FLE &in)
// {
// return ((*this == in) || (low() >= in.high()));
// }
2021-06-16 07:32:34 -04:00
bool FLE::operator > (const FLE &in)
{
return low() > in.high();
}
2023-10-30 10:37:30 -04:00
// bool FLE::operator <= (const FLE &in)
// {
// return ((*this == in) || (high() <= in.low()) );
// }
2021-06-16 07:32:34 -04:00
bool FLE::operator < (const FLE &in)
{
return high() < in.low();
}
/////////////////////////////////////////////////
//
2023-10-30 10:37:30 -04:00
// MISC OPERATORS
2021-06-16 07:32:34 -04:00
//
bool FLE::in(FLE a)
{
return ( a.low() <= low() && high() <= a.high());
}
2022-12-08 11:29:26 -05:00
FLE FLE::shared(FLE a)
{
float v, e;
2023-10-30 10:37:30 -04:00
// six possible cases.
// case 1, 6
2022-12-08 11:29:26 -05:00
if ((*this < a) || (*this > a)) return FLE(NAN, NAN); // no overlap
2023-10-30 10:37:30 -04:00
// case 3, 4
2022-12-08 11:29:26 -05:00
if (a.in(*this)) return a;
if (this->in(a)) return *this;
2023-10-30 10:37:30 -04:00
// case 2
2022-12-08 11:29:26 -05:00
if (low() < a.low())
{
v = (a.low() + high())/2;
e = v - a.low();
}
2023-10-30 10:37:30 -04:00
// case 5
2022-12-08 11:29:26 -05:00
else
{
v = (low() + a.high())/2;
e = v - low();
}
return FLE(v, e);
}
FLE FLE::lower(FLE a)
{
2023-10-30 10:37:30 -04:00
return FLE(0,0); // TODO
2022-12-08 11:29:26 -05:00
}
FLE FLE::higher(FLE a)
{
2023-10-30 10:37:30 -04:00
return FLE(0,0); // TODO
2022-12-08 11:29:26 -05:00
}
2021-06-16 07:32:34 -04:00
/////////////////////////////////////////////////
//
2023-10-30 10:37:30 -04:00
// WEAK PROPOSITIONS TODO elaborate
2021-06-16 07:32:34 -04:00
//
2023-10-30 10:37:30 -04:00
// possible equal
2022-12-08 11:29:26 -05:00
bool FLE::peq(const FLE &a)
2021-06-16 07:32:34 -04:00
{
2022-12-08 11:29:26 -05:00
if (a.low() <= low() && a.high() >= low() ) return true;
if (low() <= a.low() && high() >= a.low() ) return true;
2021-06-16 07:32:34 -04:00
return false;
}
2023-10-30 10:37:30 -04:00
// possible not equal
2022-12-08 11:29:26 -05:00
bool FLE::pne(const FLE &a)
{
return !(*this == a);
}
2023-10-30 10:37:30 -04:00
// possible less than
2022-12-08 11:29:26 -05:00
bool FLE::plt(const FLE &a)
{
return (this->low() < a.low()); // TODO
}
2023-10-30 10:37:30 -04:00
// possible less equal
2022-12-08 11:29:26 -05:00
bool FLE::ple(const FLE &a)
{
return (this->low() <= a.low()); // TODO
}
2023-10-30 10:37:30 -04:00
// possible greater than
2022-12-08 11:29:26 -05:00
bool FLE::pgt(const FLE &a)
{
return (this->high() > a.high());
}
2023-10-30 10:37:30 -04:00
// possible greater equal
2022-12-08 11:29:26 -05:00
bool FLE::pge(const FLE &a)
2021-06-16 07:32:34 -04:00
{
2022-12-08 11:29:26 -05:00
return (this->high() >= a.high()); // TODO
2021-06-16 07:32:34 -04:00
}
2023-10-30 10:37:30 -04:00
// -- END OF FILE --