fraction.js
Advanced tools
Comparing version 3.0.0 to 3.1.0
{ | ||
"name": "fraction.js", | ||
"main": "fraction.js", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"homepage": "http://www.xarg.org/2014/03/precise-calculations-in-javascript/", | ||
@@ -6,0 +6,0 @@ "description": "A rational number library", |
/** | ||
* @license Fraction.js v3.0.0 09/09/2015 | ||
* @license Fraction.js v3.1.0 09/09/2015 | ||
* http://www.xarg.org/2014/03/precise-calculations-in-javascript/ | ||
@@ -46,3 +46,3 @@ * | ||
// Example: 1/7 = 0.(142857) has 6 repeating decimal places. | ||
// When number gets reduced, long cycles will not be detected and toString() only gets the first 10 digits | ||
// If MAX_CYCLE_LEN gets reduced, long cycles will not be detected and toString() only gets the first 10 digits | ||
var MAX_CYCLE_LEN = 2000; | ||
@@ -210,5 +210,5 @@ | ||
if (B.length <= A) { // Check for more tokens on the stack | ||
d = y * z; | ||
s = /* void */ | ||
n = x + z * (v * y + w); | ||
d = y * z; | ||
n = x + d * v + z * w; | ||
break; | ||
@@ -533,17 +533,8 @@ } | ||
"pow": function(m) { | ||
var d = this["d"]; | ||
var n = this["n"]; | ||
if (m < 0) { | ||
this["d"] = Math.pow(n, -m); | ||
this["n"] = Math.pow(d, -m); | ||
return new Fraction(Math.pow(this['s'] * this["d"],-m), Math.pow(this["n"],-m)); | ||
} else { | ||
this["d"] = Math.pow(d, m); | ||
this["n"] = Math.pow(n, m); | ||
return new Fraction(Math.pow(this['s'] * this["n"], m), Math.pow(this["d"], m)); | ||
} | ||
if (0 === (m % 2)) { | ||
this["s"] = 1; | ||
} | ||
return this; | ||
}, | ||
@@ -582,3 +573,3 @@ | ||
parse(a, b); | ||
return !!(P["n"] * this["d"]) && !((this["n"] * P["d"]) % (P["n"] * this["d"])); | ||
return !(!(P["n"] * this["d"]) || ((this["n"] * P["d"]) % (P["n"] * this["d"]))); | ||
}, | ||
@@ -658,2 +649,24 @@ | ||
}, | ||
/** | ||
* Returns an array of continued fraction elements | ||
* | ||
* Ex: new Fraction("7/8").toContinued() => [0,1,7] | ||
*/ | ||
'toContinued': function() { | ||
var t; | ||
var a = this['n']; | ||
var b = this['d']; | ||
var res = []; | ||
do { | ||
res.push(Math.floor(a / b)); | ||
t = a % b; | ||
a = b; | ||
b = t; | ||
} while(a !== 1); | ||
return res; | ||
}, | ||
@@ -742,3 +755,3 @@ /** | ||
} | ||
})(this); |
/* | ||
Fraction.js v3.0.0 09/09/2015 | ||
Fraction.js v3.1.0 09/09/2015 | ||
http://www.xarg.org/2014/03/precise-calculations-in-javascript/ | ||
@@ -10,8 +10,8 @@ | ||
a&&(l=a,a=-a);if(0===a%1)b=a;else if(0<a){1<=a&&(q=Math.pow(10,Math.floor(1+Math.log(a)/Math.LN10)),a/=q);for(;1E7>=f&&1E7>=r;)if(b=(e+k)/(f+r),a===b){1E7>=f+r?(b=e+k,m=f+r):r>f?(b=k,m=r):(b=e,m=f);break}else a>b?(e+=k,f+=r):(k+=e,r+=f),1E7<f?(b=k,m=r):(b=e,m=f);b*=q}break;case "string":f=a.match(/\d+|./g);"-"===f[e]?(l=-1,e++):"+"===f[e]&&e++;if(f.length===e+1)p=t(f[e++],l);else if("."===f[e+1]||"."===f[e]){"."!==f[e]&&(g=t(f[e++],l));e++;if(e+1===f.length||"("===f[e+1]&&")"===f[e+3]||"'"===f[e+ | ||
1]&&"'"===f[e+3])p=t(f[e],l),h=Math.pow(10,f[e].length),e++;if("("===f[e]&&")"===f[e+2]||"'"===f[e]&&"'"===f[e+2])n=t(f[e+1],l),q=Math.pow(10,f[e+1].length)-1,e+=3}else"/"===f[e+1]||":"===f[e+1]?(p=t(f[e],l),h=t(f[e+2],1),e+=3):"/"===f[e+3]&&" "===f[e+1]&&(g=t(f[e],l),p=t(f[e+2],l),h=t(f[e+4],1),e+=5);if(f.length<=e){l=b=n+q*(g*h+p);m=h*q;break}default:v()}if(!m)throw"DIV/0";c.s=0>l?-1:1;c.n=Math.abs(b);c.d=Math.abs(m)}function t(a,d){isNaN(a=parseInt(a,10))&&v();return a*d}function v(){throw"Invalid Param"; | ||
1]&&"'"===f[e+3])p=t(f[e],l),h=Math.pow(10,f[e].length),e++;if("("===f[e]&&")"===f[e+2]||"'"===f[e]&&"'"===f[e+2])n=t(f[e+1],l),q=Math.pow(10,f[e+1].length)-1,e+=3}else"/"===f[e+1]||":"===f[e+1]?(p=t(f[e],l),h=t(f[e+2],1),e+=3):"/"===f[e+3]&&" "===f[e+1]&&(g=t(f[e],l),p=t(f[e+2],l),h=t(f[e+4],1),e+=5);if(f.length<=e){m=h*q;l=b=n+m*g+q*p;break}default:v()}if(!m)throw"DIV/0";c.s=0>l?-1:1;c.n=Math.abs(b);c.d=Math.abs(m)}function t(a,d){isNaN(a=parseInt(a,10))&&v();return a*d}function v(){throw"Invalid Param"; | ||
}function g(a,d){if(!(this instanceof g))return new g(a,d);k(a,d);g.REDUCE?a=u(c.d,c.n):a=1;this.s=c.s;this.n=c.n/a;this.d=c.d/a}var c={s:1,n:0,d:1};g.REDUCE=1;g.prototype={s:1,n:0,d:1,abs:function(){return new g(this.n,this.d)},neg:function(){return new g(-this.s*this.n,this.d)},add:function(a,d){k(a,d);return new g(this.s*this.n*c.d+c.s*this.d*c.n,this.d*c.d)},sub:function(a,d){k(a,d);return new g(this.s*this.n*c.d-c.s*this.d*c.n,this.d*c.d)},mul:function(a,d){k(a,d);return new g(this.s*c.s*this.n* | ||
c.n,this.d*c.d)},div:function(a,d){k(a,d);return new g(this.s*c.s*this.n*c.d,this.d*c.n)},clone:function(){return new g(this)},mod:function(a,d){if(void 0===a)return new g(this.s*this.n%this.d,1);k(a,d);0===c.n*this.d&&g(0,0);return new g(this.s*c.d*this.n%(c.n*this.d),c.d*this.d)},gcd:function(a,d){k(a,d);return new g(u(c.n,this.n),c.d*this.d/u(c.d,this.d))},lcm:function(a,d){k(a,d);return new g(c.n*this.n/u(c.n,this.n),u(c.d,this.d))},ceil:function(){return new g(Math.ceil(this.s*this.n/this.d), | ||
1)},floor:function(){return new g(Math.floor(this.s*this.n/this.d),1)},round:function(){return new g(Math.round(this.s*this.n/this.d),1)},inverse:function(){return new g(this.s*this.d,this.n)},pow:function(a){var d=this.d,b=this.n;0>a?(this.d=Math.pow(b,-a),this.n=Math.pow(d,-a)):(this.d=Math.pow(d,a),this.n=Math.pow(b,a));0===a%2&&(this.s=1);return this},equals:function(a,d){k(a,d);return this.s*this.n*c.d===c.s*c.n*this.d},compare:function(a,d){k(a,d);var b=this.s*this.n*c.d-c.s*c.n*this.d;return(0< | ||
b)-(0>b)},divisible:function(a,d){k(a,d);return!!(c.n*this.d)&&!(this.n*c.d%(c.n*this.d))},valueOf:function(){return this.s*this.n/this.d},toFraction:function(a){var d,b="",m=this.n,c=this.d;0>this.s&&(b+="-");1===c?b+=m:(a&&0<(d=Math.floor(m/c))&&(b=b+d+" ",m%=c),b+=m,b+="/",b+=c);return b},toLatex:function(a){var d,b="",c=this.n,g=this.d;0>this.s&&(b+="-");1===g?b+=c:(a&&0<(d=Math.floor(c/g))&&(b+=d,c%=g),b=b+"\\frac{"+c,b+="}{",b+=g,b+="}");return b},toString:function(){var a,d=this.n,b=this.d; | ||
g.REDUCE||(a=u(d,b),d/=a,b/=a);a=String(d).split("");var d=0,c=[~this.s?"":"-","",""],l="",k=x(b),p;a:{p=1;var n;n=k;for(var h=10,q=1;0<n;h=h*h%b,n>>=1)n&1&&(q=q*h%b);n=q;for(h=0;300>h;h++){if(p===n){p=h;break a}p=10*p%b;n=10*n%b}p=0}n=-1;for(var h=1,q=10+k+p+a.length,e=0;e<q;e++,d*=10){e<a.length?d+=Number(a[e]):(h=2,n++);if(0<k)if(n===p)c[h]+=l+"(",l="";else if(n===k+p){c[h]+=l+")";break}d>=b?(c[h]+=l+(d/b|0),l="",d%=b):1<h?l+="0":c[h]&&(c[h]+="0")}c[0]+=c[1]||"0";return c[2]?c[0]+"."+c[2]:c[0]}}; | ||
"function"===typeof define&&define.amd?define([],function(){return g}):"object"===typeof exports?module.exports=g:w.Fraction=g})(this); | ||
1)},floor:function(){return new g(Math.floor(this.s*this.n/this.d),1)},round:function(){return new g(Math.round(this.s*this.n/this.d),1)},inverse:function(){return new g(this.s*this.d,this.n)},pow:function(a){return 0>a?new g(Math.pow(this.s*this.d,-a),Math.pow(this.n,-a)):new g(Math.pow(this.s*this.n,a),Math.pow(this.d,a))},equals:function(a,d){k(a,d);return this.s*this.n*c.d===c.s*c.n*this.d},compare:function(a,d){k(a,d);var b=this.s*this.n*c.d-c.s*c.n*this.d;return(0<b)-(0>b)},divisible:function(a, | ||
d){k(a,d);return!(!(c.n*this.d)||this.n*c.d%(c.n*this.d))},valueOf:function(){return this.s*this.n/this.d},toFraction:function(a){var d,b="",m=this.n,c=this.d;0>this.s&&(b+="-");1===c?b+=m:(a&&0<(d=Math.floor(m/c))&&(b=b+d+" ",m%=c),b+=m,b+="/",b+=c);return b},toLatex:function(a){var d,b="",c=this.n,g=this.d;0>this.s&&(b+="-");1===g?b+=c:(a&&0<(d=Math.floor(c/g))&&(b+=d,c%=g),b=b+"\\frac{"+c,b+="}{",b+=g,b+="}");return b},toContinued:function(){var a,d=this.n,b=this.d,c=[];do c.push(Math.floor(d/ | ||
b)),a=d%b,d=b,b=a;while(1!==d);return c},toString:function(){var a,d=this.n,b=this.d;g.REDUCE||(a=u(d,b),d/=a,b/=a);a=String(d).split("");var d=0,c=[~this.s?"":"-","",""],l="",k=x(b),p;a:{p=1;var n;n=k;for(var h=10,q=1;0<n;h=h*h%b,n>>=1)n&1&&(q=q*h%b);n=q;for(h=0;300>h;h++){if(p===n){p=h;break a}p=10*p%b;n=10*n%b}p=0}n=-1;for(var h=1,q=10+k+p+a.length,e=0;e<q;e++,d*=10){e<a.length?d+=+a[e]:(h=2,n++);if(0<k)if(n===p)c[h]+=l+"(",l="";else if(n===k+p){c[h]+=l+")";break}d>=b?(c[h]+=l+(d/b|0),l="",d%= | ||
b):1<h?l+="0":c[h]&&(c[h]+="0")}c[0]+=c[1]||"0";return c[2]?c[0]+"."+c[2]:c[0]}};"function"===typeof define&&define.amd?define([],function(){return g}):"object"===typeof exports?module.exports=g:w.Fraction=g})(this); |
{ | ||
"name": "fraction.js", | ||
"title": "fraction.js", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"homepage": "http://www.xarg.org/2014/03/precise-calculations-in-javascript/", | ||
@@ -12,2 +12,5 @@ "bugs": "https://github.com/infusion/Fraction.js/issues", | ||
"readmeFilename": "README.md", | ||
"directories": { | ||
"example": "examples" | ||
}, | ||
"licenses": [{ | ||
@@ -14,0 +17,0 @@ "type": "MIT", |
@@ -18,3 +18,3 @@ # Fraction.js - ℚ in JavaSript | ||
Fraction(a).div(98).mul(98) // = 1 | ||
Fraction(1).div(98).mul(98) // = 1 | ||
``` | ||
@@ -386,2 +386,11 @@ | ||
Array toContinued() | ||
--- | ||
Gets an array of the fraction represented as a continued fraction. The first element always contains the whole part. | ||
```javascript | ||
var f = new Fraction('88/33'); | ||
var c = f.toContinued(); // [2, 1, 2] | ||
``` | ||
Fraction clone() | ||
@@ -388,0 +397,0 @@ --- |
@@ -618,2 +618,8 @@ var assert = require('assert'); | ||
}, { | ||
label: "", | ||
set: -3, | ||
fn: "pow", | ||
param: -3, | ||
expect: "-0.(037)" | ||
}, { | ||
label: "-3", | ||
@@ -625,2 +631,8 @@ set: -3, | ||
}, { | ||
label: "-3", | ||
set: -3, | ||
fn: "pow", | ||
param: 3, | ||
expect: "-27" | ||
}, { | ||
label: "0^0", | ||
@@ -1055,1 +1067,47 @@ set: 0, | ||
}); | ||
describe('Fraction toContined', function() { | ||
it("Should pass 415/93", function() { | ||
var tmp = new Fraction(415, 93); | ||
assert.equal('4,2,6,7', tmp.toContinued().toString()); | ||
}); | ||
it("Should pass 0/2", function() { | ||
var tmp = new Fraction(0, 2); | ||
assert.equal('0', tmp.toContinued().toString()); | ||
}); | ||
it("Should pass 1/7", function() { | ||
var tmp = new Fraction(1, 7); | ||
assert.equal('0,7', tmp.toContinued().toString()); | ||
}); | ||
it("Should pass 23/88", function() { | ||
var tmp = new Fraction('23/88'); | ||
assert.equal('0,3,1,4,1,3', tmp.toContinued().toString()); | ||
}); | ||
it("Should pass 1/99", function() { | ||
var tmp = new Fraction('1/99'); | ||
assert.equal('0,99', tmp.toContinued().toString()); | ||
}); | ||
it("Should pass 1768/99", function() { | ||
var tmp = new Fraction('1768/99'); | ||
assert.equal('17,1,6,14', tmp.toContinued().toString()); | ||
}); | ||
it("Should pass 1768/99", function() { | ||
var tmp = new Fraction('7/8'); | ||
assert.equal('0,1,7', tmp.toContinued().toString()); | ||
}); | ||
}); |
72762
1842
481