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

172 lines
2.5 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: Interval.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-21
2022-11-14 13:10:33 -05:00
// VERSION: 0.1.3
// PURPOSE: Arduino library for Interval data type
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/Interval
//
2022-11-14 13:10:33 -05:00
// HISTORY: see changelog.md
2021-01-29 06:31:58 -05:00
#include "Interval.h"
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval::Interval(float lo, float hi)
{
if (lo <= hi)
{
_lo = lo;
_hi = hi;
}
else
{
_lo = hi;
_hi = lo;
}
};
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval::Interval(float f)
{
_lo = f;
_hi = f;
};
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval::Interval()
{
_lo = 0;
_hi = 0;
};
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
float Interval::relAccuracy()
{
if (value() == 0.0) return -1;
2022-11-14 13:10:33 -05:00
return abs(range() / value()); // TODO /2 ?
2021-01-29 06:31:58 -05:00
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
void Interval::setRange(float r)
{
float f = value();
_lo = f - r / 2;
_hi = f + r / 2;
}
2022-11-14 13:10:33 -05:00
// PRINTABLE
2021-01-29 06:31:58 -05:00
size_t Interval::printTo(Print& p) const
{
size_t n = 0;
n += p.print('[');
n += p.print(_lo, _decimals);
n += p.print(", ");
n += p.print(_hi, _decimals);
n += p.print(']');
return n;
};
/////////////////////////////////////////////////
//
2022-11-14 13:10:33 -05:00
// MATH BASIC OPERATIONS
2021-01-29 06:31:58 -05:00
//
Interval Interval::operator + (const Interval &in)
{
return Interval(_lo + in._lo, _hi + in._hi);
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval Interval::operator - (const Interval &in)
{
return Interval(_lo - in._hi, _hi - in._lo);
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval Interval::operator * (const Interval &in)
{
return Interval(_lo * in._lo, _hi * in._hi);
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval Interval::operator / (const Interval &in)
{
return Interval(_lo / in._hi, _hi / in._lo);
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval Interval::operator += (const Interval &in)
{
_lo += in._lo;
_hi += in._hi;
return *this;
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval Interval::operator -= (const Interval &in)
{
_lo -= in._hi;
_hi -= in._lo;
return *this;
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval Interval::operator *= (const Interval &in)
{
_lo *= in._lo;
_hi *= in._hi;
return *this;
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
Interval Interval::operator /= (const Interval &in)
{
_lo /= in._hi;
_hi /= in._lo;
return *this;
}
/////////////////////////////////////////////////
//
2022-11-14 13:10:33 -05:00
// COMPARISON OPERATIONS
2021-01-29 06:31:58 -05:00
//
bool Interval::operator == (const Interval &in)
{
return ((_lo == in._lo) && (_hi == in._hi));
}
2021-05-28 07:35:27 -04:00
2021-01-29 06:31:58 -05:00
bool Interval::operator != (const Interval &in)
{
return ((_lo != in._lo) || (_hi != in._hi));
}
2022-11-14 13:10:33 -05:00
// VALUE FOR NOW...
2021-01-29 06:31:58 -05:00
// bool Interval::operator > (const Interval &in)
// {
// return this->value() > in.value();
// }
//
// bool Interval::operator >= (const Interval &in)
// {
// return this->value() >= in.value();
// }
//
// bool Interval::operator < (const Interval &in)
// {
// return this->value() < in.value();
// }
//
// bool Interval::operator <= (const Interval &in)
// {
// return this->value() <= in.value();
// }
// -- END OF FILE --