2015-03-01 06:03:22 -05:00
|
|
|
//
|
|
|
|
// FILE: fraction.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2015-03-01 06:13:08 -05:00
|
|
|
// VERSION: 0.1.04
|
2015-03-01 06:03:22 -05:00
|
|
|
// PURPOSE: library for fractions for Arduino
|
|
|
|
// URL:
|
|
|
|
//
|
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
// TODO
|
2015-03-01 06:09:21 -05:00
|
|
|
// - get math for negative fractions OK
|
2015-03-01 06:03:22 -05:00
|
|
|
// - divide by zero errors
|
|
|
|
// - test extensively
|
2015-03-01 06:09:21 -05:00
|
|
|
//
|
2015-03-01 06:13:08 -05:00
|
|
|
// 0.1.04 - stabilizing code, add simplifies() for some code paths
|
2015-03-01 06:11:21 -05:00
|
|
|
// 0.1.03 - added toDouble(), tested several fractionize() codes, bug fixes.
|
2015-03-01 06:09:21 -05:00
|
|
|
// 0.1.02 - faster fractionize code
|
|
|
|
// 0.1.01 - some fixes
|
|
|
|
// 0.1.00 - initial version
|
2015-03-01 06:03:22 -05:00
|
|
|
|
|
|
|
#include "fraction.h"
|
|
|
|
|
|
|
|
Fraction::Fraction(double f)
|
|
|
|
{
|
2015-03-01 06:13:08 -05:00
|
|
|
// handle special cases?
|
|
|
|
// PI = 355/113; // 2.7e-7
|
|
|
|
// PI*2 = 710/113;
|
|
|
|
// PI/2 = 335/226;
|
|
|
|
// EULER = 2721/1001; // 1.1e-7
|
|
|
|
// EULER = 1264/465; // 2.2e-6
|
|
|
|
|
|
|
|
// get robust for small values.
|
|
|
|
if (abs(f) < 0.00001)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
d = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normalize
|
|
|
|
bool neg = f < 0;
|
|
|
|
if (neg) f = -f;
|
|
|
|
bool rec = f > 1;
|
|
|
|
if (rec) f = 1/f;
|
|
|
|
|
|
|
|
fractionize(f);
|
|
|
|
simplify();
|
|
|
|
|
|
|
|
if (rec)
|
|
|
|
{
|
|
|
|
int32_t t = n;
|
|
|
|
n = d;
|
|
|
|
d = t;
|
|
|
|
}
|
2015-03-01 06:05:22 -05:00
|
|
|
if (neg) n = -n;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Fraction::Fraction(int32_t p, int32_t q)
|
|
|
|
{
|
|
|
|
if (p == 0)
|
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
d = 1;
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 06:09:21 -05:00
|
|
|
n = p;
|
|
|
|
d = q;
|
2015-03-01 06:03:22 -05:00
|
|
|
simplify();
|
|
|
|
}
|
|
|
|
|
|
|
|
Fraction::Fraction(int32_t p)
|
|
|
|
{
|
|
|
|
n = p;
|
|
|
|
d = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fraction::Fraction(int16_t p)
|
|
|
|
{
|
|
|
|
n = p;
|
|
|
|
d = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fraction::Fraction(int8_t p)
|
|
|
|
{
|
|
|
|
n = p;
|
|
|
|
d = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fraction::Fraction(const Fraction &f)
|
|
|
|
{
|
|
|
|
n = f.n;
|
|
|
|
d = f.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PRINTING
|
|
|
|
size_t Fraction::printTo(Print& p) const
|
|
|
|
{
|
|
|
|
size_t s = 0;
|
2015-03-01 06:09:21 -05:00
|
|
|
// if (n >= d)
|
|
|
|
// {
|
|
|
|
|
|
|
|
// s += p.print(n/d, DEC);
|
|
|
|
// s += p.print(".");
|
|
|
|
// }
|
|
|
|
|
|
|
|
// s += p.print(n%d, DEC);
|
|
|
|
s += p.print(n, DEC);
|
2015-03-01 06:03:22 -05:00
|
|
|
s += p.print('/');
|
|
|
|
s += p.print(d, DEC);
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
|
|
|
// EQUALITIES
|
|
|
|
bool Fraction::operator == (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return (n * c.d) == (d * c.n);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Fraction::operator != (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return (n * c.d) != (d * c.n);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Fraction::operator > (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
// TODO neg values
|
2015-03-01 06:03:22 -05:00
|
|
|
return (n * c.d) > (d * c.n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Fraction::operator >= (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
// TODO neg values
|
2015-03-01 06:03:22 -05:00
|
|
|
return (n * c.d) >= (d * c.n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Fraction::operator < (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
// TODO neg values
|
2015-03-01 06:03:22 -05:00
|
|
|
return (n * c.d) < (d * c.n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Fraction::operator <= (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
// TODO neg values
|
2015-03-01 06:03:22 -05:00
|
|
|
return (n * c.d) <= (d * c.n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NEGATE
|
|
|
|
Fraction Fraction::operator - ()
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return Fraction(-n, d);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// BASIC MATH
|
|
|
|
Fraction Fraction::operator + (Fraction c)
|
|
|
|
{
|
|
|
|
if (d == c.d) return Fraction(n + c.n, d);
|
2015-03-01 06:11:21 -05:00
|
|
|
return Fraction(n*c.d + c.n*d, d * c.d);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Fraction Fraction::operator - (Fraction c)
|
|
|
|
{
|
|
|
|
if (d == c.d) return Fraction(n - c.n, d);
|
2015-03-01 06:11:21 -05:00
|
|
|
return Fraction(n*c.d - c.n*d, d * c.d);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Fraction Fraction::operator * (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return Fraction(n * c.n, d * c.d);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Fraction Fraction::operator / (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
// TODO test for zero division
|
|
|
|
return Fraction(n * c.d, d * c.n);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fraction::operator += (Fraction c)
|
|
|
|
{
|
|
|
|
if (d == c.d)
|
|
|
|
{
|
|
|
|
n += c.n;
|
|
|
|
}
|
2015-03-01 06:13:08 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
n = n * c.d + c.n * d;
|
|
|
|
d *= c.d;
|
|
|
|
}
|
2015-03-01 06:05:22 -05:00
|
|
|
simplify();
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fraction::operator -= (Fraction c)
|
|
|
|
{
|
|
|
|
if (d == c.d)
|
|
|
|
{
|
|
|
|
n -= c.n;
|
|
|
|
}
|
2015-03-01 06:13:08 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
n = n * c.d - c.n * d;
|
|
|
|
d *= c.d;
|
|
|
|
}
|
2015-03-01 06:05:22 -05:00
|
|
|
simplify();
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fraction::operator *= (Fraction c)
|
|
|
|
{
|
|
|
|
n *= c.n;
|
|
|
|
d *= c.d;
|
2015-03-01 06:05:22 -05:00
|
|
|
simplify();
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fraction::operator /= (Fraction c)
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
// TODO test for zero division
|
2015-03-01 06:03:22 -05:00
|
|
|
n *= c.d;
|
|
|
|
d *= c.n;
|
2015-03-01 06:05:22 -05:00
|
|
|
simplify();
|
2015-03-01 06:11:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
double Fraction::toDouble()
|
|
|
|
{
|
|
|
|
double f = (1.0 * n) / d;
|
|
|
|
return f;
|
|
|
|
}
|
2015-03-01 06:13:08 -05:00
|
|
|
/*
|
|
|
|
// Mediant - http://www.cut-the-knot.org/Curriculum/Arithmetic/FCExercise.shtml
|
|
|
|
// operator # ?
|
|
|
|
Fraction Fraction::mediant(Fraction c)
|
|
|
|
{
|
|
|
|
return (n + c.n)/(d + c.d);
|
|
|
|
}
|
|
|
|
|
|
|
|
// wikipedia
|
|
|
|
// fraction is proper if abs(fraction) < 1
|
|
|
|
bool Fraction::proper()
|
|
|
|
{
|
|
|
|
return abs(n) < abs(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
// visualize franction as an angle
|
|
|
|
double Fraction::angle()
|
|
|
|
{
|
|
|
|
return arctan(d, n);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-03-01 06:03:22 -05:00
|
|
|
|
|
|
|
// PRIVATE
|
2015-03-01 06:13:08 -05:00
|
|
|
// http://en.wikipedia.org/wiki/Binary_GCD_algorithm
|
2015-03-01 06:03:22 -05:00
|
|
|
int32_t Fraction::gcd(int32_t a , int32_t b)
|
|
|
|
{
|
|
|
|
long c;
|
|
|
|
while ( a != 0 )
|
|
|
|
{
|
|
|
|
c = a;
|
|
|
|
a = b % a;
|
|
|
|
b = c;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not that simple ...
|
|
|
|
void Fraction::simplify()
|
|
|
|
{
|
2015-03-01 06:13:08 -05:00
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
d = 1;
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 06:09:21 -05:00
|
|
|
bool neg = (n < 0) != (d < 0);
|
|
|
|
int32_t p = abs(n);
|
|
|
|
int32_t q = abs(d);
|
|
|
|
int32_t x = gcd(p,q);
|
|
|
|
p = p/x;
|
|
|
|
q = q/x;
|
|
|
|
|
|
|
|
// denominator max 4 digits keeps mul and div simple
|
|
|
|
while (q > 10000)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
p = (p + 5)/10;
|
|
|
|
q = (q + 5)/10;
|
|
|
|
x = gcd(p, q);
|
|
|
|
p = p/x;
|
|
|
|
q = q/x;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
2015-03-01 06:09:21 -05:00
|
|
|
n = (neg)?-p:p;
|
|
|
|
d = q;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-01 06:11:21 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2015-03-01 06:05:22 -05:00
|
|
|
// fractionize() is a core function to find the fraction representation
|
2015-03-01 06:11:21 -05:00
|
|
|
// PRE: 0 <= f < 1.0
|
|
|
|
//
|
|
|
|
// minimalistic is fast and small
|
|
|
|
//
|
2015-03-01 06:05:22 -05:00
|
|
|
// check for a discussion found later
|
2015-03-01 06:09:21 -05:00
|
|
|
// - http://mathforum.org/library/drmath/view/51886.html
|
2015-03-01 06:05:22 -05:00
|
|
|
// - http://www.gamedev.net/topic/354209-how-do-i-convert-a-decimal-to-a-fraction-in-c/
|
|
|
|
//
|
2015-03-01 06:11:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
// MINIMALISTIC
|
|
|
|
// (100x) micros()=51484
|
2015-03-01 06:13:08 -05:00
|
|
|
/*
|
2015-03-01 06:11:21 -05:00
|
|
|
double Fraction::fractionize(double f) // simple, small, 2nd fastest
|
|
|
|
{
|
|
|
|
n = round(f * 10000); // why not 1000000 ?
|
|
|
|
d = 10000;
|
|
|
|
simplify();
|
|
|
|
return 0; // abs(f - this.toDouble());
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// LINEAR SEARCH
|
2015-03-01 06:13:08 -05:00
|
|
|
// (100x) micros()=18873064
|
|
|
|
// slow not stable
|
|
|
|
/*
|
2015-03-01 06:11:21 -05:00
|
|
|
double Fraction::fractionize(double f)
|
2015-03-01 06:09:21 -05:00
|
|
|
{
|
|
|
|
long nn = 1, dd = 1;
|
2015-03-01 06:05:22 -05:00
|
|
|
|
2015-03-01 06:09:21 -05:00
|
|
|
float r = 1 / f;
|
|
|
|
float delta = f * dd - nn;
|
2015-03-01 06:13:08 -05:00
|
|
|
while (abs(delta) > 0.00001 && (dd < 10000))
|
2015-03-01 06:09:21 -05:00
|
|
|
{
|
|
|
|
dd++;
|
|
|
|
if (delta < 0)
|
|
|
|
{
|
|
|
|
nn++;
|
|
|
|
dd = nn * r;
|
|
|
|
}
|
|
|
|
delta = f * dd - nn;
|
|
|
|
}
|
|
|
|
n = nn;
|
|
|
|
d = dd;
|
|
|
|
return delta;
|
|
|
|
}
|
2015-03-01 06:13:08 -05:00
|
|
|
*/
|
2015-03-01 06:11:21 -05:00
|
|
|
|
2015-03-01 06:09:21 -05:00
|
|
|
/*
|
2015-03-01 06:11:21 -05:00
|
|
|
// LINEAR SEARCH (mirror optimized)
|
2015-03-01 06:13:08 -05:00
|
|
|
// (100x) micros()=
|
2015-03-01 06:11:21 -05:00
|
|
|
// slow but stable version
|
|
|
|
/*
|
|
|
|
double Fraction::fractionize(double f)
|
2015-03-01 06:09:21 -05:00
|
|
|
{
|
|
|
|
long nn = 1, dd = 1;
|
|
|
|
bool inverse = false;
|
2015-03-01 06:11:21 -05:00
|
|
|
|
2015-03-01 06:09:21 -05:00
|
|
|
if (f > 0.5)
|
|
|
|
{
|
|
|
|
f = 1-f;
|
|
|
|
inverse = true;
|
|
|
|
}
|
2015-03-01 06:05:22 -05:00
|
|
|
|
2015-03-01 06:09:21 -05:00
|
|
|
float r = 1 / f;
|
|
|
|
float delta = f * dd - nn;
|
|
|
|
while (abs(delta) > 0.00001 && (dd < 10000))
|
|
|
|
{
|
|
|
|
dd++;
|
|
|
|
if (delta < 0)
|
|
|
|
{
|
|
|
|
nn++;
|
|
|
|
dd = nn * r;
|
|
|
|
}
|
|
|
|
delta = f * dd - nn;
|
|
|
|
}
|
|
|
|
n = inverse?(dd - nn):nn;
|
|
|
|
d = dd;
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-03-01 06:11:21 -05:00
|
|
|
|
2015-03-01 06:13:08 -05:00
|
|
|
// ADD BY DIGIT
|
|
|
|
// - does not find "magic fractions" e.g. pi = 355/113
|
2015-03-01 06:11:21 -05:00
|
|
|
// (100x) micros()=392620
|
2015-03-01 06:09:21 -05:00
|
|
|
/*
|
|
|
|
double Fraction::fractionize(double f) // divide and conquer, simple, small, 2nd fastest
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:05:22 -05:00
|
|
|
Fraction t((long)0);
|
2015-03-01 06:11:21 -05:00
|
|
|
for (long dd = 10; dd < 1000001; dd *= 10)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:05:22 -05:00
|
|
|
f *= 10;
|
|
|
|
int ff = f;
|
|
|
|
t += Fraction(ff, dd);
|
|
|
|
f -= ff;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
2015-03-01 06:05:22 -05:00
|
|
|
n = t.n;
|
|
|
|
d = t.d;
|
2015-03-01 06:09:21 -05:00
|
|
|
return f;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
2015-03-01 06:09:21 -05:00
|
|
|
*/
|
|
|
|
|
2015-03-01 06:11:21 -05:00
|
|
|
|
|
|
|
// Dr. Peterson
|
2015-03-01 06:09:21 -05:00
|
|
|
// - http://mathforum.org/library/drmath/view/51886.html
|
2015-03-01 06:13:08 -05:00
|
|
|
// (100x) micros()=96048
|
|
|
|
// showed errors for very small values around 0
|
2015-03-01 06:11:21 -05:00
|
|
|
double Fraction::fractionize(double val)
|
2015-03-01 06:09:21 -05:00
|
|
|
{ // find nearest fraction
|
|
|
|
double Precision = 0.000001;
|
|
|
|
Fraction low(0, 1); // "A" = 0/1
|
|
|
|
Fraction high(1, 1); // "B" = 1/1
|
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
{
|
|
|
|
double testLow = low.d * val - low.n;
|
|
|
|
double testHigh = high.n - high.d * val;
|
|
|
|
if (testHigh < Precision * high.d)
|
|
|
|
break; // high is answer
|
|
|
|
if (testLow < Precision * low.d)
|
|
|
|
{ // low is answer
|
|
|
|
high = low;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i & 1)
|
|
|
|
{ // odd step: add multiple of low to high
|
|
|
|
double test = testHigh / testLow;
|
2015-03-01 06:11:21 -05:00
|
|
|
int32_t count = (int32_t)test; // "N"
|
|
|
|
int32_t n = (count + 1) * low.n + high.n;
|
|
|
|
int32_t d = (count + 1) * low.d + high.d;
|
2015-03-01 06:09:21 -05:00
|
|
|
if ((n > 0x8000) || (d > 0x10000))
|
|
|
|
break;
|
|
|
|
high.n = n - low.n; // new "A"
|
|
|
|
high.d = d - low.d;
|
|
|
|
low.n = n; // new "B"
|
|
|
|
low.d = d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // even step: add multiple of high to low
|
|
|
|
double test = testLow / testHigh;
|
2015-03-01 06:11:21 -05:00
|
|
|
int32_t count = (int32_t)test; // "N"
|
|
|
|
int32_t n = low.n + (count + 1) * high.n;
|
|
|
|
int32_t d = low.d + (count + 1) * high.d;
|
2015-03-01 06:09:21 -05:00
|
|
|
if ((n > 0x10000) || (d > 0x10000))
|
|
|
|
break;
|
|
|
|
low.n = n - high.n; // new "A"
|
|
|
|
low.d = d - high.d;
|
|
|
|
high.n = n; // new "B"
|
|
|
|
high.d = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n = high.n;
|
|
|
|
d = high.d;
|
|
|
|
}
|
2015-03-01 06:13:08 -05:00
|
|
|
|
2015-03-01 06:09:21 -05:00
|
|
|
|
2015-03-01 06:11:21 -05:00
|
|
|
// BINARY SEARCH
|
2015-03-01 06:09:21 -05:00
|
|
|
// - http://www.gamedev.net/topic/354209-how-do-i-convert-a-decimal-to-a-fraction-in-c/
|
2015-03-01 06:11:21 -05:00
|
|
|
// (100x) micros()=1292452
|
|
|
|
// slower
|
2015-03-01 06:09:21 -05:00
|
|
|
/*
|
|
|
|
double Fraction::fractionize(double value) // size ok, too slow.
|
|
|
|
{
|
|
|
|
int max_denominator = 10000;
|
2015-03-01 06:11:21 -05:00
|
|
|
|
2015-03-01 06:09:21 -05:00
|
|
|
int low_n = 0;
|
|
|
|
int low_d = 1;
|
|
|
|
int high_n = 1;
|
|
|
|
int high_d = 1;
|
|
|
|
int mid_n;
|
|
|
|
int mid_d;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
mid_n = low_n + high_n;
|
|
|
|
mid_d = low_d + high_d;
|
|
|
|
if ( mid_n < value * mid_d )
|
|
|
|
{
|
|
|
|
low_n = mid_n;
|
|
|
|
low_d = mid_d;
|
|
|
|
n = high_n;
|
|
|
|
d = high_d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
high_n = mid_n;
|
|
|
|
high_d = mid_d;
|
|
|
|
n = low_n;
|
|
|
|
d = low_d;
|
|
|
|
}
|
|
|
|
} while ( mid_d <= max_denominator );
|
|
|
|
return 0;
|
2015-03-01 06:11:21 -05:00
|
|
|
}
|
2015-03-01 06:09:21 -05:00
|
|
|
*/
|
2015-03-01 06:05:22 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// END OF FILE
|
|
|
|
//
|