-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfractional-arithmetic.min.js
2 lines (2 loc) · 2.93 KB
/
fractional-arithmetic.min.js
1
2
/*fractional-arithmetic 1.2.1 139d75d "2017-01-12 21:51:17 +0200" */
function NotAFractionError(a){this.name="NotAFractionError",this.message=a||"Not a Fraction"}function Fraction(a,b){if("undefined"==typeof a)throw new NotAFractionError("You must specify a fraction");if(!(this instanceof Fraction))return new Fraction(a,b);if(a instanceof Fraction&&"undefined"==typeof b)return this.n=a.n,void(this.d=a.d);if(isInteger(a)&&isInteger(b))return this.n=parseInt(a),void(this.d=parseInt(b));if(isInteger(a)&&"undefined"==typeof b)return this.n=parseInt(a),void(this.d=1);if("number"==typeof a){var c=""+a,d=c.length-c.indexOf(".")-1;return this.n=parseInt(c.replace(".","")),void(this.d=Math.pow(10,d))}throw new NotAFractionError("Cannot instantiate Fraction("+a+("undefined"==typeof b?"":b)+")")}var isInteger=function(a){return!isNaN(a)&&parseInt(a)==parseFloat(a)};module.exports.isInteger=isInteger;var gcd=function(a,b){a=Math.abs(a),b=Math.abs(b);for(var c;b>0;)c=b,b=a%b,a=c;return a};module.exports.gcd=gcd;var lcm=function(a,b){return a=Math.abs(a),b=Math.abs(b),a*(b/gcd(a,b))};module.exports.lcm=lcm,NotAFractionError.prototype=new Error,NotAFractionError.prototype.constructor=NotAFractionError,Fraction.prototype.toString=Fraction.prototype.toS=Fraction.prototype.inspect=function(){return"("+this.n+"/"+this.d+")"},Fraction.prototype.toNumber=function(){return this.n/this.d},Fraction.prototype.toLatex=function(){return"\\frac{"+this.n+"}{"+this.d+"}"},Fraction.prototype.toMathML=function(){return"<mfrac><mn>"+this.n+"</mn><mn>"+this.d+"</mfrac>"},Fraction.prototype.simplify=function(){this.d<0&&(this.n*=-1,this.d*=-1);var a=gcd(this.n,this.d);return 1==a?this:new Fraction(this.n/a,this.d/a)},Fraction.prototype.inverse=function(){return new Fraction(this.d,this.n)},Fraction.prototype.times=Fraction.prototype.multiply=function(a,b){if(a instanceof Fraction&&"undefined"==typeof b)return new Fraction(this.n*a.n,this.d*a.d).simplify();if(isInteger(a)&&isInteger(b))return this.times(new Fraction(a,b));throw new NotAFractionError("Cannot multiply "+this+" with n="+a+", d="+b)},Fraction.prototype.dividedBy=Fraction.prototype.div=function(a,b){if(a instanceof Fraction&&"undefined"==typeof b)return a.inverse().times(this);if(isInteger(a)&&isInteger(b))return this.times(new Fraction(b,a));throw new NotAFractionError("Cannot divide "+this+" by n="+a+", d="+b)},Fraction.prototype.plus=function(a,b){if(a instanceof Fraction&&"undefined"==typeof b){var c=lcm(this.d,a.d);return new Fraction(this.n*c/this.d+a.n*c/a.d,c)}if(isInteger(a)&&isInteger(b))return this.plus(new Fraction(a,b));throw new NotAFractionError("Cannot add "+this+" to n="+a+", d="+b)},Fraction.prototype.minus=function(a,b){if(a instanceof Fraction&&"undefined"==typeof b){var c=lcm(this.d,a.d);return new Fraction(this.n*c/this.d-a.n*c/a.d,c)}if(isInteger(a)&&isInteger(b))return this.minus(new Fraction(a,b));throw new NotAFractionError("Cannot add "+this+" to n="+a+", d="+b)},module.exports.Fraction=Fraction;