@yaffle/bigdecimal
Advanced tools
Comparing version 1.0.25 to 1.0.26
@@ -33,3 +33,2 @@ /*jslint bigint: true, vars: true, indent: 2*/ | ||
// BigDecimal.sqrt(a, rounding) | ||
// BigDecimal.cbrt(a, rounding) | ||
// "simple" Math functions: | ||
@@ -701,3 +700,3 @@ // BigDecimal.abs(a) | ||
const internalRounding = { | ||
maximumSignificantDigits: Math.ceil(Math.max(rounding.maximumSignificantDigits || (rounding.maximumFractionDigits + 1 + getExpectedResultIntegerDigits(x) - 1), significandDigits(x)) * Math.cbrt(2**(i - 1))) + 2 + (BASE === 2 ? 1 : 0), | ||
maximumSignificantDigits: Math.ceil(Math.max(rounding.maximumSignificantDigits || (rounding.maximumFractionDigits + 1 + getExpectedResultIntegerDigits(x) - 1), significandDigits(x)) * Math.pow(2, Math.ceil((i - 1) / 3))) + 2 + (BASE === 2 ? 1 : 0), | ||
roundingMode: "half-even" | ||
@@ -914,10 +913,5 @@ }; | ||
function nthRoot(x, n, rounding) { | ||
const exponentiateX = function (x, n) { | ||
return n === 1 ? x : (n % 2 === 0 ? exponentiateX(BigDecimal.multiply(x, x), n / 2) : BigDecimal.multiply(x, exponentiate(x, n - 1))); | ||
}; | ||
BigDecimal.sqrt = function (x, rounding) { | ||
if (BigDecimal.lessThan(x, BigDecimal.BigDecimal(0))) { | ||
if (n % 2 === 0) { | ||
throw new RangeError(); | ||
} | ||
throw new RangeError(); | ||
} | ||
@@ -928,15 +922,15 @@ if (BigDecimal.equal(x, BigDecimal.BigDecimal(0))) { | ||
// https://en.wikipedia.org/wiki/Nth_root#Using_Newton's_method | ||
const e = getCountOfDigits(x) / BigInt(n); | ||
const e = getCountOfDigits(x) / 2n; | ||
const t = exponentiate(BASE, e); | ||
const y = BigDecimal.multiply(x, exponentiate(BASE, -(BigInt(n) * e))); | ||
const y = BigDecimal.multiply(x, exponentiate(BASE, -(2n * e))); | ||
const k = Math.floor(Math.log2(Number.MAX_SAFE_INTEGER + 1) / Math.log2(BASE)) - 1; | ||
const xn = BigDecimal.toNumber(BigDecimal.round(BigDecimal.multiply(y, exponentiate(BASE, k)), {maximumFractionDigits: 0, roundingMode: "half-even"})) / BASE**k; | ||
const r = n === 2 ? Math.sqrt(xn) : (n === 3 ? Math.cbrt(xn) : Math.exp(Math.log(xn) / n)); | ||
const resultSignificantDigits = 8 * (rounding.maximumSignificantDigits || (rounding.maximumFractionDigits + Math.ceil(significandDigits(x) / n)) || 1); | ||
const r = Math.sqrt(xn); | ||
//TODO: fix | ||
const resultSignificantDigits = 2 * (rounding.maximumSignificantDigits || (rounding.maximumFractionDigits + Math.ceil(significandDigits(x) / 2)) || 1); | ||
let result = BigDecimal.multiply(BigDecimal.BigDecimal(Math.sign(r) * Math.floor(Math.abs(r) * BASE**k + 0.5)), exponentiate(BASE, -k)); | ||
const iteration = function (result, rounding) { | ||
const resultInNm1 = exponentiateX(result, n - 1); | ||
return BigDecimal.divide(BigDecimal.add(y, BigDecimal.multiply(BigDecimal.BigDecimal(n - 1), BigDecimal.multiply(result, resultInNm1))), BigDecimal.multiply(BigDecimal.BigDecimal(n), resultInNm1), rounding); | ||
const iteration = function (result, internalRounding) { | ||
return BigDecimal.divide(BigDecimal.add(y, BigDecimal.multiply(result, result)), BigDecimal.multiply(BigDecimal.BigDecimal(2), result), internalRounding); | ||
}; | ||
for (let i = Math.max(k - (n - 1), 1); i < resultSignificantDigits; i *= 2) { | ||
for (let i = Math.max(k - 1, 1); i <= resultSignificantDigits; i *= 2) { | ||
const internalRounding = {maximumSignificantDigits: i, roundingMode: "half-even"}; | ||
@@ -947,12 +941,4 @@ result = iteration(result, internalRounding); | ||
return BigDecimal.multiply(result, t); | ||
} | ||
BigDecimal.sqrt = function (x, rounding) { | ||
return nthRoot(x, 2, rounding); | ||
}; | ||
BigDecimal.cbrt = function (x, rounding) { | ||
return nthRoot(x, 3, rounding); | ||
}; | ||
return BigDecimal; | ||
@@ -959,0 +945,0 @@ }; |
{ | ||
"name": "@yaffle/bigdecimal", | ||
"version": "1.0.25", | ||
"version": "1.0.26", | ||
"description": "Arbitrary precision decimal arithmetic library. Polyfill for decimal proposal. Implemented on the top of BigInt.", | ||
@@ -5,0 +5,0 @@ "main": "BigDecimal.js", |
@@ -53,3 +53,2 @@ # BigDecimal | ||
BigDecimal.sqrt(a, rounding) | ||
BigDecimal.cbrt(a, rounding) | ||
@@ -56,0 +55,0 @@ The `rounding` argument may look like `{maximumFractionDigits: 10, roundingMode: "half-even"}` or `{maximumSignificantDigits: 10, roundingMode: "half-even"}`, |
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
45700
909
110