76 lines
1.8 KiB
C
Raw Normal View History

2015-03-01 12:03:22 +01:00
//
// FILE: fraction.h
// AUTHOR: Rob Tillaart
2015-03-05 18:26:06 +01:00
// VERSION: 0.1.07
2015-03-01 12:03:22 +01:00
// PURPOSE: demo library for fractions for Arduino
// URL:
//
// Released to the public domain
//
#ifndef Fraction_h
#define Fraction_h
#include "Arduino.h"
2015-03-05 18:26:06 +01:00
#define FRACTIONLIBVERSION "0.1.07"
2015-03-01 12:03:22 +01:00
class Fraction: public Printable
{
public:
Fraction(double);
Fraction(int32_t, int32_t);
Fraction(int32_t p) : n(p), d(1) {}
Fraction(int16_t p) : n(p), d(1) {}
Fraction(int8_t p) : n(p), d(1) {}
Fraction(uint32_t p) : n(p), d(1) {}
Fraction(uint16_t p) : n(p), d(1) {}
Fraction(uint8_t p) : n(p), d(1) {}
2015-03-05 18:26:06 +01:00
Fraction(const Fraction &f) : n(f.n), d(f.d) {}
2015-03-01 12:03:22 +01:00
size_t printTo(Print& p) const;
// equalities
2015-03-05 17:42:23 +10:00
bool operator == (const Fraction&);
bool operator != (const Fraction&);
bool operator > (const Fraction&);
bool operator >= (const Fraction&);
bool operator < (const Fraction&);
bool operator <= (const Fraction&);
2015-03-01 12:03:22 +01:00
// negation
Fraction operator - ();
2015-03-01 12:03:22 +01:00
// basic math
2015-03-05 17:42:23 +10:00
Fraction operator + (const Fraction&);
Fraction operator - (const Fraction&);
Fraction operator * (const Fraction&);
Fraction operator / (const Fraction&);
2015-03-01 12:03:22 +01:00
2015-03-05 17:42:23 +10:00
Fraction& operator += (const Fraction&);
Fraction& operator -= (const Fraction&);
Fraction& operator *= (const Fraction&);
Fraction& operator /= (const Fraction&);
2015-03-01 12:03:22 +01:00
double toDouble();
bool isProper(); // abs(f) < 1
double toAngle();
static Fraction mediant(const Fraction&, const Fraction&);
// approximate a fraction with defined denominator
static Fraction setDenominator(const Fraction&, uint16_t);
protected:
2015-03-01 12:03:22 +01:00
void simplify();
double fractionize(double);
2015-03-05 18:29:58 +10:00
int32_t gcd(int32_t, int32_t);
2015-03-01 12:03:22 +01:00
int32_t n;
int32_t d;
};
#endif
//
// -- END OF FILE --
//