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

@phensley/decimal

v1.9.2

Published

Arbitrary precision decimal math

Downloads

2,535

Readme

@phensley/decimal

@phensley/decimal min+gzip

Arbitrary precision decimal math, used to support @phensley/cldr, but can be used by itself.

Installation

NPM:

npm install --save @phensley/decimal

Yarn:

yarn add @phensley/decimal

Example

import { Decimal, DecimalConstants, MathContext } from '@phensley/decimal';

const area = (radius: Decimal, ctx: MathContext) =>
  DecimalConstants.PI.multiply(radius, ctx).multiply(radius, ctx);

const calc = (ctx: MathContext) => {
  for (const r of ['.002', '1', '1.5', '999.999']) {
    const radius = new Decimal(r);
    console.log(
      `area of circle with radius ${r.padEnd(7)} is ${area(radius, ctx)}`
    );
  }
  console.log();
};

for (const scale of [5, 15]) {
  console.log(`Scale ${scale}:`);
  calc({ scale });
}

for (const precision of [10, 30]) {
  console.log(`Precision ${precision}:`);
  calc({ precision });
}

const LIGHT_YEAR_KM = new Decimal('9.461e12');
const NEAREST_STARS: any = {
  'Proxima Centauri': '4.32',
  "Barnard's Star": '5.96',
  'Wolf 359': '7.78',
  'Lalande 21185': '8.29'
};

for (const name of Object.keys(NEAREST_STARS)) {
  const distly = NEAREST_STARS[name];
  const distkm = new Decimal(distly).multiply(LIGHT_YEAR_KM, { precision: 30 });
  console.log(
    `Distance to ${name.padEnd(16)} is ${distkm.toString()} kilometers`
  );
}
Scale 5:
area of circle with radius .002    is 0.00001
area of circle with radius 1       is 3.14159
area of circle with radius 1.5     is 7.06858
area of circle with radius 999.999 is 3141586.36841

Scale 15:
area of circle with radius .002    is 0.000012566370614
area of circle with radius 1       is 3.141592653589793
area of circle with radius 1.5     is 7.068583470577035
area of circle with radius 999.999 is 3141586.370407627651860

Precision 10:
area of circle with radius .002    is 0.00001256637061
area of circle with radius 1       is 3.141592654
area of circle with radius 1.5     is 7.068583470
area of circle with radius 999.999 is 3141586.370

Precision 30:
area of circle with radius .002    is 0.0000125663706143591729538505735331
area of circle with radius 1       is 3.14159265358979323846264338328
area of circle with radius 1.5     is 7.06858347057703478654094761238
area of circle with radius 999.999 is 3141586.37040762765152975625124

Distance to Proxima Centauri is 40871520000000 kilometers
Distance to Barnard's Star   is 56387560000000 kilometers
Distance to Wolf 359         is 73606580000000 kilometers
Distance to Lalande 21185    is 78431690000000 kilometers