2015-03-01 06:03:22 -05:00
|
|
|
//
|
2018-04-02 14:30:05 -04:00
|
|
|
// FILE: fraction.cpp
|
2015-03-01 06:03:22 -05:00
|
|
|
// AUTHOR: Rob Tillaart
|
2020-11-27 05:16:22 -05:00
|
|
|
// VERSION: 0.1.10
|
|
|
|
// PURPOSE: Arduino library to implement a Fraction datatype
|
|
|
|
// URL: https://github.com/RobTillaart/Fraction
|
2015-03-01 06:03:22 -05:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// TODO
|
|
|
|
// - divide by zero errors
|
|
|
|
// - test extensively
|
2015-03-01 06:09:21 -05:00
|
|
|
//
|
2020-11-27 05:16:22 -05:00
|
|
|
// 0.1.10 2020-06-10 fix library.json
|
|
|
|
// 0.1.9 refactor
|
2018-04-02 14:30:05 -04:00
|
|
|
// 0.1.8 - refactor made constructors explicit; fix issue #33 double --> float
|
2015-03-05 12:26:06 -05:00
|
|
|
// 0.1.07 - major refactoring by Chris-A
|
2015-03-01 06:47:02 -05:00
|
|
|
// 0.1.06 - added proper(), mediant(), angle();
|
2015-03-01 06:15:35 -05:00
|
|
|
// 0.1.05 - tested negative Fractions math, added constructors,
|
|
|
|
// minor refactoring,
|
|
|
|
// 0.1.04 - stabilizing code, add simplify() 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"
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2015-03-01 06:15:35 -05:00
|
|
|
// CONSTRUCTORS
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2018-04-02 14:30:05 -04:00
|
|
|
Fraction::Fraction(double d)
|
|
|
|
{
|
|
|
|
Fraction::split(float(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
Fraction::Fraction(float f)
|
|
|
|
{
|
|
|
|
Fraction::split(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fraction::split(float f)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
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
|
2015-03-01 06:15:35 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
// get robust for small values. (effectively zero)
|
2015-03-01 06:13:08 -05:00
|
|
|
if (abs(f) < 0.00001)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
d = 1;
|
|
|
|
return;
|
|
|
|
}
|
2020-11-27 05:16:22 -05:00
|
|
|
if (int32_t(f) == f)
|
|
|
|
{
|
|
|
|
n = int32_t(f);
|
|
|
|
d = 1;
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 06:15:35 -05:00
|
|
|
// Normalize to 0.0 ... 1.0
|
2018-04-02 14:30:05 -04:00
|
|
|
bool negative = f < 0;
|
|
|
|
if (negative) f = -f;
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
// TODO investigate different strategy:
|
|
|
|
// intpart = int32_t(f); // strip of the integer part.
|
|
|
|
// f = f - intpart; // determine remainder
|
|
|
|
// determine n, d
|
|
|
|
// n += intpart * d; // add integer part * denominator to fraction.
|
|
|
|
|
2018-04-02 14:30:05 -04:00
|
|
|
bool reciproke = f > 1;
|
|
|
|
if (reciproke) f = 1/f;
|
2015-03-01 06:03:22 -05:00
|
|
|
|
|
|
|
fractionize(f);
|
|
|
|
simplify();
|
|
|
|
|
2015-03-01 06:15:35 -05:00
|
|
|
// denormalize
|
2018-04-02 14:30:05 -04:00
|
|
|
if (reciproke)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
int32_t t = n;
|
|
|
|
n = d;
|
|
|
|
d = t;
|
|
|
|
}
|
2018-04-02 14:30:05 -04:00
|
|
|
if (negative)
|
2015-03-01 06:15:35 -05:00
|
|
|
{
|
|
|
|
n = -n;
|
|
|
|
}
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 02:46:55 -05:00
|
|
|
Fraction::Fraction(int32_t p, int32_t q) : n(p), d(q)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
simplify();
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2015-03-01 06:03:22 -05:00
|
|
|
// PRINTING
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2015-03-01 06:03:22 -05:00
|
|
|
size_t Fraction::printTo(Print& p) const
|
|
|
|
{
|
|
|
|
size_t s = 0;
|
2020-11-27 05:16:22 -05:00
|
|
|
// TODO split of sign first
|
|
|
|
//
|
2015-03-01 06:15:35 -05:00
|
|
|
// vs 22/7 => 3_1/7
|
2015-03-01 06:09:21 -05:00
|
|
|
// if (n >= d)
|
|
|
|
// {
|
|
|
|
// s += p.print(n/d, DEC);
|
2015-03-01 06:15:35 -05:00
|
|
|
// s += p.print("_");
|
2015-03-01 06:09:21 -05:00
|
|
|
// }
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2015-03-01 06:03:22 -05:00
|
|
|
// EQUALITIES
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2015-03-05 02:42:23 -05:00
|
|
|
bool Fraction::operator == (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return (n * c.d) == (d * c.n);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
// bool Fraction::operator == (const float &f)
|
|
|
|
// {
|
|
|
|
// Fraction c(f);
|
|
|
|
// return (n * c.d) == (d * c.n);
|
|
|
|
// }
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
bool Fraction::operator != (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return (n * c.d) != (d * c.n);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
bool Fraction::operator > (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
return (n * c.d) > (d * c.n);
|
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
bool Fraction::operator >= (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
return (n * c.d) >= (d * c.n);
|
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
bool Fraction::operator < (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
return (n * c.d) < (d * c.n);
|
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
bool Fraction::operator <= (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
return (n * c.d) <= (d * c.n);
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2015-03-01 06:03:22 -05:00
|
|
|
// NEGATE
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2015-03-01 06:03:22 -05:00
|
|
|
Fraction Fraction::operator - ()
|
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return Fraction(-n, d);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2015-03-01 06:03:22 -05:00
|
|
|
// BASIC MATH
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction Fraction::operator + (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:15:35 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction Fraction::operator - (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:15:35 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction Fraction::operator * (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:09:21 -05:00
|
|
|
return Fraction(n * c.n, d * c.d);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction Fraction::operator / (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:15:35 -05:00
|
|
|
// division by zero returns 0
|
2015-03-01 06:09:21 -05:00
|
|
|
return Fraction(n * c.d, d * c.n);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction& Fraction::operator += (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
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-05 12:26:06 -05:00
|
|
|
return *this;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction& Fraction::operator -= (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
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-05 12:26:06 -05:00
|
|
|
return *this;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction& Fraction::operator *= (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
n *= c.n;
|
|
|
|
d *= c.d;
|
2015-03-01 06:05:22 -05:00
|
|
|
simplify();
|
2015-03-05 12:26:06 -05:00
|
|
|
return *this;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction& Fraction::operator /= (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:15:35 -05:00
|
|
|
// division by zero returns 0
|
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-05 12:26:06 -05:00
|
|
|
return *this;
|
2015-03-01 06:11:21 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 14:30:05 -04:00
|
|
|
float Fraction::toDouble()
|
2015-03-01 06:11:21 -05:00
|
|
|
{
|
2015-03-01 06:47:02 -05:00
|
|
|
return (1.0 * n) / d;
|
2015-03-01 06:13:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// fraction is proper if abs(fraction) < 1
|
2015-03-01 06:47:02 -05:00
|
|
|
bool Fraction::isProper()
|
2015-03-01 06:13:08 -05:00
|
|
|
{
|
|
|
|
return abs(n) < abs(d);
|
|
|
|
}
|
|
|
|
|
2015-03-01 06:47:02 -05:00
|
|
|
// visualize fraction as an angle in degrees
|
2018-04-02 14:30:05 -04:00
|
|
|
float Fraction::toAngle()
|
2015-03-01 06:47:02 -05:00
|
|
|
{
|
|
|
|
return atan2(n, d) * 180.0 / PI;
|
|
|
|
}
|
|
|
|
|
2018-04-02 14:30:05 -04:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2015-03-01 06:47:02 -05:00
|
|
|
// STATIC
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2015-03-01 06:47:02 -05:00
|
|
|
// Mediant - http://www.cut-the-knot.org/Curriculum/Arithmetic/FCExercise.shtml
|
|
|
|
// void Fraction::mediant(Fraction c)
|
|
|
|
// {
|
2020-11-27 05:16:22 -05:00
|
|
|
// n += c.n;
|
|
|
|
// d += c.d;
|
|
|
|
// simplify();
|
2015-03-01 06:47:02 -05:00
|
|
|
// }
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2015-03-01 06:47:02 -05:00
|
|
|
|
|
|
|
// the mediant is a fraction that is always between 2 fractions
|
|
|
|
// at least if within precission.
|
2015-03-05 03:06:21 -05:00
|
|
|
Fraction Fraction::mediant(const Fraction &a, const Fraction &b)
|
2015-03-01 06:47:02 -05:00
|
|
|
{
|
|
|
|
return Fraction(a.n + b.n, a.d + b.d);
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
// the middle is a fraction that is between 2 fractions
|
|
|
|
// at least if within precission.
|
|
|
|
Fraction Fraction::middle(const Fraction &a, const Fraction &b)
|
|
|
|
{
|
|
|
|
return Fraction(a.n*b.d + b.n*a.d, 2 * a.d * b.d);
|
|
|
|
}
|
|
|
|
|
2015-03-01 06:47:02 -05:00
|
|
|
// approximate a fraction with defined denominator
|
|
|
|
// sort of setDenominator(uint16_t den);
|
2015-03-05 03:06:21 -05:00
|
|
|
Fraction Fraction::setDenominator(const Fraction &a, uint16_t b)
|
2015-03-01 06:13:08 -05:00
|
|
|
{
|
2015-03-01 06:47:02 -05:00
|
|
|
int32_t n = round((a.n * b * 1.0) / a.d);
|
|
|
|
int32_t d = b;
|
|
|
|
return Fraction(n, d);
|
2015-03-01 06:13:08 -05:00
|
|
|
}
|
|
|
|
|
2015-03-01 06:03:22 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
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
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2015-03-05 03:29:58 -05:00
|
|
|
int32_t Fraction::gcd(int32_t a , int32_t b)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
|
|
|
while ( a != 0 )
|
|
|
|
{
|
2018-04-02 14:30:05 -04:00
|
|
|
int32_t c = a;
|
2015-03-01 06:03:22 -05:00
|
|
|
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);
|
2020-11-27 05:16:22 -05:00
|
|
|
p = p / x;
|
|
|
|
q = q / x;
|
2015-03-01 06:09:21 -05:00
|
|
|
|
|
|
|
// denominator max 4 digits keeps mul and div simple
|
2015-03-01 06:15:35 -05:00
|
|
|
// in preventing overflow
|
2015-03-01 06:09:21 -05:00
|
|
|
while (q > 10000)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2015-03-01 06:15:35 -05:00
|
|
|
// rounding might need improvement
|
2015-03-01 06:09:21 -05:00
|
|
|
p = (p + 5)/10;
|
|
|
|
q = (q + 5)/10;
|
|
|
|
x = gcd(p, q);
|
2020-11-27 05:16:22 -05:00
|
|
|
p = p / x;
|
|
|
|
q = q / x;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
2020-11-27 05:16:22 -05:00
|
|
|
n = (neg) ? -p : p;
|
2015-03-01 06:09:21 -05:00
|
|
|
d = q;
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2015-03-01 06:11:21 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2018-04-02 14:30:05 -04:00
|
|
|
// fractionize() - finds the fraction representation of a float
|
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
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2018-04-02 14:30:05 -04:00
|
|
|
void Fraction::fractionize(float val)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
|
|
|
// find nearest fraction
|
|
|
|
float Precision = 0.0000001;
|
2015-03-01 06:09:21 -05:00
|
|
|
Fraction low(0, 1); // "A" = 0/1
|
|
|
|
Fraction high(1, 1); // "B" = 1/1
|
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
{
|
2018-04-02 14:30:05 -04:00
|
|
|
float testLow = low.d * val - low.n;
|
|
|
|
float testHigh = high.n - high.d * val;
|
2015-03-01 06:09:21 -05:00
|
|
|
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
|
2018-04-02 14:30:05 -04:00
|
|
|
float 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
|
2018-04-02 14:30:05 -04:00
|
|
|
float 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
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
// -- END OF FILE --
|