-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters