17 lines
5.0 KiB
JavaScript
17 lines
5.0 KiB
JavaScript
/*
|
|
Fraction.js v4.0.0 09/09/2015
|
|
http://www.xarg.org/2014/03/rational-numbers-in-javascript/
|
|
|
|
Copyright (c) 2015, Robert Eisele (robert@xarg.org)
|
|
Dual licensed under the MIT or GPL Version 2 licenses.
|
|
*/
|
|
(function(w){function q(a,c){if(!a)return c;if(!c)return a;for(;;){a%=c;if(!a)return c;c%=a;if(!c)return a}}function x(a){for(;0===a%2;a/=2);for(;0===a%5;a/=5);if(1===a)return 0;for(var c=10%a,d=1;1!==c;d++)if(c=10*c%a,2E3<d)return 0;return d}function k(a,c){var d=0,l=1,e=1,k=0,m=0,n=0,h=1,r=1,f=0,g=1,q=1,t=1;if(void 0!==a&&null!==a)if(void 0!==c)d=a,l=c,e=d*l;else switch(typeof a){case "object":if("d"in a&&"n"in a)d=a.n,l=a.d,"s"in a&&(d*=a.s);else if(0 in a)d=a[0],1 in a&&(l=a[1]);else throw new u;
|
|
e=d*l;break;case "number":0>a&&(e=a,a=-a);if(0===a%1)d=a;else if(0<a){1<=a&&(r=Math.pow(10,Math.floor(1+Math.log(a)/Math.LN10)),a/=r);for(;1E7>=g&&1E7>=t;)if(d=(f+q)/(g+t),a===d){1E7>=g+t?(d=f+q,l=g+t):t>g?(d=q,l=t):(d=f,l=g);break}else a>d?(f+=q,g+=t):(q+=f,t+=g),1E7<g?(d=q,l=t):(d=f,l=g);d*=r}else if(isNaN(a)||isNaN(c))l=d=NaN;break;case "string":g=a.match(/\d+|./g);"-"===g[f]?(e=-1,f++):"+"===g[f]&&f++;if(g.length===f+1)m=p(g[f++],e);else if("."===g[f+1]||"."===g[f]){"."!==g[f]&&(k=p(g[f++],e));
|
|
f++;if(f+1===g.length||"("===g[f+1]&&")"===g[f+3]||"'"===g[f+1]&&"'"===g[f+3])m=p(g[f],e),h=Math.pow(10,g[f].length),f++;if("("===g[f]&&")"===g[f+2]||"'"===g[f]&&"'"===g[f+2])n=p(g[f+1],e),r=Math.pow(10,g[f+1].length)-1,f+=3}else"/"===g[f+1]||":"===g[f+1]?(m=p(g[f],e),h=p(g[f+2],1),f+=3):"/"===g[f+3]&&" "===g[f+1]&&(k=p(g[f],e),m=p(g[f+2],e),h=p(g[f+4],1),f+=5);if(g.length<=f){l=h*r;e=d=n+l*k+r*m;break}default:throw new u;}if(0===l)throw new y;b.s=0>e?-1:1;b.n=Math.abs(d);b.d=Math.abs(l)}function v(a){function c(){}
|
|
function d(){var c=Error.apply(this,arguments);c.name=this.name=a;this.stack=c.stack;this.message=c.message}c.prototype=Error.prototype;d.prototype=new c;return d}function p(a,c){if(isNaN(a=parseInt(a,10)))throw new u;return a*c}function e(a,c){if(!(this instanceof e))return new e(a,c);k(a,c);a=e.REDUCE?q(b.d,b.n):1;this.s=b.s;this.n=b.n/a;this.d=b.d/a}var b={s:1,n:0,d:1},y=e.DivisionByZero=v("DivisionByZero"),u=e.InvalidParameter=v("InvalidParameter");e.REDUCE=1;e.prototype={s:1,n:0,d:1,abs:function(){return new e(this.n,
|
|
this.d)},neg:function(){return new e(-this.s*this.n,this.d)},add:function(a,c){k(a,c);return new e(this.s*this.n*b.d+b.s*this.d*b.n,this.d*b.d)},sub:function(a,c){k(a,c);return new e(this.s*this.n*b.d-b.s*this.d*b.n,this.d*b.d)},mul:function(a,c){k(a,c);return new e(this.s*b.s*this.n*b.n,this.d*b.d)},div:function(a,c){k(a,c);return new e(this.s*b.s*this.n*b.d,this.d*b.n)},clone:function(){return new e(this)},mod:function(a,c){if(isNaN(this.n)||isNaN(this.d))return new e(NaN);if(void 0===a)return new e(this.s*
|
|
this.n%this.d,1);k(a,c);0===b.n&&0===this.d&&e(0,0);return new e(this.s*b.d*this.n%(b.n*this.d),b.d*this.d)},gcd:function(a,c){k(a,c);return new e(q(b.n,this.n),b.d*this.d/q(b.d,this.d))},lcm:function(a,c){k(a,c);return 0===b.n&&0===this.n?new e:new e(b.n*this.n/q(b.n,this.n),q(b.d,this.d))},ceil:function(a){a=Math.pow(10,a||0);return isNaN(this.n)||isNaN(this.d)?new e(NaN):new e(Math.ceil(a*this.s*this.n/this.d),a)},floor:function(a){a=Math.pow(10,a||0);return isNaN(this.n)||isNaN(this.d)?new e(NaN):
|
|
new e(Math.floor(a*this.s*this.n/this.d),a)},round:function(a){a=Math.pow(10,a||0);return isNaN(this.n)||isNaN(this.d)?new e(NaN):new e(Math.round(a*this.s*this.n/this.d),a)},inverse:function(){return new e(this.s*this.d,this.n)},pow:function(a){return 0>a?new e(Math.pow(this.s*this.d,-a),Math.pow(this.n,-a)):new e(Math.pow(this.s*this.n,a),Math.pow(this.d,a))},equals:function(a,c){k(a,c);return this.s*this.n*b.d===b.s*b.n*this.d},compare:function(a,c){k(a,c);var d=this.s*this.n*b.d-b.s*b.n*this.d;
|
|
return(0<d)-(0>d)},divisible:function(a,c){k(a,c);return!(!(b.n*this.d)||this.n*b.d%(b.n*this.d))},valueOf:function(){return this.s*this.n/this.d},toFraction:function(a){var c,d="",e=this.n,b=this.d;0>this.s&&(d+="-");1===b?d+=e:(a&&0<(c=Math.floor(e/b))&&(d=d+c+" ",e%=b),d=d+e+"/",d+=b);return d},toLatex:function(a){var c,d="",e=this.n,b=this.d;0>this.s&&(d+="-");1===b?d+=e:(a&&0<(c=Math.floor(e/b))&&(d+=c,e%=b),d=d+"\\frac{"+e+"}{"+b,d+="}");return d},toContinued:function(){var a,c=this.n,d=this.d,
|
|
e=[];do e.push(Math.floor(c/d)),a=c%d,c=d,d=a;while(1!==c);return e},toString:function(){var a,c=this.n,d=this.d;if(isNaN(c)||isNaN(d))return"NaN";e.REDUCE||(a=q(c,d),c/=a,d/=a);a=String(c).split("");var c=0,b=[~this.s?"":"-","",""],k="",p=x(d),m;a:{m=1;var n;n=p;for(var h=10,r=1;0<n;h=h*h%d,n>>=1)n&1&&(r=r*h%d);n=r;for(h=0;300>h;h++){if(m===n){m=h;break a}m=10*m%d;n=10*n%d}m=0}n=-1;for(var h=1,r=15+p+m+a.length,f=0;f<r;f++,c*=10){f<a.length?c+=Number(a[f]):(h=2,n++);if(0<p)if(n===m)b[h]+=k+"(",k=
|
|
"";else if(n===p+m){b[h]+=k+")";break}c>=d?(b[h]+=k+(c/d|0),k="",c%=d):1<h?k+="0":b[h]&&(b[h]+="0")}b[0]+=b[1]||"0";return b[2]?b[0]+"."+b[2]:b[0]}};"function"===typeof define&&define.amd?define([],function(){return e}):"object"===typeof exports?module.exports=e:w.Fraction=e})(this); |