@numio/bigmath 1.0.10 → 1.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 +17 -2
- package/index.d.ts +6 -5
- package/index.js +6 -5
- package/package.json +4 -2
- package/src/IQR/utils.js +3 -3
- package/src/MAD/utils.js +5 -5
- package/src/compare/utils.js +14 -10
- package/src/mean/utils.js +3 -4
- package/src/{mathOperations → operations}/sub/utils.js +1 -1
- package/src/pipe/main.d.ts +2 -3
- package/src/pipe/main.js +27 -13
- package/src/pipe/utils.d.ts +11 -0
- package/src/pipe/utils.js +34 -0
- package/src/quartile/utils.js +7 -6
- package/src/shared/types.d.ts +1 -0
- package/src/shared/utils.d.ts +2 -1
- package/src/shared/utils.js +13 -0
- 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 +34 -0
- /package/src/{mathOperations → operations}/add/main.d.ts +0 -0
- /package/src/{mathOperations → operations}/add/main.js +0 -0
- /package/src/{mathOperations → operations}/add/types.d.ts +0 -0
- /package/src/{mathOperations → operations}/add/utils.d.ts +0 -0
- /package/src/{mathOperations → operations}/add/utils.js +0 -0
- /package/src/{mathOperations → operations}/div/main.d.ts +0 -0
- /package/src/{mathOperations → operations}/div/main.js +0 -0
- /package/src/{mathOperations → operations}/div/types.d.ts +0 -0
- /package/src/{mathOperations → operations}/div/utils.d.ts +0 -0
- /package/src/{mathOperations → operations}/div/utils.js +0 -0
- /package/src/{mathOperations → operations}/mul/main.d.ts +0 -0
- /package/src/{mathOperations → operations}/mul/main.js +0 -0
- /package/src/{mathOperations → operations}/mul/types.d.ts +0 -0
- /package/src/{mathOperations → operations}/mul/utils.d.ts +0 -0
- /package/src/{mathOperations → operations}/mul/utils.js +0 -0
- /package/src/{mathOperations → operations}/sub/main.d.ts +0 -0
- /package/src/{mathOperations → operations}/sub/main.js +0 -0
- /package/src/{mathOperations → operations}/sub/types.d.ts +0 -0
- /package/src/{mathOperations → operations}/sub/utils.d.ts +0 -0
package/README.md
CHANGED
@@ -17,6 +17,8 @@
|
|
17
17
|
* **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
18
|
* **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
19
|
* **Round Based on Significant Figures:** Control rounding based on the number of significant figures, crucial for scientific and engineering applications.
|
20
|
+
* **Calculate Roots:**
|
21
|
+
* **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
22
|
* **Chain Operations with Pipe:** Simplify complex calculations by chaining arithmetic operations in a readable and intuitive manner.
|
21
23
|
* **Analyze Data Distribution:**
|
22
24
|
* **Calculate Quartiles (Q1, Q2, Q3):** Understand the spread and central tendency of your numerical data, helping identify outliers and the shape of the distribution.
|
@@ -44,8 +46,9 @@ With `@numio/bigmath`, you can confidently perform complex arithmetic operations
|
|
44
46
|
|
45
47
|
### Latest update
|
46
48
|
|
47
|
-
Added `
|
48
|
-
Added `
|
49
|
+
Added `sqrt` - square root of a number\
|
50
|
+
Added `isEqual` and `isLeftGreater` - to compare 2 numbers.\
|
51
|
+
Added `MAD` - Median Absolute Deviation.\
|
49
52
|
Added `IQR` - Interquartile Range.
|
50
53
|
|
51
54
|
# Install:
|
@@ -288,6 +291,18 @@ import { IQR } from "@numio/bigmath";
|
|
288
291
|
IQR(["7", "15", "36", "39", "40", "41"]) // 25;
|
289
292
|
```
|
290
293
|
|
294
|
+
### SQRT - square root of a number
|
295
|
+
```javascript
|
296
|
+
import { sqrt } from "@numio/bigmath";
|
297
|
+
|
298
|
+
sqrt("81") // 9;
|
299
|
+
sqrt("3") // 1.7320508075689;
|
300
|
+
|
301
|
+
// you can change precision of a result (second parameter),
|
302
|
+
sqrt("3", "0.01") // 1.732;
|
303
|
+
sqrt("3", "0.000000000000000000001") // 1.732050807568877293527;
|
304
|
+
```
|
305
|
+
|
291
306
|
Does not have a limitation on the number of digits. You can use any length you'd
|
292
307
|
like
|
293
308
|
|
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": "1.0
|
4
|
+
"version": "1.1.0",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
@@ -44,7 +44,9 @@
|
|
44
44
|
"IQR",
|
45
45
|
"Interquartile Range",
|
46
46
|
"isEqual",
|
47
|
-
"equality"
|
47
|
+
"equality",
|
48
|
+
"sqrt",
|
49
|
+
"square root"
|
48
50
|
],
|
49
51
|
"repository": {
|
50
52
|
"type": "git",
|
package/src/IQR/utils.js
CHANGED
@@ -1,13 +1,13 @@
|
|
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 {
|
5
|
+
import { NIL, ONE } from "../shared/constant.js";
|
6
6
|
export var MIN_LENGTH_FOR_MAD = 30;
|
7
7
|
export var IQRInner = function (array, sigNum) {
|
8
8
|
if (sigNum === void 0) { sigNum = false; }
|
9
9
|
var _a = quartileInner(array), Q1 = _a.Q1, Q3 = _a.Q3;
|
10
|
-
var sub =
|
10
|
+
var sub = new PipeInner().subInner([Q3, Q1]).result;
|
11
11
|
if (sigNum) {
|
12
12
|
var isEqual = isEqualInner({ left: Q3, right: Q1 });
|
13
13
|
var MAD = MADInner(array);
|
package/src/MAD/utils.js
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
import {
|
1
|
+
import { PipeInner } from "../pipe/utils.js";
|
2
2
|
import { quartileInner } from "../quartile/utils.js";
|
3
|
-
import {
|
3
|
+
import { cloneInner } from "../shared/utils.js";
|
4
4
|
import { ASC } from "../sort/constants.js";
|
5
5
|
import { sortInner } from "../sort/utils.js";
|
6
6
|
export var MADInner = function (array) {
|
7
7
|
var median = quartileInner(array).Q2;
|
8
8
|
var madArray = array.map(function (el) {
|
9
|
-
var
|
10
|
-
|
11
|
-
return
|
9
|
+
var result = new PipeInner().subInner([el, cloneInner(median)]).result;
|
10
|
+
result.isNegative = false;
|
11
|
+
return result;
|
12
12
|
});
|
13
13
|
var sorted = sortInner(madArray, ASC);
|
14
14
|
return quartileInner(sorted).Q2;
|
package/src/compare/utils.js
CHANGED
@@ -28,42 +28,46 @@ export var compareInner = function (l, r) {
|
|
28
28
|
var bothIntNeg = lIntLen <= 0 && rIntLen <= 0;
|
29
29
|
var isNilL = l.array.length === 1 && l.array[0] === 48;
|
30
30
|
var isNilR = r.array.length === 1 && r.array[0] === 48;
|
31
|
-
if (lIsNeg && !rIsNeg)
|
32
|
-
return [r, -1];
|
33
31
|
if (!lIsNeg && rIsNeg)
|
34
32
|
return [l, 1];
|
35
|
-
if (
|
33
|
+
if (lIsNeg && !rIsNeg)
|
36
34
|
return [r, -1];
|
37
35
|
if (bothPos && !isNilL && isNilR)
|
38
36
|
return [l, 1];
|
37
|
+
if (bothPos && isNilL && !isNilR)
|
38
|
+
return [r, -1];
|
39
39
|
if (bothNeg && isNilL && !isNilR)
|
40
40
|
return [l, 1];
|
41
41
|
if (bothNeg && !isNilL && isNilR)
|
42
42
|
return [r, -1];
|
43
|
+
if (bothPos && lIntLen > rIntLen)
|
44
|
+
return [l, 1];
|
45
|
+
if (bothPos && lIntLen < rIntLen)
|
46
|
+
return [r, -1];
|
43
47
|
if (bothPos && lIntLen > 0 && rIntLen === 0)
|
44
48
|
return [l, 1];
|
45
49
|
if (bothPos && lIntLen === 0 && rIntLen > 0)
|
46
50
|
return [r, -1];
|
51
|
+
if (!bothPos && lIntLen === 0 && rIntLen > 0)
|
52
|
+
return [l, 1];
|
47
53
|
if (!bothPos && lIntLen > 0 && rIntLen === 0)
|
48
54
|
return [r, -1];
|
49
|
-
if (
|
55
|
+
if (bothPos && bothIntPos && lIntLen > rIntLen)
|
50
56
|
return [l, 1];
|
51
57
|
if (bothPos && bothIntPos && lIntLen < rIntLen)
|
52
58
|
return [r, -1];
|
53
|
-
if (bothPos && bothIntNeg && lIntLen < rIntLen)
|
54
|
-
return [r, -1];
|
55
|
-
if (bothNeg && bothIntPos && lIntLen < rIntLen)
|
56
|
-
return [l, 1];
|
57
59
|
if (bothNeg && bothIntNeg && lIntLen < rIntLen)
|
58
60
|
return [l, 1];
|
59
61
|
if (bothNeg && bothIntNeg && lIntLen > rIntLen)
|
60
62
|
return [r, -1];
|
63
|
+
if (bothNeg && bothIntPos && lIntLen < rIntLen)
|
64
|
+
return [l, 1];
|
61
65
|
if (bothNeg && bothIntPos && lIntLen > rIntLen)
|
62
66
|
return [r, -1];
|
63
|
-
if (bothPos && bothIntPos && lIntLen > rIntLen)
|
64
|
-
return [l, 1];
|
65
67
|
if (bothPos && bothIntNeg && lIntLen > rIntLen)
|
66
68
|
return [l, 1];
|
69
|
+
if (bothPos && bothIntNeg && lIntLen < rIntLen)
|
70
|
+
return [r, -1];
|
67
71
|
if (bothNeg)
|
68
72
|
_a = [r, l], left = _a[0], right = _a[1];
|
69
73
|
while (idx < (lenL > lenR ? lenL : lenR)) {
|
package/src/mean/utils.js
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
import { addRoute } from "../
|
2
|
-
import {
|
1
|
+
import { addRoute } from "../operations/add/utils.js";
|
2
|
+
import { PipeInner } from "../pipe/utils.js";
|
3
3
|
import { DEFAULT } from "../shared/constant.js";
|
4
4
|
import { s2a } from "../shared/utils.js";
|
5
5
|
export var meanInner = function (array) {
|
6
6
|
var left = addRoute(array, DEFAULT);
|
7
7
|
var right = s2a(array.length.toString());
|
8
|
-
|
9
|
-
return res;
|
8
|
+
return new PipeInner().divInner([left, right]).result;
|
10
9
|
};
|
package/src/pipe/main.d.ts
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
import type { A2S } from "../shared/types.d.ts";
|
2
|
-
import type { InputData } from "../types.d.ts";
|
3
2
|
export declare class Pipe {
|
4
|
-
|
5
|
-
constructor(
|
3
|
+
#private;
|
4
|
+
constructor();
|
6
5
|
add(strs: string[]): Pipe;
|
7
6
|
sub(strs: string[]): Pipe;
|
8
7
|
div(strs: string[], limit?: number): Pipe;
|
package/src/pipe/main.js
CHANGED
@@ -1,37 +1,51 @@
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
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");
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
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
|
+
var _Pipe_result;
|
1
13
|
import { DEFAULT } from "../shared/constant.js";
|
2
14
|
import { a2s, s2a } from "../shared/utils.js";
|
3
|
-
import { subRoute } from "../
|
4
|
-
import { addRoute } from "../
|
5
|
-
import { divRoute } from "../
|
6
|
-
import { mulRoute } from "../
|
15
|
+
import { subRoute } from "../operations/sub/utils.js";
|
16
|
+
import { addRoute } from "../operations/add/utils.js";
|
17
|
+
import { divRoute } from "../operations/div/utils.js";
|
18
|
+
import { mulRoute } from "../operations/mul/utils.js";
|
7
19
|
var Pipe = /** @class */ (function () {
|
8
|
-
function Pipe(
|
9
|
-
this
|
20
|
+
function Pipe() {
|
21
|
+
_Pipe_result.set(this, void 0);
|
22
|
+
__classPrivateFieldSet(this, _Pipe_result, DEFAULT, "f");
|
10
23
|
}
|
11
24
|
Pipe.prototype.add = function (strs) {
|
12
|
-
this
|
25
|
+
__classPrivateFieldSet(this, _Pipe_result, addRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
13
26
|
return this;
|
14
27
|
};
|
15
28
|
Pipe.prototype.sub = function (strs) {
|
16
|
-
this
|
29
|
+
__classPrivateFieldSet(this, _Pipe_result, subRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
17
30
|
return this;
|
18
31
|
};
|
19
32
|
Pipe.prototype.div = function (strs, limit) {
|
20
33
|
if (limit === void 0) { limit = 20; }
|
21
|
-
this
|
34
|
+
__classPrivateFieldSet(this, _Pipe_result, divRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f"), limit), "f");
|
22
35
|
return this;
|
23
36
|
};
|
24
37
|
Pipe.prototype.mul = function (strs) {
|
25
|
-
this
|
38
|
+
__classPrivateFieldSet(this, _Pipe_result, mulRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
26
39
|
return this;
|
27
40
|
};
|
28
41
|
Pipe.prototype.calc = function () {
|
29
|
-
var result = a2s(this
|
30
|
-
this
|
42
|
+
var result = a2s(__classPrivateFieldGet(this, _Pipe_result, "f"));
|
43
|
+
__classPrivateFieldSet(this, _Pipe_result, DEFAULT, "f");
|
31
44
|
return result;
|
32
45
|
};
|
33
46
|
return Pipe;
|
34
47
|
}());
|
35
48
|
export { Pipe };
|
49
|
+
_Pipe_result = new WeakMap();
|
36
50
|
/** Using this function you can chain operations (add, sub, div, mul). */
|
37
|
-
export var pipe = new Pipe(
|
51
|
+
export var pipe = new Pipe();
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import type { A2S } from "../shared/types.d.ts";
|
2
|
+
import type { InputData } from "../types.d.ts";
|
3
|
+
export declare class PipeInner {
|
4
|
+
result: InputData;
|
5
|
+
constructor();
|
6
|
+
addInner(array: InputData[]): PipeInner;
|
7
|
+
subInner(array: InputData[]): PipeInner;
|
8
|
+
divInner(array: InputData[]): PipeInner;
|
9
|
+
mulInner(array: InputData[]): PipeInner;
|
10
|
+
calc(): ReturnType<A2S>;
|
11
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { DEFAULT } from "../shared/constant.js";
|
2
|
+
import { a2s, cloneInner } from "../shared/utils.js";
|
3
|
+
import { subRoute } from "../operations/sub/utils.js";
|
4
|
+
import { addRoute } from "../operations/add/utils.js";
|
5
|
+
import { divRoute } from "../operations/div/utils.js";
|
6
|
+
import { mulRoute } from "../operations/mul/utils.js";
|
7
|
+
var PipeInner = /** @class */ (function () {
|
8
|
+
function PipeInner() {
|
9
|
+
this.result = DEFAULT;
|
10
|
+
}
|
11
|
+
PipeInner.prototype.addInner = function (array) {
|
12
|
+
this.result = addRoute(array, cloneInner(this.result));
|
13
|
+
return this;
|
14
|
+
};
|
15
|
+
PipeInner.prototype.subInner = function (array) {
|
16
|
+
this.result = subRoute(array, cloneInner(this.result));
|
17
|
+
return this;
|
18
|
+
};
|
19
|
+
PipeInner.prototype.divInner = function (array) {
|
20
|
+
this.result = divRoute(array, cloneInner(this.result), 20);
|
21
|
+
return this;
|
22
|
+
};
|
23
|
+
PipeInner.prototype.mulInner = function (array) {
|
24
|
+
this.result = mulRoute(array, cloneInner(this.result));
|
25
|
+
return this;
|
26
|
+
};
|
27
|
+
PipeInner.prototype.calc = function () {
|
28
|
+
var result = a2s(this.result);
|
29
|
+
this.result = DEFAULT;
|
30
|
+
return result;
|
31
|
+
};
|
32
|
+
return PipeInner;
|
33
|
+
}());
|
34
|
+
export { PipeInner };
|
package/src/quartile/utils.js
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import { DEFAULT } from "../shared/constant.js";
|
1
|
+
import { PipeInner } from "../pipe/utils.js";
|
2
|
+
import { cloneInner } from "../shared/utils.js";
|
4
3
|
var meanQ = function (idx, array) {
|
5
|
-
|
6
|
-
|
4
|
+
return new PipeInner().addInner([
|
5
|
+
cloneInner(array[idx]),
|
6
|
+
cloneInner(array[idx - 1]),
|
7
|
+
]).divInner([{
|
7
8
|
array: [50],
|
8
9
|
intLength: 1,
|
9
10
|
isFloat: false,
|
10
11
|
isNegative: false,
|
11
|
-
}]
|
12
|
+
}]).result;
|
12
13
|
};
|
13
14
|
export var quartileInner = function (array) {
|
14
15
|
if (array.length < 3) {
|
package/src/shared/types.d.ts
CHANGED
@@ -2,3 +2,4 @@ import type { InputData } from "../types.d.ts";
|
|
2
2
|
export type A2S = (input: InputData) => string;
|
3
3
|
export type S2A = (strings: string) => InputData;
|
4
4
|
export type Convert = (isNegative: InputData["isNegative"], array: InputData["array"]) => string;
|
5
|
+
export type CloneInner = (inner: InputData) => InputData;
|
package/src/shared/utils.d.ts
CHANGED
package/src/shared/utils.js
CHANGED
@@ -68,3 +68,16 @@ export var s2a = function (string) {
|
|
68
68
|
isFloat: dec > 0,
|
69
69
|
};
|
70
70
|
};
|
71
|
+
export var cloneInner = function (inner) {
|
72
|
+
var len = inner.array.length;
|
73
|
+
var clone = Array(len);
|
74
|
+
for (var i = 0; i < len; i++) {
|
75
|
+
clone[i] = inner.array[i];
|
76
|
+
}
|
77
|
+
return {
|
78
|
+
intLength: inner.intLength,
|
79
|
+
isFloat: inner.isFloat,
|
80
|
+
isNegative: inner.isNegative,
|
81
|
+
array: clone,
|
82
|
+
};
|
83
|
+
};
|
package/src/sqrt/main.js
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
import { round } from "../round/main.js";
|
2
|
+
import { a2s, s2a } from "../shared/utils.js";
|
3
|
+
import { sqrtInner } from "./utils.js";
|
4
|
+
/** Find square root of a number */
|
5
|
+
export var sqrt = function (str, precision) {
|
6
|
+
var inputInner = s2a(str);
|
7
|
+
var _a = sqrtInner(inputInner, precision ? s2a(precision) : undefined), outputInner = _a[0], decimals = _a[1];
|
8
|
+
var outputStr = a2s(outputInner);
|
9
|
+
return round(outputStr, { decimals: decimals + 1 });
|
10
|
+
};
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { isLeftGreaterInner } from "../compare/utils.js";
|
2
|
+
import { PipeInner } from "../pipe/utils.js";
|
3
|
+
import { cloneInner } from "../shared/utils.js";
|
4
|
+
var halfInner = {
|
5
|
+
array: [53],
|
6
|
+
intLength: 0,
|
7
|
+
isNegative: false,
|
8
|
+
isFloat: true,
|
9
|
+
};
|
10
|
+
var precDef = {
|
11
|
+
array: [49],
|
12
|
+
intLength: -12,
|
13
|
+
isNegative: false,
|
14
|
+
isFloat: true,
|
15
|
+
};
|
16
|
+
export var sqrtInner = function (input, prec, max) {
|
17
|
+
if (prec === void 0) { prec = precDef; }
|
18
|
+
if (max === void 0) { max = 100; }
|
19
|
+
var guess = cloneInner(input);
|
20
|
+
for (var i = 0; i < max; i++) {
|
21
|
+
var nextGuess = new PipeInner()
|
22
|
+
.divInner([cloneInner(input), cloneInner(guess)])
|
23
|
+
.addInner([guess])
|
24
|
+
.mulInner([halfInner]).result;
|
25
|
+
var candidate = new PipeInner()
|
26
|
+
.subInner([cloneInner(nextGuess), cloneInner(guess)]).result;
|
27
|
+
candidate.isNegative = false;
|
28
|
+
if (isLeftGreaterInner({ left: prec, right: candidate })) {
|
29
|
+
return [nextGuess, prec.intLength * -1];
|
30
|
+
}
|
31
|
+
guess = nextGuess;
|
32
|
+
}
|
33
|
+
return [guess, prec.intLength * -1];
|
34
|
+
};
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|