Arbitrary precision decimal arithmetic library. Polyfill for decimal proposal. Implemented on the top of BigInt.
npm install @splendo/bigdecimalForked from https://github.com/Yaffle/BigDecimal
npm install @splendo/bigdecimal BigDecimal.BigDecimal(string)
BigDecimal.BigDecimal(bigint)
BigDecimal.BigDecimal(number) // (only integers)
a.toString()
a.toFixed(fractionDigits[, roundingMode = "half-up"])
a.toPrecision(precision[, roundingMode = "half-up"])
a.toExponential(fractionDigits[, roundingMode = "half-up"])
BigDecimal.toBigInt(a) // (not in the spec)
BigDecimal.toNumber(a) // (not in the spec, only integers)
BigDecimal.unaryMinus(a)
BigDecimal.add(a, b[, rounding])
BigDecimal.subtract(a, b[, rounding])
BigDecimal.multiply(a, b[, rounding])
BigDecimal.divide(a, b, rounding)
BigDecimal.round(a, rounding)
BigDecimal.equal(a, b)
BigDecimal.lessThan(a, b)
BigDecimal.greaterThan(a, b)
BigDecimal.abs(a)
BigDecimal.sign(a)
BigDecimal.max(a, b)
BigDecimal.min(a, b)
BigDecimal.log(a, rounding)
BigDecimal.exp(a, rounding)
BigDecimal.sin(a, rounding)
BigDecimal.cos(a, rounding)
BigDecimal.atan(a, rounding)
BigDecimal.sqrt(a, rounding)
BigDecimal.cbrt(a, rounding)
The rounding argument may look like {maximumFractionDigits: 10, roundingMode: "half-even"} or {maximumSignificantDigits: 10, roundingMode: "half-even"},
where the roundingMode can be:
* "floor"
* "ceil"
* "half-even"
* "half-up"
* "half-down".
javascriptimport {BigDecimal} from "./node_modules/@splendo/bigdecimal/BigDecimal.js";
const rounding = {maximumSignificantDigits: 1000, roundingMode: "half-even"};
let pi = BigDecimal.multiply(BigDecimal.BigDecimal(4), BigDecimal.atan(BigDecimal.BigDecimal(1), rounding));
console.log(pi.toString());
`Note:
* Consider to use only "half-even" rounding mode and rounding to a maximum number of significant digits for floating-point arithmetic,
or only "floor" rounding to a maximum number of fraction digits for fixed-point arithmetic.
* For the best performance use BigFloat and rounding like {maximumFractionDigits: number, roundingMode: "floor"}.
* Use to round to an integer BigDecimal.round(a, {maximumFractionDigits: 0, roundingMode: "half-even"})`.Results of running the benchmark from https://github.com/munsocket/jampary#early-stage-results-without-wasm-and-fma :
| library | time |
|--------------|---------|
| Jampary_Wasm | 466 ms |
| Jampary | 204 ms |
| BigNumberJs | 263 ms |
| DecimalJs | 424 ms |
| BigFloat | 85 ms |
| BigDecimal | 135 ms |
| BigJs | 3403 ms |
| BigFloat32 | 158 ms |