2020-11-27 05:16:22 -05:00
|
|
|
#pragma once
|
2015-03-01 06:03:22 -05:00
|
|
|
//
|
|
|
|
// FILE: fraction.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-02-03 07:14:59 -05:00
|
|
|
// VERSION: 0.1.15
|
|
|
|
// PURPOSE: Arduino library to implement a Fraction data type
|
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"
|
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
#define FRACTION_LIB_VERSION (F("0.1.15"))
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2015-03-01 06:03:22 -05:00
|
|
|
|
|
|
|
class Fraction: public Printable
|
|
|
|
{
|
|
|
|
public:
|
2018-04-02 14:30:05 -04:00
|
|
|
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
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// CONSTRUCTORS
|
2018-04-02 14:30:05 -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;
|
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// EQUALITIES
|
2015-03-05 02:42:23 -05:00
|
|
|
bool operator == (const Fraction&);
|
2023-02-03 07:14:59 -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
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// NEGATE
|
2015-03-01 06:15:35 -05:00
|
|
|
Fraction operator - ();
|
2015-03-01 06:03:22 -05:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// BASIC MATH
|
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
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// CONVERSION
|
|
|
|
double toDouble();
|
|
|
|
float toFloat();
|
|
|
|
bool isProper(); // abs(f) < 1
|
2018-04-02 14:30:05 -04:00
|
|
|
float toAngle();
|
2015-03-01 06:47:02 -05:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
int32_t nominator();
|
|
|
|
int32_t denominator();
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// MISCELLANEOUS (static)
|
2015-03-05 03:06:21 -05:00
|
|
|
static Fraction mediant(const Fraction&, const Fraction&);
|
2020-11-27 05:16:22 -05:00
|
|
|
static Fraction middle(const Fraction&, const Fraction&);
|
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// approximate a fraction with defined denominator
|
2015-03-05 03:06:21 -05:00
|
|
|
static Fraction setDenominator(const Fraction&, uint16_t);
|
2015-03-01 06:47:02 -05:00
|
|
|
|
2015-03-01 06:11:21 -05:00
|
|
|
|
2015-03-05 03:06:21 -05:00
|
|
|
protected:
|
2018-04-02 14:30:05 -04:00
|
|
|
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
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// -- END OF FILE --
|