npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@splendo/bigdecimal

v1.0.26

Published

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

Downloads

501

Readme

Forked from https://github.com/Yaffle/BigDecimal

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 @splendo/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)
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".

BigFloat

Similar to BigDecimal, but uses base 2.

Example:


import {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"}).

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 |