Skip to content

Commit

Permalink
feat(utils): 新增 Calculator 科学计算器
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 25, 2022
1 parent 5ef338a commit 5880148
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"cli-table3": "^0.6.0",
"cuid": "^2.1.8",
"date-fns": "^2.24.0",
"decimal.js": "^10.3.1",
"fast-xml-parser": "^3.18.0",
"ioredis": "^4.22.0",
"lodash-uni": "^1.1.0",
Expand Down
38 changes: 38 additions & 0 deletions src/utils/Calculator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Calculator } from './Calculator'

describe('Calculator', () => {
test('加正常', () => {
expect(0.1 + 0.2).not.toBe(0.3)
expect(Calculator.add(0.1, 0.2)).toBe(0.3)

expect(0.1 + 0.7).not.toBe(0.8)
expect(Calculator.add(0.1, 0.7)).toBe(0.8)

expect(0.2 + 0.4).not.toBe(0.6)
expect(Calculator.add(0.2, 0.4)).toBe(0.6)
})

test('减正常', () => {
expect(1 - 0.9).not.toBe(0.1)
expect(Calculator.sub(1, 0.9)).toBe(0.1)

expect(1.5 - 1.2).not.toBe(0.3)
expect(Calculator.sub(1.5, 1.2)).toBe(0.3)
})

test('乘正常', () => {
expect(0.8 * 3).not.toBe(2.4)
expect(Calculator.mul(0.8, 3)).toBe(2.4)

expect(0.55 * 100).not.toBe(55)
expect(Calculator.mul(0.55, 100)).toBe(55)
})

test('除正常', () => {
expect(0.3 / 0.1).not.toBe(3)
expect(Calculator.div(0.3, 0.1)).toBe(3)

expect(0.69 / 10).not.toBe(0.069)
expect(Calculator.div(0.69, 10)).toBe(0.069)
})
})
82 changes: 82 additions & 0 deletions src/utils/Calculator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Decimal from 'decimal.js'

/**
* 科学计算器。主要是为了避免 js 的浮点数精度计算问题。
*/
export class Calculator {
/**
* 加。
*
* - 加数列表为空时返回 0;
* - 加数列表长度为 1 时返回第 1 个的值。
*
* @param values 加数列表
*/
static add(...values: number[]): number {
return values.length === 0
? 0
: values.length === 1
? values[0]
: values.reduce((res, value) => res.add(value), new Decimal(0)).toNumber()
}

/**
* 减。
*
* - 减数列表为空时返回 0;
* - 减数列表长度为 1 时返回第 1 个的值。
*
* @param values 减数列表
*/
static sub(...values: number[]): number {
return values.length === 0
? 0
: values.length === 1
? values[0]
: values
.reduce(
(res, value, index) =>
index === 0 ? res.add(value) : res.sub(value),
new Decimal(0),
)
.toNumber()
}

/**
* 乘。
*
* - 乘数列表为空时返回 0;
* - 乘数列表长度为 1 时返回第 1 个的值。
*
* @param values 乘数列表
*/
static mul(...values: number[]): number {
return values.length === 0
? 0
: values.length === 1
? values[0]
: values.reduce((res, value) => res.mul(value), new Decimal(1)).toNumber()
}

/**
* 除。
*
* - 除数列表为空时返回 0;
* - 除数列表长度为 1 时返回第 1 个的值。
*
* @param values 除数列表
*/
static div(...values: number[]): number {
return values.length === 0
? 0
: values.length === 1
? values[0]
: values
.reduce(
(res, value, index) =>
index === 0 ? res.add(value) : res.div(value),
new Decimal(0),
)
.toNumber()
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from 'lodash-uni'
// @index(['./**/*.ts', '!./**/*.{test,perf}.*'], f => `export * from '${f.path}'`)
export * from './base64'
export * from './bindEvent'
export * from './Calculator'
export * from './cartesianProduct'
export * from './chooseFile'
export * from './chunkEqual'
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3449,6 +3449,11 @@ decimal.js@^10.2.1:
resolved "https://registry.nlark.com/decimal.js/download/decimal.js-10.3.1.tgz#d8c3a444a9c6774ba60ca6ad7261c3a94fd5e783"
integrity sha1-2MOkRKnGd0umDKatcmHDqU/V54M=

decimal.js@^10.3.1:
version "10.3.1"
resolved "https://registry.npmmirror.com/decimal.js/download/decimal.js-10.3.1.tgz#d8c3a444a9c6774ba60ca6ad7261c3a94fd5e783"
integrity sha512-V0pfhfr8suzyPGOx3nmq4aHqabehUZn6Ch9kyFpV79TGDTWFmHqUqXdabR7QHqxzrYolF4+tVmJhUG4OURg5dQ==

decode-uri-component@^0.2.0:
version "0.2.0"
resolved "https://registry.npm.taobao.org/decode-uri-component/download/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
Expand Down

0 comments on commit 5880148

Please sign in to comment.