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

106 lines
2.8 KiB
C
Raw Permalink Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: FLE.h
// 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 "Arduino.h"
#include "math.h"
2023-10-30 10:37:30 -04:00
#define FLE_LIB_VERSION (F("0.1.4"))
2021-05-28 07:27:47 -04:00
2021-01-29 06:31:58 -05:00
class FLE: public Printable
{
public:
2023-10-30 10:37:30 -04:00
// CONSTRUCTOR
2021-01-29 06:31:58 -05:00
FLE(float val = 0, float error = 0);
2023-10-30 10:37:30 -04:00
// PRINTABLE
2021-01-29 06:31:58 -05:00
size_t printTo(Print& p) const;
FLE setDecimals(uint8_t n) { _decimals = n; return *this; };
FLE setSeparator(char c) { _sep = c; return *this; };
2023-10-30 10:37:30 -04:00
// BASE FUNCTIONS
2021-01-29 06:31:58 -05:00
float value() const{ return _v; };
float error() const{ return _e; };
float relError() const{ return (_v == 0 ? 0 : abs(_e / _v)); };
float high() const{ return _v + _e; };
float low() const{ return _v - _e; };
2023-10-30 10:37:30 -04:00
// MATH OPERATORS
FLE operator - (); // negation c = -a;
2021-06-16 07:32:34 -04:00
2021-01-29 06:31:58 -05:00
FLE operator + (const FLE&);
2023-10-30 10:37:30 -04:00
FLE operator - (const FLE&); // minus c = a - b;
2021-01-29 06:31:58 -05:00
FLE operator * (const FLE&);
FLE operator / (const FLE&);
FLE operator += (const FLE&);
FLE operator -= (const FLE&);
FLE operator *= (const FLE&);
FLE operator /= (const FLE&);
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
// for all value pairs of a and b
bool operator == (const FLE&);
2023-10-30 10:37:30 -04:00
// bool operator == (const FLE);
2021-06-16 07:32:34 -04:00
bool operator != (const FLE&);
bool operator > (const FLE&);
bool operator < (const FLE&);
2023-10-30 10:37:30 -04:00
// MISC OPERATORS
2021-06-16 07:32:34 -04:00
// FLE lies completely in range a
// meaning FLE is more precise than a => smaller error.
bool in(FLE a);
2023-10-30 10:37:30 -04:00
// returns overlap == common part, or FLE(NAN, NAN) otherwise
2022-12-08 11:29:26 -05:00
FLE shared(FLE a);
2023-10-30 10:37:30 -04:00
FLE lower(FLE a); // part of this lower than a;
FLE higher(FLE a); // part of this higher than a;
2021-06-16 07:32:34 -04:00
2023-10-30 10:37:30 -04:00
// EXPERIMENTAL - INVESTIGATE
2021-06-16 07:32:34 -04:00
// weak propositions.
2023-10-30 10:37:30 -04:00
bool peq (const FLE& a); // possible equal
bool pne (const FLE& a); // possible not equal
bool plt (const FLE& a); // possible less than
bool ple (const FLE& a); // possible less equal
bool pgt (const FLE& a); // possible greater than
bool pge (const FLE& a); // possible greater equal
2021-06-16 07:32:34 -04:00
// semantic meaning not 100% clear
// bool operator >= (const FLE&);
// bool operator <= (const FLE&);
2023-10-30 10:37:30 -04:00
// SET LIKE MATH
// FLE shared(FLE a, FLE b); // overlap.
// FLE lower(FLE a, FLE b); // part of a lower than b;
// FLE higher(FLE a, FLE b); // part of a higher than b;
2021-06-16 07:32:34 -04:00
2023-10-30 10:37:30 -04:00
// float both%(FLE a, FLE b); // overlap percentage
// float sub%(FLE a, FLE b); // part of a lower than b;
// float sup%(FLE a, FLE b); // part of a higher than b;
2021-06-16 07:32:34 -04:00
//
2023-10-30 10:37:30 -04:00
//
// infinite ?
// NAN ?
2021-06-16 07:32:34 -04:00
2021-01-29 06:31:58 -05:00
private:
float _v;
float _e;
uint8_t _decimals = 2;
2021-06-16 07:32:34 -04:00
char _sep = '~'; // _sep = '±'; // gives warning.
2021-01-29 06:31:58 -05:00
};
2023-10-30 10:37:30 -04:00
// -- END OF FILE --