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
|
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
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2015-03-01 06:03:22 -05:00
|
|
|
|
|
|
|
#include "fraction.h"
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2023-02-03 07:14:59 -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));
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2018-04-02 14:30:05 -04:00
|
|
|
Fraction::Fraction(float f)
|
|
|
|
{
|
|
|
|
Fraction::split(f);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2018-04-02 14:30:05 -04:00
|
|
|
void Fraction::split(float f)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2023-02-03 07:14:59 -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. (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;
|
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
if (int32_t(f) == f)
|
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
n = int32_t(f);
|
|
|
|
d = 1;
|
2021-01-29 06:31:58 -05:00
|
|
|
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;
|
2021-12-18 08:10:11 -05:00
|
|
|
|
2023-02-03 07:14:59 -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.
|
2021-12-18 08:10:11 -05:00
|
|
|
|
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();
|
|
|
|
|
2023-02-03 07:14:59 -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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2023-02-03 07:14:59 -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;
|
2023-02-03 07:14:59 -05:00
|
|
|
// TODO split of sign first
|
2021-01-29 06:31:58 -05:00
|
|
|
//
|
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;
|
|
|
|
};
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2023-02-03 07:14:59 -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)
|
|
|
|
// {
|
2021-01-29 06:31:58 -05:00
|
|
|
// Fraction c(f);
|
2020-11-27 05:16:22 -05:00
|
|
|
// return (n * c.d) == (d * c.n);
|
|
|
|
// }
|
|
|
|
|
2021-11-01 10:56:22 -04: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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2023-02-03 07:14:59 -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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2023-02-03 07:14:59 -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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction Fraction::operator * (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2021-12-18 08:10:11 -05:00
|
|
|
return Fraction(n * c.n, d * c.d);
|
2015-03-01 06:03:22 -05:00
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction Fraction::operator / (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2023-02-03 07:14:59 -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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04: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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2015-03-05 02:42:23 -05:00
|
|
|
Fraction& Fraction::operator /= (const Fraction &c)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2023-02-03 07:14:59 -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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
double Fraction::toDouble()
|
2015-03-01 06:11:21 -05:00
|
|
|
{
|
2023-02-03 07:14:59 -05:00
|
|
|
return double(n) / d;
|
2015-03-01 06:13:08 -05:00
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
float Fraction::toFloat()
|
|
|
|
{
|
|
|
|
return float(n) / d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -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
|
|
|
{
|
2023-02-03 07:14:59 -05:00
|
|
|
return atan2(n, d) * (180.0 / PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t Fraction::nominator()
|
|
|
|
{
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t Fraction::denominator()
|
|
|
|
{
|
|
|
|
return d;
|
2015-03-01 06:47:02 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 14:30:05 -04:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2023-02-03 07:14:59 -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
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// the mediant is a fraction that is always between 2 fractions
|
|
|
|
// at least if within precision.
|
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);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// the middle is a fraction that is between 2 fractions
|
|
|
|
// at least if within precision.
|
2020-11-27 05:16:22 -05:00
|
|
|
Fraction Fraction::middle(const Fraction &a, const Fraction &b)
|
|
|
|
{
|
|
|
|
return Fraction(a.n*b.d + b.n*a.d, 2 * a.d * b.d);
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -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
|
|
|
//////////////////////////////////////
|
|
|
|
//
|
2023-02-03 07:14:59 -05:00
|
|
|
// PROTECTED
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// not that simple ...
|
2015-03-01 06:03:22 -05:00
|
|
|
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
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// denominator max 4 digits keeps mul and div simple
|
|
|
|
// in preventing overflow
|
2015-03-01 06:09:21 -05:00
|
|
|
while (q > 10000)
|
2015-03-01 06:03:22 -05:00
|
|
|
{
|
2023-02-03 07:14:59 -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
|
|
|
}
|
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2015-03-01 06:11:21 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2020-11-27 05:16:22 -05:00
|
|
|
//
|
2023-02-03 07:14:59 -05:00
|
|
|
// fractionize() - finds the fraction representation of a float
|
|
|
|
// PRE: 0 <= f < 1.0
|
2015-03-01 06:11:21 -05:00
|
|
|
//
|
2023-02-03 07:14:59 -05:00
|
|
|
// minimalistic is fast and small
|
2015-03-01 06:11:21 -05:00
|
|
|
//
|
2023-02-03 07:14:59 -05:00
|
|
|
// check for a discussion found later
|
|
|
|
// - http://mathforum.org/library/drmath/view/51886.html
|
|
|
|
// - http://www.gamedev.net/topic/354209-how-do-i-convert-a-decimal-to-a-fraction-in-c/
|
2015-03-01 06:05:22 -05:00
|
|
|
//
|
2015-03-01 06:11:21 -05:00
|
|
|
|
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// Dr. Peterson
|
|
|
|
// - http://mathforum.org/library/drmath/view/51886.html
|
|
|
|
// (100x) micros()=96048
|
|
|
|
// showed errors for very small values around 0
|
2018-04-02 14:30:05 -04:00
|
|
|
void Fraction::fractionize(float val)
|
2021-12-18 08:10:11 -05:00
|
|
|
{
|
2023-02-03 07:14:59 -05:00
|
|
|
// find nearest fraction
|
2020-11-27 05:16:22 -05:00
|
|
|
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)
|
2023-02-03 07:14:59 -05:00
|
|
|
break; // high is answer
|
2015-03-01 06:09:21 -05:00
|
|
|
if (testLow < Precision * low.d)
|
2023-02-03 07:14:59 -05:00
|
|
|
{ // low is answer
|
2015-03-01 06:09:21 -05:00
|
|
|
high = low;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i & 1)
|
2023-02-03 07:14:59 -05:00
|
|
|
{ // odd step: add multiple of low to high
|
2018-04-02 14:30:05 -04:00
|
|
|
float test = testHigh / testLow;
|
2023-02-03 07:14:59 -05:00
|
|
|
int32_t count = (int32_t)test; // "N"
|
2015-03-01 06:11:21 -05:00
|
|
|
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;
|
2023-02-03 07:14:59 -05:00
|
|
|
high.n = n - low.n; // new "A"
|
2015-03-01 06:09:21 -05:00
|
|
|
high.d = d - low.d;
|
2023-02-03 07:14:59 -05:00
|
|
|
low.n = n; // new "B"
|
2015-03-01 06:09:21 -05:00
|
|
|
low.d = d;
|
|
|
|
}
|
|
|
|
else
|
2023-02-03 07:14:59 -05:00
|
|
|
{ // even step: add multiple of high to low
|
2018-04-02 14:30:05 -04:00
|
|
|
float test = testLow / testHigh;
|
2023-02-03 07:14:59 -05:00
|
|
|
int32_t count = (int32_t)test; // "N"
|
2015-03-01 06:11:21 -05:00
|
|
|
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;
|
2023-02-03 07:14:59 -05:00
|
|
|
low.n = n - high.n; // new "A"
|
2015-03-01 06:09:21 -05:00
|
|
|
low.d = d - high.d;
|
2023-02-03 07:14:59 -05:00
|
|
|
high.n = n; // new "B"
|
2015-03-01 06:09:21 -05:00
|
|
|
high.d = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n = high.n;
|
|
|
|
d = high.d;
|
|
|
|
}
|
2015-03-01 06:13:08 -05:00
|
|
|
|
2021-11-01 10:56:22 -04:00
|
|
|
|
2023-02-03 07:14:59 -05:00
|
|
|
// -- END OF FILE --
|
2021-11-01 10:56:22 -04:00
|
|
|
|