@lakuna/umath 0.0.1 → 0.0.3
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/LICENSE +21 -21
- package/README.md +9 -2
- package/dist/algorithms/greatestCommonDivisor.d.ts +3 -0
- package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -0
- package/dist/algorithms/greatestCommonDivisor.js +13 -0
- package/dist/algorithms/greatestCommonDivisor.js.map +1 -0
- package/dist/algorithms/summation.d.ts +2 -0
- package/dist/algorithms/summation.d.ts.map +1 -0
- package/dist/algorithms/summation.js +7 -0
- package/dist/algorithms/summation.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types/IntegerRepresentation.d.ts +2 -0
- package/dist/types/IntegerRepresentation.d.ts.map +1 -0
- package/dist/types/IntegerRepresentation.js +2 -0
- package/dist/types/IntegerRepresentation.js.map +1 -0
- package/dist/utility/BigNumber.d.ts +15 -0
- package/dist/utility/BigNumber.d.ts.map +1 -0
- package/dist/utility/BigNumber.js +72 -0
- package/dist/utility/BigNumber.js.map +1 -0
- package/package.json +51 -51
- package/src/algorithms/greatestCommonDivisor.ts +30 -0
- package/src/algorithms/summation.ts +20 -0
- package/src/index.ts +4 -1
- package/src/types/IntegerRepresentation.ts +2 -0
- package/src/utility/BigNumber.ts +129 -0
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023 Travis Martin
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Travis Martin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
[](https://bundlephobia.com/package/@lakuna/umath)
|
|
2
|
+
|
|
3
|
+
# μMath
|
|
4
|
+
|
|
5
|
+
A lightweight math library.
|
|
6
|
+
|
|
7
|
+
Documentation is automatically generated and uploaded to [the website](https://umath.lakuna.pw/).
|
|
8
|
+
|
|
9
|
+
The source code can be found on [the GitHub repository](https://github.com/Lakuna/umath).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"greatestCommonDivisor.d.ts","sourceRoot":"","sources":["../../src/algorithms/greatestCommonDivisor.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;AAS5E,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"greatestCommonDivisor.js","sourceRoot":"","sources":["../../src/algorithms/greatestCommonDivisor.ts"],"names":[],"mappings":"AAkBA,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,CAAkB,EAAE,CAAkB;IAEnF,IAAI,CAAC,GAAG,CAAC,EAAE;QAAE,CAAC,GAAG,CAAC,CAAC,CAAC;KAAE;IACtB,IAAI,CAAC,GAAG,CAAC,EAAE;QAAE,CAAC,GAAG,CAAC,CAAC,CAAC;KAAE;IAGtB,OAAO,CAAC,EAAE;QACT,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAG,CAAY,GAAI,CAAY,CAAC,CAAC;KAC5C;IAED,OAAO,CAAC,CAAC;AACV,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"summation.d.ts","sourceRoot":"","sources":["../../src/algorithms/summation.ts"],"names":[],"mappings":"AAiBA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAE/F"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"summation.js","sourceRoot":"","sources":["../../src/algorithms/summation.ts"],"names":[],"mappings":"AAMA,SAAS,iBAAiB,CAAC,CAAS,EAAE,GAAW,EAAE,IAA2B;IAC7E,OAAO,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACtE,CAAC;AASD,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,GAAW,EAAE,GAAW,EAAE,IAA2B;IACtF,OAAO,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;AAC1C,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as greatestCommonDivisor } from "./algorithms/greatestCommonDivisor.js";
|
|
2
|
+
export { default as summation } from "./algorithms/summation.js";
|
|
3
|
+
export type { IntegerRepresentation } from "./types/IntegerRepresentation.js";
|
|
4
|
+
export { default as BigNumber } from "./utility/BigNumber.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AACzF,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACjE,YAAY,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAC9E,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AACzF,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAEjE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IntegerRepresentation.d.ts","sourceRoot":"","sources":["../../src/types/IntegerRepresentation.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IntegerRepresentation.js","sourceRoot":"","sources":["../../src/types/IntegerRepresentation.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type IntegerRepresentation } from "../index.js";
|
|
2
|
+
export default class BigNumber {
|
|
3
|
+
constructor(source?: IntegerRepresentation);
|
|
4
|
+
constructor(dividend?: IntegerRepresentation, divisor?: IntegerRepresentation);
|
|
5
|
+
dividend: bigint;
|
|
6
|
+
divisor: bigint;
|
|
7
|
+
private simplify;
|
|
8
|
+
add(n: IntegerRepresentation | BigNumber): this;
|
|
9
|
+
subtract(n: IntegerRepresentation | BigNumber): this;
|
|
10
|
+
multiply(n: IntegerRepresentation | BigNumber): this;
|
|
11
|
+
divide(n: IntegerRepresentation | BigNumber): this;
|
|
12
|
+
toNumber(): number;
|
|
13
|
+
toString(): string;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=BigNumber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BigNumber.d.ts","sourceRoot":"","sources":["../../src/utility/BigNumber.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,KAAK,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGhF,MAAM,CAAC,OAAO,OAAO,SAAS;gBAKV,MAAM,CAAC,EAAE,qBAAqB;gBAO9B,QAAQ,CAAC,EAAE,qBAAqB,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAS7E,QAAQ,EAAE,MAAM,CAAC;IAGjB,OAAO,EAAE,MAAM,CAAC;IAGvB,OAAO,CAAC,QAAQ;IAoBT,GAAG,CAAC,CAAC,EAAE,qBAAqB,GAAG,SAAS,GAAG,IAAI;IAiB/C,QAAQ,CAAC,CAAC,EAAE,qBAAqB,GAAG,SAAS,GAAG,IAAI;IAiBpD,QAAQ,CAAC,CAAC,EAAE,qBAAqB,GAAG,SAAS,GAAG,IAAI;IAiBpD,MAAM,CAAC,CAAC,EAAE,qBAAqB,GAAG,SAAS,GAAG,IAAI;IAgBlD,QAAQ,IAAI,MAAM;IAQlB,QAAQ,IAAI,MAAM;CAGzB"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { greatestCommonDivisor } from "../index.js";
|
|
2
|
+
export default class BigNumber {
|
|
3
|
+
constructor(dividend = 0, divisor = 1) {
|
|
4
|
+
this.dividend = BigInt(dividend);
|
|
5
|
+
this.divisor = BigInt(divisor);
|
|
6
|
+
this.simplify();
|
|
7
|
+
}
|
|
8
|
+
dividend;
|
|
9
|
+
divisor;
|
|
10
|
+
simplify() {
|
|
11
|
+
if (this.divisor < 0) {
|
|
12
|
+
this.dividend = -this.dividend;
|
|
13
|
+
this.divisor = -this.divisor;
|
|
14
|
+
}
|
|
15
|
+
const gcd = greatestCommonDivisor(this.dividend, this.divisor);
|
|
16
|
+
if (gcd > 1) {
|
|
17
|
+
this.dividend /= gcd;
|
|
18
|
+
this.divisor /= gcd;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
add(n) {
|
|
22
|
+
if (n instanceof BigNumber) {
|
|
23
|
+
this.dividend = this.dividend * n.divisor + n.dividend * this.divisor;
|
|
24
|
+
this.divisor *= n.divisor;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.dividend += BigInt(n) * this.divisor;
|
|
28
|
+
}
|
|
29
|
+
this.simplify();
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
subtract(n) {
|
|
33
|
+
if (n instanceof BigNumber) {
|
|
34
|
+
this.dividend = this.dividend * n.divisor - n.dividend * this.divisor;
|
|
35
|
+
this.divisor *= n.divisor;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
this.dividend -= BigInt(n) * this.divisor;
|
|
39
|
+
}
|
|
40
|
+
this.simplify();
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
multiply(n) {
|
|
44
|
+
if (n instanceof BigNumber) {
|
|
45
|
+
this.dividend *= n.dividend;
|
|
46
|
+
this.divisor *= n.divisor;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.dividend *= BigInt(n);
|
|
50
|
+
}
|
|
51
|
+
this.simplify();
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
divide(n) {
|
|
55
|
+
if (n instanceof BigNumber) {
|
|
56
|
+
this.dividend *= n.divisor;
|
|
57
|
+
this.divisor *= n.dividend;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.divisor *= BigInt(n);
|
|
61
|
+
}
|
|
62
|
+
this.simplify();
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
toNumber() {
|
|
66
|
+
return Number(this.dividend / this.divisor) + (Number(this.dividend % this.divisor) / Number(this.divisor));
|
|
67
|
+
}
|
|
68
|
+
toString() {
|
|
69
|
+
return `${this.dividend}/${this.divisor}`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=BigNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BigNumber.js","sourceRoot":"","sources":["../../src/utility/BigNumber.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@lakuna/umath",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "A lightweight math library.",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"math"
|
|
7
|
-
],
|
|
8
|
-
"homepage": "https://umath.lakuna.pw",
|
|
9
|
-
"bugs": {
|
|
10
|
-
"url": "https://github.com/Lakuna/umath/issues",
|
|
11
|
-
"email": "tjmartin2003@gmail.com"
|
|
12
|
-
},
|
|
13
|
-
"license": "MIT",
|
|
14
|
-
"author": {
|
|
15
|
-
"name": "Travis Martin",
|
|
16
|
-
"email": "tjmartin2003@gmail.com",
|
|
17
|
-
"url": "https://www.lakuna.pw/"
|
|
18
|
-
},
|
|
19
|
-
"funding": {
|
|
20
|
-
"type": "individual",
|
|
21
|
-
"url": "https://github.com/sponsors/Lakuna"
|
|
22
|
-
},
|
|
23
|
-
"files": [
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
],
|
|
27
|
-
"main": "./dist/index.js",
|
|
28
|
-
"browser": "./dist/index.js",
|
|
29
|
-
"exports": {
|
|
30
|
-
".": "./dist/index.js"
|
|
31
|
-
},
|
|
32
|
-
"types": "./dist/index.d.ts",
|
|
33
|
-
"repository": {
|
|
34
|
-
"type": "git",
|
|
35
|
-
"url": "github:Lakuna/umath"
|
|
36
|
-
},
|
|
37
|
-
"scripts": {
|
|
38
|
-
"build": "tsc",
|
|
39
|
-
"lint": "eslint src",
|
|
40
|
-
"doc": "typedoc src"
|
|
41
|
-
},
|
|
42
|
-
"type": "module",
|
|
43
|
-
"sideEffects": false,
|
|
44
|
-
"devDependencies": {
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^5.59.1",
|
|
46
|
-
"@typescript-eslint/parser": "^5.59.1",
|
|
47
|
-
"eslint": "^8.39.0",
|
|
48
|
-
"typedoc": "^0.24.6",
|
|
49
|
-
"typescript": "^5.0.4"
|
|
50
|
-
}
|
|
51
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@lakuna/umath",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "A lightweight math library.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"math"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://umath.lakuna.pw",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/Lakuna/umath/issues",
|
|
11
|
+
"email": "tjmartin2003@gmail.com"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "Travis Martin",
|
|
16
|
+
"email": "tjmartin2003@gmail.com",
|
|
17
|
+
"url": "https://www.lakuna.pw/"
|
|
18
|
+
},
|
|
19
|
+
"funding": {
|
|
20
|
+
"type": "individual",
|
|
21
|
+
"url": "https://github.com/sponsors/Lakuna"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/*",
|
|
25
|
+
"src/*"
|
|
26
|
+
],
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"browser": "./dist/index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "github:Lakuna/umath"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc",
|
|
39
|
+
"lint": "eslint src",
|
|
40
|
+
"doc": "typedoc src"
|
|
41
|
+
},
|
|
42
|
+
"type": "module",
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@typescript-eslint/eslint-plugin": "^5.59.1",
|
|
46
|
+
"@typescript-eslint/parser": "^5.59.1",
|
|
47
|
+
"eslint": "^8.39.0",
|
|
48
|
+
"typedoc": "^0.24.6",
|
|
49
|
+
"typescript": "^5.0.4"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines the greatest common divisor of two integers.
|
|
3
|
+
* @param a The first integer.
|
|
4
|
+
* @param b The second integer.
|
|
5
|
+
* @returns The greatest common divisor of the integers.
|
|
6
|
+
* @see [Wikipedia](https://en.wikipedia.org/wiki/Greatest_common_divisor)
|
|
7
|
+
*/
|
|
8
|
+
export default function greatestCommonDivisor(a: bigint, b: bigint): bigint;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Determines the greatest common divisor of two integers.
|
|
12
|
+
* @param a The first integer.
|
|
13
|
+
* @param b The second integer.
|
|
14
|
+
* @returns The greatest common divisor of the integers.
|
|
15
|
+
* @see [Wikipedia](https://en.wikipedia.org/wiki/Greatest_common_divisor)
|
|
16
|
+
*/
|
|
17
|
+
export default function greatestCommonDivisor(a: number, b: number): number;
|
|
18
|
+
|
|
19
|
+
export default function greatestCommonDivisor(a: bigint | number, b: bigint | number): bigint | number {
|
|
20
|
+
// Ensure that all of the integers are positive.
|
|
21
|
+
if (a < 0) { a = -a; }
|
|
22
|
+
if (b < 0) { b = -b; }
|
|
23
|
+
|
|
24
|
+
// Apply the Euclidean algorithm.
|
|
25
|
+
while (b) {
|
|
26
|
+
[a, b] = [b, (a as number) % (b as number)];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return a;
|
|
30
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively sums up the results of the given function.
|
|
3
|
+
* @param i The index of summation.
|
|
4
|
+
* @param max The upper bound of the index of summation.
|
|
5
|
+
* @param term The function used to generate each term.
|
|
6
|
+
*/
|
|
7
|
+
function summationInternal(i: number, max: number, term: (i: number) => number): number {
|
|
8
|
+
return term(i) + (i < max ? summationInternal(i + 1, max, term) : 0);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Performs a summation.
|
|
13
|
+
* @param min The lower bound (inclusive) of the index of summation.
|
|
14
|
+
* @param max The upper bound (inclusive) of the index of summation.
|
|
15
|
+
* @param term The function used to generate each term.
|
|
16
|
+
* @returns The summation.
|
|
17
|
+
*/
|
|
18
|
+
export default function summation(min: number, max: number, term: (i: number) => number): number {
|
|
19
|
+
return summationInternal(min, max, term);
|
|
20
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { default as greatestCommonDivisor } from "./algorithms/greatestCommonDivisor.js";
|
|
2
|
+
export { default as summation } from "./algorithms/summation.js";
|
|
3
|
+
export type { IntegerRepresentation } from "./types/IntegerRepresentation.js";
|
|
4
|
+
export { default as BigNumber } from "./utility/BigNumber.js";
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { greatestCommonDivisor, type IntegerRepresentation } from "../index.js";
|
|
2
|
+
|
|
3
|
+
/** A number with no maximum precise size. */
|
|
4
|
+
export default class BigNumber {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a number.
|
|
7
|
+
* @param source The number.
|
|
8
|
+
*/
|
|
9
|
+
public constructor(source?: IntegerRepresentation);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a number from a fraction.
|
|
13
|
+
* @param dividend The dividend of the fraction.
|
|
14
|
+
* @param divisor The divisor of the fraction.
|
|
15
|
+
*/
|
|
16
|
+
public constructor(dividend?: IntegerRepresentation, divisor?: IntegerRepresentation);
|
|
17
|
+
|
|
18
|
+
public constructor(dividend: IntegerRepresentation = 0, divisor: IntegerRepresentation = 1) {
|
|
19
|
+
this.dividend = BigInt(dividend);
|
|
20
|
+
this.divisor = BigInt(divisor);
|
|
21
|
+
this.simplify();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The dividend of this fraction. */
|
|
25
|
+
public dividend: bigint;
|
|
26
|
+
|
|
27
|
+
/** The divisor of this fraction. */
|
|
28
|
+
public divisor: bigint;
|
|
29
|
+
|
|
30
|
+
/** Simplifies this fraction. */
|
|
31
|
+
private simplify(): void {
|
|
32
|
+
// Switches signs such that only the dividend can be negative.
|
|
33
|
+
if (this.divisor < 0) {
|
|
34
|
+
this.dividend = -this.dividend;
|
|
35
|
+
this.divisor = -this.divisor;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Divide both by their greatest common divisor.
|
|
39
|
+
const gcd: bigint = greatestCommonDivisor(this.dividend, this.divisor);
|
|
40
|
+
if (gcd > 1) {
|
|
41
|
+
this.dividend /= gcd;
|
|
42
|
+
this.divisor /= gcd;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Adds a number to this number.
|
|
48
|
+
* @param n The other number.
|
|
49
|
+
* @returns This number.
|
|
50
|
+
*/
|
|
51
|
+
public add(n: IntegerRepresentation | BigNumber): this {
|
|
52
|
+
if (n instanceof BigNumber) {
|
|
53
|
+
this.dividend = this.dividend * n.divisor + n.dividend * this.divisor;
|
|
54
|
+
this.divisor *= n.divisor;
|
|
55
|
+
} else {
|
|
56
|
+
this.dividend += BigInt(n) * this.divisor;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.simplify();
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Subtracts a number from this number.
|
|
65
|
+
* @param n The other number.
|
|
66
|
+
* @returns This number.
|
|
67
|
+
*/
|
|
68
|
+
public subtract(n: IntegerRepresentation | BigNumber): this {
|
|
69
|
+
if (n instanceof BigNumber) {
|
|
70
|
+
this.dividend = this.dividend * n.divisor - n.dividend * this.divisor;
|
|
71
|
+
this.divisor *= n.divisor;
|
|
72
|
+
} else {
|
|
73
|
+
this.dividend -= BigInt(n) * this.divisor;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.simplify();
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Multiplies this number by a number.
|
|
82
|
+
* @param n The other number.
|
|
83
|
+
* @returns This number.
|
|
84
|
+
*/
|
|
85
|
+
public multiply(n: IntegerRepresentation | BigNumber): this {
|
|
86
|
+
if (n instanceof BigNumber) {
|
|
87
|
+
this.dividend *= n.dividend;
|
|
88
|
+
this.divisor *= n.divisor;
|
|
89
|
+
} else {
|
|
90
|
+
this.dividend *= BigInt(n);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
this.simplify();
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Divides this number by a number.
|
|
99
|
+
* @param n The other number.
|
|
100
|
+
* @returns This number.
|
|
101
|
+
*/
|
|
102
|
+
public divide(n: IntegerRepresentation | BigNumber): this {
|
|
103
|
+
if (n instanceof BigNumber) {
|
|
104
|
+
this.dividend *= n.divisor;
|
|
105
|
+
this.divisor *= n.dividend;
|
|
106
|
+
} else {
|
|
107
|
+
this.divisor *= BigInt(n);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this.simplify();
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Converts this number to a regular `number`. Might result in loss of precision.
|
|
116
|
+
* @returns This number as a `number`.
|
|
117
|
+
*/
|
|
118
|
+
public toNumber(): number {
|
|
119
|
+
return Number(this.dividend / this.divisor) + (Number(this.dividend % this.divisor) / Number(this.divisor));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Converts this number to a string.
|
|
124
|
+
* @returns A string representation of this number.
|
|
125
|
+
*/
|
|
126
|
+
public toString(): string {
|
|
127
|
+
return `${this.dividend}/${this.divisor}`;
|
|
128
|
+
}
|
|
129
|
+
}
|