Skip to content

Commit

Permalink
update minus function, fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
camsong committed Oct 18, 2017
1 parent 934842f commit 3e1f681
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function times(num1, num2) {
return num1Changed * num2Changed / Math.pow(10, baseNum);
}


/**
* 精确加法
*/
Expand All @@ -46,7 +45,7 @@ function plus(num1, num2) {
*/
function minus(num1, num2) {
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (num1 * baseNum - num2 * baseNum) / baseNum;
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ describe('NP.minus', () => {
}

it('can do minus operation', () => {
check(0.07, 0.01, 0.06);
check(0.7, 0.1, 0.6);
check(1.0, 0.9, 0.1);
check(1, 0, 1);
check(1, -0, 1);
Expand All @@ -78,6 +80,8 @@ describe('NP.times', () => {
}

it('can do times operation', () => {
check(0.07, 100, 7);
check(0.7, 0.1, 0.07);
check(3, 0.3, 0.9);
check(0.362, 100, 36.2);
check(1.1, 1.1, 1.21);
Expand Down

0 comments on commit 3e1f681

Please sign in to comment.