@yaffle/bigdecimal
Advanced tools
Comparing version 1.0.22 to 1.0.23
@@ -76,7 +76,7 @@ /*jslint bigint: true, vars: true, indent: 2*/ | ||
// `normalize` will change the exponent which is not good for fixed-point arithmetic (?) | ||
/*let b = normalize(a, null); | ||
while (a !== b) { | ||
a = b; | ||
b = normalize(a, null); | ||
}*/ | ||
//let b = normalize(a, null); | ||
//while (a !== b) { | ||
// a = b; | ||
// b = normalize(a, null); | ||
//} | ||
return a; | ||
@@ -88,9 +88,14 @@ }; | ||
BigDecimal.toBigInt = function (a) { | ||
if (a.exponent === 0) { | ||
const exponent = Number(a.exponent); | ||
if (exponent === 0) { | ||
return a.significand; | ||
} | ||
if (Number(a.exponent) < 0 && a.significand % BIGINT_BASE**BigInt(-a.exponent) !== 0n) { | ||
throw new RangeError("The BigDecimal " + a.toString() + " cannot be converted to a BigInt because it is not an integer"); | ||
if (exponent < 0) { | ||
const result = bigIntUnscale(a.significand, -exponent); | ||
if (bigIntScale(result, -exponent) !== a.significand) { | ||
throw new RangeError("The BigDecimal " + a.toString() + " cannot be converted to a BigInt because it is not an integer"); | ||
} | ||
return result; | ||
} | ||
return Number(a.exponent) < 0 ? a.significand / BIGINT_BASE**BigInt(-a.exponent) : BIGINT_BASE**BigInt(a.exponent) * a.significand; | ||
return bigIntScale(a.significand, exponent); | ||
}; | ||
@@ -140,3 +145,3 @@ function create(significand, exponent) { | ||
const i = Math.floor(e + 0.5); | ||
return a >= BIGINT_BASE**BigInt(i) ? i + 1 : i; | ||
return a >= cachedPower(i) ? i + 1 : i; | ||
} | ||
@@ -181,33 +186,26 @@ function sum(a, b) { | ||
} | ||
let cache = {}; | ||
let cacheSize = 0; | ||
function cachedBigInt(k) { | ||
// k === maximumFractionDigits | ||
let lastValue = cache[k]; | ||
if (lastValue == null) { | ||
if (cacheSize > 100) { | ||
cache = {}; | ||
cacheSize = 0; | ||
function cachedFunction(f) { | ||
let cache = {}; | ||
let cacheSize = 0; | ||
return function (k) { | ||
let lastValue = cache[k]; | ||
if (lastValue == null) { | ||
if (cacheSize > 100) { | ||
cache = {}; | ||
cacheSize = 0; | ||
} | ||
lastValue = f(k); | ||
cache[k] = lastValue; | ||
cacheSize += 1; | ||
} | ||
lastValue = BigInt(k); | ||
cache[k] = lastValue; | ||
cacheSize += 1; | ||
} | ||
return lastValue; | ||
return lastValue; | ||
}; | ||
} | ||
let cache2 = {}; | ||
let cache2Size = 0; | ||
function cachedPower(k) { | ||
let lastValue = cache2[k]; | ||
if (lastValue == null) { | ||
if (cache2Size > 10) { | ||
cache2 = {}; | ||
cache2Size = 0; | ||
} | ||
lastValue = BIGINT_BASE**BigInt(k); | ||
cache2[k] = lastValue; | ||
cache2Size += 1; | ||
} | ||
return lastValue; | ||
} | ||
const cachedBigInt = cachedFunction(function (k) { | ||
// k === maximumFractionDigits | ||
return BigInt(k); | ||
}); | ||
const cachedPower = cachedFunction(function (k) { | ||
return BIGINT_BASE**BigInt(k); | ||
}); | ||
function round(a, rounding) { | ||
@@ -326,2 +324,5 @@ if (rounding != null) { | ||
} | ||
function bigIntUnscale(a, unscaling) { | ||
return (BASE === 2 ? (a >> cachedBigInt(unscaling)) : a / cachedPower(unscaling)); | ||
} | ||
BigDecimal.divide = function (a, b, rounding = null) { | ||
@@ -697,2 +698,3 @@ if (a.significand === 0n) { | ||
if (i > 4 * ((9 + 1) / BASE) && rounding.maximumSignificantDigits != null && rounding.roundingMode === "half-even" && name !== "sin" && name !== "cos") { | ||
console.error(x, rounding); | ||
throw new Error(); | ||
@@ -914,4 +916,5 @@ } | ||
if (BigDecimal.greaterThan(BigDecimal.abs(x), BigDecimal.BigDecimal(1))) { | ||
//Note: rounding to maximumFractionDigits | ||
const internalRounding = { | ||
maximumSignificantDigits: rounding.maximumSignificantDigits + 1, | ||
maximumFractionDigits: rounding.maximumSignificantDigits + 1, | ||
roundingMode: "half-even" | ||
@@ -918,0 +921,0 @@ }; |
{ | ||
"name": "@yaffle/bigdecimal", | ||
"version": "1.0.22", | ||
"version": "1.0.23", | ||
"description": "Arbitrary precision decimal arithmetic library. Polyfill for decimal proposal. Implemented on the top of BigInt.", | ||
@@ -5,0 +5,0 @@ "main": "BigDecimal.js", |
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
45191
913