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

82 lines
2.0 KiB
C
Raw Normal View History

2021-11-27 10:28:52 -05:00
#pragma once
//
// FILE: float16.h
// AUTHOR: Rob Tillaart
2022-11-07 08:26:49 -05:00
// VERSION: 0.1.7
2021-11-27 10:28:52 -05:00
// PURPOSE: Arduino library to implement float16 data type.
// half-precision floating point format,
// used for efficient storage and transport.
// URL: https://github.com/RobTillaart/float16
//
#include "Arduino.h"
2022-11-07 08:26:49 -05:00
#define FLOAT16_LIB_VERSION (F("0.1.7"))
2021-11-27 10:28:52 -05:00
class float16: public Printable
{
public:
// Constructors
2021-12-02 14:27:32 -05:00
float16(void) { _value = 0x0000; };
2021-11-27 10:28:52 -05:00
float16(double f);
2021-12-02 14:27:32 -05:00
float16(const float16 &f) { _value = f._value; };
2021-11-27 10:28:52 -05:00
// Conversion
double toDouble(void) const;
// access the 2 byte representation.
2021-12-02 14:27:32 -05:00
uint16_t getBinary() { return _value; };
void setBinary(uint16_t u) { _value = u; };
2021-11-27 10:28:52 -05:00
// Printable
size_t printTo(Print& p) const;
void setDecimals(uint8_t d) { _decimals = d; };
uint8_t getDecimals() { return _decimals; };
2021-12-02 14:27:32 -05:00
// equalities
bool operator == (const float16& f);
bool operator != (const float16& f);
2021-11-27 10:28:52 -05:00
2021-12-02 14:27:32 -05:00
bool operator > (const float16& f);
bool operator >= (const float16& f);
bool operator < (const float16& f);
bool operator <= (const float16& f);
2021-11-27 10:28:52 -05:00
// negation
float16 operator - ();
// basic math
2021-12-02 14:27:32 -05:00
float16 operator + (const float16& f);
float16 operator - (const float16& f);
float16 operator * (const float16& f);
float16 operator / (const float16& f);
float16& operator += (const float16& f);
float16& operator -= (const float16& f);
float16& operator *= (const float16& f);
float16& operator /= (const float16& f);
// math helper functions
int sign(); // 1 = positive 0 = zero -1 = negative.
bool isZero();
// bool isNaN();
bool isInf();
2021-11-27 10:28:52 -05:00
2021-12-02 14:27:32 -05:00
// CORE CONVERSION
2021-11-27 10:28:52 -05:00
// should be private but for testing...
float f16tof32(uint16_t) const;
uint16_t f32tof16(float) const;
private:
uint8_t _decimals = 4;
2021-12-02 14:27:32 -05:00
uint16_t _value;
2021-11-27 10:28:52 -05:00
};
// -- END OF FILE --