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

@synap/sig-fig-calculator

v3.2.3

Published

a small helper class for extracting and handling significant figures

Downloads

32

Readme

SigFig Calculator

This is a library aimed at automating significant figure math.

Installation

Install with npm i @synap/sig-fig-calculator or yarn add @synap/sig-fig-calculator.

Getting Started

To get started using SigFig in your project:

import { SigFig } from '@synap/sig-fig-calculator';

SigFig.toParsed('-1.2345');
// { value: -1.2345, precision: { total: 5, index: 4, decimal: 4 } }

SigFig.multiply('-1.2345', '+1.00e-1').toString();
// '-0.123'

Vocabulary & Concepts

There are a couple of words worth pointing out up front:

  • "total" precision is the number of significant digits when represented in scientific notation
  • "decimal" precision is the number of digits shown to the right of the decimal point
  • "index" precision is the index, relative to the decimal point, at which the last significant digit appears which means this value can be negative unlike the "decimal" precision
  • an "exact" number is one which has infinite significance; e.g., constants, fractions, or conversion factors
  • when multiplying or dividing by an exact number, the user must be explicit about whether to do so "as a ratio" or "as a conversion"; e.g., '1.200' * 100 = '120.000' when treated as ratio multiplication but '1.200' * 100 = '120.0' when treated as conversion multiplication, or in more technical terms, multiplying "as a ratio" will hold the index precision constant while multiplying "as a conversion" will hold the total precision constant

API

This module mainly exports the SigFig class, although it also exports various constants and functions.

SigFig Class

This class can create instances (prefer SigFig.from) but also has various static utilities that may achieve what you want.

Instance Utilities

Here is a list of the public properties and methods available when you create a SigFig instance.

  • The following examples assume const sf = SigFig.from('1.23e-4')

    • value:number

      • returns the numerical value extracted from the input
      • e.g. sf.value => 1.23e-4
    • get precision():{ total:number, index:number, decimal:number }

      • returns info about the precision of the input
      • e.g. sf.precision => { total: 3, index: 6, decimal: 6 }
    • get parsed():{ value: number, precision: { total: number, decimal: number } }

      • returns all the information about the input
      • e.g. sf.parsed => { value: 0.000123, precision: { total: 3, index: 6, decimal: 6 } }
    • add|divide|multiply|subtract(b:number|SigFig.Type):this

      • adds, divides, multiplies, or subtracts the input against itself and returns itself; number inputs have no significant figures, and the rules of significant figures will be followed in all cases
      • e.g. sf.add(2).toString() => '2.000123'
      • e.g. sf.add('2').toString() => '2'
      • e.g. sf.divide(2).toString() => '0.0000615'
      • e.g. sf.divide('2').toString() => '0.00006'
      • e.g. sf.multiply(2).toString() => '0.000246'
      • e.g. sf.multiply('2').toString() => '0.0002'
      • e.g. sf.subtract(2).toString() => '-1.999877'
      • e.g. sf.subtract('2').toString() => '-2'
    • toFixed():string

      • returns the string which results from calling .toFixed() on the numerical value with the decimal precision as the argument; this should never show scientific notation
      • e.g. sf.toFixed() => '0.000123'
    • toJSON():string

      • returns this.toPrecision() so sig fig objects can be easily serialized in JSON
      • e.g. JSON.stringify({ sigFig: sf }) => '{"sigFig":"0.000123"}'
    • toPrecision():string

      • returns the string which results from calling .toPrecision() on the numerical value with the total precision as the argument; in some cases it may resort to scientific notation to avoid ambiguity
      • e.g. sf.toPrecision() => '0.000123'
    • toString():string

      • return this.toPrecision() so sig fig objects can be easily interpolated within strings; if you prefer values to be shown without scientific notation, you may want to use .toFixed()
      • e.g. 'the value is ' + sf => 'the value is 0.000123'
    • updatePrecision(precision:{ total:number }|{ index:number }|{ decimal:number }):this

      • allows for updating the number of significant figures; supply either decimal or total, not both, as the other will automatically be calculated appropriately
      • e.g. sf.updatePrecision({ decimal: 8 }).toString() => '0.00012300'
      • e.g. sf.updatePrecision({ total: 4 }).toString() => '0.0001230'

Static Utilities

Here is a list of the most important public static properties and methods available when you import the SigFig class. Other properties and methods are available, but less common, and can thus be read about in the documentation comments.

  • SigFig

    • static add|divide|multiply|subtract(a:SigFig.Type, b:number|SigFig.Type):SigFig

      • this is equivalent to SigFig.from(a).add|divide|multiply|subtract(b)
    • static isType(obj:any):obj is SigFig.Type

      • this will check an input to see if it can be parsed by the SigFig class
    • static toParsed(measurement:SigFig.Type):{ value: number, precision: { total:number, index:number, decimal:number } }

      • this is equivalent to SigFig.from(measurement).parsed

Constants

The following constants are available as a matter of convenience, but are mainly used internally.

rJsonNumber:RegExp

This RegExp is used internally to extract various details from a significant figure string.

rJsonNumber.exec('1.1234e-4');
// => [ '1.1234e-4', '1', '.123', '4', '-4', index: 0, input: '1.1234e-4', groups: undefined ]

NOTE: this RegExp is also exported on the class itself as SigFig.rSigFigString.

Functions

The following functions are available as a matter of convenience, but are mainly used internally.

getPrecisionInfo(value:string):{ decimal:number, total:number }

Use this function to quickly extract the precision info from a number stored as a string.

getPrecisionInfo('1.234');
// => { total: 4, index: 3, decimal: 3 }

isInteger(value:any):value is number

Function is mainly for internal use. It checks if the input is a number and if it can be evenly divided by 1. It also acts as a type guard.

isInteger(3.0); // => true
isInteger(3.3); // => false

isValidTotalPrecision(value:any):value is number

Function is mainly for internal use. It checks if the input is an integer in the mathematical set [1,100]. It also acts as a type guard.

isValidTotalPrecision(0); // => false
isValidTotalPrecision(50); // => true

isValidFixedPrecision(value:any):value is number

Function is mainly for internal use. It checks if the input is an integer in the mathematical set [0,100]. It also acts as a type guard.

isValidFixedPrecision(-1); // => false
isValidFixedPrecision(0); // => false
isValidFixedPrecision(50); // => true