Skip to content

About

Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Numeric Calculator

Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue


Installation

It is available as an npm package.

// with npm
npm install @rinxun/numeric-calculator

// with yarn
yarn add @rinxun/numeric-calculator

License

This project is licensed under the terms of the MIT license.

Why do we need this

0.1 + 0.2 = 0.30000000000000004; 
0.1 + 0.2 - 0.3 = 5.551115123125783e-17;

Constructor

  • new NC(operand: IOperand, config: IConfig)

    • number: Initial numeric

      • optional
      • type IOperand = instance | number | string
    • config: Configurations

      • optional

      • config = {
          precision?: number, // global default precision, default to 15, should be in the range 0 - 20, effects `toPrecision` when `toPrecision` has no args
          fractionDigits?: number, // global default decimal length, default to 2, should be in the range 0 - 20, effects `toFixed` when `toFixed` has no args
          enableCheckBoundary?: boolean // if true, it will check if the value is out of the safe boundary
        }

Methods

The instance supports chain operations.

  • plus(operands: IOperand[]) => instance
  • minus(operands: IOperand[]) => instance
  • times(operands: IOperand[]) => instance
  • divide(operands: IOperand[]) => instance
  • round(fractionDigits?: number) => number
  • toPrecision(precision?: number) => number
  • toFixed(fractionDigits?: number) => string

Usage

Import

// CommonJS
const { default: NC } = require('@rinxun/numeric-calculator');

// ES6 
import NC from '@rinxun/numeric-calculator';

Instantiate

const inst1 = new NC(); 
// or with initial numeric
const inst2 = new NC(10); 
const inst3 = new NC('10'); 
const inst4 = new NC(new NC(10)); 
// or with config
const inst5 = new NC(10, { enableCheckBoundary: true }); 

inst.plus(operands: IOperand[])

const result = new NC().plus(2.134).toPrecision(); // 2.134
const result = new NC().plus(2.1341, new NC(1.09).plus(2), '0.65').toPrecision(); // => 2.1341+(1.09+2)+0.65 = 5.8741
const result = new NC(10).plus(2.134).toPrecision(); // => 10+2.134 = 12.134
const result = new NC(10).plus(2.1341, '3.09', 0.65).toPrecision(); // => 10+2.1341+3.09+0.65 = 15.8741

inst.minus(operands: IOperand[])

const result = new NC().minus(0.4).toPrecision(); // 0.4
const result = new NC().minus(0.25, new NC(2.5).minus(1), '3.498').toPrecision(); // => 0.25-(2.5-1)-3.498 -4.748
const result = new NC(2).minus(0.4).toPrecision(); // => 2-0.4 = 1.6
const result = new NC(-2).minus(0.25, 1.5, 3.498).toPrecision(); // -2-0.25-1.5-3.498 = -7.248

inst.times(operands: IOperand[])

const result = new NC().times(0.12).toPrecision(); // 0.12
const result = new NC().times(0.12, new NC(8).times(12.5), '5').toPrecision(); // => 0.12*(8*12.5)*5 = 60
const result = new NC(-4).times(0.12).toPrecision(); // => -4*0.12 = -0.48
const result = new NC(3.2).times(0.12, '100', 5).toPrecision(); // => 3.2*0.12*100*5 = 192

inst.divide(operands: IOperand[])

const result = new NC().divide(3).toPrecision(); // 3
const result = new NC().divide(100, new NC(60).divide(3), '2').toPrecision(); // => 100/(60/3)/2 = 2.5
const result = new NC(36.12).divide(3).toPrecision(); // => 36.12/3 = 12.04
const result = new NC(-1000).divide(100, '20', 2).toPrecision(); // -1000/100/20/2 = -0.25

inst.round(fractionDigits?: number, options?: { snap?: boolean })

const result = new NC().round(3); // 0
const result = new NC(5.255).round(2); // 5.26
const result = new NC(7.65).times(16.5).round(2); // => 7.65 * 16.5 = 126.225 ≈ 126.23
const result = new NC(26.05).times(16.5).round(2); // => 26.05 * 16.5 = 429.825 ≈ 429.83

options.snap (default false) makes rounding tolerant to binary floating-point representation dust. After plain float arithmetic upstream, a true 95.015 can arrive as 95.01499999999942, and exact-decimal rounding of the represented value then rounds down, disagreeing with the true value. With snap, the amplified value is nudged by a few ULPs relative to its own magnitude before Math.round, so only values sitting exactly on a rounding boundary are affected — everything beyond a few ULPs of the boundary keeps its exact-decimal result, and there is no magic number to tune:

const dust = 2959.9649999999997; // true value 2959.965, distorted by float subtraction upstream
const result = new NC(dust).round(2); // 2959.96 — exact decimal of the represented value
const result = new NC(dust).round(2, { snap: true }); // 2959.97 — matches the true half-up value
const result = new NC(719.6446).round(2, { snap: true }); // 719.64 — values beyond a few ULPs of the boundary are untouched

inst.toPrecision(precision?: number)

const result = new NC(1.2345).toPrecision(); // 1.2345
const result = new NC(1.2345).toPrecision(4); // 1.234
const result = new NC(1.2e-2).toPrecision(); // 0.012

inst.toFixed(fractionDigits?: number)

Follow the Banker's Rounding rules

const result = new NC(1.2345).toFixed(); // '1.23'
const result = new NC(1.2345).toFixed(3); // '1.234'
const result = new NC(1.2346).toFixed(3); // '1.235'
const result = new NC(1.2e-2).toFixed(); // '0.01'

chain operations

const result = new NC(10).plus(2).minus(4).times(2).divide(8).toPrecision(); // => (10+2-4)*2/8 = 2
const result = new NC(10).plus(new NC().times(2, 4), '6').divide(new NC(12).minus(4)).toPrecision(); // => (10+(2*4)+6)/(12-4) = 3

Changelog

If you have recently updated, please read the changelog for details of what has changed.

About

Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages