@numio/bigmath 2.0.0 → 2.1.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 +58 -3
- package/package.json +1 -1
- package/src/IQR/main.js +3 -3
- package/src/IQR/utils.js +8 -9
- package/src/MAD/main.js +3 -3
- package/src/MAD/utils.js +5 -5
- package/src/abs/main.d.ts +2 -0
- package/src/abs/main.js +6 -0
- package/src/abs/types.d.ts +3 -0
- package/src/abs/utils.d.ts +2 -0
- package/src/abs/utils.js +3 -0
- package/src/cbrt/main.d.ts +3 -0
- package/src/cbrt/main.js +9 -0
- package/src/cbrt/types.d.ts +3 -0
- package/src/cbrt/utils.d.ts +2 -0
- package/src/cbrt/utils.js +22 -0
- package/src/compare/main.d.ts +3 -1
- package/src/compare/main.js +13 -11
- package/src/compare/types.d.ts +11 -3
- package/src/compare/utils.d.ts +2 -1
- package/src/compare/utils.js +24 -23
- package/src/mean/main.js +3 -3
- package/src/mean/utils.js +3 -3
- package/src/operations/add/main.js +3 -3
- package/src/operations/div/main.js +3 -4
- package/src/operations/div/utils.js +12 -12
- package/src/operations/mul/main.js +3 -3
- package/src/operations/sub/main.js +2 -2
- package/src/pipe/main.d.ts +4 -3
- package/src/pipe/main.js +49 -34
- package/src/pipe/utils.js +17 -21
- package/src/quartile/main.js +3 -3
- package/src/quartile/utils.js +8 -8
- package/src/round/constants.js +7 -7
- package/src/round/main.js +16 -17
- package/src/round/utils.js +24 -25
- package/src/shared/constant.js +1 -1
- package/src/shared/utils.js +25 -25
- package/src/sort/constants.js +3 -3
- package/src/sort/main.js +3 -7
- package/src/sort/utils.js +10 -12
- package/src/sqrt/main.js +4 -5
- package/src/sqrt/types.d.ts +2 -2
- package/src/sqrt/utils.js +7 -11
- package/src/toBase/main.d.ts +2 -0
- package/src/toBase/main.js +3 -0
- package/src/toBase/types.d.ts +4 -0
package/README.md
CHANGED
@@ -21,6 +21,8 @@
|
|
21
21
|
* **Round Based on Significant Figures:** Control rounding based on the number of significant figures, crucial for scientific and engineering applications.
|
22
22
|
* **Calculate Roots:**
|
23
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.
|
24
|
+
* **NEW! Calculate Cube Root (`cbrt`):** Determine the cube root of a number with arbitrary precision, allowing you to specify the desired accuracy.
|
25
|
+
|
24
26
|
* **Chain Operations with Pipe:** Simplify complex calculations by chaining arithmetic operations in a readable and intuitive manner.
|
25
27
|
* **Analyze Data Distribution:**
|
26
28
|
* **Calculate Quartiles (Q1, Q2, Q3):** Understand the spread and central tendency of your numerical data, helping identify outliers and the shape of the distribution.
|
@@ -29,9 +31,12 @@
|
|
29
31
|
* **Compare Numbers:**
|
30
32
|
* **Check for Equality (`isEqual`):** Accurately determine if two arbitrary-precision numbers are equal.
|
31
33
|
* **Check if Left is Greater (`isLeftGreater`):** Precisely compare two arbitrary-precision numbers to see if the left operand is greater than the right.
|
34
|
+
* **NEW! Check if Left is Greater or Equal (`isLeftGreaterOrEqual`):** Precisely compare two arbitrary-precision numbers to determine if the left operand is greater than or equal to the right.
|
32
35
|
* **Sort Numbers Accurately:** Sort arrays of numbers, including negative and decimal values, in ascending or descending order, correctly handling string representations of numbers that JavaScript's native sort might misinterpret.
|
33
36
|
* **Calculate Central Tendency:** Easily compute the mean (average) of a set of numbers.
|
34
37
|
* **Identify Extremes:** Find the maximum and minimum values within an array of numbers.
|
38
|
+
* **NEW! Calculate Absolute Value (`abs`):** Determine the non-negative value of a number, regardless of its sign.
|
39
|
+
* **NEW! Convert Number to Another Base (`toBase`):** Seamlessly convert numbers between decimal, hexadecimal (HEX), binary, and octal representations.
|
35
40
|
|
36
41
|
## When is @numio/bigmath essential?
|
37
42
|
|
@@ -208,18 +213,24 @@ round("1.000119", { decimals: 2, sigFig: true }); // 1
|
|
208
213
|
### Pipe
|
209
214
|
|
210
215
|
```javascript
|
211
|
-
import {
|
216
|
+
import { Pipe } from "@numio/bigmath";
|
212
217
|
|
213
218
|
const addNums = ["1", "2", "3"];
|
214
219
|
const subNums = ["0.2", "0.3"];
|
215
220
|
const divNums = ["4"];
|
216
221
|
const mulNums = ["2", "5", "0.2"];
|
217
222
|
|
218
|
-
|
223
|
+
new Pipe().add(addNums) // 6
|
219
224
|
.div(divNums) // 6 / 4 = 1.5
|
220
225
|
.sub(subNums) // 1.5 - 0.2 - 0.3 = 1
|
221
226
|
.mul(mulNums) // 1 * 2 * 5 * 0.2 = 2
|
222
227
|
.calc() // convert end result to readable string
|
228
|
+
|
229
|
+
new Pipe().sub(["1", "5"]) // 1 - 5 = -4
|
230
|
+
.abs() // 4 - returns absolute value
|
231
|
+
|
232
|
+
new Pipe().add(["10", "5"]) // 11 + 5 = 15
|
233
|
+
.resultToBase(16) // f - returns result converted to base 16
|
223
234
|
```
|
224
235
|
|
225
236
|
### Quartile
|
@@ -289,6 +300,16 @@ isLeftGreater({left: "0.1", right: "0.1"}) // false;
|
|
289
300
|
isLeftGreater({left: "0.1", right: "-0.1"}) // true;
|
290
301
|
```
|
291
302
|
|
303
|
+
### IsLeftGreaterOrEqual
|
304
|
+
```javascript
|
305
|
+
import { isLeftGreaterOrEqual } from "@numio/bigmath";
|
306
|
+
|
307
|
+
isLeftGreaterOrEqual({left: "0.1", right: "2"}) // false;
|
308
|
+
isLeftGreaterOrEqual({left: "2", right: "0.1"}) // true;
|
309
|
+
isLeftGreaterOrEqual({left: "0.1", right: "0.1"}) // true;
|
310
|
+
isLeftGreaterOrEqual({left: "0.1", right: "-0.1"}) // true;
|
311
|
+
```
|
312
|
+
|
292
313
|
### MAD - Median Absolute Deviation
|
293
314
|
```javascript
|
294
315
|
import { MAD } from "@numio/bigmath";
|
@@ -315,12 +336,46 @@ sqrt("3", "0.01") // 1.732;
|
|
315
336
|
sqrt("3", "0.000000000000000000001") // 1.732050807568877293527;
|
316
337
|
```
|
317
338
|
|
339
|
+
### CBRT - cube root of a number
|
340
|
+
```javascript
|
341
|
+
import { cbrt } from "@numio/bigmath";
|
342
|
+
|
343
|
+
cbrt("37") // 3;
|
344
|
+
cbrt("1000") // 10;
|
345
|
+
cbrt("15"), // 2.4662120743305
|
346
|
+
|
347
|
+
// you can change precision of a result (second parameter),
|
348
|
+
cbrt("15", "0.001") // 2.466
|
349
|
+
```
|
350
|
+
|
351
|
+
### ABS - absolute value
|
352
|
+
```javascript
|
353
|
+
import { abs } from "@numio/bigmath";
|
354
|
+
|
355
|
+
abs("-1") // 1;
|
356
|
+
abs("1") // 1;
|
357
|
+
```
|
358
|
+
|
359
|
+
### toBase - convert number to another base
|
360
|
+
```javascript
|
361
|
+
import { toBase } from "@numio/bigmath";
|
362
|
+
|
363
|
+
toBase({ value: "11", toBase: 16 }) // b
|
364
|
+
// 0x - HEX
|
365
|
+
toBase({ value: "0xb", toBase: 10 }) // 11
|
366
|
+
// 0b - binary
|
367
|
+
toBase({ value: "0b101", toBase: 10 }) // 5
|
368
|
+
toBase({ value: "0b1101", toBase: 16 }) // d
|
369
|
+
// 0o - octal
|
370
|
+
toBase({ value: "0o11", toBase: 10 }) // 9
|
371
|
+
```
|
372
|
+
|
318
373
|
Does not have a limitation on the number of digits. You can use any length you'd
|
319
374
|
like
|
320
375
|
|
321
376
|
```javascript
|
322
377
|
// NO precision loss using numeric literals with more than 15 significant digits.
|
323
|
-
const int =
|
378
|
+
const int = add(
|
324
379
|
"999999999999999999999999999999999999999999999999999999999999999",
|
325
380
|
"2",
|
326
381
|
); // "1000000000000000000000000000000000000000000000000000000000000001"
|
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": "2.
|
4
|
+
"version": "2.1.0",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
package/src/IQR/main.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { IQRInner } from "./utils.js";
|
3
3
|
//** This function returns Interquartile Range */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const IQR = (array, sigNum) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = IQRInner(arrayInner, sigNum);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
package/src/IQR/utils.js
CHANGED
@@ -2,16 +2,15 @@ import { isEqualInner } from "../compare/utils.js";
|
|
2
2
|
import { MADInner } from "../MAD/utils.js";
|
3
3
|
import { PipeInner } from "../pipe/utils.js";
|
4
4
|
import { quartileInner } from "../quartile/utils.js";
|
5
|
-
export
|
6
|
-
export
|
7
|
-
|
8
|
-
|
9
|
-
var sub = new PipeInner().sub([Q3, Q1]).calc();
|
5
|
+
export const MIN_LENGTH_FOR_MAD = 30;
|
6
|
+
export const IQRInner = (array, sigNum = false) => {
|
7
|
+
const { Q1, Q3 } = quartileInner(array);
|
8
|
+
const sub = new PipeInner().sub([Q3, Q1]).calc();
|
10
9
|
if (sigNum) {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
const isEqual = isEqualInner({ left: Q3, right: Q1 });
|
11
|
+
const MAD = MADInner(array);
|
12
|
+
const isNil = isEqualInner({ left: MAD, right: [0n, 0] });
|
13
|
+
const nonNilMAD = isNil ? [1n, 0] : MAD;
|
15
14
|
return isEqual ? nonNilMAD : sub;
|
16
15
|
}
|
17
16
|
return sub;
|
package/src/MAD/main.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
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
|
5
|
-
|
6
|
-
|
4
|
+
export const MAD = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = MADInner(arrayInner);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
package/src/MAD/utils.js
CHANGED
@@ -2,12 +2,12 @@ import { PipeInner } from "../pipe/utils.js";
|
|
2
2
|
import { quartileInner } from "../quartile/utils.js";
|
3
3
|
import { ASC } from "../sort/constants.js";
|
4
4
|
import { sortInner } from "../sort/utils.js";
|
5
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
export const MADInner = (array) => {
|
6
|
+
const median = quartileInner(array).Q2;
|
7
|
+
const madArray = array.map((el) => {
|
8
|
+
const [bi, fpe] = new PipeInner().sub([el, median]).calc();
|
9
9
|
return bi < 0n ? [bi * -1n, fpe] : [bi, fpe];
|
10
10
|
});
|
11
|
-
|
11
|
+
const sorted = sortInner(madArray, ASC);
|
12
12
|
return quartileInner(sorted).Q2;
|
13
13
|
};
|
package/src/abs/main.js
ADDED
package/src/abs/utils.js
ADDED
package/src/cbrt/main.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import { round } from "../round/main.js";
|
2
|
+
import { bi2s, s2bi } from "../shared/utils.js";
|
3
|
+
import { cbrtInner } from "./utils.js";
|
4
|
+
/** Find cube root of a number */
|
5
|
+
export const cbrt = (value, precision) => {
|
6
|
+
const valueInner = s2bi(value);
|
7
|
+
const [[bi, fpe], decimals] = cbrtInner(valueInner, precision ? s2bi(precision) : undefined);
|
8
|
+
return round(bi2s(bi, fpe), { decimals });
|
9
|
+
};
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { isLeftGreaterInner } from "../compare/utils.js";
|
2
|
+
import { divInner } from "../operations/div/utils.js";
|
3
|
+
import { PipeInner } from "../pipe/utils.js";
|
4
|
+
import { calcInner } from "../shared/utils.js";
|
5
|
+
export const cbrtInner = (value, prec = [1n, 13], max = 100) => {
|
6
|
+
let guess = value;
|
7
|
+
for (let i = 0; i < max; i++) {
|
8
|
+
const mul = calcInner([guess, guess], (a, b) => a * b);
|
9
|
+
const nextGuess = new PipeInner().add([
|
10
|
+
calcInner([guess, [2n, 0]], (a, b) => a * b),
|
11
|
+
divInner([value, mul], 20),
|
12
|
+
]).div([[3n, 0]]).calc();
|
13
|
+
let [bi, fpe] = new PipeInner().sub([nextGuess, guess]).calc();
|
14
|
+
if (bi < 0n)
|
15
|
+
bi *= -1n;
|
16
|
+
if (isLeftGreaterInner({ left: prec, right: [bi, fpe] })) {
|
17
|
+
return [nextGuess, prec[1]];
|
18
|
+
}
|
19
|
+
guess = nextGuess;
|
20
|
+
}
|
21
|
+
return [guess, prec[1]];
|
22
|
+
};
|
package/src/compare/main.d.ts
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
import type { IsEqual, IsLeftGreater, Max, Min } from "./types.d.ts";
|
1
|
+
import type { IsEqual, IsLeftGreater, IsLeftGreaterOrEqual, Max, Min } from "./types.d.ts";
|
2
2
|
/** This function returns max value. */
|
3
3
|
export declare const max: Max;
|
4
4
|
/** This function returns min value. */
|
5
5
|
export declare const min: Min;
|
6
6
|
/** This function returns if left value is greater than right value */
|
7
7
|
export declare const isLeftGreater: IsLeftGreater;
|
8
|
+
/** This function returns if left value is greater than right value */
|
9
|
+
export declare const isLeftGreaterOrEqual: IsLeftGreaterOrEqual;
|
8
10
|
/** This function returns if left and right values are equal */
|
9
11
|
export declare const isEqual: IsEqual;
|
package/src/compare/main.js
CHANGED
@@ -1,24 +1,26 @@
|
|
1
1
|
import { bi2s, s2bi } from "../shared/utils.js";
|
2
|
-
import { isEqualInner, isLeftGreaterInner, maxInner, minInner, } from "./utils.js";
|
2
|
+
import { isEqualInner, isLeftGreaterInner, isLeftGreaterOrEqualInner, maxInner, minInner, } from "./utils.js";
|
3
3
|
/** This function returns max value. */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const max = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = maxInner(arrayInner);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
9
9
|
/** This function returns min value. */
|
10
|
-
export
|
11
|
-
|
12
|
-
|
10
|
+
export const min = (array) => {
|
11
|
+
const arrayInner = array.map((str) => s2bi(str));
|
12
|
+
const [bi, fpe] = minInner(arrayInner);
|
13
13
|
return bi2s(bi, fpe);
|
14
14
|
};
|
15
15
|
/** This function returns if left value is greater than right value */
|
16
|
-
export
|
17
|
-
var left = _a.left, right = _a.right;
|
16
|
+
export const isLeftGreater = ({ left, right }) => {
|
18
17
|
return isLeftGreaterInner({ left: s2bi(left), right: s2bi(right) });
|
19
18
|
};
|
19
|
+
/** This function returns if left value is greater than right value */
|
20
|
+
export const isLeftGreaterOrEqual = ({ left, right }) => {
|
21
|
+
return isLeftGreaterOrEqualInner({ left: s2bi(left), right: s2bi(right) });
|
22
|
+
};
|
20
23
|
/** This function returns if left and right values are equal */
|
21
|
-
export
|
22
|
-
var left = _a.left, right = _a.right;
|
24
|
+
export const isEqual = ({ left, right }) => {
|
23
25
|
return isEqualInner({ left: s2bi(left), right: s2bi(right) });
|
24
26
|
};
|
package/src/compare/types.d.ts
CHANGED
@@ -1,16 +1,24 @@
|
|
1
1
|
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type Min = (strs: string[]) => string;
|
3
3
|
export type Max = Min;
|
4
|
-
export type IsLeftGreater = (
|
4
|
+
export type IsLeftGreater = (arg: {
|
5
|
+
left: string;
|
6
|
+
right: string;
|
7
|
+
}) => boolean;
|
8
|
+
export type IsEqual = (arg: {
|
9
|
+
left: string;
|
10
|
+
right: string;
|
11
|
+
}) => boolean;
|
12
|
+
export type IsLeftGreaterOrEqual = (arg: {
|
5
13
|
left: string;
|
6
14
|
right: string;
|
7
15
|
}) => boolean;
|
8
|
-
export type IsEqual = IsLeftGreater;
|
9
16
|
export type CompareInner = (left: BI, right: BI) => [BI, number];
|
10
17
|
export type MinInner = (array: BI[]) => BI;
|
11
18
|
export type MaxInner = MinInner;
|
12
|
-
export type IsEqualInner = (
|
19
|
+
export type IsEqualInner = (arg: {
|
13
20
|
left: BI;
|
14
21
|
right: BI;
|
15
22
|
}) => boolean;
|
16
23
|
export type IsLeftGreaterInner = IsEqualInner;
|
24
|
+
export type IsLeftGreaterOrEqualInner = IsEqualInner;
|
package/src/compare/utils.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
import type { CompareInner, IsEqualInner, IsLeftGreaterInner, MaxInner, MinInner } from "./types.d.ts";
|
1
|
+
import type { CompareInner, IsEqualInner, IsLeftGreaterInner, IsLeftGreaterOrEqualInner, MaxInner, MinInner } from "./types.d.ts";
|
2
2
|
export declare const maxInner: MaxInner;
|
3
3
|
export declare const minInner: MinInner;
|
4
4
|
export declare const compareInner: CompareInner;
|
5
5
|
export declare const isEqualInner: IsEqualInner;
|
6
6
|
export declare const isLeftGreaterInner: IsLeftGreaterInner;
|
7
|
+
export declare const isLeftGreaterOrEqualInner: IsLeftGreaterOrEqualInner;
|
package/src/compare/utils.js
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
export
|
2
|
-
|
3
|
-
for (
|
1
|
+
export const maxInner = (array) => {
|
2
|
+
let max = array[0];
|
3
|
+
for (let i = 1; i < array.length; i++) {
|
4
4
|
if (compareInner(array[i], max)[1] === 1)
|
5
5
|
max = array[i];
|
6
6
|
}
|
7
7
|
return max;
|
8
8
|
};
|
9
|
-
export
|
10
|
-
|
11
|
-
for (
|
9
|
+
export const minInner = (array) => {
|
10
|
+
let min = array[0];
|
11
|
+
for (let i = 1; i < array.length; i++) {
|
12
12
|
if (compareInner(array[i], min)[1] === -1)
|
13
13
|
min = array[i];
|
14
14
|
}
|
15
15
|
return min;
|
16
16
|
};
|
17
|
-
export
|
18
|
-
|
19
|
-
|
17
|
+
export const compareInner = (l, r) => {
|
18
|
+
const [biL, fpeL] = l;
|
19
|
+
const [biR, fpeR] = r;
|
20
20
|
if (biL > 0n && biR < 0n)
|
21
21
|
return [l, 1];
|
22
22
|
if (biL < 0n && biR > 0n)
|
@@ -36,11 +36,11 @@ export var compareInner = function (l, r) {
|
|
36
36
|
if (fpeL === fpeR)
|
37
37
|
return [l, 0];
|
38
38
|
}
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
const fpeBiL = BigInt(fpeL);
|
40
|
+
const fpeBiR = BigInt(fpeR);
|
41
|
+
const max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
|
42
|
+
const powL = biL * 10n ** (max - fpeBiL);
|
43
|
+
const powR = biR * 10n ** (max - fpeBiR);
|
44
44
|
if (powL > powR)
|
45
45
|
return [l, 1];
|
46
46
|
if (powL < powR)
|
@@ -55,11 +55,11 @@ export var compareInner = function (l, r) {
|
|
55
55
|
if (fpeL === fpeR)
|
56
56
|
return [l, 0];
|
57
57
|
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
58
|
+
const fpeBiL = BigInt(fpeL);
|
59
|
+
const fpeBiR = BigInt(fpeR);
|
60
|
+
const max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
|
61
|
+
const powL = biL * 10n ** (max - fpeBiL);
|
62
|
+
const powR = biR * 10n ** (max - fpeBiR);
|
63
63
|
if (powL > powR)
|
64
64
|
return [l, 1];
|
65
65
|
if (powL < powR)
|
@@ -67,11 +67,12 @@ export var compareInner = function (l, r) {
|
|
67
67
|
}
|
68
68
|
return [l, 0];
|
69
69
|
};
|
70
|
-
export
|
71
|
-
var left = _a.left, right = _a.right;
|
70
|
+
export const isEqualInner = ({ left, right }) => {
|
72
71
|
return compareInner(left, right)[1] === 0;
|
73
72
|
};
|
74
|
-
export
|
75
|
-
var left = _a.left, right = _a.right;
|
73
|
+
export const isLeftGreaterInner = ({ left, right }) => {
|
76
74
|
return compareInner(left, right)[1] > 0;
|
77
75
|
};
|
76
|
+
export const isLeftGreaterOrEqualInner = ({ left, right }) => {
|
77
|
+
return compareInner(left, right)[1] >= 0;
|
78
|
+
};
|
package/src/mean/main.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
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
|
5
|
-
|
6
|
-
|
4
|
+
export const mean = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = meanInner(arrayInner);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
package/src/mean/utils.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { PipeInner } from "../pipe/utils.js";
|
2
|
-
export
|
3
|
-
|
4
|
-
|
2
|
+
export const meanInner = (array) => {
|
3
|
+
const left = new PipeInner().add(array).calc();
|
4
|
+
const right = [BigInt(array.length), 0];
|
5
5
|
return new PipeInner().div([left, right]).calc();
|
6
6
|
};
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
2
|
import { calcInner } from "../../shared/utils.js";
|
3
3
|
/** This function adds numbers (as string). */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const add = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a + b);
|
7
7
|
return bi2s(bigInt, fpe);
|
8
8
|
};
|
@@ -1,9 +1,8 @@
|
|
1
1
|
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
2
|
import { divInner } from "./utils.js";
|
3
3
|
/** This function should divide numbers (as string). */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
7
|
-
var _a = divInner(arrayInner, limit !== null && limit !== void 0 ? limit : 20), bigInt = _a[0], fpe = _a[1];
|
4
|
+
export const div = (array, limit = 20) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bigInt, fpe] = divInner(arrayInner, limit !== null && limit !== void 0 ? limit : 20);
|
8
7
|
return bi2s(bigInt, fpe);
|
9
8
|
};
|
@@ -1,10 +1,10 @@
|
|
1
|
-
export
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
for (
|
6
|
-
|
7
|
-
|
1
|
+
export const divInner = (array, limit, def) => {
|
2
|
+
let bigInt = def ? def[0] : array[0][0];
|
3
|
+
let fpe = def ? def[1] : array[0][1];
|
4
|
+
const isNeg = bigInt < 0n;
|
5
|
+
for (let i = def ? 0 : 1; i < array.length; i++) {
|
6
|
+
let [bigCurrent, dpLen] = array[i];
|
7
|
+
let r = 0n;
|
8
8
|
if (bigInt === 0n && fpe === 0)
|
9
9
|
return [0n, 0];
|
10
10
|
if (fpe === dpLen) {
|
@@ -12,7 +12,7 @@ export var divInner = function (array, limit, def) {
|
|
12
12
|
dpLen = 0;
|
13
13
|
}
|
14
14
|
if (dpLen > 0 && fpe < dpLen) {
|
15
|
-
bigInt *=
|
15
|
+
bigInt *= 10n ** BigInt(dpLen - fpe);
|
16
16
|
fpe = 0;
|
17
17
|
}
|
18
18
|
if (dpLen > 0 && fpe > dpLen)
|
@@ -23,13 +23,13 @@ export var divInner = function (array, limit, def) {
|
|
23
23
|
fpe += 1;
|
24
24
|
bigInt *= 10n;
|
25
25
|
}
|
26
|
-
|
26
|
+
const q = bigInt / bigCurrent;
|
27
27
|
r = bigInt - q * bigCurrent;
|
28
28
|
bigInt = q;
|
29
29
|
while ((isNeg ? r < 0n : r > 0n) && fpe < limit) {
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
const nextBigInt = r * 10n;
|
31
|
+
const nextQ = nextBigInt / bigCurrent;
|
32
|
+
const nextRemained = nextBigInt - nextQ * bigCurrent;
|
33
33
|
bigInt = bigInt * 10n + nextQ;
|
34
34
|
r = nextRemained;
|
35
35
|
fpe += 1;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { bi2s, calcInner, s2bi } from "../../shared/utils.js";
|
2
2
|
/** This function multiplies numbers (as string). */
|
3
|
-
export
|
4
|
-
|
5
|
-
|
3
|
+
export const mul = (array) => {
|
4
|
+
const arrayInner = array.map((str) => s2bi(str));
|
5
|
+
const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a * b);
|
6
6
|
return bi2s(bigInt, fpe);
|
7
7
|
};
|
@@ -2,7 +2,7 @@ import { bi2s, s2bi } from "../../shared/utils.js";
|
|
2
2
|
import { calcInner } from "../../shared/utils.js";
|
3
3
|
/** This function subtracts numbers (as string). */
|
4
4
|
export function sub(array) {
|
5
|
-
|
6
|
-
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a - b);
|
7
7
|
return bi2s(bigInt, fpe);
|
8
8
|
}
|
package/src/pipe/main.d.ts
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
import type { BI2S } from "../shared/types.d.ts";
|
2
|
+
/** Using this function you can chain operations (add, sub, div, mul). */
|
2
3
|
export declare class Pipe {
|
3
4
|
#private;
|
4
|
-
constructor();
|
5
|
+
constructor(value?: string);
|
5
6
|
add(array: string[]): Pipe;
|
6
7
|
sub(array: string[]): Pipe;
|
7
8
|
div(array: string[], limit?: number): Pipe;
|
8
9
|
mul(array: string[]): Pipe;
|
10
|
+
abs(): Pipe;
|
9
11
|
calc(): ReturnType<BI2S>;
|
12
|
+
resultToBase(radix: number): string;
|
10
13
|
}
|
11
|
-
/** Using this function you can chain operations (add, sub, div, mul). */
|
12
|
-
export declare const pipe: Pipe;
|
package/src/pipe/main.js
CHANGED
@@ -1,52 +1,67 @@
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
5
|
-
};
|
6
1
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
7
2
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
8
3
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
9
4
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
10
5
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
11
6
|
};
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11
|
+
};
|
12
12
|
var _Pipe_result;
|
13
13
|
import { bi2s, calcInner, s2bi } from "../shared/utils.js";
|
14
14
|
import { divInner } from "../operations/div/utils.js";
|
15
|
-
|
16
|
-
|
15
|
+
import { absInner } from "../abs/utils.js";
|
16
|
+
/** Using this function you can chain operations (add, sub, div, mul). */
|
17
|
+
export class Pipe {
|
18
|
+
constructor(value) {
|
17
19
|
_Pipe_result.set(this, void 0);
|
20
|
+
value && (__classPrivateFieldSet(this, _Pipe_result, s2bi(value), "f"));
|
18
21
|
}
|
19
|
-
|
20
|
-
|
21
|
-
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner,
|
22
|
+
add(array) {
|
23
|
+
const arrayInner = array.map((str) => s2bi(str));
|
24
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a + b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
22
25
|
return this;
|
23
|
-
}
|
24
|
-
|
25
|
-
|
26
|
-
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner,
|
26
|
+
}
|
27
|
+
sub(array) {
|
28
|
+
const arrayInner = array.map((str) => s2bi(str));
|
29
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a - b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
27
30
|
return this;
|
28
|
-
}
|
29
|
-
|
30
|
-
|
31
|
-
var arrayInner = array.map(function (str) { return s2bi(str); });
|
31
|
+
}
|
32
|
+
div(array, limit = 20) {
|
33
|
+
const arrayInner = array.map((str) => s2bi(str));
|
32
34
|
__classPrivateFieldSet(this, _Pipe_result, divInner(arrayInner, limit, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
33
35
|
return this;
|
34
|
-
}
|
35
|
-
|
36
|
-
|
37
|
-
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner,
|
36
|
+
}
|
37
|
+
mul(array) {
|
38
|
+
const arrayInner = array.map((str) => s2bi(str));
|
39
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a * b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
40
|
+
return this;
|
41
|
+
}
|
42
|
+
abs() {
|
43
|
+
if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
|
44
|
+
throw new Error("Cannot calculate the absolute value of an undefined.");
|
45
|
+
}
|
46
|
+
__classPrivateFieldSet(this, _Pipe_result, absInner(__classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
38
47
|
return this;
|
39
|
-
}
|
40
|
-
|
48
|
+
}
|
49
|
+
calc() {
|
50
|
+
if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
|
51
|
+
throw new Error("Cannot calculate based on an undefined input.");
|
52
|
+
}
|
53
|
+
const [bi, fpe] = __classPrivateFieldGet(this, _Pipe_result, "f");
|
54
|
+
return bi2s(bi, fpe);
|
55
|
+
}
|
56
|
+
resultToBase(radix) {
|
41
57
|
var _a;
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
58
|
+
if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
|
59
|
+
throw new Error(`Cannot convert an undefined result to base ${radix}.`);
|
60
|
+
}
|
61
|
+
if (__classPrivateFieldGet(this, _Pipe_result, "f")[1] > 0) {
|
62
|
+
throw new Error(`Cannot convert non integer result to base ${radix}.`);
|
63
|
+
}
|
64
|
+
return (_a = __classPrivateFieldGet(this, _Pipe_result, "f")) === null || _a === void 0 ? void 0 : _a[0].toString(radix);
|
65
|
+
}
|
66
|
+
}
|
50
67
|
_Pipe_result = new WeakMap();
|
51
|
-
/** Using this function you can chain operations (add, sub, div, mul). */
|
52
|
-
export var pipe = new Pipe();
|
package/src/pipe/utils.js
CHANGED
@@ -1,30 +1,26 @@
|
|
1
1
|
import { calcInner } from "../shared/utils.js";
|
2
2
|
import { divInner } from "../operations/div/utils.js";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
this.result = calcInner(array, function (a, b) { return a + b; }, this.result);
|
3
|
+
export class PipeInner {
|
4
|
+
constructor() { }
|
5
|
+
add(array) {
|
6
|
+
this.result = calcInner(array, (a, b) => a + b, this.result);
|
8
7
|
return this;
|
9
|
-
}
|
10
|
-
|
11
|
-
this.result = calcInner(array,
|
8
|
+
}
|
9
|
+
sub(array) {
|
10
|
+
this.result = calcInner(array, (a, b) => a - b, this.result);
|
12
11
|
return this;
|
13
|
-
}
|
14
|
-
|
15
|
-
if (limit === void 0) { limit = 20; }
|
12
|
+
}
|
13
|
+
div(array, limit = 20) {
|
16
14
|
this.result = divInner(array, limit, this.result);
|
17
15
|
return this;
|
18
|
-
}
|
19
|
-
|
20
|
-
this.result = calcInner(array,
|
16
|
+
}
|
17
|
+
mul(array) {
|
18
|
+
this.result = calcInner(array, (a, b) => a * b, this.result);
|
21
19
|
return this;
|
22
|
-
}
|
23
|
-
|
20
|
+
}
|
21
|
+
calc() {
|
24
22
|
var _a;
|
25
|
-
|
23
|
+
const res = (_a = this.result) !== null && _a !== void 0 ? _a : [0n, 0];
|
26
24
|
return res;
|
27
|
-
}
|
28
|
-
|
29
|
-
}());
|
30
|
-
export { PipeInner };
|
25
|
+
}
|
26
|
+
}
|
package/src/quartile/main.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { quartileInner } from "./utils.js";
|
3
3
|
/** This function returns Q1, Q2, Q3 (quartile). */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const quartile = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const { Q1: [Q1bi, Q1fpe], Q2: [Q2bi, Q2fpe], Q3: [Q3bi, Q3fpe] } = quartileInner(arrayInner);
|
7
7
|
return {
|
8
8
|
Q1: bi2s(Q1bi, Q1fpe),
|
9
9
|
Q2: bi2s(Q2bi, Q2fpe),
|
package/src/quartile/utils.js
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
import { PipeInner } from "../pipe/utils.js";
|
2
|
-
|
3
|
-
|
2
|
+
const meanQ = (idx, array) => {
|
3
|
+
const left = new PipeInner().add([array[idx], array[idx - 1]]).calc();
|
4
4
|
return new PipeInner().div([left, [2n, 0]]).calc();
|
5
5
|
};
|
6
|
-
export
|
6
|
+
export const quartileInner = (array) => {
|
7
7
|
if (array.length < 3) {
|
8
8
|
throw Error("To calculate quartile you need at least 3 elements");
|
9
9
|
}
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
const { length } = array;
|
11
|
+
const Q2Idx = length >> 1;
|
12
|
+
const Q1Idx = Q2Idx >> 1;
|
13
|
+
const Q3Idx = length - Q1Idx;
|
14
|
+
const isEvenHalf = Q2Idx % 2 !== 0;
|
15
15
|
return {
|
16
16
|
Q1: isEvenHalf ? array[Q1Idx] : meanQ(Q1Idx, array),
|
17
17
|
Q2: length % 2 !== 0 ? array[Q2Idx] : meanQ(Q2Idx, array),
|
package/src/round/constants.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
export
|
2
|
-
export
|
3
|
-
export
|
4
|
-
export
|
5
|
-
export
|
6
|
-
export
|
7
|
-
export
|
1
|
+
export const UP = "up";
|
2
|
+
export const DOWN = "down";
|
3
|
+
export const HALF_UP = "half-up";
|
4
|
+
export const HALF_DOWN = "half-down";
|
5
|
+
export const HALF_EVEN = "half-even";
|
6
|
+
export const HALF_ODD = "half-odd";
|
7
|
+
export const ROUND_MODE = [
|
8
8
|
UP,
|
9
9
|
DOWN,
|
10
10
|
HALF_UP,
|
package/src/round/main.js
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
import { HALF_UP } from "./constants.js";
|
2
2
|
import { calcLast, handleCarryOver, handleTail } from "./utils.js";
|
3
3
|
/** This function round number. */
|
4
|
-
export
|
5
|
-
var _a;
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
var charCode = value.charCodeAt(i);
|
4
|
+
export const round = (value, options) => {
|
5
|
+
var _a, _b, _c, _d;
|
6
|
+
const roundMode = (_a = options === null || options === void 0 ? void 0 : options.roundMode) !== null && _a !== void 0 ? _a : HALF_UP;
|
7
|
+
let array = [];
|
8
|
+
let sigFig = (_b = options === null || options === void 0 ? void 0 : options.sigFig) !== null && _b !== void 0 ? _b : false;
|
9
|
+
let decimals = (_c = options === null || options === void 0 ? void 0 : options.decimals) !== null && _c !== void 0 ? _c : 0;
|
10
|
+
let isFloat = false;
|
11
|
+
let isNil = true;
|
12
|
+
for (let i = 0; i < value.length; i++) {
|
13
|
+
const charCode = value.charCodeAt(i);
|
15
14
|
if (sigFig && charCode > 48)
|
16
15
|
sigFig = false;
|
17
16
|
if (isNil && charCode > 48)
|
@@ -21,14 +20,14 @@ export var round = function (value, options) {
|
|
21
20
|
if (charCode === 46)
|
22
21
|
isFloat = true;
|
23
22
|
if (isFloat && decimals === 0) {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
const next = (_d = value.charCodeAt(i + 1)) !== null && _d !== void 0 ? _d : 0;
|
24
|
+
const prev = value.charCodeAt(i - 1);
|
25
|
+
const curr = charCode === 46 ? prev : charCode;
|
26
|
+
const [last, hasCarryOver] = calcLast(curr, next, roundMode);
|
28
27
|
if (charCode === 46)
|
29
28
|
isFloat = false;
|
30
29
|
if (hasCarryOver) {
|
31
|
-
|
30
|
+
[array, isFloat] = handleCarryOver(array, isFloat);
|
32
31
|
break;
|
33
32
|
}
|
34
33
|
charCode === 46
|
@@ -41,5 +40,5 @@ export var round = function (value, options) {
|
|
41
40
|
handleTail(array, isFloat);
|
42
41
|
return (isNil && array[array.length - 1] <= 48)
|
43
42
|
? "0"
|
44
|
-
: String.fromCharCode
|
43
|
+
: String.fromCharCode(...array);
|
45
44
|
};
|
package/src/round/utils.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { DOWN, HALF_DOWN, HALF_EVEN, HALF_ODD, HALF_UP, UP, } from "./constants.js";
|
2
|
-
export
|
3
|
-
|
2
|
+
export const handleTail = (array, isFloat) => {
|
3
|
+
let lastIdx = array.length - 1;
|
4
4
|
while (isFloat && array[lastIdx] <= 48) {
|
5
5
|
if (array[lastIdx] === 46 || array.length === 1)
|
6
6
|
isFloat = false;
|
@@ -8,53 +8,52 @@ export var handleTail = function (array, isFloat) {
|
|
8
8
|
lastIdx -= 1;
|
9
9
|
}
|
10
10
|
};
|
11
|
-
export
|
11
|
+
export const handleCarryOver = (array, isFloat) => {
|
12
12
|
var _a;
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
[57,
|
13
|
+
let hasCarryOver = true;
|
14
|
+
let idx = array.length - 1;
|
15
|
+
const mapFloat = new Map([
|
16
|
+
[57, () => {
|
17
17
|
array.pop();
|
18
18
|
idx -= 1;
|
19
19
|
}],
|
20
|
-
[46,
|
20
|
+
[46, () => {
|
21
21
|
array.pop();
|
22
22
|
idx -= 1;
|
23
23
|
isFloat = false;
|
24
24
|
}],
|
25
|
-
[0,
|
25
|
+
[0, () => {
|
26
26
|
array[idx] += 1;
|
27
27
|
hasCarryOver = false;
|
28
28
|
}],
|
29
29
|
]);
|
30
|
-
|
31
|
-
[57,
|
30
|
+
const mapInt = new Map([
|
31
|
+
[57, () => {
|
32
32
|
array[idx] = 48;
|
33
33
|
idx -= 1;
|
34
34
|
}],
|
35
|
-
[0,
|
35
|
+
[0, () => {
|
36
36
|
array[idx] += 1;
|
37
37
|
hasCarryOver = false;
|
38
38
|
}],
|
39
39
|
]);
|
40
40
|
while (hasCarryOver && idx >= 0) {
|
41
|
-
|
42
|
-
|
41
|
+
const map = isFloat ? mapFloat : mapInt;
|
42
|
+
const key = map.has(array[idx]) ? array[idx] : 0;
|
43
43
|
(_a = map.get(key)) === null || _a === void 0 ? void 0 : _a();
|
44
44
|
}
|
45
45
|
hasCarryOver && array.unshift(49);
|
46
46
|
return [array, isFloat];
|
47
47
|
};
|
48
|
-
export
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
_a);
|
48
|
+
export const calcLast = (current, next, roundMode) => {
|
49
|
+
const isEven = current % 2 === 1;
|
50
|
+
const map = {
|
51
|
+
[UP]: current + (next > 48 ? 1 : 0),
|
52
|
+
[DOWN]: current,
|
53
|
+
[HALF_UP]: current + (next >= 53 ? 1 : 0),
|
54
|
+
[HALF_DOWN]: current + (next > 53 ? 1 : 0),
|
55
|
+
[HALF_EVEN]: current + ((isEven && next >= 53) ? 1 : 0),
|
56
|
+
[HALF_ODD]: current + ((!isEven && next >= 53) ? 1 : 0),
|
57
|
+
};
|
59
58
|
return map[roundMode] > 57 ? [48, true] : [map[roundMode], false];
|
60
59
|
};
|
package/src/shared/constant.js
CHANGED
package/src/shared/utils.js
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
export
|
1
|
+
export const bi2s = (bigInt, fpe) => {
|
2
2
|
if (bigInt === 0n)
|
3
3
|
return "0";
|
4
|
-
|
4
|
+
const isNeg = bigInt < 0n;
|
5
5
|
isNeg && (bigInt *= -1n);
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
const dp = bigInt % (10n ** BigInt(fpe));
|
7
|
+
const bigStr = bigInt.toString();
|
8
|
+
let fpIdx = bigStr.length - fpe;
|
9
9
|
if (fpIdx < 0)
|
10
10
|
fpIdx = 0;
|
11
|
-
|
12
|
-
|
11
|
+
const before = bigStr.slice(0, fpIdx);
|
12
|
+
const after = bigStr.slice(fpIdx);
|
13
13
|
if (before) {
|
14
14
|
return fillHead(bigStr.length, fpe, isNeg, true) +
|
15
|
-
(fpe > 0 && dp > 0 ? trimTail(
|
15
|
+
(fpe > 0 && dp > 0 ? trimTail(`${before}.${after}`) : before);
|
16
16
|
}
|
17
|
-
return
|
17
|
+
return `${fillHead(bigStr.length, fpe, isNeg, false)}${trimTail(after)}`;
|
18
18
|
};
|
19
|
-
export
|
20
|
-
|
19
|
+
export const s2bi = (str) => {
|
20
|
+
const fpi = str.indexOf(".");
|
21
21
|
if (fpi === -1)
|
22
22
|
return [BigInt(str), 0];
|
23
23
|
return [
|
@@ -25,12 +25,12 @@ export var s2bi = function (str) {
|
|
25
25
|
fpi === -1 ? 0 : str.length - 1 - fpi,
|
26
26
|
];
|
27
27
|
};
|
28
|
-
export
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
for (
|
33
|
-
|
28
|
+
export const calcInner = (array, op, def) => {
|
29
|
+
let bigInt = def ? def[0] : array[0][0];
|
30
|
+
let fpe = def ? def[1] : array[0][1];
|
31
|
+
const opm = op(1n, 1n);
|
32
|
+
for (let i = def ? 0 : 1; i < array.length; i++) {
|
33
|
+
const [bigCurrent, dpLen] = array[i];
|
34
34
|
if (dpLen === 0 && fpe === 0) {
|
35
35
|
bigInt = op(bigInt, bigCurrent);
|
36
36
|
continue;
|
@@ -41,11 +41,11 @@ export var calcInner = function (array, op, def) {
|
|
41
41
|
}
|
42
42
|
else {
|
43
43
|
if (fpe < dpLen) {
|
44
|
-
|
45
|
-
bigInt = op(bigInt * (
|
44
|
+
const fpDiff = dpLen - fpe;
|
45
|
+
bigInt = op(bigInt * (10n ** BigInt(fpDiff)), bigCurrent);
|
46
46
|
}
|
47
47
|
if (fpe > dpLen) {
|
48
|
-
bigInt = op(bigInt, bigCurrent * (
|
48
|
+
bigInt = op(bigInt, bigCurrent * (10n ** BigInt(fpe - dpLen)));
|
49
49
|
}
|
50
50
|
if (fpe === dpLen)
|
51
51
|
bigInt = op(bigInt, bigCurrent);
|
@@ -55,19 +55,19 @@ export var calcInner = function (array, op, def) {
|
|
55
55
|
}
|
56
56
|
return [bigInt, fpe];
|
57
57
|
};
|
58
|
-
export
|
59
|
-
|
58
|
+
export const fillHead = (len, fpe, isNeg, hasBefore) => {
|
59
|
+
let head = (isNeg ? "-" : "") + (hasBefore ? "" : "0.");
|
60
60
|
while (len < fpe) {
|
61
61
|
head = head + "0";
|
62
62
|
len += 1;
|
63
63
|
}
|
64
64
|
return head;
|
65
65
|
};
|
66
|
-
export
|
66
|
+
export const trimTail = (str) => {
|
67
67
|
if (str[str.length - 1] !== "0")
|
68
68
|
return str;
|
69
|
-
|
70
|
-
for (
|
69
|
+
let count = 0;
|
70
|
+
for (let i = str.length - 1; i >= 0; i -= 1) {
|
71
71
|
if (str[i] !== "0")
|
72
72
|
return str.slice(0, str.length - count);
|
73
73
|
count += 1;
|
package/src/sort/constants.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
export
|
2
|
-
export
|
3
|
-
export
|
1
|
+
export const ASC = "asc";
|
2
|
+
export const DESC = "desc";
|
3
|
+
export const sortingArray = [ASC, DESC];
|
package/src/sort/main.js
CHANGED
@@ -2,12 +2,8 @@ import { bi2s, s2bi } from "../shared/utils.js";
|
|
2
2
|
import { ASC } from "./constants.js";
|
3
3
|
import { sortInner } from "./utils.js";
|
4
4
|
/** Using this function sort an array. */
|
5
|
-
export
|
6
|
-
|
7
|
-
var arrayInner = array.map(function (str) { return s2bi(str); });
|
5
|
+
export const sort = (array, sorting = ASC) => {
|
6
|
+
const arrayInner = array.map((str) => s2bi(str));
|
8
7
|
sortInner(arrayInner, sorting);
|
9
|
-
return arrayInner.map(
|
10
|
-
var bi = _a[0], fpe = _a[1];
|
11
|
-
return bi2s(bi, fpe);
|
12
|
-
});
|
8
|
+
return arrayInner.map(([bi, fpe]) => bi2s(bi, fpe));
|
13
9
|
};
|
package/src/sort/utils.js
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
import { compareInner } from "../compare/utils.js";
|
2
2
|
import { ASC } from "./constants.js";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
var idx = i;
|
3
|
+
const heapify = (array, len, i, sorting) => {
|
4
|
+
const idxL = 2 * i + 1;
|
5
|
+
const idxR = idxL + 1;
|
6
|
+
let idx = i;
|
8
7
|
if (sorting === ASC) {
|
9
8
|
if (idxL < len && compareInner(array[idxL], array[idx])[1] >= 0)
|
10
9
|
idx = idxL;
|
@@ -18,18 +17,17 @@ var heapify = function (array, len, i, sorting) {
|
|
18
17
|
idx = idxR;
|
19
18
|
}
|
20
19
|
if (idx !== i) {
|
21
|
-
|
20
|
+
[array[i], array[idx]] = [array[idx], array[i]];
|
22
21
|
heapify(array, len, idx, sorting);
|
23
22
|
}
|
24
23
|
};
|
25
|
-
export
|
26
|
-
|
27
|
-
|
28
|
-
for (var i = (len >> 1) - 1; i >= 0; i--) {
|
24
|
+
export const sortInner = (array, sorting) => {
|
25
|
+
const { length: len } = array;
|
26
|
+
for (let i = (len >> 1) - 1; i >= 0; i--) {
|
29
27
|
heapify(array, len, i, sorting);
|
30
28
|
}
|
31
|
-
for (
|
32
|
-
|
29
|
+
for (let i = len - 1; i >= 0; i--) {
|
30
|
+
[array[0], array[i]] = [array[i], array[0]];
|
33
31
|
heapify(array, i, 0, sorting);
|
34
32
|
}
|
35
33
|
return array;
|
package/src/sqrt/main.js
CHANGED
@@ -2,9 +2,8 @@ import { round } from "../round/main.js";
|
|
2
2
|
import { bi2s, s2bi } from "../shared/utils.js";
|
3
3
|
import { sqrtInner } from "./utils.js";
|
4
4
|
/** Find square root of a number */
|
5
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
return round(outputStr, { decimals: decimals });
|
5
|
+
export const sqrt = (value, precision) => {
|
6
|
+
const valueInner = s2bi(value);
|
7
|
+
const [[bi, fpe], decimals] = sqrtInner(valueInner, precision ? s2bi(precision) : undefined);
|
8
|
+
return round(bi2s(bi, fpe), { decimals });
|
10
9
|
};
|
package/src/sqrt/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
import type { BI } from "../shared/types.d.ts";
|
2
|
-
export type SqrtInner = (
|
3
|
-
export type Sqrt = (
|
2
|
+
export type SqrtInner = (value: BI, prec?: BI, max?: number) => [BI, number];
|
3
|
+
export type Sqrt = (value: string, precision?: string) => string;
|
package/src/sqrt/utils.js
CHANGED
@@ -1,18 +1,14 @@
|
|
1
1
|
import { isLeftGreaterInner } from "../compare/utils.js";
|
2
2
|
import { PipeInner } from "../pipe/utils.js";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
var guess = input;
|
9
|
-
for (var i = 0; i < max; i++) {
|
10
|
-
var nextGuess = new PipeInner()
|
11
|
-
.div([input, guess])
|
3
|
+
export const sqrtInner = (value, prec = [1n, 13], max = 100) => {
|
4
|
+
let guess = value;
|
5
|
+
for (let i = 0; i < max; i++) {
|
6
|
+
const nextGuess = new PipeInner()
|
7
|
+
.div([value, guess])
|
12
8
|
.add([guess])
|
13
|
-
.mul([
|
9
|
+
.mul([[5n, 1]])
|
14
10
|
.calc();
|
15
|
-
|
11
|
+
let [bi, fpe] = new PipeInner().sub([nextGuess, guess]).calc();
|
16
12
|
if (bi < 0n)
|
17
13
|
bi *= -1n;
|
18
14
|
if (isLeftGreaterInner({ left: prec, right: [bi, fpe] })) {
|