-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
344 lines (300 loc) · 9.77 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
type IConfig = {
precision?: number;
fractionDigits?: number;
enableCheckBoundary?: boolean;
};
type IOperand = Calculator | number | string;
/**
* @description Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue
* @author Janden Ma
* @copyright Symply Software Inc
*/
class Calculator {
// #region Private Variables
/**
* @private
* @description global register for temporary results, core for chaining operations
*/
private _VALUE: number | undefined = undefined;
/**
* @private
* @description global default precision, default to 15, should be in the range 0 - 20, effects `toPrecision` when `toPrecision` has no args
*/
private _precision: number = 15;
/**
* @private
* @description global default decimal length, default to 2, should be in the range 0 - 20, effects `toFixed` when `toFixed` has no args
*/
private _fractionDigits: number = 2;
/**
* @private
* @description for debugging, if true, it will check if the value is out of the safe boundary
*/
private _enableCheckBoundary: boolean = false;
// #endregion
// #region Private Functions
/**
* @description check if the value is out of the safe boundary
* @param {number} value
*/
private checkBoundary(value: number) {
if (this._enableCheckBoundary) {
const val = value || this._VALUE || 0;
if (val > Number.MAX_SAFE_INTEGER || val < Number.MIN_SAFE_INTEGER) {
console.warn(
`${val} is out of the safe boundary, the result may be not accurate!`
);
}
}
}
/**
* @description get the length of the mantissa of the numeric
* @param {number} num numeric
*/
private getMantissaLen(num: number) {
const arrBySplittingExponent = num.toString().split(/[Ee]/); // eg: 2.1337983389e-12
const exponentPrefix = arrBySplittingExponent[0]; // value before e (eg: 2.1337983389)
const exponentSuffix = arrBySplittingExponent[1] || 0; // exponent length (eg: -12)
const decimals = exponentPrefix.split(".")[1] || ""; // decimals before e (eg: 1337983389)
const len = decimals.length - +exponentSuffix;
return len > 0 ? len : 0;
}
/**
* @description according to the type of operand, parse it to number
* @param {IOperand} operand operand
*/
private parseOperandToNum(operand: IOperand) {
if (operand instanceof Calculator) {
return operand._VALUE;
} else if (typeof operand === "number") {
return operand;
} else {
const val = Number(operand);
return Number.isNaN(val) ? 0 : val;
}
}
/**
* get the precise value
* @param {number} num numeric
* @param {number} precision default 15
*/
private processPrecision(num: number, precision?: number) {
return +parseFloat(num.toPrecision(precision || this._precision || 15));
}
/**
* get the fixed-point value
* @param {number} num numeric
* @param {number} fractionDigits default 2
*/
private processFixed(num: number, fractionDigits?: number) {
return this.processPrecision(num).toFixed(
fractionDigits || this._fractionDigits || 2
);
}
/**
* core processor for times
* @param {number} num1 operand 1
* @param {number} num2 operand 2
*/
private processTimes(num1: number, num2: number) {
const num1MantissaLen = this.getMantissaLen(num1);
const num2MantissaLen = this.getMantissaLen(num2);
const amplifyingNum1 = num1 * Math.pow(10, num1MantissaLen);
const amplifyingNum2 = num2 * Math.pow(10, num2MantissaLen);
const magnification = Math.pow(10, num1MantissaLen + num2MantissaLen);
this.checkBoundary(amplifyingNum1);
this.checkBoundary(amplifyingNum2);
const amplifyingResult = amplifyingNum1 * amplifyingNum2;
this.checkBoundary(amplifyingResult);
return amplifyingResult / magnification;
}
/**
* core processor for divide
* @param {number} num1 operand 1
* @param {number} num2 operand 2
*/
private processDivide(num1: number, num2: number) {
const num1MantissaLen = this.getMantissaLen(num1);
const num2MantissaLen = this.getMantissaLen(num2);
const amplifyingNum1 = num1 * Math.pow(10, num1MantissaLen);
const amplifyingNum2 = num2 * Math.pow(10, num2MantissaLen);
const magnification = Math.pow(10, num2MantissaLen - num1MantissaLen);
this.checkBoundary(amplifyingNum1);
this.checkBoundary(amplifyingNum2);
const amplifyingResult = amplifyingNum1 / amplifyingNum2;
return this.processTimes(
this.processPrecision(amplifyingResult),
magnification
);
}
/**
* core processor for minus
* @param {number} num1 operand 1
* @param {number} num2 operand 2
*/
private processMinus(num1: number, num2: number) {
const num1MantissaLen = this.getMantissaLen(num1);
const num2MantissaLen = this.getMantissaLen(num2);
const maxMantissaLen = Math.max(num1MantissaLen, num2MantissaLen);
const magnification = Math.pow(10, maxMantissaLen);
const amplifyingNum1 = this.processTimes(num1, magnification);
const amplifyingNum2 = this.processTimes(num2, magnification);
this.checkBoundary(amplifyingNum1);
this.checkBoundary(amplifyingNum2);
const amplifyingResult = amplifyingNum1 - amplifyingNum2;
this.checkBoundary(amplifyingResult);
return amplifyingResult / magnification;
}
/**
* core processor for plus
* @param {number} num1 operand 1
* @param {number} num2 operand 2
*/
private processPlus(num1: number, num2: number) {
const num1MantissaLen = this.getMantissaLen(num1);
const num2MantissaLen = this.getMantissaLen(num2);
const maxMantissaLen = Math.max(num1MantissaLen, num2MantissaLen);
const magnification = Math.pow(10, maxMantissaLen);
const amplifyingNum1 = this.processTimes(num1, magnification);
const amplifyingNum2 = this.processTimes(num2, magnification);
this.checkBoundary(amplifyingNum1);
this.checkBoundary(amplifyingNum2);
const amplifyingResult = amplifyingNum1 + amplifyingNum2;
this.checkBoundary(amplifyingResult);
return amplifyingResult / magnification;
}
/**
* core processor for times
* @param {IOperand[]} operands operands
* @param {"plus" | "minus" | "times" | "divide"} op operator
*/
private calculate(
operands: Array<IOperand>,
op: "plus" | "minus" | "times" | "divide"
) {
const values = [...operands];
let firstOperand = this._VALUE;
const analyzeFirstOperand = (): number => {
const parsedVal = this.parseOperandToNum(values[0]);
values.shift();
if (parsedVal === undefined) {
return analyzeFirstOperand();
}
return parsedVal;
};
if (firstOperand === undefined) {
firstOperand = analyzeFirstOperand();
}
const result = values.reduce((prevOperand, currOperand) => {
const prev = this.parseOperandToNum(prevOperand)!;
const current = this.parseOperandToNum(currOperand);
if (current === undefined) {
return prev;
}
switch (op) {
case "plus": {
if (current === 0) {
return prev;
}
return this.processPlus(prev, current);
}
case "minus": {
if (current === 0) {
return prev;
}
return this.processMinus(prev, current);
}
case "times": {
if (current === 0) {
return 0;
}
return this.processTimes(prev, current);
}
case "divide": {
if (current === 0) {
return 0;
}
return this.processDivide(prev, current);
}
default:
return prev!;
}
}, firstOperand);
this._VALUE = this.parseOperandToNum(result);
}
// #endregion
// #region Constructor
/**
* @constructor
* @param {IOperand|undefined} operand Initial operand
* @param {IConfig|undefined} config Configurations
*/
constructor(operand?: IOperand, config?: IConfig) {
if (operand !== undefined && operand !== null) {
this._VALUE = this.parseOperandToNum(operand);
}
if (config) {
if (config.precision !== undefined) {
this._precision = config.precision;
}
if (config.fractionDigits !== undefined) {
this._fractionDigits = config.fractionDigits;
}
if (config.enableCheckBoundary) {
this._enableCheckBoundary = config.enableCheckBoundary;
}
}
}
// #endregion
// #region Public Functions
/**
* @description parse the result to precise numeric
* @param {number} precision default to 15
* @returns {number} precise result
*/
public toPrecision(precision?: number): number {
return this.processPrecision(this._VALUE || 0, precision);
}
/**
* @description parse the result to fixed-point value
* @param {number} fractionDigits default to 2, should be in the range 0 - 20
* @returns {string} fixed-point result
*/
public toFixed(fractionDigits?: number): string {
return this.processFixed(this._VALUE || 0, fractionDigits);
}
/**
* @description plus operator
* @param {Array<IOperand>} operands operands
*/
public plus(...operands: Array<IOperand>) {
this.calculate(operands, "plus");
return this;
}
/**
* @description minus operator
* @param {Array<IOperand>} operands operands
*/
public minus(...operands: Array<IOperand>) {
this.calculate(operands, "minus");
return this;
}
/**
* @description times operator
* @param {Array<IOperand>} operands operands
*/
public times(...operands: Array<IOperand>) {
this.calculate(operands, "times");
return this;
}
/**
* @description divide operator
* @param {Array<IOperand>} operands operands
*/
public divide(...operands: Array<IOperand>) {
this.calculate(operands, "divide");
return this;
}
// #endregion
}
export default Calculator;