1.0.36 • Published 9 months ago

@yaffle/bigdecimal v1.0.36

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

BigDecimal

a polyfill for decimal propocal - https://github.com/tc39/proposal-decimal It is implemented on the top of native BigInt. It can be compiled using https://www.npmjs.com/package/babel-plugin-transform-bigint to use JSBI.

Usage:

Installation:

npm install @yaffle/bigdecimal

Type conversion:

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)

Arithmetic:

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)

Comparison:

BigDecimal.equal(a, b)
BigDecimal.lessThan(a, b)
BigDecimal.greaterThan(a, b)

Math: (not in the spec)

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)

The rounding argument may look like {maximumFractionDigits: 10, roundingMode: "half-even"} or {maximumSignificantDigits: 10, roundingMode: "half-even"}, where the roundingMode can be:

  • "floor"
  • "down"
  • "ceil"
  • "up"
  • "half-even"
  • "half-up"
  • "half-down".

BigFloat

Similar to BigDecimal, but uses base 2.

Example:

import {BigDecimal} from "./node_modules/@yaffle/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"}).

Similar projects:

  1. https://github.com/MikeMcl/decimal.js/ (decimal)
  2. https://github.com/uzyn/bigdenary (decimal)
  3. https://github.com/MikeMcl/big.js/ (decimal)
  4. https://github.com/MikeMcl/bignumber.js/ (decimal)
  5. https://github.com/davidmartinez10/bigfloat#readme (decimal)
  6. https://github.com/plow-technologies/bs-Zarith (decimal)
  7. https://github.com/royNiladri/js-big-decimal (decimal)
  8. https://github.com/charto/bigfloat (binary)
  9. https://github.com/munsocket/jampary (binary)
  10. ...

Benchmark results:

Results of running the benchmark from https://github.com/munsocket/jampary#early-stage-results-without-wasm-and-fma :

librarytime
Jampary_Wasm466 ms
Jampary204 ms
BigNumberJs263 ms
DecimalJs424 ms
BigFloat85 ms
BigDecimal135 ms
BigJs3403 ms
BigFloat32158 ms
1.0.29

9 months ago

1.0.28

10 months ago

1.0.32

9 months ago

1.0.30

9 months ago

1.0.36

9 months ago

1.0.35

9 months ago

1.0.34

9 months ago

1.0.27

11 months ago

1.0.26

2 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.23

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.10

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago