fraction.js
Advanced tools
Comparing version 4.0.13 to 4.0.14
/** | ||
* @license Fraction.js v4.0.12 09/09/2015 | ||
* @license Fraction.js v4.0.14 09/09/2015 | ||
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/ | ||
@@ -100,2 +100,28 @@ * | ||
function factorize(num) { | ||
var factors = {}; | ||
var n = num; | ||
var i = C_TWO; | ||
var s = C_FIVE - C_ONE; | ||
while (s <= n) { | ||
while (n % i === C_ZERO) { | ||
n /= i; | ||
factors[i] = (factors[i] || C_ZERO) + C_ONE; | ||
} | ||
s += C_ONE + C_TWO * i++; | ||
} | ||
if (n !== num) { | ||
if (n > 1) | ||
factors[n] = (factors[n] || C_ZERO) + C_ONE; | ||
} else { | ||
factors[num] = (factors[num] || C_ZERO) + C_ONE; | ||
} | ||
return factors; | ||
} | ||
const parse = function(p1, p2) { | ||
@@ -547,9 +573,55 @@ | ||
*/ | ||
"pow": function(m) { | ||
"pow": function(a, b) { | ||
if (m < 0) { | ||
return new Fraction((this['s'] * this["d"]) ** BigInt(-m), this["n"] ** BigInt(-m)); | ||
} else { | ||
return new Fraction((this['s'] * this["n"]) ** BigInt(+m), this["d"] ** BigInt(+m)); | ||
parse(a, b); | ||
// Trivial case when exp is an integer | ||
if (P['d'] === C_ONE) { | ||
if (P['s'] < C_ZERO) { | ||
return new Fraction((this['s'] * this["d"]) ** P['n'], this["n"] ** P['n']); | ||
} else { | ||
return new Fraction((this['s'] * this["n"]) ** P['n'], this["d"] ** P['n']); | ||
} | ||
} | ||
// Negative roots become complex | ||
if (this['s'] < C_ZERO) return null; | ||
// Now prime factor n and d | ||
var N = factorize(this['n']); | ||
var D = factorize(this['d']); | ||
// Exponentiate and take root for n and d individually | ||
var n = this['s']; | ||
var d = C_ONE; | ||
for (var k in N) { | ||
if (k === '1') continue; | ||
if (k === '0') { | ||
n = C_ZERO; | ||
break; | ||
} | ||
N[k]*= P['n']; | ||
if (N[k] % P['d'] === C_ZERO) { | ||
N[k]/= P['d']; | ||
} else return null; | ||
n*= BigInt(k) ** N[k]; | ||
} | ||
for (var k in D) { | ||
if (k === '1') continue; | ||
D[k]*= P['n']; | ||
if (D[k] % P['d'] === C_ZERO) { | ||
D[k]/= P['d']; | ||
} else return null; | ||
d*= BigInt(k) ** D[k]; | ||
} | ||
if (P['s'] < C_ZERO) { | ||
return new Fraction(d, n); | ||
} | ||
return new Fraction(n, d); | ||
}, | ||
@@ -556,0 +628,0 @@ |
/** | ||
* @license Fraction.js v4.0.12 09/09/2015 | ||
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/ | ||
* @license Fraction.js v4.0.14 09/09/2015 | ||
* https://www.xarg.org/2014/03/rational-numbers-in-javascript/ | ||
* | ||
@@ -92,2 +92,28 @@ * Copyright (c) 2015, Robert Eisele (robert@xarg.org) | ||
function factorize(num) { | ||
var factors = {}; | ||
var n = num; | ||
var i = 2; | ||
var s = 4; | ||
while (s <= n) { | ||
while (n % i === 0) { | ||
n /= i; | ||
factors[i] = (factors[i] || 0) + 1; | ||
} | ||
s += 1 + 2 * i++; | ||
} | ||
if (n !== num) { | ||
if (n > 1) | ||
factors[n] = (factors[n] || 0) + 1; | ||
} else { | ||
factors[num] = (factors[num] || 0) + 1; | ||
} | ||
return factors; | ||
} | ||
var parse = function(p1, p2) { | ||
@@ -588,13 +614,59 @@ | ||
/** | ||
* Calculates the fraction to some integer exponent | ||
* Calculates the fraction to some rational exponent, if possible | ||
* | ||
* Ex: new Fraction(-1,2).pow(-3) => -8 | ||
*/ | ||
"pow": function(m) { | ||
"pow": function(a, b) { | ||
if (m < 0) { | ||
return new Fraction(Math.pow(this['s'] * this["d"], -m), Math.pow(this["n"], -m)); | ||
} else { | ||
return new Fraction(Math.pow(this['s'] * this["n"], m), Math.pow(this["d"], m)); | ||
parse(a, b); | ||
// Trivial case when exp is an integer | ||
if (P['d'] === 1) { | ||
if (P['s'] < 0) { | ||
return new Fraction(Math.pow(this['s'] * this["d"], P['n']), Math.pow(this["n"], P['n'])); | ||
} else { | ||
return new Fraction(Math.pow(this['s'] * this["n"], P['n']), Math.pow(this["d"], P['n'])); | ||
} | ||
} | ||
// Negative roots become complex | ||
if (this['s'] < 0) return null; | ||
// Now prime factor n and d | ||
var N = factorize(this['n']); | ||
var D = factorize(this['d']); | ||
// Exponentiate and take root for n and d individually | ||
var n = this['s']; | ||
var d = 1; | ||
for (var k in N) { | ||
if (k === '1') continue; | ||
if (k === '0') { | ||
n = 0; | ||
break; | ||
} | ||
N[k]*= P['n']; | ||
if (N[k] % P['d'] === 0) { | ||
N[k]/= P['d']; | ||
} else return null; | ||
n*= Math.pow(k, N[k]); | ||
} | ||
for (var k in D) { | ||
if (k === '1') continue; | ||
D[k]*= P['n']; | ||
if (D[k] % P['d'] === 0) { | ||
D[k]/= P['d']; | ||
} else return null; | ||
d*= Math.pow(k, D[k]); | ||
} | ||
if (P['s'] < 0) { | ||
return new Fraction(d, n); | ||
} | ||
return new Fraction(n, d); | ||
}, | ||
@@ -783,3 +855,3 @@ | ||
dec = dec || 15; // 15 = decimal places when no repitation | ||
dec = dec || 15; // 15 = decimal places when no repetation | ||
@@ -786,0 +858,0 @@ var cycLen = cycleLen(N, D); // Cycle length |
{ | ||
"name": "fraction.js", | ||
"title": "fraction.js", | ||
"version": "4.0.13", | ||
"version": "4.0.14", | ||
"homepage": "http://www.xarg.org/2014/03/rational-numbers-in-javascript/", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/infusion/Fraction.js/issues", |
@@ -315,4 +315,3 @@ # Fraction.js - ℚ in JavaScript | ||
--- | ||
Returns the power of the actual number, raised to an integer exponent. | ||
*Note:* Rational exponents are planned, but would slow down the function a lot, because of a kinda slow root finding algorithm, whether the result will become irrational. So for now, only integer exponents are implemented. | ||
Returns the power of the actual number, raised to an possible rational exponent. If the result becomes non-rational the function returns `null`. | ||
@@ -482,3 +481,3 @@ Fraction mod(n) | ||
=== | ||
Fraction.js tries to circumvent floating point errors, by having an internal representation of numerator and denominator. As it relies on JavaScript, there is also a limit. The biggest number representable is `|Number.MAX_SAFE_INTEGER / 1|` and the smallest is `|1 / Number.MAX_SAFE_INTEGER|`, with `Number.MAX_SAFE_INTEGER=9007199254740991`. | ||
Fraction.js tries to circumvent floating point errors, by having an internal representation of numerator and denominator. As it relies on JavaScript, there is also a limit. The biggest number representable is `Number.MAX_SAFE_INTEGER / 1` and the smallest is `-1 / Number.MAX_SAFE_INTEGER`, with `Number.MAX_SAFE_INTEGER=9007199254740991`. If this is not enough, there is `bigfraction.js` shipped experimentally, which relies on `BigInt` and should become the new Fraction.js eventually. | ||
@@ -485,0 +484,0 @@ Testing |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65477
1517
495