continuedfraction.js 0.0.1 → 0.0.2
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 +11 -1
- package/continuedfraction.d.mts +69 -0
- package/continuedfraction.d.ts +24 -42
- package/dist/continuedfraction.min.js +1 -1
- package/package.json +31 -10
- package/src/continuedfraction.js +1 -1
package/README.md
CHANGED
|
@@ -75,27 +75,37 @@ Import the static utility class and use its generators and evaluator:
|
|
|
75
75
|
const sqrtGen = ContinuedFraction.sqrt(23);
|
|
76
76
|
console.log(sqrtGen.next().value); // 4
|
|
77
77
|
console.log(sqrtGen.next().value); // 1, 3, 1, 8, …
|
|
78
|
+
````
|
|
78
79
|
|
|
80
|
+
```javascript
|
|
79
81
|
// 2) Continued‑fraction of a decimal
|
|
80
82
|
let cnt = 0;
|
|
81
83
|
for (let piCf of ContinuedFraction.fromNumber(Math.PI)) {
|
|
82
84
|
console.log(piCf);
|
|
83
85
|
if (cnt++ >= 10) break;
|
|
84
86
|
}
|
|
87
|
+
````
|
|
85
88
|
|
|
89
|
+
```javascript
|
|
86
90
|
// 3) Golden ratio φ terms
|
|
87
91
|
const phiGen = ContinuedFraction.PHI();
|
|
88
92
|
console.log(phiGen.next().value); // 1, and forever 1…
|
|
93
|
+
````
|
|
89
94
|
|
|
95
|
+
```javascript
|
|
90
96
|
// 4) Evaluate the first 10 terms of e’s CF to a Fraction
|
|
91
97
|
const approxE = ContinuedFraction.eval(ContinuedFraction.E, 10);
|
|
92
98
|
console.log(approxE.toString()); // e ≈ “193/71”
|
|
99
|
+
````
|
|
93
100
|
|
|
101
|
+
```javascript
|
|
94
102
|
// 5) Generalized CF for π, 4/π
|
|
95
103
|
const genPi = ContinuedFraction.PI();
|
|
96
104
|
console.log(genPi.next().value); // { a: 3, b: 0 }
|
|
97
105
|
console.log(genPi.next().value); // { a: 6, b: 1 }
|
|
106
|
+
````
|
|
98
107
|
|
|
108
|
+
```javascript
|
|
99
109
|
// 6) Continued‑fraction from two integers
|
|
100
110
|
const halfCf = ContinuedFraction.fromFraction(1, 2);
|
|
101
111
|
console.log([...halfCf]); // [0, 2]
|
|
@@ -112,7 +122,7 @@ console.log([...halfCf]); // [0, 2]
|
|
|
112
122
|
| `FOUR_OVER_PI()` | `Generator<CFTerm>` | Generalized CF terms for 4/π |
|
|
113
123
|
| `PI()` | `Generator<CFTerm>` | Generalized CF terms for π |
|
|
114
124
|
| `E()` | `Generator<number>` | CF expansion of e |
|
|
115
|
-
| `eval(generator, steps)` | `
|
|
125
|
+
| `eval(generator, steps)` | `Generator (() => Generator, steps?: number) ⇒ Fraction` | Evaluate (generalized) continued fraction to a `Fraction` approximation |
|
|
116
126
|
|
|
117
127
|
## Coding Style
|
|
118
128
|
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type Fraction from 'fraction.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A term of a generalized continued fraction.
|
|
5
|
+
*/
|
|
6
|
+
export interface CFTerm {
|
|
7
|
+
/** The aₙ coefficient */
|
|
8
|
+
a: number;
|
|
9
|
+
/** The bₙ coefficient */
|
|
10
|
+
b: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Utility class for generating continued-fraction expansions of various constants.
|
|
15
|
+
* All methods are static.
|
|
16
|
+
*/
|
|
17
|
+
declare class ContinuedFraction {
|
|
18
|
+
/**
|
|
19
|
+
* Infinite generator for the continued‐fraction terms of √N.
|
|
20
|
+
* @param N Integer whose square‐root CF expansion is desired (N ≥ 0)
|
|
21
|
+
* @yields Next continued‐fraction term of √N
|
|
22
|
+
*/
|
|
23
|
+
static sqrt(N: number): Generator<number, void, unknown>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Generator for continued‐fraction terms of any real number via Fraction.js.
|
|
27
|
+
* @param n The real number to convert (as number, string, or bigint)
|
|
28
|
+
* @yields Next continued‐fraction term of n
|
|
29
|
+
*/
|
|
30
|
+
static fromNumber(n: number | string | bigint): Generator<number, void, unknown>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Generator for continued‐fraction terms of a rational a/b via Fraction.js.
|
|
34
|
+
* @param a Numerator (number, string, bigint, or Fraction)
|
|
35
|
+
* @param b Denominator (number, string, or bigint)
|
|
36
|
+
* @yields Next continued‐fraction term of a/b
|
|
37
|
+
*/
|
|
38
|
+
static fromFraction(
|
|
39
|
+
a: number | string | bigint | Fraction,
|
|
40
|
+
b: number | string | bigint
|
|
41
|
+
): Generator<number, void, unknown>;
|
|
42
|
+
|
|
43
|
+
/** Infinite generator of the golden ratio φ = [1; 1, 1, 1, …]. */
|
|
44
|
+
static PHI(): Generator<number, void, unknown>;
|
|
45
|
+
|
|
46
|
+
/** Infinite generator for the generalized continued‐fraction terms of 4/π. */
|
|
47
|
+
static FOUR_OVER_PI(): Generator<CFTerm, void, unknown>;
|
|
48
|
+
|
|
49
|
+
/** Infinite generator for the generalized continued‐fraction terms of π. */
|
|
50
|
+
static PI(): Generator<CFTerm, void, unknown>;
|
|
51
|
+
|
|
52
|
+
/** Infinite generator of e = [2; 1, 2, 1, 1, 4, 1, …]. */
|
|
53
|
+
static E(): Generator<number, void, unknown>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Evaluate a (simple or generalized) continued fraction generator.
|
|
57
|
+
* @param generator A CF term generator or a function returning one
|
|
58
|
+
* @param steps Number of terms to include (default 10)
|
|
59
|
+
* @returns Rational approximation as a Fraction
|
|
60
|
+
*/
|
|
61
|
+
static eval(
|
|
62
|
+
generator:
|
|
63
|
+
| Generator<number | CFTerm, any, any>
|
|
64
|
+
| (() => Generator<number | CFTerm, any, any>),
|
|
65
|
+
steps?: number
|
|
66
|
+
): Fraction;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { ContinuedFraction as default, ContinuedFraction };
|
package/continuedfraction.d.ts
CHANGED
|
@@ -1,26 +1,6 @@
|
|
|
1
|
+
import Fraction = require('fraction.js');
|
|
1
2
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A term of a generalized continued fraction.
|
|
6
|
-
*/
|
|
7
|
-
export interface CFTerm {
|
|
8
|
-
/** The aₙ coefficient */
|
|
9
|
-
a: number;
|
|
10
|
-
/** The bₙ coefficient */
|
|
11
|
-
b: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Utility class for generating continued-fraction expansions of various constants.
|
|
16
|
-
* All methods are static.
|
|
17
|
-
*/
|
|
18
|
-
export default class ContinuedFraction {
|
|
19
|
-
/**
|
|
20
|
-
* Infinite generator for the continued‐fraction terms of √N.
|
|
21
|
-
* @param N Integer whose square‐root CF expansion is desired (N ≥ 0)
|
|
22
|
-
* @yields Next continued‐fraction term of √N
|
|
23
|
-
*/
|
|
3
|
+
declare class ContinuedFraction {
|
|
24
4
|
static sqrt(N: number): Generator<number, void, unknown>;
|
|
25
5
|
|
|
26
6
|
/**
|
|
@@ -41,28 +21,16 @@ export default class ContinuedFraction {
|
|
|
41
21
|
b: number | string | bigint
|
|
42
22
|
): Generator<number, void, unknown>;
|
|
43
23
|
|
|
44
|
-
/**
|
|
45
|
-
* Infinite generator of the golden ratio φ = [1; 1, 1, 1, …].
|
|
46
|
-
* @yields Always 1
|
|
47
|
-
*/
|
|
24
|
+
/** Infinite generator of the golden ratio φ = [1; 1, 1, 1, …]. */
|
|
48
25
|
static PHI(): Generator<number, void, unknown>;
|
|
49
26
|
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
* @yields Term pairs { a, b } for the continued‐fraction
|
|
53
|
-
*/
|
|
54
|
-
static FOUR_OVER_PI(): Generator<CFTerm, void, unknown>;
|
|
27
|
+
/** Infinite generator for the generalized continued‐fraction terms of 4/π. */
|
|
28
|
+
static FOUR_OVER_PI(): Generator<ContinuedFraction.CFTerm, void, unknown>;
|
|
55
29
|
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
* @yields Term pairs { a, b } for the continued‐fraction
|
|
59
|
-
*/
|
|
60
|
-
static PI(): Generator<CFTerm, void, unknown>;
|
|
30
|
+
/** Infinite generator for the generalized continued‐fraction terms of π. */
|
|
31
|
+
static PI(): Generator<ContinuedFraction.CFTerm, void, unknown>;
|
|
61
32
|
|
|
62
|
-
/**
|
|
63
|
-
* Infinite generator of e = [2; 1, 2, 1, 1, 4, 1, …].
|
|
64
|
-
* @yields Next continued‐fraction term of e
|
|
65
|
-
*/
|
|
33
|
+
/** Infinite generator of e = [2; 1, 2, 1, 1, 4, 1, …]. */
|
|
66
34
|
static E(): Generator<number, void, unknown>;
|
|
67
35
|
|
|
68
36
|
/**
|
|
@@ -73,8 +41,22 @@ export default class ContinuedFraction {
|
|
|
73
41
|
*/
|
|
74
42
|
static eval(
|
|
75
43
|
generator:
|
|
76
|
-
| Generator<number | CFTerm, any, any>
|
|
77
|
-
| (() => Generator<number | CFTerm, any, any>),
|
|
44
|
+
| Generator<number | ContinuedFraction.CFTerm, any, any>
|
|
45
|
+
| (() => Generator<number | ContinuedFraction.CFTerm, any, any>),
|
|
78
46
|
steps?: number
|
|
79
47
|
): Fraction;
|
|
80
48
|
}
|
|
49
|
+
|
|
50
|
+
declare namespace ContinuedFraction {
|
|
51
|
+
/**
|
|
52
|
+
* A term of a generalized continued fraction.
|
|
53
|
+
*/
|
|
54
|
+
interface CFTerm {
|
|
55
|
+
/** The aₙ coefficient */
|
|
56
|
+
a: number;
|
|
57
|
+
/** The bₙ coefficient */
|
|
58
|
+
b: number;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export = ContinuedFraction;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "continuedfraction.js",
|
|
3
3
|
"title": "ContinuedFraction.js",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"description": "The RAW continued fractions library",
|
|
5
6
|
"homepage": "https://github.com/rawify/ContinuedFraction.js",
|
|
6
7
|
"bugs": "https://github.com/rawify/ContinuedFraction.js/issues",
|
|
7
|
-
"description": "The RAW continued fractions library",
|
|
8
8
|
"keywords": [
|
|
9
9
|
"math",
|
|
10
10
|
"continued fraction",
|
|
@@ -14,20 +14,41 @@
|
|
|
14
14
|
"private": false,
|
|
15
15
|
"main": "./dist/continuedfraction.js",
|
|
16
16
|
"module": "./dist/continuedfraction.mjs",
|
|
17
|
-
"types": "./continuedfraction.d.ts",
|
|
18
17
|
"browser": "./dist/continuedfraction.min.js",
|
|
19
18
|
"unpkg": "./dist/continuedfraction.min.js",
|
|
20
|
-
"
|
|
19
|
+
"types": "./continuedfraction.d.ts",
|
|
21
20
|
"exports": {
|
|
22
21
|
".": {
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./continuedfraction.d.mts",
|
|
24
|
+
"default": "./dist/continuedfraction.mjs"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./continuedfraction.d.ts",
|
|
28
|
+
"default": "./dist/continuedfraction.js"
|
|
29
|
+
},
|
|
30
|
+
"browser": {
|
|
31
|
+
"types": "./continuedfraction.d.ts",
|
|
32
|
+
"default": "./dist/continuedfraction.min.js"
|
|
33
|
+
},
|
|
34
|
+
"default": {
|
|
35
|
+
"types": "./continuedfraction.d.ts",
|
|
36
|
+
"default": "./dist/continuedfraction.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json"
|
|
40
|
+
},
|
|
41
|
+
"typesVersions": {
|
|
42
|
+
"<4.7": {
|
|
43
|
+
"*": [
|
|
44
|
+
"continuedfraction.d.ts"
|
|
45
|
+
]
|
|
26
46
|
}
|
|
27
47
|
},
|
|
48
|
+
"sideEffects": false,
|
|
28
49
|
"repository": {
|
|
29
50
|
"type": "git",
|
|
30
|
-
"url": "git@github.com
|
|
51
|
+
"url": "git+ssh://git@github.com/rawify/ContinuedFraction.js.git"
|
|
31
52
|
},
|
|
32
53
|
"funding": {
|
|
33
54
|
"type": "github",
|
|
@@ -50,8 +71,8 @@
|
|
|
50
71
|
"test": "mocha tests/*.js"
|
|
51
72
|
},
|
|
52
73
|
"devDependencies": {
|
|
53
|
-
"crude-build": "^0.1.
|
|
54
|
-
"mocha": "^11.1
|
|
74
|
+
"crude-build": "^0.1.2",
|
|
75
|
+
"mocha": "^11.7.1"
|
|
55
76
|
},
|
|
56
77
|
"dependencies": {
|
|
57
78
|
"fraction.js": "^5.2.2"
|
package/src/continuedfraction.js
CHANGED