Ratio.js
Ratio.js copied to clipboard
Alternative method for .toQuantityOf()
http://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations
This might work.
Ratio.prototype.getContinuedFraction = function(){
var arr = [],
a = Math.abs(this.numerator),
b = this.denominator,
c = Math.floor(a/b),
limit = 400,
tmp;
while( limit-- && isFinite(a/b) && 0 < a/b ){
c = Math.floor(a/b);
arr.push( c );
a -= b * c;
tmp = a;
a = b;
b = tmp;
}
return arr;
};
I'm not sure how to use continued fractions. Need more research.
Transverse the Stern-Brocot tree to find the best approximation.