1.1.0 • Published 5 years ago

@lzwme/asmd-calc v1.1.0

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

@lzwme/asmd-calc

@lzwme/asmd-calc

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

一个短小精悍的四则运算 JS 工具方法库,支持浮点数精度的加减乘除计算。

提供常见的加(add)、减(sub)、乘(mul)、除(div)计算方法,可满足涉及金额、价格处理等前端数据计算的大多数应用场景。

为什么选用 asmd-calc

  • 短小精悍。仅包含加减乘除计算,核心代码不足百行,无其他依赖,文件小巧够用
  • 执行性能高。仅考虑前端可精确表达的四则运算,实现逻辑简洁,执行性能优于功能丰富复杂的开源库(详见 Benchmark 部分)
  • 准确稳定。数个金融类交易系统多年开发实践经验的积累,覆盖了常见的各种计算用例误差场景

不适用的情况:

  • 对于涉及超大数等前端无法精确表示的数值处理,建议由后端语言计算并使用字符串方式返回给前端展示。也可使用 math.js 等开源库以字符串形式执行计算。
  • 对于较为复杂的数学科学计算需求,推荐使用开源库如 math.jsdecimal.jscalculatorjsbignumber.jsbig.js/等,具体可分别参见其官方文档。

安装

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

用法 (USEAGE)

1. 使用简单的工具方法计算示例

es module

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

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

import * as calc from '@lzwme/asmd-calc';

console.log(calc.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. 使用链式操作类计算示例

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) 加法运算
  • sub(...args) 减法运算
  • mul(...args) 乘法运算
  • div(...args) 除法运算
  • keepDotLength(value: number | string, precision: number, type: 'ceil' | 'round' | 'fround' | 'floor' | boolean = 'floor'): number | null 保留 N 位小数,返回数值
  • toFixed(value: number | string, precision: number, type: 'ceil' | 'round' | 'fround' | 'floor' | boolean = 'floor'): number | null 保留 N 位小数(默认四舍五入floor,返回字符串)
  • getDecimalLen(num) 获取指定数值的小数位长度
  • toNonExponential(num) 将指定的浮点数转换为非科学计数法的字符串格式

开发与测试

  • 开发
pnpm install
pnpm start
  • 测试
# 单元测试
npm test

# 基准测试
npm run benchmark
  • 构建
npm run build

Benchmark

npm run benchmark

基准测试代码参见:Benchmark

以下结果为在同一机器上分别执行 10000*N 次的耗时对比:

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

预执行 100_000 次后再执行 10000*N 次的耗时对比:

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

Benchmark Details

随机数据集(测试用例):

type/timesjsRawCalcasmdCalcdecimalmathjs
add-100004.973ms144.934ms192.637ms363.513ms
sub-100006.971ms21.84ms65.373ms165.045ms
mul-100008.45ms36.014ms107.898ms223.708ms
div-1000014.427ms64.409ms154.766ms290.645ms

纯小数数据集:

type/timesjsRawCalcasmdCalcdecimalmathjs
add-100000.248ms16.958ms30.875ms55.538ms
sub-100000.462ms22.529ms32.719ms46.321ms
mul-100000.232ms18.006ms34.765ms46.195ms
div-100000.519ms17.37ms36.031ms47.271ms

纯整数数据集:

type/timesjsRawCalcasmdCalcdecimalmathjs
add-100000.172ms0.681ms10.248ms35.004ms
sub-100000.816ms1.069ms12.736ms32.836ms
mul-100000.178ms0.733ms13.827ms33.486ms
div-100000.488ms0.699ms19.847ms42.217ms

相关参考

License

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

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

1.3.4

8 months ago

1.3.2

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.5

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.4

6 years ago