@rinxun/numeric-calculator 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/index.js +39 -23
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -159,7 +159,7 @@ Follow the [Banker's Rounding rules](https://rounding.to/understanding-the-banke
159
159
  const result = new NC(1.2345).toFixed(); // '1.23'
160
160
  const result = new NC(1.2345).toFixed(3); // '1.234'
161
161
  const result = new NC(1.2346).toFixed(3); // '1.235'
162
- const result = new NC(1.2e-2).toFixed(); // 0.01
162
+ const result = new NC(1.2e-2).toFixed(); // '0.01'
163
163
  ```
164
164
 
165
165
 
package/index.js CHANGED
@@ -123,11 +123,11 @@ var Calculator = /** @class */ (function () {
123
123
  * @param {number} num2 operand 2
124
124
  */
125
125
  Calculator.prototype.processTimes = function (num1, num2) {
126
- var num1MatissaLen = this.getMantissaLen(num1);
127
- var num2MatissaLen = this.getMantissaLen(num2);
128
- var amplifyingNum1 = num1 * Math.pow(10, num1MatissaLen);
129
- var amplifyingNum2 = num2 * Math.pow(10, num2MatissaLen);
130
- var magnification = Math.pow(10, num1MatissaLen + num2MatissaLen);
126
+ var num1MantissaLen = this.getMantissaLen(num1);
127
+ var num2MantissaLen = this.getMantissaLen(num2);
128
+ var amplifyingNum1 = num1 * Math.pow(10, num1MantissaLen);
129
+ var amplifyingNum2 = num2 * Math.pow(10, num2MantissaLen);
130
+ var magnification = Math.pow(10, num1MantissaLen + num2MantissaLen);
131
131
  this.checkBoundary(amplifyingNum1);
132
132
  this.checkBoundary(amplifyingNum2);
133
133
  var amplifyingResult = amplifyingNum1 * amplifyingNum2;
@@ -140,11 +140,11 @@ var Calculator = /** @class */ (function () {
140
140
  * @param {number} num2 operand 2
141
141
  */
142
142
  Calculator.prototype.processDivide = function (num1, num2) {
143
- var num1MatissaLen = this.getMantissaLen(num1);
144
- var num2MatissaLen = this.getMantissaLen(num2);
145
- var amplifyingNum1 = num1 * Math.pow(10, num1MatissaLen);
146
- var amplifyingNum2 = num2 * Math.pow(10, num2MatissaLen);
147
- var magnification = Math.pow(10, num2MatissaLen - num1MatissaLen);
143
+ var num1MantissaLen = this.getMantissaLen(num1);
144
+ var num2MantissaLen = this.getMantissaLen(num2);
145
+ var amplifyingNum1 = num1 * Math.pow(10, num1MantissaLen);
146
+ var amplifyingNum2 = num2 * Math.pow(10, num2MantissaLen);
147
+ var magnification = Math.pow(10, num2MantissaLen - num1MantissaLen);
148
148
  this.checkBoundary(amplifyingNum1);
149
149
  this.checkBoundary(amplifyingNum2);
150
150
  var amplifyingResult = amplifyingNum1 / amplifyingNum2;
@@ -156,10 +156,10 @@ var Calculator = /** @class */ (function () {
156
156
  * @param {number} num2 operand 2
157
157
  */
158
158
  Calculator.prototype.processMinus = function (num1, num2) {
159
- var num1MatissaLen = this.getMantissaLen(num1);
160
- var num2MatissaLen = this.getMantissaLen(num2);
161
- var maxMatissaLen = Math.max(num1MatissaLen, num2MatissaLen);
162
- var magnification = Math.pow(10, maxMatissaLen);
159
+ var num1MantissaLen = this.getMantissaLen(num1);
160
+ var num2MantissaLen = this.getMantissaLen(num2);
161
+ var maxMantissaLen = Math.max(num1MantissaLen, num2MantissaLen);
162
+ var magnification = Math.pow(10, maxMantissaLen);
163
163
  var amplifyingNum1 = this.processTimes(num1, magnification);
164
164
  var amplifyingNum2 = this.processTimes(num2, magnification);
165
165
  this.checkBoundary(amplifyingNum1);
@@ -174,10 +174,10 @@ var Calculator = /** @class */ (function () {
174
174
  * @param {number} num2 operand 2
175
175
  */
176
176
  Calculator.prototype.processPlus = function (num1, num2) {
177
- var num1MatissaLen = this.getMantissaLen(num1);
178
- var num2MatissaLen = this.getMantissaLen(num2);
179
- var maxMatissaLen = Math.max(num1MatissaLen, num2MatissaLen);
180
- var magnification = Math.pow(10, maxMatissaLen);
177
+ var num1MantissaLen = this.getMantissaLen(num1);
178
+ var num2MantissaLen = this.getMantissaLen(num2);
179
+ var maxMantissaLen = Math.max(num1MantissaLen, num2MantissaLen);
180
+ var magnification = Math.pow(10, maxMantissaLen);
181
181
  var amplifyingNum1 = this.processTimes(num1, magnification);
182
182
  var amplifyingNum2 = this.processTimes(num2, magnification);
183
183
  this.checkBoundary(amplifyingNum1);
@@ -209,18 +209,34 @@ var Calculator = /** @class */ (function () {
209
209
  var result = values.reduce(function (prevOperand, currOperand) {
210
210
  var prev = _this.parseOperandToNum(prevOperand);
211
211
  var current = _this.parseOperandToNum(currOperand);
212
- if (!current) {
212
+ if (current === undefined) {
213
213
  return prev;
214
214
  }
215
215
  switch (op) {
216
- case "plus":
216
+ case "plus": {
217
+ if (current === 0) {
218
+ return prev;
219
+ }
217
220
  return _this.processPlus(prev, current);
218
- case "minus":
221
+ }
222
+ case "minus": {
223
+ if (current === 0) {
224
+ return prev;
225
+ }
219
226
  return _this.processMinus(prev, current);
220
- case "times":
227
+ }
228
+ case "times": {
229
+ if (current === 0) {
230
+ return 0;
231
+ }
221
232
  return _this.processTimes(prev, current);
222
- case "divide":
233
+ }
234
+ case "divide": {
235
+ if (current === 0) {
236
+ return 0;
237
+ }
223
238
  return _this.processDivide(prev, current);
239
+ }
224
240
  default:
225
241
  return prev;
226
242
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rinxun/numeric-calculator",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue",
5
5
  "keywords": [
6
6
  "long",