Socket
Socket
Sign inDemoInstall

fraction.js

Package Overview
Dependencies
0
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.4 to 4.0.5

2

bower.json
{
"name": "fraction.js",
"main": "fraction.js",
"version": "4.0.4",
"version": "4.0.5",
"homepage": "http://www.xarg.org/2014/03/rational-numbers-in-javascript/",

@@ -6,0 +6,0 @@ "description": "A rational number library",

/**
* @license Fraction.js v4.0.4 09/09/2015
* @license Fraction.js v4.0.5 09/09/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/

@@ -35,3 +35,3 @@ *

*
* var f = new Fraction("9.4'31'");
* const f = new Fraction("9.4'31'");
* f.mul([-4, 3]).div(4.9);

@@ -41,3 +41,3 @@ *

(function (root) {
(function(root) {

@@ -49,6 +49,6 @@ "use strict";

// 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;
const MAX_CYCLE_LEN = 2000;
// Parsed data to avoid calling "new" all the time
var P = {
let P = {
"s": 1,

@@ -60,10 +60,16 @@ "n": 0,

function createError(name) {
var errorConstructor = function () {
var temp = Error.apply(this, arguments);
temp.name = this.name = name;
this.stack = temp.stack;
this.message = temp.message;
};
var IntermediateInheritor = function () {};
function errorConstructor() {
const temp = Error.apply(this, arguments);
temp['name'] = this['name'] = name;
this['stack'] = temp['stack'];
this['message'] = temp['message'];
}
/**
* Error constructor
*
* @constructor
*/
function IntermediateInheritor() {}
IntermediateInheritor.prototype = Error.prototype;

@@ -75,4 +81,4 @@ errorConstructor.prototype = new IntermediateInheritor();

var DivisionByZero = Fraction['DivisionByZero'] = createError('DivisionByZero');
var InvalidParameter = Fraction['InvalidParameter'] = createError('InvalidParameter');
const DivisionByZero = Fraction['DivisionByZero'] = createError('DivisionByZero');
const InvalidParameter = Fraction['InvalidParameter'] = createError('InvalidParameter');

@@ -91,12 +97,12 @@ function assign(n, s) {

var parse = function (p1, p2) {
const parse = function(p1, p2) {
var n = 0, d = 1, s = 1;
var v = 0, w = 0, x = 0, y = 1, z = 1;
let n = 0, d = 1, s = 1;
let v = 0, w = 0, x = 0, y = 1, z = 1;
var A = 0, B = 1;
var C = 1, D = 1;
let A = 0, B = 1;
let C = 1, D = 1;
var N = 10000000;
var M;
let N = 10000000;
let M;

@@ -118,3 +124,3 @@ if (p1 === undefined || p1 === null) {

if ("s" in p1)
n*= p1["s"];
n *= p1["s"];
} else if (0 in p1) {

@@ -143,3 +149,3 @@ n = p1[0];

z = Math.pow(10, Math.floor(1 + Math.log(p1) / Math.LN10));
p1/= z;
p1 /= z;
}

@@ -169,7 +175,7 @@

if (p1 > M) {
A+= C;
B+= D;
A += C;
B += D;
} else {
C+= A;
D+= B;
C += A;
D += B;
}

@@ -186,3 +192,3 @@

}
n*= z;
n *= z;
} else if (isNaN(p1) || isNaN(p2)) {

@@ -196,3 +202,3 @@ d = n = NaN;

B = p1.match(/\d+|./g);
if (B === null)

@@ -228,3 +234,3 @@ throwInvalidParam();

z = Math.pow(10, B[A + 1].length) - 1;
A+= 3;
A += 3;
}

@@ -235,3 +241,3 @@

y = assign(B[A + 2], 1);
A+= 3;
A += 3;
} else if (B[A + 3] === '/' && B[A + 1] === ' ') { // Check for a complex fraction "123 1/2"

@@ -241,3 +247,3 @@ v = assign(B[A], s);

y = assign(B[A + 4], 1);
A+= 5;
A += 5;
}

@@ -267,5 +273,6 @@

var modpow = function (b, e, m) {
function modpow(b, e, m) {
for (var r = 1; e > 0; b = (b * b) % m, e >>= 1) {
let r = 1;
for (; e > 0; b = (b * b) % m, e >>= 1) {

@@ -277,11 +284,14 @@ if (e & 1) {

return r;
};
}
var cycleLen = function (n, d) {
function cycleLen(n, d) {
for (; d % 2 === 0;
d/= 2) {}
d /= 2) {
}
for (; d % 5 === 0;
d/= 5) {}
d /= 5) {
}

@@ -296,5 +306,6 @@ if (d === 1) // Catch non-cyclic numbers

var rem = 10 % d;
let rem = 10 % d;
let t = 1;
for (var t = 1; rem !== 1; t++) {
for (; rem !== 1; t++) {
rem = rem * 10 % d;

@@ -306,10 +317,11 @@

return t;
};
}
var cycleStart = function (n, d, len) {
var rem1 = 1;
var rem2 = modpow(10, len, d);
function cycleStart(n, d, len) {
for (var t = 0; t < 300; t++) { // s < ~log10(Number.MAX_VALUE)
let rem1 = 1;
let rem2 = modpow(10, len, d);
for (let t = 0; t < 300; t++) { // s < ~log10(Number.MAX_VALUE)
// Solve 10^s == 10^(s+t) (mod d)

@@ -324,14 +336,18 @@

return 0;
};
}
var gcd = function (a, b) {
function gcd(a, b) {
if (!a) return b;
if (!b) return a;
if (!a)
return b;
if (!b)
return a;
while (1) {
a%= b;
if (!a) return b;
b%= a;
if (!b) return a;
a %= b;
if (!a)
return b;
b %= a;
if (!b)
return a;
}

@@ -344,3 +360,3 @@ };

* @constructor
* @param {number|Fraction} a
* @param {number|Fraction=} a
* @param {number=} b

@@ -384,3 +400,3 @@ */

**/
"abs": function () {
"abs": function() {

@@ -395,3 +411,3 @@ return new Fraction(this["n"], this["d"]);

**/
"neg": function () {
"neg": function() {

@@ -406,3 +422,3 @@ return new Fraction(-this["s"] * this["n"], this["d"]);

**/
"add": function (a, b) {
"add": function(a, b) {

@@ -421,3 +437,3 @@ parse(a, b);

**/
"sub": function (a, b) {
"sub": function(a, b) {

@@ -436,3 +452,3 @@ parse(a, b);

**/
"mul": function (a, b) {
"mul": function(a, b) {

@@ -451,3 +467,3 @@ parse(a, b);

**/
"div": function (a, b) {
"div": function(a, b) {

@@ -466,3 +482,3 @@ parse(a, b);

**/
"clone": function () {
"clone": function() {
return new Fraction(this);

@@ -476,3 +492,3 @@ },

**/
"mod": function (a, b) {
"mod": function(a, b) {

@@ -507,3 +523,3 @@ if (isNaN(this['n']) || isNaN(this['d'])) {

return new Fraction(
(this["s"] * P["d"] * this["n"]) % (P["n"] * this["d"]),
this["s"] * (P["d"] * this["n"]) % (P["n"] * this["d"]),
P["d"] * this["d"]

@@ -518,3 +534,3 @@ );

*/
"gcd": function (a, b) {
"gcd": function(a, b) {

@@ -525,3 +541,3 @@ parse(a, b);

return new Fraction(gcd(P["n"], this["n"]), P["d"] * this["d"] / gcd(P["d"], this["d"]));
return new Fraction(gcd(P["n"], this["n"]) * gcd(P["d"], this["d"]), P["d"] * this["d"]);
},

@@ -534,3 +550,3 @@

*/
"lcm": function (a, b) {
"lcm": function(a, b) {

@@ -544,3 +560,3 @@ parse(a, b);

}
return new Fraction(P["n"] * this["n"] / gcd(P["n"], this["n"]), gcd(P["d"], this["d"]));
return new Fraction(P["n"] * this["n"], gcd(P["n"], this["n"]) * gcd(P["d"], this["d"]));
},

@@ -553,3 +569,3 @@

**/
"ceil": function (places) {
"ceil": function(places) {

@@ -569,3 +585,3 @@ places = Math.pow(10, places || 0);

**/
"floor": function (places) {
"floor": function(places) {

@@ -585,3 +601,3 @@ places = Math.pow(10, places || 0);

**/
"round": function (places) {
"round": function(places) {

@@ -601,3 +617,3 @@ places = Math.pow(10, places || 0);

**/
"inverse": function () {
"inverse": function() {

@@ -612,3 +628,3 @@ return new Fraction(this["s"] * this["d"], this["n"]);

*/
"pow": function (m) {
"pow": function(m) {

@@ -627,3 +643,3 @@ if (m < 0) {

**/
"equals": function (a, b) {
"equals": function(a, b) {

@@ -639,9 +655,36 @@ parse(a, b);

**/
"compare": function (a, b) {
"compare": function(a, b) {
parse(a, b);
var t = (this["s"] * this["n"] * P["d"] - P["s"] * P["n"] * this["d"]);
const t = (this["s"] * this["n"] * P["d"] - P["s"] * P["n"] * this["d"]);
return (0 < t) - (t < 0);
},
"simplify": function(eps) {
// First naive implementation, needs improvement
if (isNaN(this['n']) || isNaN(this['d'])) {
return this;
}
var cont = this['abs']()['toContinued']();
eps = eps || 0.001;
function rec(a) {
if (a.length === 1)
return new Fraction(a[0]);
return rec(a.slice(1))['inverse']()['add'](a[0]);
}
for (var i = 0; i < cont.length; i++) {
var tmp = rec(cont.slice(0, i + 1));
if (tmp['sub'](this['abs']())['abs']().valueOf() < eps) {
return tmp['mul'](this['s']);
}
}
return this;
},
/**

@@ -652,3 +695,3 @@ * Check if two rational numbers are divisible

*/
"divisible": function (a, b) {
"divisible": function(a, b) {

@@ -664,3 +707,3 @@ parse(a, b);

**/
'valueOf': function () {
'valueOf': function() {

@@ -675,24 +718,24 @@ return this["s"] * this["n"] / this["d"];

**/
'toFraction': function (excludeWhole) {
'toFraction': function(excludeWhole) {
var whole, str = "";
var n = this["n"];
var d = this["d"];
let whole, str = "";
let n = this["n"];
let d = this["d"];
if (this["s"] < 0) {
str+= '-';
str += '-';
}
if (d === 1) {
str+= n;
str += n;
} else {
if (excludeWhole && (whole = Math.floor(n / d)) > 0) {
str+= whole;
str+= " ";
n%= d;
str += whole;
str += " ";
n %= d;
}
str+= n;
str+= '/';
str+= d;
str += n;
str += '/';
str += d;
}

@@ -707,25 +750,25 @@ return str;

**/
'toLatex': function (excludeWhole) {
'toLatex': function(excludeWhole) {
var whole, str = "";
var n = this["n"];
var d = this["d"];
let whole, str = "";
let n = this["n"];
let d = this["d"];
if (this["s"] < 0) {
str+= '-';
str += '-';
}
if (d === 1) {
str+= n;
str += n;
} else {
if (excludeWhole && (whole = Math.floor(n / d)) > 0) {
str+= whole;
n%= d;
str += whole;
n %= d;
}
str+= "\\frac{";
str+= n;
str+= '}{';
str+= d;
str+= '}';
str += "\\frac{";
str += n;
str += '}{';
str += d;
str += '}';
}

@@ -740,9 +783,13 @@ return str;

*/
'toContinued': function () {
'toContinued': function() {
var t;
var a = this['n'];
var b = this['d'];
var res = [];
let t;
let a = this['n'];
let b = this['d'];
const res = [];
if (isNaN(this['n']) || isNaN(this['d'])) {
return res;
}
do {

@@ -763,7 +810,7 @@ res.push(Math.floor(a / b));

**/
'toString': function () {
'toString': function() {
var g;
var N = this["n"];
var D = this["d"];
let g;
let N = this["n"];
let D = this["d"];

@@ -776,40 +823,40 @@ if (isNaN(N) || isNaN(D)) {

g = gcd(N, D);
N/= g;
D/= g;
N /= g;
D /= g;
}
var dec = 15; // 15 = decimal places when no repitation
let dec = 15; // 15 = decimal places when no repitation
var cycLen = cycleLen(N, D); // Cycle length
var cycOff = cycleStart(N, D, cycLen); // Cycle start
let cycLen = cycleLen(N, D); // Cycle length
let cycOff = cycleStart(N, D, cycLen); // Cycle start
var str = this['s'] === -1 ? "-" : "";
let str = this['s'] === -1 ? "-" : "";
str+= N / D | 0;
str += N / D | 0;
N%= D;
N*= 10;
N %= D;
N *= 10;
if (N)
str+= ".";
str += ".";
if (cycLen) {
for (var i = cycOff; i--; ) {
str+= N / D | 0;
N%= D;
N*= 10;
for (let i = cycOff; i--; ) {
str += N / D | 0;
N %= D;
N *= 10;
}
str+= "(";
for (var i = cycLen; i--; ) {
str+= N / D | 0;
N%= D;
N*= 10;
str += "(";
for (let i = cycLen; i--; ) {
str += N / D | 0;
N %= D;
N *= 10;
}
str+= ")";
str += ")";
} else {
for (var i = dec; N && i--; ) {
str+= N / D | 0;
N%= D;
N*= 10;
for (let i = dec; N && i--; ) {
str += N / D | 0;
N %= D;
N *= 10;
}

@@ -822,7 +869,10 @@ }

if (typeof define === "function" && define["amd"]) {
define([], function () {
define([], function() {
return Fraction;
});
} else if (typeof exports === "object") {
module["exports"] = Fraction;
Object.defineProperty(exports, "__esModule", {'value': true});
module['exports'] = Fraction;
exports['Angles'] = Fraction;
exports['default'] = Fraction;
} else {

@@ -829,0 +879,0 @@ root['Fraction'] = Fraction;

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

@@ -6,0 +6,0 @@ "bugs": "https://github.com/infusion/Fraction.js/issues",

@@ -105,14 +105,14 @@ # Fraction.js - ℚ in JavaSript

var c = new Fraction(a).add(b).div(2);
var c = new Fraction(a).add(b).div(2);
console.log(n + "\t" + a.n + "/" + a.d + "\t" + b.n + "/" + b.d + "\t" + c.n + "/" + c.d + "\t" + x);
console.log(n + "\t" + a.n + "/" + a.d + "\t" + b.n + "/" + b.d + "\t" + c.n + "/" + c.d + "\t" + x);
if (c.add(2).pow(2) < 5) {
a = c;
x = "1";
} else {
b = c;
x = "0";
}
s+= x;
if (c.add(2).pow(2) < 5) {
a = c;
x = "1";
} else {
b = c;
x = "0";
}
s+= x;
}

@@ -125,14 +125,14 @@ console.log(s)

```
n a[n] b[n] c[n] x[n]
0 0/1 1/1 1/2 /
1 0/1 1/2 1/4 0
2 0/1 1/4 1/8 0
3 1/8 1/4 3/16 1
4 3/16 1/4 7/32 1
5 7/32 1/4 15/64 1
6 15/64 1/4 31/128 1
7 15/64 31/128 61/256 0
8 15/64 61/256 121/512 0
9 15/64 121/512 241/1024 0
10 241/1024 121/512 483/2048 1
n a[n] b[n] c[n] x[n]
0 0/1 1/1 1/2 /
1 0/1 1/2 1/4 0
2 0/1 1/4 1/8 0
3 1/8 1/4 3/16 1
4 3/16 1/4 7/32 1
5 7/32 1/4 15/64 1
6 15/64 1/4 31/128 1
7 15/64 31/128 61/256 0
8 15/64 61/256 121/512 0
9 15/64 121/512 241/1024 0
10 241/1024 121/512 483/2048 1
```

@@ -161,6 +161,6 @@ Thus the approximation after 11 iterations of the bisection method is *483 / 2048* and the binary representation is 0.00111100011 (see [WolframAlpha](http://www.wolframalpha.com/input/?i=sqrt%285%29-2+binary))

console.log(new Fraction(a)
.mod(b)); // Not correct, usual Modulo
.mod(b)); // Not correct, usual Modulo
console.log(new Fraction(a)
.mod(b).add(b).mod(b)); // Correct! Mathematical Modulo
.mod(b).add(b).mod(b)); // Correct! Mathematical Modulo
```

@@ -242,28 +242,28 @@

var comma, pre, offset, pad, times, repeat;
var comma, pre, offset, pad, times, repeat;
if (-1 === (comma = str.indexOf(".")))
return str;
if (-1 === (comma = str.indexOf(".")))
return str;
pre = str.substr(0, comma + 1);
str = str.substr(comma + 1);
pre = str.substr(0, comma + 1);
str = str.substr(comma + 1);
for (var i = 0; i < str.length; i++) {
for (var i = 0; i < str.length; i++) {
offset = str.substr(0, i);
offset = str.substr(0, i);
for (var j = 0; j < 5; j++) {
for (var j = 0; j < 5; j++) {
pad = str.substr(i, j + 1);
pad = str.substr(i, j + 1);
times = Math.ceil((str.length - offset.length) / pad.length);
times = Math.ceil((str.length - offset.length) / pad.length);
repeat = new Array(times + 1).join(pad); // Silly String.repeat hack
repeat = new Array(times + 1).join(pad); // Silly String.repeat hack
if (0 === (offset + repeat).indexOf(str)) {
return pre + offset + "(" + pad + ")";
}
}
if (0 === (offset + repeat).indexOf(str)) {
return pre + offset + "(" + pad + ")";
}
}
return null;
}
return null;
}

@@ -273,3 +273,3 @@

if (x !== null) {
f = new Fraction(x);
f = new Fraction(x);
}

@@ -355,2 +355,6 @@ ```

Fraction simplify([eps=0.001])
---
Simplifies the rational number under a certain error threshold. Ex. `0.333` will be `1/3` with `eps=0.001`
boolean equals(n)

@@ -357,0 +361,0 @@ ---

var assert = require('assert');
var Fraction = require('../fraction');
var Fraction = require('../fraction.js');

@@ -1443,2 +1443,9 @@ var tests = [{

break;
case 'simplify':
it("Should pass " + i, function() {
var x = Fraction(NaN)[i]();
assert.equal('NaN', x.toString());
});
break;
case 'add':

@@ -1445,0 +1452,0 @@ case 'sub':

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc