fraction.js
Advanced tools
Comparing version 4.0.12 to 4.0.13
@@ -43,6 +43,6 @@ /** | ||
"use strict"; | ||
// Set Identity function to downgrade BigInt to Number if needed | ||
if (!BigInt) BigInt = function(n) {return n;}; | ||
if (!BigInt) BigInt = function(n) { return n; }; | ||
const C_ONE = BigInt(1); | ||
@@ -80,3 +80,3 @@ const C_ZERO = BigInt(0); | ||
*/ | ||
function IntermediateInheritor() {} | ||
function IntermediateInheritor() { } | ||
IntermediateInheritor.prototype = Error.prototype; | ||
@@ -92,3 +92,3 @@ errorConstructor.prototype = new IntermediateInheritor(); | ||
function assign(n, s) { | ||
try { | ||
@@ -129,4 +129,8 @@ n = BigInt(n); | ||
s = n * d; | ||
} else if (typeof p1 === "bigint") { | ||
n = p1; | ||
s = p1; | ||
d = BigInt(1); | ||
} else if (typeof p1 === "number") { | ||
if (isNaN(p1)) { | ||
@@ -158,3 +162,3 @@ throw new InvalidParameter(); | ||
// Using Farey Sequences | ||
while (B <= N && D <= N) { | ||
@@ -201,5 +205,5 @@ let M = (A + C) / (B + D); | ||
} | ||
} else if (typeof p1 === "string") { | ||
let ndx = 0; | ||
@@ -212,3 +216,3 @@ | ||
if (match === null) | ||
throw new InvalidParameter() | ||
throw new InvalidParameter() | ||
@@ -293,7 +297,7 @@ if (match[ndx] === '-') {// Check for minus sign at the beginning | ||
for (; d % C_TWO === C_ZERO; | ||
d /= C_TWO) { | ||
d /= C_TWO) { | ||
} | ||
for (; d % C_FIVE === C_ZERO; | ||
d /= C_FIVE) { | ||
d /= C_FIVE) { | ||
} | ||
@@ -320,3 +324,3 @@ | ||
} | ||
function cycleStart(n, d, len) { | ||
@@ -338,3 +342,3 @@ | ||
} | ||
function gcd(a, b) { | ||
@@ -378,3 +382,3 @@ | ||
} | ||
Fraction.prototype = { | ||
@@ -395,3 +399,3 @@ | ||
}, | ||
/** | ||
@@ -406,3 +410,3 @@ * Inverts the sign of the current fraction | ||
}, | ||
/** | ||
@@ -417,5 +421,5 @@ * Adds two rational numbers | ||
return new Fraction( | ||
this["s"] * this["n"] * P["d"] + P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
this["s"] * this["n"] * P["d"] + P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
}, | ||
@@ -432,5 +436,5 @@ | ||
return new Fraction( | ||
this["s"] * this["n"] * P["d"] - P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
this["s"] * this["n"] * P["d"] - P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
}, | ||
@@ -447,5 +451,5 @@ | ||
return new Fraction( | ||
this["s"] * P["s"] * this["n"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
this["s"] * P["s"] * this["n"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
}, | ||
@@ -462,5 +466,5 @@ | ||
return new Fraction( | ||
this["s"] * P["s"] * this["n"] * P["d"], | ||
this["d"] * P["n"] | ||
); | ||
this["s"] * P["s"] * this["n"] * P["d"], | ||
this["d"] * P["n"] | ||
); | ||
}, | ||
@@ -476,3 +480,3 @@ | ||
}, | ||
/** | ||
@@ -509,7 +513,7 @@ * Calculates the modulo of two rational numbers - a more precise fmod | ||
return new Fraction( | ||
this["s"] * (P["d"] * this["n"]) % (P["n"] * this["d"]), | ||
P["d"] * this["d"] | ||
); | ||
this["s"] * (P["d"] * this["n"]) % (P["n"] * this["d"]), | ||
P["d"] * this["d"] | ||
); | ||
}, | ||
/** | ||
@@ -545,5 +549,5 @@ * Calculates the fractional gcd of two rational numbers | ||
}, | ||
/** | ||
* Gets the inverse of the fraction, means numerator and denumerator are exchanged | ||
* Gets the inverse of the fraction, means numerator and denominator are exchanged | ||
* | ||
@@ -556,3 +560,3 @@ * Ex: new Fraction([-3, 4]).inverse() => -4 / 3 | ||
}, | ||
/** | ||
@@ -592,3 +596,3 @@ * Calculates the fraction to some integer exponent | ||
let t = (this["s"] * this["n"] * P["d"] - P["s"] * P["n"] * this["d"]); | ||
return (C_ZERO < t) - (t < C_ZERO); | ||
@@ -683,4 +687,4 @@ }, | ||
for (let i = cycOff; i--; ) { | ||
str += N / D | C_ZERO; | ||
for (let i = cycOff; i--;) { | ||
str += N / D | C_ZERO; | ||
N %= D; | ||
@@ -690,3 +694,3 @@ N *= C_TEN; | ||
str += "("; | ||
for (let i = cycLen; i--; ) { | ||
for (let i = cycLen; i--;) { | ||
str += N / D | C_ZERO; | ||
@@ -698,3 +702,3 @@ N %= D; | ||
} else { | ||
for (let i = dec; N && i--; ) { | ||
for (let i = dec; N && i--;) { | ||
str += N / D | C_ZERO; | ||
@@ -707,3 +711,3 @@ N %= D; | ||
}, | ||
/** | ||
@@ -765,3 +769,3 @@ * Returns a string-fraction representation of a Fraction object | ||
}, | ||
/** | ||
@@ -787,3 +791,3 @@ * Returns an array of continued fraction elements | ||
}, | ||
"simplify": function(eps) { | ||
@@ -812,3 +816,3 @@ | ||
}; | ||
if (typeof define === "function" && define["amd"]) { | ||
@@ -819,3 +823,3 @@ define([], function() { | ||
} else if (typeof exports === "object") { | ||
Object.defineProperty(exports, "__esModule", {'value': true}); | ||
Object.defineProperty(exports, "__esModule", { 'value': true }); | ||
Fraction['default'] = Fraction; | ||
@@ -822,0 +826,0 @@ Fraction['Fraction'] = Fraction; |
264
fraction.js
@@ -70,3 +70,3 @@ /** | ||
*/ | ||
function IntermediateInheritor() {} | ||
function IntermediateInheritor() { } | ||
IntermediateInheritor.prototype = Error.prototype; | ||
@@ -114,135 +114,135 @@ errorConstructor.prototype = new IntermediateInheritor(); | ||
case "object": | ||
{ | ||
if ("d" in p1 && "n" in p1) { | ||
n = p1["n"]; | ||
d = p1["d"]; | ||
if ("s" in p1) | ||
n *= p1["s"]; | ||
} else if (0 in p1) { | ||
n = p1[0]; | ||
if (1 in p1) | ||
d = p1[1]; | ||
} else { | ||
throwInvalidParam(); | ||
{ | ||
if ("d" in p1 && "n" in p1) { | ||
n = p1["n"]; | ||
d = p1["d"]; | ||
if ("s" in p1) | ||
n *= p1["s"]; | ||
} else if (0 in p1) { | ||
n = p1[0]; | ||
if (1 in p1) | ||
d = p1[1]; | ||
} else { | ||
throwInvalidParam(); | ||
} | ||
s = n * d; | ||
break; | ||
} | ||
s = n * d; | ||
break; | ||
} | ||
case "number": | ||
{ | ||
if (p1 < 0) { | ||
s = p1; | ||
p1 = -p1; | ||
} | ||
{ | ||
if (p1 < 0) { | ||
s = p1; | ||
p1 = -p1; | ||
} | ||
if (p1 % 1 === 0) { | ||
n = p1; | ||
} else if (p1 > 0) { // check for != 0, scale would become NaN (log(0)), which converges really slow | ||
if (p1 % 1 === 0) { | ||
n = p1; | ||
} else if (p1 > 0) { // check for != 0, scale would become NaN (log(0)), which converges really slow | ||
if (p1 >= 1) { | ||
z = Math.pow(10, Math.floor(1 + Math.log(p1) / Math.LN10)); | ||
p1 /= z; | ||
} | ||
if (p1 >= 1) { | ||
z = Math.pow(10, Math.floor(1 + Math.log(p1) / Math.LN10)); | ||
p1 /= z; | ||
} | ||
// Using Farey Sequences | ||
// http://www.johndcook.com/blog/2010/10/20/best-rational-approximation/ | ||
// Using Farey Sequences | ||
// http://www.johndcook.com/blog/2010/10/20/best-rational-approximation/ | ||
while (B <= N && D <= N) { | ||
M = (A + C) / (B + D); | ||
while (B <= N && D <= N) { | ||
M = (A + C) / (B + D); | ||
if (p1 === M) { | ||
if (B + D <= N) { | ||
n = A + C; | ||
d = B + D; | ||
} else if (D > B) { | ||
n = C; | ||
d = D; | ||
if (p1 === M) { | ||
if (B + D <= N) { | ||
n = A + C; | ||
d = B + D; | ||
} else if (D > B) { | ||
n = C; | ||
d = D; | ||
} else { | ||
n = A; | ||
d = B; | ||
} | ||
break; | ||
} else { | ||
n = A; | ||
d = B; | ||
} | ||
break; | ||
} else { | ||
if (p1 > M) { | ||
A += C; | ||
B += D; | ||
} else { | ||
C += A; | ||
D += B; | ||
} | ||
if (p1 > M) { | ||
A += C; | ||
B += D; | ||
} else { | ||
C += A; | ||
D += B; | ||
if (B > N) { | ||
n = C; | ||
d = D; | ||
} else { | ||
n = A; | ||
d = B; | ||
} | ||
} | ||
if (B > N) { | ||
n = C; | ||
d = D; | ||
} else { | ||
n = A; | ||
d = B; | ||
} | ||
} | ||
n *= z; | ||
} else if (isNaN(p1) || isNaN(p2)) { | ||
d = n = NaN; | ||
} | ||
n *= z; | ||
} else if (isNaN(p1) || isNaN(p2)) { | ||
d = n = NaN; | ||
break; | ||
} | ||
break; | ||
} | ||
case "string": | ||
{ | ||
B = p1.match(/\d+|./g); | ||
{ | ||
B = p1.match(/\d+|./g); | ||
if (B === null) | ||
throwInvalidParam(); | ||
if (B === null) | ||
throwInvalidParam(); | ||
if (B[A] === '-') {// Check for minus sign at the beginning | ||
s = -1; | ||
A++; | ||
} else if (B[A] === '+') {// Check for plus sign at the beginning | ||
A++; | ||
} | ||
if (B[A] === '-') {// Check for minus sign at the beginning | ||
s = -1; | ||
A++; | ||
} else if (B[A] === '+') {// Check for plus sign at the beginning | ||
A++; | ||
} | ||
if (B.length === A + 1) { // Check if it's just a simple number "1234" | ||
w = assign(B[A++], s); | ||
} else if (B[A + 1] === '.' || B[A] === '.') { // Check if it's a decimal number | ||
if (B.length === A + 1) { // Check if it's just a simple number "1234" | ||
w = assign(B[A++], s); | ||
} else if (B[A + 1] === '.' || B[A] === '.') { // Check if it's a decimal number | ||
if (B[A] !== '.') { // Handle 0.5 and .5 | ||
v = assign(B[A++], s); | ||
} | ||
A++; | ||
if (B[A] !== '.') { // Handle 0.5 and .5 | ||
v = assign(B[A++], s); | ||
} | ||
A++; | ||
// Check for decimal places | ||
if (A + 1 === B.length || B[A + 1] === '(' && B[A + 3] === ')' || B[A + 1] === "'" && B[A + 3] === "'") { | ||
// Check for decimal places | ||
if (A + 1 === B.length || B[A + 1] === '(' && B[A + 3] === ')' || B[A + 1] === "'" && B[A + 3] === "'") { | ||
w = assign(B[A], s); | ||
y = Math.pow(10, B[A].length); | ||
A++; | ||
} | ||
// Check for repeating places | ||
if (B[A] === '(' && B[A + 2] === ')' || B[A] === "'" && B[A + 2] === "'") { | ||
x = assign(B[A + 1], s); | ||
z = Math.pow(10, B[A + 1].length) - 1; | ||
A += 3; | ||
} | ||
} else if (B[A + 1] === '/' || B[A + 1] === ':') { // Check for a simple fraction "123/456" or "123:456" | ||
w = assign(B[A], s); | ||
y = Math.pow(10, B[A].length); | ||
A++; | ||
y = assign(B[A + 2], 1); | ||
A += 3; | ||
} else if (B[A + 3] === '/' && B[A + 1] === ' ') { // Check for a complex fraction "123 1/2" | ||
v = assign(B[A], s); | ||
w = assign(B[A + 2], s); | ||
y = assign(B[A + 4], 1); | ||
A += 5; | ||
} | ||
// Check for repeating places | ||
if (B[A] === '(' && B[A + 2] === ')' || B[A] === "'" && B[A + 2] === "'") { | ||
x = assign(B[A + 1], s); | ||
z = Math.pow(10, B[A + 1].length) - 1; | ||
A += 3; | ||
if (B.length <= A) { // Check for more tokens on the stack | ||
d = y * z; | ||
s = /* void */ | ||
n = x + d * v + z * w; | ||
break; | ||
} | ||
} else if (B[A + 1] === '/' || B[A + 1] === ':') { // Check for a simple fraction "123/456" or "123:456" | ||
w = assign(B[A], s); | ||
y = assign(B[A + 2], 1); | ||
A += 3; | ||
} else if (B[A + 3] === '/' && B[A + 1] === ' ') { // Check for a complex fraction "123 1/2" | ||
v = assign(B[A], s); | ||
w = assign(B[A + 2], s); | ||
y = assign(B[A + 4], 1); | ||
A += 5; | ||
/* Fall through on error */ | ||
} | ||
if (B.length <= A) { // Check for more tokens on the stack | ||
d = y * z; | ||
s = /* void */ | ||
n = x + d * v + z * w; | ||
break; | ||
} | ||
/* Fall through on error */ | ||
} | ||
default: | ||
@@ -277,7 +277,7 @@ throwInvalidParam(); | ||
for (; d % 2 === 0; | ||
d /= 2) { | ||
d /= 2) { | ||
} | ||
for (; d % 5 === 0; | ||
d /= 5) { | ||
d /= 5) { | ||
} | ||
@@ -306,3 +306,3 @@ | ||
function cycleStart(n, d, len) { | ||
function cycleStart(n, d, len) { | ||
@@ -408,5 +408,5 @@ var rem1 = 1; | ||
return new Fraction( | ||
this["s"] * this["n"] * P["d"] + P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
this["s"] * this["n"] * P["d"] + P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
}, | ||
@@ -423,5 +423,5 @@ | ||
return new Fraction( | ||
this["s"] * this["n"] * P["d"] - P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
this["s"] * this["n"] * P["d"] - P["s"] * this["d"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
}, | ||
@@ -438,5 +438,5 @@ | ||
return new Fraction( | ||
this["s"] * P["s"] * this["n"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
this["s"] * P["s"] * this["n"] * P["n"], | ||
this["d"] * P["d"] | ||
); | ||
}, | ||
@@ -453,5 +453,5 @@ | ||
return new Fraction( | ||
this["s"] * P["s"] * this["n"] * P["d"], | ||
this["d"] * P["n"] | ||
); | ||
this["s"] * P["s"] * this["n"] * P["d"], | ||
this["d"] * P["n"] | ||
); | ||
}, | ||
@@ -503,5 +503,5 @@ | ||
return new Fraction( | ||
this["s"] * (P["d"] * this["n"]) % (P["n"] * this["d"]), | ||
P["d"] * this["d"] | ||
); | ||
this["s"] * (P["d"] * this["n"]) % (P["n"] * this["d"]), | ||
P["d"] * this["d"] | ||
); | ||
}, | ||
@@ -586,3 +586,3 @@ | ||
/** | ||
* Gets the inverse of the fraction, means numerator and denumerator are exchanged | ||
* Gets the inverse of the fraction, means numerator and denominator are exchanged | ||
* | ||
@@ -756,3 +756,3 @@ * Ex: new Fraction([-3, 4]).inverse() => -4 / 3 | ||
if (isNaN(this['n']) || isNaN(this['d'])) { | ||
if (isNaN(a) || isNaN(b)) { | ||
return res; | ||
@@ -809,3 +809,3 @@ } | ||
for (var i = cycOff; i--; ) { | ||
for (var i = cycOff; i--;) { | ||
str += N / D | 0; | ||
@@ -816,3 +816,3 @@ N %= D; | ||
str += "("; | ||
for (var i = cycLen; i--; ) { | ||
for (var i = cycLen; i--;) { | ||
str += N / D | 0; | ||
@@ -824,3 +824,3 @@ N %= D; | ||
} else { | ||
for (var i = dec; N && i--; ) { | ||
for (var i = dec; N && i--;) { | ||
str += N / D | 0; | ||
@@ -840,3 +840,3 @@ N %= D; | ||
} else if (typeof exports === "object") { | ||
Object.defineProperty(exports, "__esModule", {'value': true}); | ||
Object.defineProperty(Fraction, "__esModule", { 'value': true }); | ||
Fraction['default'] = Fraction; | ||
@@ -843,0 +843,0 @@ Fraction['Fraction'] = Fraction; |
{ | ||
"name": "fraction.js", | ||
"title": "fraction.js", | ||
"version": "4.0.12", | ||
"version": "4.0.13", | ||
"homepage": "http://www.xarg.org/2014/03/rational-numbers-in-javascript/", | ||
@@ -19,2 +19,3 @@ "bugs": "https://github.com/infusion/Fraction.js/issues", | ||
"main": "fraction", | ||
"types": "./fraction.d.ts", | ||
"private": false, | ||
@@ -25,3 +26,3 @@ "readmeFilename": "README.md", | ||
}, | ||
"license": "MIT OR GPL-2.0", | ||
"license": "MIT OR GPL-2.0-or-later", | ||
"repository": { | ||
@@ -28,0 +29,0 @@ "type": "git", |
@@ -1,2 +0,2 @@ | ||
# Fraction.js - ℚ in JavaSript | ||
# Fraction.js - ℚ in JavaScript | ||
@@ -276,3 +276,3 @@ [![NPM Package](https://nodei.co/npm-dl/fraction.js.png?months=6&height=1)](https://npmjs.org/package/fraction.js) | ||
The Fraction object allows direct access to the numerator, denominator and sign attributes. It is ensured that only the sign-attribute holds sign information so that a sign comparision is only necessary against this attribute. | ||
The Fraction object allows direct access to the numerator, denominator and sign attributes. It is ensured that only the sign-attribute holds sign information so that a sign comparison is only necessary against this attribute. | ||
@@ -335,13 +335,13 @@ ```javascript | ||
Fraction ceil([places=0]) | ||
Fraction ceil([places=0-16]) | ||
--- | ||
Returns the ceiling of a rational number (rounded up) | ||
Returns the ceiling of a rational number with Math.ceil | ||
Fraction floor([places=0]) | ||
Fraction floor([places=0-16]) | ||
--- | ||
Returns the floor of a rational number (rounded down) | ||
Returns the floor of a rational number with Math.floor | ||
Fraction round([places=0]) | ||
Fraction round([places=0-16]) | ||
--- | ||
Returns the rational number rounded (normal round) | ||
Returns the rational number rounded with Math.round | ||
@@ -496,3 +496,3 @@ Fraction inverse() | ||
=== | ||
Copyright (c) 2014-2017, [Robert Eisele](https://www.xarg.org/) | ||
Copyright (c) 2014-2019, [Robert Eisele](https://www.xarg.org/) | ||
Dual licensed under the MIT or GPL Version 2 licenses. |
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
62536
1405
0