@rinxun/numeric-calculator 1.0.1 → 1.0.4
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.
- package/README.md +10 -6
- package/index.d.ts +4 -2
- package/index.js +10 -4
- package/package.json +10 -6
- package/.gitignore +0 -15
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<h1 align="center">Numeric Calculator</h1>
|
|
2
2
|
|
|
3
|
-
<p align="center">Calculate addition,
|
|
3
|
+
<p align="center">Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue</p>
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
@@ -48,6 +48,7 @@ This project is licensed under the terms of the [MIT license](https://github.com
|
|
|
48
48
|
|
|
49
49
|
- ```js
|
|
50
50
|
config = {
|
|
51
|
+
precision?: number, // global precision, default to 15
|
|
51
52
|
enableCheckBoundary?: boolean // if true, it will check if the value is out of the safe boundary
|
|
52
53
|
}
|
|
53
54
|
```
|
|
@@ -94,7 +95,7 @@ const inst5 = new NC(10, { enableCheckBoundary: true });
|
|
|
94
95
|
|
|
95
96
|
|
|
96
97
|
|
|
97
|
-
<h4>inst.plus</h4>
|
|
98
|
+
<h4>inst.plus(operands: IOperand[])</h4>
|
|
98
99
|
|
|
99
100
|
```typescript
|
|
100
101
|
const result = new NC().plus(2.134).toPrecision(); // 2.134
|
|
@@ -105,7 +106,7 @@ const result = new NC(10).plus(2.1341, '3.09', 0.65).toPrecision(); // => 10+2.1
|
|
|
105
106
|
|
|
106
107
|
|
|
107
108
|
|
|
108
|
-
<h4>inst.minus</h4>
|
|
109
|
+
<h4>inst.minus(operands: IOperand[])</h4>
|
|
109
110
|
|
|
110
111
|
```typescript
|
|
111
112
|
const result = new NC().minus(0.4).toPrecision(); // 0.4
|
|
@@ -116,7 +117,7 @@ const result = new NC(-2).minus(0.25, 1.5, 3.498).toPrecision(); // -2-0.25-1.5-
|
|
|
116
117
|
|
|
117
118
|
|
|
118
119
|
|
|
119
|
-
<h4>inst.times</h4>
|
|
120
|
+
<h4>inst.times(operands: IOperand[])</h4>
|
|
120
121
|
|
|
121
122
|
```typescript
|
|
122
123
|
const result = new NC().times(0.12).toPrecision(); // 0.12
|
|
@@ -127,7 +128,7 @@ const result = new NC(3.2).times(0.12, '100', 5).toPrecision(); // => 3.2*0.12*1
|
|
|
127
128
|
|
|
128
129
|
|
|
129
130
|
|
|
130
|
-
<h4>inst.divide</h4>
|
|
131
|
+
<h4>inst.divide(operands: IOperand[])</h4>
|
|
131
132
|
|
|
132
133
|
```typescript
|
|
133
134
|
const result = new NC().divide(3).toPrecision(); // 3
|
|
@@ -138,7 +139,7 @@ const result = new NC(-1000).divide(100, '20', 2).toPrecision(); // -1000/100/20
|
|
|
138
139
|
|
|
139
140
|
|
|
140
141
|
|
|
141
|
-
<h4>inst.toPrecision</h4>
|
|
142
|
+
<h4>inst.toPrecision(precision?: number)</h4>
|
|
142
143
|
|
|
143
144
|
```typescript
|
|
144
145
|
const result = new NC(1.2345).toPrecision(); // 1.2345
|
|
@@ -157,3 +158,6 @@ const result = new NC(10).plus(new NC().times(2, 4), '6').divide(new NC(12).minu
|
|
|
157
158
|
|
|
158
159
|
|
|
159
160
|
|
|
161
|
+
<h2>Changelog</h2>
|
|
162
|
+
|
|
163
|
+
If you have recently updated, please read the [changelog](https://github.com/rinxun/numeric-calculator/releases) for details of what has changed.
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
declare type IConfig = {
|
|
2
|
+
precision?: number;
|
|
2
3
|
enableCheckBoundary?: boolean;
|
|
3
4
|
};
|
|
4
5
|
declare type IOperand = Calculator | number | string;
|
|
@@ -13,6 +14,7 @@ declare class Calculator {
|
|
|
13
14
|
* @description global register for temporary results, core for chaining operations
|
|
14
15
|
*/
|
|
15
16
|
private _VALUE;
|
|
17
|
+
private _precision;
|
|
16
18
|
/**
|
|
17
19
|
* @private
|
|
18
20
|
* @description for debugging, if true, it will check if the value is out of the safe boundary
|
|
@@ -71,8 +73,8 @@ declare class Calculator {
|
|
|
71
73
|
private calculate;
|
|
72
74
|
/**
|
|
73
75
|
* @constructor
|
|
74
|
-
* @param {IOperand|undefined} operand
|
|
75
|
-
* @param {
|
|
76
|
+
* @param {IOperand|undefined} operand Initial operand
|
|
77
|
+
* @param {IConfig|undefined} config Configurations
|
|
76
78
|
*/
|
|
77
79
|
constructor(operand?: IOperand, config?: IConfig);
|
|
78
80
|
/**
|
package/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
2
3
|
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
4
|
if (ar || !(i in from)) {
|
|
@@ -7,6 +8,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
7
8
|
}
|
|
8
9
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
10
|
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
12
|
/**
|
|
11
13
|
* @description Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue
|
|
12
14
|
* @author Janden Ma
|
|
@@ -17,8 +19,8 @@ var Calculator = /** @class */ (function () {
|
|
|
17
19
|
// #region Constructor
|
|
18
20
|
/**
|
|
19
21
|
* @constructor
|
|
20
|
-
* @param {IOperand|undefined} operand
|
|
21
|
-
* @param {
|
|
22
|
+
* @param {IOperand|undefined} operand Initial operand
|
|
23
|
+
* @param {IConfig|undefined} config Configurations
|
|
22
24
|
*/
|
|
23
25
|
function Calculator(operand, config) {
|
|
24
26
|
// #region Private Variables
|
|
@@ -27,6 +29,7 @@ var Calculator = /** @class */ (function () {
|
|
|
27
29
|
* @description global register for temporary results, core for chaining operations
|
|
28
30
|
*/
|
|
29
31
|
this._VALUE = undefined;
|
|
32
|
+
this._precision = 15;
|
|
30
33
|
/**
|
|
31
34
|
* @private
|
|
32
35
|
* @description for debugging, if true, it will check if the value is out of the safe boundary
|
|
@@ -36,6 +39,9 @@ var Calculator = /** @class */ (function () {
|
|
|
36
39
|
this._VALUE = this.parseOperandToNum(operand);
|
|
37
40
|
}
|
|
38
41
|
if (config) {
|
|
42
|
+
if (config.precision !== undefined) {
|
|
43
|
+
this._precision = config.precision;
|
|
44
|
+
}
|
|
39
45
|
if (config.enableCheckBoundary) {
|
|
40
46
|
this._enableCheckBoundary = config.enableCheckBoundary;
|
|
41
47
|
}
|
|
@@ -89,7 +95,7 @@ var Calculator = /** @class */ (function () {
|
|
|
89
95
|
* @param {number} precision default 15
|
|
90
96
|
*/
|
|
91
97
|
Calculator.prototype.processPrecision = function (num, precision) {
|
|
92
|
-
return +parseFloat(num.toPrecision(precision || 15));
|
|
98
|
+
return +parseFloat(num.toPrecision(precision || this._precision || 15));
|
|
93
99
|
};
|
|
94
100
|
/**
|
|
95
101
|
* core processor for times
|
|
@@ -261,4 +267,4 @@ var Calculator = /** @class */ (function () {
|
|
|
261
267
|
};
|
|
262
268
|
return Calculator;
|
|
263
269
|
}());
|
|
264
|
-
|
|
270
|
+
exports.default = Calculator;
|
package/package.json
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rinxun/numeric-calculator",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Calculate addition,
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"long",
|
|
7
|
+
"float",
|
|
8
|
+
"decimal",
|
|
7
9
|
"numeric",
|
|
8
|
-
"
|
|
9
|
-
"calculator"
|
|
10
|
+
"precision",
|
|
11
|
+
"calculator",
|
|
12
|
+
"addition",
|
|
13
|
+
"subtraction",
|
|
14
|
+
"multiplication",
|
|
15
|
+
"division"
|
|
10
16
|
],
|
|
11
17
|
"main": "index.js",
|
|
12
|
-
"type": "module",
|
|
13
18
|
"types": "**/*.d.ts",
|
|
14
19
|
"license": "MIT",
|
|
15
20
|
"private": false,
|
|
@@ -37,7 +42,6 @@
|
|
|
37
42
|
"files": [
|
|
38
43
|
"*.js",
|
|
39
44
|
"*.d.ts",
|
|
40
|
-
".gitignore",
|
|
41
45
|
"package.json",
|
|
42
46
|
"README.md",
|
|
43
47
|
"LICENSE"
|