LarryBattle / Ratio.js

Rational numbers for Javascript
http://larrybattle.github.com/Ratio.js/
MIT License
113 stars 9 forks source link

valueOf improvement #28

Closed LarryBattle closed 11 years ago

LarryBattle commented 11 years ago

Does this help .valueOf() produce better values when computing a/b where a and b are e notated?

valueOf : function () {
    var top = this.numerator,
        bottom = this.denominator;
    if( 1e21 <= top && 1e21 <= bottom && top < Infinity && bottom < Infinity ){
        var arr = (top).toString().split("e"),
            arr2 = (bottom).toString().split("e");
        arr[1] = Number(arr[1]) + (-1 * arr2[1]);
        top = Number( arr.join("e") );
        bottom = Number(arr2[0]);
    }
    return top/bottom;
},
LarryBattle commented 11 years ago

Implemented Ratio.simplifyENotation() for Ratio.prototype.valueOf() in Version 0.3.10. Need more testing to see how it goes though.