@numio/bigmath 1.0.11 → 2.0.0
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 +30 -3
- package/index.d.ts +6 -5
- package/index.js +6 -5
- package/package.json +9 -2
- package/src/IQR/main.js +5 -4
- package/src/IQR/types.d.ts +2 -2
- package/src/IQR/utils.js +4 -4
- package/src/MAD/main.js +5 -4
- package/src/MAD/types.d.ts +2 -2
- package/src/MAD/utils.js +3 -5
- package/src/compare/main.js +12 -10
- package/src/compare/types.d.ts +5 -5
- package/src/compare/utils.js +43 -60
- package/src/mean/main.js +5 -5
- package/src/mean/types.d.ts +2 -2
- package/src/mean/utils.js +4 -8
- package/src/operations/add/main.d.ts +2 -0
- package/src/operations/add/main.js +8 -0
- package/src/operations/div/main.d.ts +2 -0
- package/src/operations/div/main.js +9 -0
- package/src/operations/div/types.d.ts +2 -0
- package/src/operations/div/utils.d.ts +2 -0
- package/src/operations/div/utils.js +39 -0
- package/src/operations/mul/main.d.ts +2 -0
- package/src/operations/mul/main.js +7 -0
- package/src/operations/sub/main.d.ts +2 -0
- package/src/operations/sub/main.js +8 -0
- package/src/pipe/main.d.ts +8 -9
- package/src/pipe/main.js +34 -19
- package/src/pipe/utils.d.ts +10 -0
- package/src/pipe/utils.js +30 -0
- package/src/quartile/main.js +7 -7
- package/src/quartile/types.d.ts +6 -6
- package/src/quartile/utils.js +3 -10
- package/src/shared/constant.d.ts +0 -12
- package/src/shared/constant.js +0 -12
- package/src/shared/types.d.ts +6 -4
- package/src/shared/utils.d.ts +6 -3
- package/src/shared/utils.js +65 -59
- package/src/sort/main.js +7 -4
- package/src/sort/types.d.ts +3 -3
- package/src/sqrt/main.d.ts +3 -0
- package/src/sqrt/main.js +10 -0
- package/src/sqrt/types.d.ts +3 -0
- package/src/sqrt/utils.d.ts +2 -0
- package/src/sqrt/utils.js +24 -0
- package/src/mathOperations/add/main.d.ts +0 -2
- package/src/mathOperations/add/main.js +0 -7
- package/src/mathOperations/add/types.d.ts +0 -2
- package/src/mathOperations/add/utils.d.ts +0 -5
- package/src/mathOperations/add/utils.js +0 -59
- package/src/mathOperations/div/main.d.ts +0 -2
- package/src/mathOperations/div/main.js +0 -10
- package/src/mathOperations/div/types.d.ts +0 -3
- package/src/mathOperations/div/utils.d.ts +0 -3
- package/src/mathOperations/div/utils.js +0 -99
- package/src/mathOperations/mul/main.d.ts +0 -2
- package/src/mathOperations/mul/main.js +0 -7
- package/src/mathOperations/mul/types.d.ts +0 -2
- package/src/mathOperations/mul/utils.d.ts +0 -5
- package/src/mathOperations/mul/utils.js +0 -50
- package/src/mathOperations/sub/main.d.ts +0 -2
- package/src/mathOperations/sub/main.js +0 -7
- package/src/mathOperations/sub/types.d.ts +0 -2
- package/src/mathOperations/sub/utils.d.ts +0 -5
- package/src/mathOperations/sub/utils.js +0 -81
- package/src/types.d.ts +0 -7
package/README.md
CHANGED
@@ -11,12 +11,16 @@
|
|
11
11
|
* **Handle Numbers of Any Size:** Perform calculations on integers and decimals of virtually unlimited length, without the risk of JavaScript's `Number` limitations.
|
12
12
|
* **Eliminate Precision Loss:** Achieve accurate results even with numeric literals exceeding 15 significant digits, ensuring the integrity of your calculations.
|
13
13
|
* **Precise Decimal Operations:** Execute addition, subtraction, multiplication, and division on decimal numbers with guaranteed accuracy, including scenarios with negative values.
|
14
|
+
* ****NEW! Multi-Base Number Support:** Seamlessly perform arithmetic operations involving **hexadecimal (HEX), octal, binary, and decimal numbers**, offering unparalleled flexibility in handling various number formats.
|
15
|
+
|
14
16
|
|
15
17
|
**Unlock Advanced Numerical Operations:**
|
16
18
|
|
17
19
|
* **Control Division Precision:** Specify the exact number of digits after the decimal point for division results, with a default precision of 20 digits for high accuracy.
|
18
20
|
* **Flexible Rounding:** Round numbers to the nearest integer or a specific number of decimal places with various rounding modes (up, down, half-up, half-down, half-even, half-odd) to meet your exact requirements.
|
19
21
|
* **Round Based on Significant Figures:** Control rounding based on the number of significant figures, crucial for scientific and engineering applications.
|
22
|
+
* **Calculate Roots:**
|
23
|
+
* **Calculate Square Root (`sqrt`):** Compute the square root of a number with arbitrary precision. You can also specify the desired precision of the result.
|
20
24
|
* **Chain Operations with Pipe:** Simplify complex calculations by chaining arithmetic operations in a readable and intuitive manner.
|
21
25
|
* **Analyze Data Distribution:**
|
22
26
|
* **Calculate Quartiles (Q1, Q2, Q3):** Understand the spread and central tendency of your numerical data, helping identify outliers and the shape of the distribution.
|
@@ -38,15 +42,15 @@ This library is particularly invaluable in applications where numerical accuracy
|
|
38
42
|
* **Cryptography:** Implementing cryptographic algorithms that rely on high-precision arithmetic.
|
39
43
|
* **E-commerce and Payments:** Handling precise amounts and avoiding rounding errors in transactions.
|
40
44
|
* **Data Analysis and Statistics:** Performing accurate statistical calculations on datasets with varying scales.
|
45
|
+
* **Low-Level Operations:** Working with different number representations commonly found in computer systems.
|
41
46
|
* **Any Scenario Exceeding JavaScript's Number Limits:** Ensuring the reliability of your calculations when dealing with numbers beyond the safe integer limit or requiring more than 15 significant digits.
|
42
47
|
|
43
48
|
With `@numio/bigmath`, you can confidently perform complex arithmetic operations with the assurance of accuracy, regardless of the size or precision of the numbers involved.
|
44
49
|
|
45
50
|
### Latest update
|
46
51
|
|
47
|
-
|
48
|
-
|
49
|
-
Added `IQR` - Interquartile Range.
|
52
|
+
The performance was improved. Adding, subtracting, dividing, and multiplying are now **2** to **5** times faster than before.\
|
53
|
+
Adding, subtracting, dividing, and multiplying support operations on HEX, octal, binary, and decimal numbers. Mixed-type calculations are allowed, with the final result converted to decimal.
|
50
54
|
|
51
55
|
# Install:
|
52
56
|
|
@@ -125,6 +129,17 @@ const negative = div(["-2", "-3", "2"]); //0.33333333333333333333
|
|
125
129
|
div(["10", "3"], 4); // 3.3333
|
126
130
|
```
|
127
131
|
|
132
|
+
### HEX, decimal, octal, binary
|
133
|
+
```javascript
|
134
|
+
import { add, sub } from "@numio/bigmath";
|
135
|
+
|
136
|
+
add(["0xA", "0x5"]) // HEX + HEX, 10 + 5 = 15
|
137
|
+
add(["0xA", "2"]) // HEX + decimal, 10 + 2 = 12
|
138
|
+
sub(["35", "0b11"]) // decimal - binary, 35 - 3 = 32
|
139
|
+
sub(["0x1A", "0o31", "0b101", ]) // HEX - octal - binary, 26 - 25 - 5 = -4
|
140
|
+
|
141
|
+
```
|
142
|
+
|
128
143
|
### Round
|
129
144
|
|
130
145
|
```javascript
|
@@ -288,6 +303,18 @@ import { IQR } from "@numio/bigmath";
|
|
288
303
|
IQR(["7", "15", "36", "39", "40", "41"]) // 25;
|
289
304
|
```
|
290
305
|
|
306
|
+
### SQRT - square root of a number
|
307
|
+
```javascript
|
308
|
+
import { sqrt } from "@numio/bigmath";
|
309
|
+
|
310
|
+
sqrt("81") // 9;
|
311
|
+
sqrt("3") // 1.7320508075689;
|
312
|
+
|
313
|
+
// you can change precision of a result (second parameter),
|
314
|
+
sqrt("3", "0.01") // 1.732;
|
315
|
+
sqrt("3", "0.000000000000000000001") // 1.732050807568877293527;
|
316
|
+
```
|
317
|
+
|
291
318
|
Does not have a limitation on the number of digits. You can use any length you'd
|
292
319
|
like
|
293
320
|
|
package/index.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import { add } from "./src/
|
2
|
-
import { mul } from "./src/
|
3
|
-
import { sub } from "./src/
|
4
|
-
import { div } from "./src/
|
1
|
+
import { add } from "./src/operations/add/main.d.ts";
|
2
|
+
import { mul } from "./src/operations/mul/main.d.ts";
|
3
|
+
import { sub } from "./src/operations/sub/main.d.ts";
|
4
|
+
import { div } from "./src/operations/div/main.d.ts";
|
5
5
|
import { round } from "./src/round/main.d.ts";
|
6
6
|
import { pipe } from "./src/pipe/main.d.ts";
|
7
7
|
import { quartile } from "./src/quartile/main.d.ts";
|
@@ -10,4 +10,5 @@ import { mean } from "./src/mean/main.d.ts";
|
|
10
10
|
import { isEqual, isLeftGreater, max, min } from "./src/compare/main.d.ts";
|
11
11
|
import { IQR } from "./src/IQR/main.d.ts";
|
12
12
|
import { MAD } from "./src/MAD/main.d.ts";
|
13
|
-
|
13
|
+
import { sqrt } from "./src/sqrt/main.d.ts";
|
14
|
+
export { add, div, IQR, isEqual, isLeftGreater, MAD, max, mean, min, mul, pipe, quartile, round, sort, sub, sqrt, };
|
package/index.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import { add } from "./src/
|
2
|
-
import { mul } from "./src/
|
3
|
-
import { sub } from "./src/
|
4
|
-
import { div } from "./src/
|
1
|
+
import { add } from "./src/operations/add/main.js";
|
2
|
+
import { mul } from "./src/operations/mul/main.js";
|
3
|
+
import { sub } from "./src/operations/sub/main.js";
|
4
|
+
import { div } from "./src/operations/div/main.js";
|
5
5
|
import { round } from "./src/round/main.js";
|
6
6
|
import { pipe } from "./src/pipe/main.js";
|
7
7
|
import { quartile } from "./src/quartile/main.js";
|
@@ -10,4 +10,5 @@ import { mean } from "./src/mean/main.js";
|
|
10
10
|
import { isEqual, isLeftGreater, max, min } from "./src/compare/main.js";
|
11
11
|
import { IQR } from "./src/IQR/main.js";
|
12
12
|
import { MAD } from "./src/MAD/main.js";
|
13
|
-
|
13
|
+
import { sqrt } from "./src/sqrt/main.js";
|
14
|
+
export { add, div, IQR, isEqual, isLeftGreater, MAD, max, mean, min, mul, pipe, quartile, round, sort, sub, sqrt, };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@numio/bigmath",
|
3
3
|
"description": "@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)",
|
4
|
-
"version": "
|
4
|
+
"version": "2.0.0",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
@@ -44,7 +44,14 @@
|
|
44
44
|
"IQR",
|
45
45
|
"Interquartile Range",
|
46
46
|
"isEqual",
|
47
|
-
"equality"
|
47
|
+
"equality",
|
48
|
+
"sqrt",
|
49
|
+
"square root",
|
50
|
+
"HEX",
|
51
|
+
"octal",
|
52
|
+
"binary",
|
53
|
+
"decimal",
|
54
|
+
"binary"
|
48
55
|
],
|
49
56
|
"repository": {
|
50
57
|
"type": "git",
|
package/src/IQR/main.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
import {
|
1
|
+
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { IQRInner } from "./utils.js";
|
3
3
|
//** This function returns Interquartile Range */
|
4
|
-
export var IQR = function (
|
5
|
-
var
|
6
|
-
|
4
|
+
export var IQR = function (array, sigNum) {
|
5
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
6
|
+
var _a = IQRInner(arrayInner, sigNum), bi = _a[0], fpe = _a[1];
|
7
|
+
return bi2s(bi, fpe);
|
7
8
|
};
|
package/src/IQR/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type TIQR = (array: string[], sigNum?: boolean) => string;
|
3
|
-
export type TIQRInner = (array:
|
3
|
+
export type TIQRInner = (array: BI[], sigNum?: boolean) => BI;
|
package/src/IQR/utils.js
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
import { isEqualInner } from "../compare/utils.js";
|
2
2
|
import { MADInner } from "../MAD/utils.js";
|
3
|
-
import {
|
3
|
+
import { PipeInner } from "../pipe/utils.js";
|
4
4
|
import { quartileInner } from "../quartile/utils.js";
|
5
|
-
import { DEFAULT, NIL, ONE } from "../shared/constant.js";
|
6
5
|
export var MIN_LENGTH_FOR_MAD = 30;
|
7
6
|
export var IQRInner = function (array, sigNum) {
|
8
7
|
if (sigNum === void 0) { sigNum = false; }
|
9
8
|
var _a = quartileInner(array), Q1 = _a.Q1, Q3 = _a.Q3;
|
10
|
-
var sub =
|
9
|
+
var sub = new PipeInner().sub([Q3, Q1]).calc();
|
11
10
|
if (sigNum) {
|
12
11
|
var isEqual = isEqualInner({ left: Q3, right: Q1 });
|
13
12
|
var MAD = MADInner(array);
|
14
|
-
var
|
13
|
+
var isNil = isEqualInner({ left: MAD, right: [0n, 0] });
|
14
|
+
var nonNilMAD = isNil ? [1n, 0] : MAD;
|
15
15
|
return isEqual ? nonNilMAD : sub;
|
16
16
|
}
|
17
17
|
return sub;
|
package/src/MAD/main.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
import {
|
1
|
+
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { MADInner } from "./utils.js";
|
3
3
|
//** This function returns Median Absolute Deviation */
|
4
|
-
export var MAD = function (
|
5
|
-
var
|
6
|
-
|
4
|
+
export var MAD = function (array) {
|
5
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
6
|
+
var _a = MADInner(arrayInner), bi = _a[0], fpe = _a[1];
|
7
|
+
return bi2s(bi, fpe);
|
7
8
|
};
|
package/src/MAD/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type TMAD = (array: string[]) => string;
|
3
|
-
export type TMADInner = (array:
|
3
|
+
export type TMADInner = (array: BI[]) => BI;
|
package/src/MAD/utils.js
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import { PipeInner } from "../pipe/utils.js";
|
2
2
|
import { quartileInner } from "../quartile/utils.js";
|
3
|
-
import { DEFAULT } from "../shared/constant.js";
|
4
3
|
import { ASC } from "../sort/constants.js";
|
5
4
|
import { sortInner } from "../sort/utils.js";
|
6
5
|
export var MADInner = function (array) {
|
7
6
|
var median = quartileInner(array).Q2;
|
8
7
|
var madArray = array.map(function (el) {
|
9
|
-
var
|
10
|
-
|
11
|
-
return res;
|
8
|
+
var _a = new PipeInner().sub([el, median]).calc(), bi = _a[0], fpe = _a[1];
|
9
|
+
return bi < 0n ? [bi * -1n, fpe] : [bi, fpe];
|
12
10
|
});
|
13
11
|
var sorted = sortInner(madArray, ASC);
|
14
12
|
return quartileInner(sorted).Q2;
|
package/src/compare/main.js
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
import {
|
2
|
-
import { isEqualInner, isLeftGreaterInner, maxInner, minInner } from "./utils.js";
|
1
|
+
import { bi2s, s2bi } from "../shared/utils.js";
|
2
|
+
import { isEqualInner, isLeftGreaterInner, maxInner, minInner, } from "./utils.js";
|
3
3
|
/** This function returns max value. */
|
4
|
-
export var max = function (
|
5
|
-
var
|
6
|
-
|
4
|
+
export var max = function (array) {
|
5
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
6
|
+
var _a = maxInner(arrayInner), bi = _a[0], fpe = _a[1];
|
7
|
+
return bi2s(bi, fpe);
|
7
8
|
};
|
8
9
|
/** This function returns min value. */
|
9
|
-
export var min = function (
|
10
|
-
var
|
11
|
-
|
10
|
+
export var min = function (array) {
|
11
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
12
|
+
var _a = minInner(arrayInner), bi = _a[0], fpe = _a[1];
|
13
|
+
return bi2s(bi, fpe);
|
12
14
|
};
|
13
15
|
/** This function returns if left value is greater than right value */
|
14
16
|
export var isLeftGreater = function (_a) {
|
15
17
|
var left = _a.left, right = _a.right;
|
16
|
-
return isLeftGreaterInner({ left:
|
18
|
+
return isLeftGreaterInner({ left: s2bi(left), right: s2bi(right) });
|
17
19
|
};
|
18
20
|
/** This function returns if left and right values are equal */
|
19
21
|
export var isEqual = function (_a) {
|
20
22
|
var left = _a.left, right = _a.right;
|
21
|
-
return isEqualInner({ left:
|
23
|
+
return isEqualInner({ left: s2bi(left), right: s2bi(right) });
|
22
24
|
};
|
package/src/compare/types.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type Min = (strs: string[]) => string;
|
3
3
|
export type Max = Min;
|
4
4
|
export type IsLeftGreater = (attr: {
|
@@ -6,11 +6,11 @@ export type IsLeftGreater = (attr: {
|
|
6
6
|
right: string;
|
7
7
|
}) => boolean;
|
8
8
|
export type IsEqual = IsLeftGreater;
|
9
|
-
export type CompareInner = (left:
|
10
|
-
export type MinInner = (array:
|
9
|
+
export type CompareInner = (left: BI, right: BI) => [BI, number];
|
10
|
+
export type MinInner = (array: BI[]) => BI;
|
11
11
|
export type MaxInner = MinInner;
|
12
12
|
export type IsEqualInner = (attr: {
|
13
|
-
left:
|
14
|
-
right:
|
13
|
+
left: BI;
|
14
|
+
right: BI;
|
15
15
|
}) => boolean;
|
16
16
|
export type IsLeftGreaterInner = IsEqualInner;
|
package/src/compare/utils.js
CHANGED
@@ -15,72 +15,55 @@ export var minInner = function (array) {
|
|
15
15
|
return min;
|
16
16
|
};
|
17
17
|
export var compareInner = function (l, r) {
|
18
|
-
var
|
19
|
-
var
|
20
|
-
|
21
|
-
var lIsNeg = l.isNegative, lIntLen = l.intLength;
|
22
|
-
var rIsNeg = r.isNegative, rIntLen = r.intLength;
|
23
|
-
var lenL = l.array.length;
|
24
|
-
var lenR = r.array.length;
|
25
|
-
var bothNeg = lIsNeg && rIsNeg;
|
26
|
-
var bothPos = !lIsNeg && !rIsNeg;
|
27
|
-
var bothIntPos = lIntLen > 0 && rIntLen > 0;
|
28
|
-
var bothIntNeg = lIntLen <= 0 && rIntLen <= 0;
|
29
|
-
var isNilL = l.array.length === 1 && l.array[0] === 48;
|
30
|
-
var isNilR = r.array.length === 1 && r.array[0] === 48;
|
31
|
-
if (lIsNeg && !rIsNeg)
|
32
|
-
return [r, -1];
|
33
|
-
if (!lIsNeg && rIsNeg)
|
34
|
-
return [l, 1];
|
35
|
-
if (bothPos && isNilL && !isNilR)
|
36
|
-
return [r, -1];
|
37
|
-
if (bothPos && !isNilL && isNilR)
|
38
|
-
return [l, 1];
|
39
|
-
if (bothNeg && isNilL && !isNilR)
|
40
|
-
return [l, 1];
|
41
|
-
if (bothNeg && !isNilL && isNilR)
|
42
|
-
return [r, -1];
|
43
|
-
if (bothPos && lIntLen > 0 && rIntLen === 0)
|
18
|
+
var biL = l[0], fpeL = l[1];
|
19
|
+
var biR = r[0], fpeR = r[1];
|
20
|
+
if (biL > 0n && biR < 0n)
|
44
21
|
return [l, 1];
|
45
|
-
if (
|
22
|
+
if (biL < 0n && biR > 0n)
|
46
23
|
return [r, -1];
|
47
|
-
if (
|
48
|
-
|
49
|
-
if (!bothPos && lIntLen === 0 && rIntLen > 0)
|
50
|
-
return [l, 1];
|
51
|
-
if (bothPos && bothIntPos && lIntLen < rIntLen)
|
52
|
-
return [r, -1];
|
53
|
-
if (bothPos && bothIntNeg && lIntLen < rIntLen)
|
54
|
-
return [r, -1];
|
55
|
-
if (bothNeg && bothIntPos && lIntLen < rIntLen)
|
56
|
-
return [l, 1];
|
57
|
-
if (bothNeg && bothIntNeg && lIntLen < rIntLen)
|
58
|
-
return [l, 1];
|
59
|
-
if (bothNeg && bothIntNeg && lIntLen > rIntLen)
|
60
|
-
return [r, -1];
|
61
|
-
if (bothNeg && bothIntPos && lIntLen > rIntLen)
|
62
|
-
return [r, -1];
|
63
|
-
if (bothPos && bothIntPos && lIntLen > rIntLen)
|
64
|
-
return [l, 1];
|
65
|
-
if (bothPos && bothIntNeg && lIntLen > rIntLen)
|
66
|
-
return [l, 1];
|
67
|
-
if (bothNeg)
|
68
|
-
_a = [r, l], left = _a[0], right = _a[1];
|
69
|
-
while (idx < (lenL > lenR ? lenL : lenR)) {
|
70
|
-
var numL = left.array[idx];
|
71
|
-
var numR = right.array[idx];
|
72
|
-
if (!numL && bothPos)
|
73
|
-
return [r, -1];
|
74
|
-
if (!numR && bothPos)
|
24
|
+
if (biL === 0n || biR === 0n) {
|
25
|
+
if (biL > biR)
|
75
26
|
return [l, 1];
|
76
|
-
if (
|
27
|
+
if (biL < biR)
|
28
|
+
return [r, -1];
|
29
|
+
}
|
30
|
+
if (biL < 0n && biR < 0n) {
|
31
|
+
if (biL === biR) {
|
32
|
+
if (fpeL > fpeR)
|
33
|
+
return [l, 1];
|
34
|
+
if (fpeL < fpeR)
|
35
|
+
return [r, -1];
|
36
|
+
if (fpeL === fpeR)
|
37
|
+
return [l, 0];
|
38
|
+
}
|
39
|
+
var fpeBiL = BigInt(fpeL);
|
40
|
+
var fpeBiR = BigInt(fpeR);
|
41
|
+
var max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
|
42
|
+
var powL = biL * Math.pow(10n, (max - fpeBiL));
|
43
|
+
var powR = biR * Math.pow(10n, (max - fpeBiR));
|
44
|
+
if (powL > powR)
|
77
45
|
return [l, 1];
|
78
|
-
if (
|
46
|
+
if (powL < powR)
|
79
47
|
return [r, -1];
|
80
|
-
|
81
|
-
|
48
|
+
}
|
49
|
+
if (biL > 0n && biR > 0n) {
|
50
|
+
if (biL === biR) {
|
51
|
+
if (fpeL > fpeR)
|
52
|
+
return [r, -1];
|
53
|
+
if (fpeL < fpeR)
|
54
|
+
return [l, 1];
|
55
|
+
if (fpeL === fpeR)
|
56
|
+
return [l, 0];
|
82
57
|
}
|
83
|
-
|
58
|
+
var fpeBiL = BigInt(fpeL);
|
59
|
+
var fpeBiR = BigInt(fpeR);
|
60
|
+
var max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
|
61
|
+
var powL = biL * Math.pow(10n, (max - fpeBiL));
|
62
|
+
var powR = biR * Math.pow(10n, (max - fpeBiR));
|
63
|
+
if (powL > powR)
|
64
|
+
return [l, 1];
|
65
|
+
if (powL < powR)
|
66
|
+
return [r, -1];
|
84
67
|
}
|
85
68
|
return [l, 0];
|
86
69
|
};
|
package/src/mean/main.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import {
|
1
|
+
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { meanInner } from "./utils.js";
|
3
3
|
/** This function returns mean of an array. */
|
4
|
-
export var mean = function (
|
5
|
-
var
|
6
|
-
var
|
7
|
-
return
|
4
|
+
export var mean = function (array) {
|
5
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
6
|
+
var _a = meanInner(arrayInner), bi = _a[0], fpe = _a[1];
|
7
|
+
return bi2s(bi, fpe);
|
8
8
|
};
|
package/src/mean/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type Mean = (array: string[]) => string;
|
3
|
-
export type MeanInner = (array:
|
3
|
+
export type MeanInner = (array: BI[]) => BI;
|
package/src/mean/utils.js
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
import {
|
2
|
-
import { divInner } from "../mathOperations/div/utils.js";
|
3
|
-
import { DEFAULT } from "../shared/constant.js";
|
4
|
-
import { s2a } from "../shared/utils.js";
|
1
|
+
import { PipeInner } from "../pipe/utils.js";
|
5
2
|
export var meanInner = function (array) {
|
6
|
-
var left =
|
7
|
-
var right =
|
8
|
-
|
9
|
-
return res;
|
3
|
+
var left = new PipeInner().add(array).calc();
|
4
|
+
var right = [BigInt(array.length), 0];
|
5
|
+
return new PipeInner().div([left, right]).calc();
|
10
6
|
};
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
|
+
import { calcInner } from "../../shared/utils.js";
|
3
|
+
/** This function adds numbers (as string). */
|
4
|
+
export var add = function (array) {
|
5
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
6
|
+
var _a = calcInner(arrayInner, function (a, b) { return a + b; }), bigInt = _a[0], fpe = _a[1];
|
7
|
+
return bi2s(bigInt, fpe);
|
8
|
+
};
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
|
+
import { divInner } from "./utils.js";
|
3
|
+
/** This function should divide numbers (as string). */
|
4
|
+
export var div = function (array, limit) {
|
5
|
+
if (limit === void 0) { limit = 20; }
|
6
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
7
|
+
var _a = divInner(arrayInner, limit !== null && limit !== void 0 ? limit : 20), bigInt = _a[0], fpe = _a[1];
|
8
|
+
return bi2s(bigInt, fpe);
|
9
|
+
};
|
@@ -0,0 +1,39 @@
|
|
1
|
+
export var divInner = function (array, limit, def) {
|
2
|
+
var bigInt = def ? def[0] : array[0][0];
|
3
|
+
var fpe = def ? def[1] : array[0][1];
|
4
|
+
var isNeg = bigInt < 0n;
|
5
|
+
for (var i = def ? 0 : 1; i < array.length; i++) {
|
6
|
+
var _a = array[i], bigCurrent = _a[0], dpLen = _a[1];
|
7
|
+
var r = 0n;
|
8
|
+
if (bigInt === 0n && fpe === 0)
|
9
|
+
return [0n, 0];
|
10
|
+
if (fpe === dpLen) {
|
11
|
+
fpe = 0;
|
12
|
+
dpLen = 0;
|
13
|
+
}
|
14
|
+
if (dpLen > 0 && fpe < dpLen) {
|
15
|
+
bigInt *= Math.pow(10n, BigInt(dpLen - fpe));
|
16
|
+
fpe = 0;
|
17
|
+
}
|
18
|
+
if (dpLen > 0 && fpe > dpLen)
|
19
|
+
fpe = fpe - dpLen;
|
20
|
+
while ((bigInt < 0 ? bigInt * -1n : bigInt) < bigCurrent) {
|
21
|
+
if (limit <= fpe)
|
22
|
+
return [bigInt / bigCurrent, fpe];
|
23
|
+
fpe += 1;
|
24
|
+
bigInt *= 10n;
|
25
|
+
}
|
26
|
+
var q = bigInt / bigCurrent;
|
27
|
+
r = bigInt - q * bigCurrent;
|
28
|
+
bigInt = q;
|
29
|
+
while ((isNeg ? r < 0n : r > 0n) && fpe < limit) {
|
30
|
+
var nextBigInt = r * 10n;
|
31
|
+
var nextQ = nextBigInt / bigCurrent;
|
32
|
+
var nextRemained = nextBigInt - nextQ * bigCurrent;
|
33
|
+
bigInt = bigInt * 10n + nextQ;
|
34
|
+
r = nextRemained;
|
35
|
+
fpe += 1;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
return [bigInt, fpe];
|
39
|
+
};
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { bi2s, calcInner, s2bi } from "../../shared/utils.js";
|
2
|
+
/** This function multiplies numbers (as string). */
|
3
|
+
export var mul = function (array) {
|
4
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
5
|
+
var _a = calcInner(arrayInner, function (a, b) { return a * b; }), bigInt = _a[0], fpe = _a[1];
|
6
|
+
return bi2s(bigInt, fpe);
|
7
|
+
};
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
|
+
import { calcInner } from "../../shared/utils.js";
|
3
|
+
/** This function subtracts numbers (as string). */
|
4
|
+
export function sub(array) {
|
5
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
6
|
+
var _a = calcInner(arrayInner, function (a, b) { return a - b; }), bigInt = _a[0], fpe = _a[1];
|
7
|
+
return bi2s(bigInt, fpe);
|
8
|
+
}
|
package/src/pipe/main.d.ts
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
-
import type {
|
2
|
-
import type { InputData } from "../types.d.ts";
|
1
|
+
import type { BI2S } from "../shared/types.d.ts";
|
3
2
|
export declare class Pipe {
|
4
|
-
|
5
|
-
constructor(
|
6
|
-
add(
|
7
|
-
sub(
|
8
|
-
div(
|
9
|
-
mul(
|
10
|
-
calc(): ReturnType<
|
3
|
+
#private;
|
4
|
+
constructor();
|
5
|
+
add(array: string[]): Pipe;
|
6
|
+
sub(array: string[]): Pipe;
|
7
|
+
div(array: string[], limit?: number): Pipe;
|
8
|
+
mul(array: string[]): Pipe;
|
9
|
+
calc(): ReturnType<BI2S>;
|
11
10
|
}
|
12
11
|
/** Using this function you can chain operations (add, sub, div, mul). */
|
13
12
|
export declare const pipe: Pipe;
|