GY-63_MS5611/libraries/Fraction/fraction.h

87 lines
2.1 KiB
C
Raw Normal View History

2020-11-27 05:16:22 -05:00
#pragma once
2015-03-01 06:03:22 -05:00
//
// FILE: fraction.h
// AUTHOR: Rob Tillaart
2022-11-07 09:02:34 -05:00
// VERSION: 0.1.14
2021-12-18 08:10:11 -05:00
// PURPOSE: Arduino library to implement a Fraction datatype
2020-11-27 05:16:22 -05:00
// URL: https://github.com/RobTillaart/Fraction
2015-03-01 06:03:22 -05:00
//
2021-11-01 10:56:22 -04:00
2015-03-01 06:03:22 -05:00
#include "Arduino.h"
2022-11-07 09:02:34 -05:00
#define FRACTION_LIB_VERSION (F("0.1.14"))
2021-11-01 10:56:22 -04:00
2015-03-01 06:03:22 -05:00
class Fraction: public Printable
{
public:
explicit Fraction(double);
explicit Fraction(float);
2015-03-01 06:03:22 -05:00
Fraction(int32_t, int32_t);
2021-11-01 10:56:22 -04:00
explicit Fraction(int32_t p) : n(p), d(1) {}
explicit Fraction(int16_t p) : n(p), d(1) {}
explicit Fraction(int8_t p) : n(p), d(1) {}
explicit Fraction(uint32_t p) : n(p), d(1) {}
explicit Fraction(uint16_t p) : n(p), d(1) {}
explicit Fraction(uint8_t p) : n(p), d(1) {}
2015-03-05 12:26:06 -05:00
Fraction(const Fraction &f) : n(f.n), d(f.d) {}
2015-03-01 06:03:22 -05:00
size_t printTo(Print& p) const;
// equalities
2015-03-05 02:42:23 -05:00
bool operator == (const Fraction&);
2020-11-27 05:16:22 -05:00
// bool operator == (const float&);
2015-03-05 02:42:23 -05:00
bool operator != (const Fraction&);
bool operator > (const Fraction&);
bool operator >= (const Fraction&);
bool operator < (const Fraction&);
bool operator <= (const Fraction&);
2015-03-01 06:03:22 -05:00
// negation
Fraction operator - ();
2015-03-01 06:03:22 -05:00
2021-11-01 10:56:22 -04:00
// basic maths
2015-03-05 02:42:23 -05:00
Fraction operator + (const Fraction&);
Fraction operator - (const Fraction&);
Fraction operator * (const Fraction&);
Fraction operator / (const Fraction&);
2015-03-01 06:03:22 -05:00
2015-03-05 02:42:23 -05:00
Fraction& operator += (const Fraction&);
Fraction& operator -= (const Fraction&);
Fraction& operator *= (const Fraction&);
Fraction& operator /= (const Fraction&);
2015-03-01 06:03:22 -05:00
2021-11-01 10:56:22 -04:00
float toDouble();
float toFloat() { return toDouble(); };
bool isProper(); // abs(f) < 1
float toAngle();
2021-11-01 10:56:22 -04:00
2020-11-27 05:16:22 -05:00
int32_t nominator() { return n; };
int32_t denominator() { return d; };
2021-01-29 06:31:58 -05:00
2021-11-01 10:56:22 -04:00
static Fraction mediant(const Fraction&, const Fraction&);
2020-11-27 05:16:22 -05:00
static Fraction middle(const Fraction&, const Fraction&);
2021-11-01 10:56:22 -04:00
// approximate a fraction with defined denominator
static Fraction setDenominator(const Fraction&, uint16_t);
protected:
void split(float);
void simplify();
void fractionize(float);
2015-03-05 03:29:58 -05:00
int32_t gcd(int32_t, int32_t);
2015-03-01 06:03:22 -05:00
int32_t n;
int32_t d;
};
2021-11-01 10:56:22 -04:00
// -- END OF FILE --