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

76 lines
1.9 KiB
C
Raw Normal View History

2024-03-06 14:05:07 -05:00
#pragma once
//
// FILE: float16ext.h
// AUTHOR: Rob Tillaart
2024-04-18 06:16:58 -04:00
// VERSION: 0.2.0
2024-03-06 14:05:07 -05:00
// PURPOSE: Arduino library to implement float16ext data type.
// half-precision floating point format,
// used for efficient storage and transport.
// URL: https://github.com/RobTillaart/float16ext
#include "Arduino.h"
2024-04-18 06:16:58 -04:00
#define FLOAT16EXT_LIB_VERSION (F("0.2.0"))
2024-03-06 14:05:07 -05:00
2024-04-18 06:16:58 -04:00
class float16ext
2024-03-06 14:05:07 -05:00
{
public:
// Constructors
float16ext(void) { _value = 0x0000; };
float16ext(double f);
float16ext(const float16ext &f) { _value = f._value; };
2024-04-18 06:16:58 -04:00
// Conversion and printing
2024-03-06 14:05:07 -05:00
double toDouble(void) const;
2024-04-18 06:16:58 -04:00
float toFloat() const;
String toString(unsigned int decimals = 2) const; // keep esp32 happy.
2024-03-06 14:05:07 -05:00
2024-04-18 06:16:58 -04:00
// access the 2 byte representation.
uint16_t getBinary() { return _value; };
void setBinary(uint16_t u) { _value = u; };
2024-03-06 14:05:07 -05:00
// equalities
bool operator == (const float16ext& f);
bool operator != (const float16ext& f);
bool operator > (const float16ext& f);
bool operator >= (const float16ext& f);
bool operator < (const float16ext& f);
bool operator <= (const float16ext& f);
// negation
float16ext operator - ();
// basic math
float16ext operator + (const float16ext& f);
float16ext operator - (const float16ext& f);
float16ext operator * (const float16ext& f);
float16ext operator / (const float16ext& f);
float16ext& operator += (const float16ext& f);
float16ext& operator -= (const float16ext& f);
float16ext& operator *= (const float16ext& f);
float16ext& operator /= (const float16ext& f);
// math helper functions
int sign(); // 1 = positive 0 = zero -1 = negative.
bool isZero();
// CORE CONVERSION
2024-04-18 06:16:58 -04:00
// should be private, needed for testing.
2024-03-06 14:05:07 -05:00
float f16tof32(uint16_t) const;
uint16_t f32tof16(float) const;
private:
uint16_t _value;
};
// -- END OF FILE --