# @yaffle/bigdecimal

> Arbitrary precision decimal arithmetic library. Polyfill for decimal proposal. Implemented on the top of BigInt.

Latest version **2.0.2** (published 2024-05-06) · $$$ license · 0 weekly downloads

## Install

```sh
npm install @yaffle/bigdecimal
pnpm add @yaffle/bigdecimal
yarn add @yaffle/bigdecimal
bun add @yaffle/bigdecimal
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2024-05-06 |
| First published | 2020-08-29 |
| Weekly downloads | 0 |
| License | $$$ |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 55.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 51 |
| Author | Viktor Mukhachev |
| Maintainers | yaffle |
| Keywords | arbitrary, precision, decimal, arithmetic, bigdecimal |

## Links

- npm: https://www.npmjs.com/package/@yaffle/bigdecimal
- Repository: https://github.com/Yaffle/BigDecimal
- Homepage: https://github.com/Yaffle/BigDecimal#readme
- Issues: https://github.com/Yaffle/BigDecimal/issues
- npm.io page: https://npm.io/package/@yaffle/bigdecimal

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2024-05-06
- 2.0.1 — 2024-05-06
- 2.0.0 — 2024-05-06
- 1.0.36 — 2023-08-20
- 1.0.35 — 2023-08-19
- 1.0.34 — 2023-08-19
- 1.0.32 — 2023-08-14
- 1.0.30 — 2023-08-13
- 1.0.29 — 2023-08-13
- 1.0.28 — 2023-07-15
- 1.0.27 — 2023-05-30
- 1.0.26 — 2022-07-26
- 1.0.25 — 2021-10-04
- 1.0.24 — 2021-06-01
- 1.0.23 — 2021-04-26
- … 21 more at https://npm.io/package/@yaffle/bigdecimal/versions

## README

# 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"])


## 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.cmp(a, b)

## Math: (not in the spec)
    To use:
    import addMath from './BigDecimalMath.js';
    addMath(BigDecimal, 10);

    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:
```javascript

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 :

| 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  |

---
_Source: https://npm.io/package/@yaffle/bigdecimal · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
