1.3.2 • Published 2 years ago

@lzwme/asmd-calc v1.3.2

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

@lzwme/asmd-calc

@lzwme/asmd-calc

build status NPM version node version npm download GitHub issues GitHub forks GitHub stars license MIT

简体中文

A short and concise JavaScript library for the four fundamental operations of arithmetic, which supports the addition, subtraction, multiplication and division calculation of floating-point precision.

Features

  • Short and sharp. It only includes the calculation of addition, subtraction, multiplication and division. The core code is less than 100 lines. There is no other dependency.
  • High execution performance. Only the four operations that can be accurately expressed by the front end are considered to realize simple logic and better execution performance than the open source library with rich and complex functions (see Benchmark for details).
  • Accurate and stable. The accumulation of many years of practical experience in the development of several financial trading systems covers various common calculation use case error scenarios.

Not Applicable

  • For numerical processing involving very large numbers that front-end can not be accurately represented.
  • Complex mathematical scientific calculation.

These libraries are recommended:

Install

# npm
npm i @lzwme/asmd-calc
# yarn
yarn add @lzwme/asmd-calc
# pnpm
pnpm add @lzwme/asmd-calc

Useage

1. Calculation example using simple tool method

es module

import { add } from '@lzwme/asmd-calc';

console.log(add(0.1, 0.2, 0.3));
// => 0.6

commonjs

const calc = require('@lzwme/asmd-calc');

console.log(calc.add(0.1, 0.2, 0.3));
// => 0.6

2. Calculation example using chain operation

es module

import { AsmdCalc } from '@lzwme/asmd-calc';

const a = new AsmdCalc(0);
console.log(+a.add(0.1).add(0.2, 0.3));
// => 0.6

const b = new AsmdCalc(0.3);
  b.add(0.1, 0.2)
    .add(0.1)
    .sub(0.1, 0.2)
    .sub(0.1)
    .div(0.3)
    .mul(0.3);
console.log(+b);
// => 0.3
console.log(b.value);
// => 0.3

commonjs

const AsmdCalc = require('@lzwme/asmd-calc').AsmdCalc;

const calc = new AsmdCalc(0);
console.log(calc.add(0.1).add(0.2, 0.3));
// => 0.6

API

  • add(...args); - Addition
  • sub(...args); - Subtraction
  • mul(...args); - Multiplication
  • div(...args); - Division
  • keepDotLength(value, len, isRounding = false): number; - Keep N decimal places
  • toFixed(value, len): string; - Similar to Number.prototype.toFixed, but fixed precision of the result
  • getDecimalLen(num): number; - Get the decimal length
  • toNonExponential(num): string; - Convert to string format of unscientific counting method

Development and testing

  • Development
pnpm install
pnpm start
  • Testing
npm test
npm run benchmark
  • Build
npm run build

Benchmark

npm run benchmark

See:Benchmark

The following results are the time-consuming comparison of executing 10000 * N times on the same machine:

type/timesjsRawasmd-calcmathjs
add-1000019.225ms169.535ms415.145ms
sub-1000016.269ms34.827ms171.263ms
mul-1000018.518ms51.625ms235.868ms
div-1000027.025ms79.504ms300.706ms

Pre execution of 1000000 times and then stats time-consuming of execution of 10000 * N times:

type/timesjsRawasmd-calcmathjs
add-100007.768ms155.836ms362.819ms
sub-100008.339ms25.147ms155.611ms
mul-100009.995ms35.685ms224.357ms
div-1000015.666ms77.407ms280.322ms

References

License

@lzwme/asmd-calc is released under the MIT license.

该插件由志文工作室开发和维护。

1.3.2

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.5

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.4

4 years ago