Ratio.js icon indicating copy to clipboard operation
Ratio.js copied to clipboard

Alternative method for .toQuantityOf()

Open LarryBattle opened this issue 13 years ago • 3 comments

http://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations

LarryBattle avatar Nov 13 '12 22:11 LarryBattle

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;
};

LarryBattle avatar Dec 04 '12 23:12 LarryBattle

I'm not sure how to use continued fractions. Need more research.

LarryBattle avatar Dec 10 '12 04:12 LarryBattle

Transverse the Stern-Brocot tree to find the best approximation.

LarryBattle avatar Aug 12 '13 02:08 LarryBattle