continuedfraction.js 0.0.2 → 0.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/LICENSE +1 -1
- package/README.md +130 -29
- package/dist/continuedfraction.d.mts +115 -0
- package/dist/continuedfraction.d.ts +16 -0
- package/dist/continuedfraction.js +248 -177
- package/dist/continuedfraction.js.map +7 -0
- package/dist/continuedfraction.min.js +8 -10
- package/dist/continuedfraction.mjs +954 -171
- package/dist/continuedfraction.mjs.map +7 -0
- package/package.json +31 -30
- package/continuedfraction.d.mts +0 -69
- package/continuedfraction.d.ts +0 -62
- package/src/continuedfraction.js +0 -179
- package/tests/continued.js +0 -50
|
@@ -1,179 +1,250 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/continuedfraction.ts
|
|
31
|
+
var continuedfraction_exports = {};
|
|
32
|
+
__export(continuedfraction_exports, {
|
|
33
|
+
ContinuedFraction: () => ContinuedFraction,
|
|
34
|
+
default: () => continuedfraction_default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(continuedfraction_exports);
|
|
37
|
+
var import_fraction = __toESM(require("fraction.js"));
|
|
6
38
|
/**
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* @param {number} N - Integer whose square root CF expansion is desired (N ≥ 0).
|
|
17
|
-
* @yields {number} - Next continued-fraction term of √N.
|
|
18
|
-
*/
|
|
19
|
-
"sqrt": function* (N) {
|
|
20
|
-
|
|
21
|
-
const a0 = Math.floor(Math.sqrt(N));
|
|
22
|
-
yield a0;
|
|
23
|
-
if (a0 * a0 === N) return; // perfect square: single-term CF
|
|
24
|
-
|
|
25
|
-
//while (true) {
|
|
26
|
-
let nk = 0;
|
|
27
|
-
let dk = 1;
|
|
28
|
-
let ak = a0;
|
|
29
|
-
|
|
30
|
-
do {
|
|
31
|
-
nk = ak * dk - nk;
|
|
32
|
-
dk = (N - nk * nk) / dk; // remains integer by construction
|
|
33
|
-
ak = Math.floor((a0 + nk) / dk);
|
|
34
|
-
yield ak;
|
|
35
|
-
} while (/* 2 * a0 !== ak */ true);
|
|
36
|
-
//}
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Generator for continued-fraction terms of any real number via Fraction.js.
|
|
41
|
-
*
|
|
42
|
-
* @exports
|
|
43
|
-
* @param {number|string|BigInt} n - The real number to convert (as number or string).
|
|
44
|
-
* @yields {number} - Next continued-fraction term of n.
|
|
45
|
-
*/
|
|
46
|
-
"fromNumber": function* (n) {
|
|
47
|
-
|
|
48
|
-
yield* Fraction(n)['toContinued']();
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Generator for continued-fraction terms of a rational a/b via Fraction.js.
|
|
53
|
-
*
|
|
54
|
-
* @param {number|string|BigInt|Fraction} a - Numerator.
|
|
55
|
-
* @param {number|string|BigInt} b - Denominator.
|
|
56
|
-
* @yields {number} - Next continued-fraction term of a/b.
|
|
57
|
-
*/
|
|
58
|
-
"fromFraction": function* (a, b) {
|
|
59
|
-
|
|
60
|
-
yield* Fraction(a, b)['toContinued']();
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Infinite generator of the golden ratio φ = [1; 1, 1, 1, …].
|
|
65
|
-
*
|
|
66
|
-
* @yields {number} - Always 1.
|
|
67
|
-
*/
|
|
68
|
-
"PHI": function* () {
|
|
69
|
-
|
|
70
|
-
while (true) {
|
|
71
|
-
yield 1;
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Infinite generator for Continued‐fraction terms of 4/π:
|
|
77
|
-
* 4/π = 1 + 1²/(3 + 2²/(5 + 3²/(7 + …)))
|
|
78
|
-
* @yields {{a:number,b:number}} - Term pair (aₙ, bₙ) of the generalized continued fraction.
|
|
79
|
-
* - first: a₀ = 1
|
|
80
|
-
* - then: aₙ = 2n+1, bₙ = n² (n ≥ 1)
|
|
81
|
-
*/
|
|
82
|
-
"FOUR_OVER_PI": function* () {
|
|
83
|
-
|
|
84
|
-
yield { "a": 1, "b": 0 }; // a₀ = 1
|
|
85
|
-
|
|
86
|
-
for (let n = 0; true; n++) {
|
|
87
|
-
yield { "a": 2, "b": (n * 2 + 1) ** 2 };
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Infinite generator for Continued‐fraction terms of π:
|
|
93
|
-
* π = 3 + 1²/(6 + 3²/(6 + 5²/(6 + …)))
|
|
94
|
-
* @yields {{a:number,b:number}} - Term pair (aₙ, bₙ) of the generalized continued fraction.
|
|
95
|
-
* - first: a₀ = 3
|
|
96
|
-
* - then: aₙ = 6, bₙ = (2n-1)² (n ≥ 1)
|
|
97
|
-
*/
|
|
98
|
-
"PI": function* () {
|
|
99
|
-
|
|
100
|
-
yield { "a": 3, "b": 0 }; // a₀ = 3
|
|
101
|
-
|
|
102
|
-
for (let n = 1; true; n++) {
|
|
103
|
-
yield { "a": 6, "b": (n * 2 - 1) ** 2 };
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Infinite generator of e = [2; 1, 2, 1, 1, 4, 1, …].
|
|
109
|
-
* Terms follow the pattern [2; (1,2m,1) for m = 1,2,3,…].
|
|
110
|
-
*
|
|
111
|
-
* @yields {number} - Next continued-fraction term of e.
|
|
112
|
-
*/
|
|
113
|
-
"E": function* () {
|
|
114
|
-
|
|
115
|
-
yield 2; // a₀ = 2
|
|
116
|
-
|
|
117
|
-
for (let m = 1; ; m++) {
|
|
118
|
-
yield 1;
|
|
119
|
-
yield 2 * m;
|
|
120
|
-
yield 1;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/* Alternative:
|
|
124
|
-
yield { a: 2, b: 0 }; // a₀ = 2
|
|
125
|
-
yield { a: 1, b: 1 }; // a₁ = 1
|
|
126
|
-
|
|
127
|
-
for (let m = 2; ; m++) {
|
|
128
|
-
yield { a: n, b: m - 1 };
|
|
129
|
-
}
|
|
130
|
-
*/
|
|
131
|
-
},
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Evaluate a (simple or generalized) continued fraction generator.
|
|
135
|
-
* For generalized: terms are objects { a, b }, else regular numbers.
|
|
136
|
-
*
|
|
137
|
-
* @param {Generator|Function} generator - Continued fraction term generator.
|
|
138
|
-
* @param {number} steps - Number of terms to evaluate.
|
|
139
|
-
* @returns {Fraction} - Rational approximation as Fraction.
|
|
140
|
-
*/
|
|
141
|
-
"eval": function (generator, steps = 10) {
|
|
142
|
-
|
|
143
|
-
if (typeof generator === "function") {
|
|
144
|
-
generator = generator();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// pull off a₀ (with its b₀)
|
|
148
|
-
const first = generator['next']();
|
|
149
|
-
if (first['done']) return Fraction(0);
|
|
150
|
-
|
|
151
|
-
const a0 = Fraction(first['value']['a'] ?? first['value']);
|
|
152
|
-
|
|
153
|
-
// initialize convergents p₋₁/q₋₁ = 1/0, p₀/q₀ = a₀/1
|
|
154
|
-
let pCurr = a0, qCurr = Fraction(1);
|
|
155
|
-
let pPrev = Fraction(1), qPrev = Fraction(0);
|
|
156
|
-
|
|
157
|
-
// run through a₁… up to `steps-1` more
|
|
158
|
-
for (let k = 1; k < steps; k++) {
|
|
159
|
-
const { value, done } = generator['next']();
|
|
160
|
-
if (done) break;
|
|
161
|
-
|
|
162
|
-
const ak = Fraction(value['a'] ?? value),
|
|
163
|
-
bk = Fraction(value['b'] ?? 1);
|
|
164
|
-
|
|
165
|
-
// pₖ = aₖ·pₖ₋₁ + bₖ·pₖ₋₂
|
|
166
|
-
// qₖ = aₖ·qₖ₋₁ + bₖ·qₖ₋₂
|
|
167
|
-
[pPrev, pCurr] = [pCurr, ak['mul'](pCurr)['add'](bk['mul'](pPrev))];
|
|
168
|
-
[qPrev, qCurr] = [qCurr, ak['mul'](qCurr)['add'](bk['mul'](qPrev))];
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// final convergent = pCurr/qCurr
|
|
172
|
-
return pCurr['div'](qCurr);
|
|
173
|
-
}
|
|
39
|
+
* @license ContinuedFraction.js v0.1.0
|
|
40
|
+
* https://github.com/rawify/ContinuedFraction.js
|
|
41
|
+
*
|
|
42
|
+
* Copyright (c) 2026, Robert Eisele (https://raw.org/)
|
|
43
|
+
* Licensed under the MIT license.
|
|
44
|
+
**/
|
|
45
|
+
var Fraction = import_fraction.default;
|
|
46
|
+
function iteratorFrom(source) {
|
|
47
|
+
return typeof source === "function" ? source() : source;
|
|
174
48
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
49
|
+
function coefficientOf(term) {
|
|
50
|
+
return typeof term === "object" && term !== null && "a" in term ? term.a : term;
|
|
51
|
+
}
|
|
52
|
+
function numeratorOf(term) {
|
|
53
|
+
return typeof term === "object" && term !== null && "b" in term ? term.b : 1;
|
|
54
|
+
}
|
|
55
|
+
function validateSteps(steps) {
|
|
56
|
+
if (!Number.isSafeInteger(steps) || steps < 0) {
|
|
57
|
+
throw new RangeError("steps must be a non-negative safe integer");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
var ContinuedFraction = class _ContinuedFraction {
|
|
61
|
+
constructor() {
|
|
62
|
+
throw new TypeError("ContinuedFraction is a static class");
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Infinite generator for the continued-fraction terms of sqrt(N).
|
|
66
|
+
* sqrt(N) = [a0; (a1, a2, ..., a_p)], with a period boundary at ak = 2*a0.
|
|
67
|
+
* For perfect squares, yields only a0 = floor(sqrt(N)) and then returns.
|
|
68
|
+
*
|
|
69
|
+
* @param N Integer whose square-root CF expansion is desired (N >= 0).
|
|
70
|
+
* @yields Next continued-fraction term of sqrt(N).
|
|
71
|
+
*/
|
|
72
|
+
static *sqrt(N) {
|
|
73
|
+
if (!Number.isSafeInteger(N) || N < 0) {
|
|
74
|
+
throw new RangeError("N must be a non-negative safe integer");
|
|
75
|
+
}
|
|
76
|
+
const a0 = Math.floor(Math.sqrt(N));
|
|
77
|
+
yield a0;
|
|
78
|
+
if (a0 * a0 === N) return;
|
|
79
|
+
let nk = 0;
|
|
80
|
+
let dk = 1;
|
|
81
|
+
let ak = a0;
|
|
82
|
+
while (true) {
|
|
83
|
+
nk = ak * dk - nk;
|
|
84
|
+
dk = (N - nk * nk) / dk;
|
|
85
|
+
ak = Math.floor((a0 + nk) / dk);
|
|
86
|
+
yield ak;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Generator for continued-fraction terms of any real number via Fraction.js.
|
|
91
|
+
*
|
|
92
|
+
* @param n The real number to convert (as number, string, or bigint).
|
|
93
|
+
* @yields Next continued-fraction term of n.
|
|
94
|
+
*/
|
|
95
|
+
static *fromNumber(n) {
|
|
96
|
+
yield* Fraction(n).toContinued();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Generator for continued-fraction terms of a rational a/b via Fraction.js.
|
|
100
|
+
*
|
|
101
|
+
* @param a Numerator, Fraction, or complete fraction string.
|
|
102
|
+
* @param b Optional denominator.
|
|
103
|
+
* @yields Next continued-fraction term of a/b.
|
|
104
|
+
*/
|
|
105
|
+
static *fromFraction(a, b) {
|
|
106
|
+
const fraction = b === void 0 ? Fraction(a) : Fraction(a).div(Fraction(b));
|
|
107
|
+
yield* fraction.toContinued();
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Yields a finite sequence of simple or generalized terms unchanged.
|
|
111
|
+
*
|
|
112
|
+
* @param terms Continued-fraction terms.
|
|
113
|
+
* @yields Each supplied term in order.
|
|
114
|
+
*/
|
|
115
|
+
static *fromTerms(terms) {
|
|
116
|
+
yield* terms;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Infinite generator of the golden ratio phi = [1; 1, 1, 1, ...].
|
|
120
|
+
*
|
|
121
|
+
* @yields Always 1.
|
|
122
|
+
*/
|
|
123
|
+
static *PHI() {
|
|
124
|
+
while (true) {
|
|
125
|
+
yield 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Infinite generator for Brouncker's generalized continued fraction of 4/pi:
|
|
130
|
+
* 4/pi = 1 + 1^2/(2 + 3^2/(2 + 5^2/(2 + ...)))
|
|
131
|
+
*
|
|
132
|
+
* @yields Term pair (a_n, b_n) of the generalized continued fraction.
|
|
133
|
+
* - first: a_0 = 1
|
|
134
|
+
* - then: a_n = 2, b_n = (2n-1)^2 (n >= 1)
|
|
135
|
+
*/
|
|
136
|
+
static *FOUR_OVER_PI() {
|
|
137
|
+
yield { a: 1, b: 0 };
|
|
138
|
+
for (let n = 1; true; n++) {
|
|
139
|
+
yield { a: 2, b: (n * 2 - 1) ** 2 };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Infinite generator for a generalized continued fraction of pi:
|
|
144
|
+
* pi = 3 + 1^2/(6 + 3^2/(6 + 5^2/(6 + ...)))
|
|
145
|
+
*
|
|
146
|
+
* @yields Term pair (a_n, b_n) of the generalized continued fraction.
|
|
147
|
+
* - first: a_0 = 3
|
|
148
|
+
* - then: a_n = 6, b_n = (2n-1)^2 (n >= 1)
|
|
149
|
+
*/
|
|
150
|
+
static *PI() {
|
|
151
|
+
yield { a: 3, b: 0 };
|
|
152
|
+
for (let n = 1; true; n++) {
|
|
153
|
+
yield { a: 6, b: (n * 2 - 1) ** 2 };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Infinite generator of e = [2; 1, 2, 1, 1, 4, 1, ...].
|
|
158
|
+
* Terms follow the pattern [2; (1, 2m, 1) for m = 1, 2, 3, ...].
|
|
159
|
+
*
|
|
160
|
+
* @yields Next continued-fraction term of e.
|
|
161
|
+
*/
|
|
162
|
+
static *E() {
|
|
163
|
+
yield 2;
|
|
164
|
+
for (let m = 1; true; m++) {
|
|
165
|
+
yield 1;
|
|
166
|
+
yield 2 * m;
|
|
167
|
+
yield 1;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Collects at most `steps` terms from a continued fraction.
|
|
172
|
+
*
|
|
173
|
+
* @param source Continued-fraction term iterator or a function returning one.
|
|
174
|
+
* @param steps Maximum number of terms to collect.
|
|
175
|
+
* @returns Collected terms in source order.
|
|
176
|
+
*/
|
|
177
|
+
static toArray(source, steps = 10) {
|
|
178
|
+
validateSteps(steps);
|
|
179
|
+
const iterator = iteratorFrom(source);
|
|
180
|
+
const terms = [];
|
|
181
|
+
while (terms.length < steps) {
|
|
182
|
+
const result = iterator.next();
|
|
183
|
+
if (result.done) break;
|
|
184
|
+
terms.push(result.value);
|
|
185
|
+
}
|
|
186
|
+
return terms;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Generates every convergent of a simple or generalized continued fraction.
|
|
190
|
+
*
|
|
191
|
+
* @param source Continued-fraction term iterator or a function returning one.
|
|
192
|
+
* @yields Exact convergents as Fraction instances.
|
|
193
|
+
*/
|
|
194
|
+
static *convergents(source) {
|
|
195
|
+
const iterator = iteratorFrom(source);
|
|
196
|
+
const first = iterator.next();
|
|
197
|
+
if (first.done) return;
|
|
198
|
+
let currentNumerator = Fraction(coefficientOf(first.value));
|
|
199
|
+
let currentDenominator = Fraction(1);
|
|
200
|
+
let previousNumerator = Fraction(1);
|
|
201
|
+
let previousDenominator = Fraction(0);
|
|
202
|
+
yield currentNumerator.div(currentDenominator);
|
|
203
|
+
while (true) {
|
|
204
|
+
const result = iterator.next();
|
|
205
|
+
if (result.done) return;
|
|
206
|
+
const coefficient = Fraction(coefficientOf(result.value));
|
|
207
|
+
const numerator = Fraction(numeratorOf(result.value));
|
|
208
|
+
[previousNumerator, currentNumerator] = [
|
|
209
|
+
currentNumerator,
|
|
210
|
+
coefficient.mul(currentNumerator).add(numerator.mul(previousNumerator))
|
|
211
|
+
];
|
|
212
|
+
[previousDenominator, currentDenominator] = [
|
|
213
|
+
currentDenominator,
|
|
214
|
+
coefficient.mul(currentDenominator).add(numerator.mul(previousDenominator))
|
|
215
|
+
];
|
|
216
|
+
yield currentNumerator.div(currentDenominator);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Evaluates a simple or generalized continued fraction generator.
|
|
221
|
+
* For generalized fractions terms are objects `{ a, b }`; otherwise they are coefficients.
|
|
222
|
+
*
|
|
223
|
+
* @param source Continued-fraction term iterator or a function returning one.
|
|
224
|
+
* @param steps Number of terms to evaluate.
|
|
225
|
+
* @returns Rational approximation as Fraction.
|
|
226
|
+
*/
|
|
227
|
+
static eval(source, steps = 10) {
|
|
228
|
+
validateSteps(steps);
|
|
229
|
+
if (steps === 0) return Fraction(0);
|
|
230
|
+
let approximation = Fraction(0);
|
|
231
|
+
let count = 0;
|
|
232
|
+
for (const convergent of _ContinuedFraction.convergents(source)) {
|
|
233
|
+
approximation = convergent;
|
|
234
|
+
count++;
|
|
235
|
+
if (count === steps) break;
|
|
236
|
+
}
|
|
237
|
+
return approximation;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
var continuedfraction_default = ContinuedFraction;
|
|
241
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
242
|
+
0 && (module.exports = {
|
|
243
|
+
ContinuedFraction
|
|
244
|
+
});
|
|
245
|
+
const ContinuedFractionExport = module.exports.default;
|
|
246
|
+
Object.defineProperty(ContinuedFractionExport, "__esModule", { value: true });
|
|
247
|
+
ContinuedFractionExport.default = ContinuedFractionExport;
|
|
248
|
+
ContinuedFractionExport.ContinuedFraction = ContinuedFractionExport;
|
|
249
|
+
module.exports = ContinuedFractionExport;
|
|
250
|
+
//# sourceMappingURL=continuedfraction.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/continuedfraction.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * @license ContinuedFraction.js v0.1.0\n * https://github.com/rawify/ContinuedFraction.js\n *\n * Copyright (c) 2026, Robert Eisele (https://raw.org/)\n * Licensed under the MIT license.\n **/\n\nimport FractionFactory, { type Fraction as FractionValue } from 'fraction.js';\n\n/** A value accepted by Fraction.js as a continued-fraction coefficient. */\nexport type Coefficient = number | string | bigint | FractionValue;\n\nconst Fraction = FractionFactory as unknown as (value: Coefficient) => FractionValue;\n\n/** A term of a generalized continued fraction. */\nexport interface CFTerm {\n /** The a_n coefficient. */\n a: Coefficient;\n /** The b_n coefficient. */\n b: Coefficient;\n}\n\n/** A simple or generalized continued-fraction term. */\nexport type ContinuedFractionTerm = Coefficient | CFTerm;\n\n/** A term iterator or a function that creates one. */\nexport type ContinuedFractionSource<T = ContinuedFractionTerm> = Iterator<T> | (() => Iterator<T>);\n\nfunction iteratorFrom<T>(source: ContinuedFractionSource<T>): Iterator<T> {\n return typeof source === 'function' ? source() : source;\n}\n\nfunction coefficientOf(term: ContinuedFractionTerm): Coefficient {\n return typeof term === 'object' && term !== null && 'a' in term ? term.a : term;\n}\n\nfunction numeratorOf(term: ContinuedFractionTerm): Coefficient {\n return typeof term === 'object' && term !== null && 'b' in term ? term.b : 1;\n}\n\nfunction validateSteps(steps: number): void {\n if (!Number.isSafeInteger(steps) || steps < 0) {\n throw new RangeError('steps must be a non-negative safe integer');\n }\n}\n\n/**\n * Utility class for generating and evaluating continued fractions.\n * All methods are static; the class cannot be instantiated.\n */\nexport class ContinuedFraction {\n private constructor() {\n throw new TypeError('ContinuedFraction is a static class');\n }\n\n /**\n * Infinite generator for the continued-fraction terms of sqrt(N).\n * sqrt(N) = [a0; (a1, a2, ..., a_p)], with a period boundary at ak = 2*a0.\n * For perfect squares, yields only a0 = floor(sqrt(N)) and then returns.\n *\n * @param N Integer whose square-root CF expansion is desired (N >= 0).\n * @yields Next continued-fraction term of sqrt(N).\n */\n static *sqrt(N: number): Generator<number, void, unknown> {\n if (!Number.isSafeInteger(N) || N < 0) {\n throw new RangeError('N must be a non-negative safe integer');\n }\n\n const a0 = Math.floor(Math.sqrt(N));\n yield a0;\n if (a0 * a0 === N) return; // perfect square: single-term CF\n\n let nk = 0;\n let dk = 1;\n let ak = a0;\n\n while (true) {\n nk = ak * dk - nk;\n dk = (N - nk * nk) / dk; // remains integer by construction\n ak = Math.floor((a0 + nk) / dk);\n yield ak;\n }\n }\n\n /**\n * Generator for continued-fraction terms of any real number via Fraction.js.\n *\n * @param n The real number to convert (as number, string, or bigint).\n * @yields Next continued-fraction term of n.\n */\n static *fromNumber(n: Coefficient): Generator<bigint, void, unknown> {\n yield* Fraction(n).toContinued();\n }\n\n /**\n * Generator for continued-fraction terms of a rational a/b via Fraction.js.\n *\n * @param a Numerator, Fraction, or complete fraction string.\n * @param b Optional denominator.\n * @yields Next continued-fraction term of a/b.\n */\n static *fromFraction(a: Coefficient, b?: Coefficient): Generator<bigint, void, unknown> {\n const fraction = b === undefined ? Fraction(a) : Fraction(a).div(Fraction(b));\n yield* fraction.toContinued();\n }\n\n /**\n * Yields a finite sequence of simple or generalized terms unchanged.\n *\n * @param terms Continued-fraction terms.\n * @yields Each supplied term in order.\n */\n static *fromTerms<T extends ContinuedFractionTerm>(terms: Iterable<T>): Generator<T, void, unknown> {\n yield* terms;\n }\n\n /**\n * Infinite generator of the golden ratio phi = [1; 1, 1, 1, ...].\n *\n * @yields Always 1.\n */\n static *PHI(): Generator<number, never, unknown> {\n while (true) {\n yield 1;\n }\n }\n\n /**\n * Infinite generator for Brouncker's generalized continued fraction of 4/pi:\n * 4/pi = 1 + 1^2/(2 + 3^2/(2 + 5^2/(2 + ...)))\n *\n * @yields Term pair (a_n, b_n) of the generalized continued fraction.\n * - first: a_0 = 1\n * - then: a_n = 2, b_n = (2n-1)^2 (n >= 1)\n */\n static *FOUR_OVER_PI(): Generator<CFTerm, never, unknown> {\n yield { a: 1, b: 0 }; // a_0 = 1\n\n for (let n = 1; true; n++) {\n yield { a: 2, b: (n * 2 - 1) ** 2 };\n }\n }\n\n /**\n * Infinite generator for a generalized continued fraction of pi:\n * pi = 3 + 1^2/(6 + 3^2/(6 + 5^2/(6 + ...)))\n *\n * @yields Term pair (a_n, b_n) of the generalized continued fraction.\n * - first: a_0 = 3\n * - then: a_n = 6, b_n = (2n-1)^2 (n >= 1)\n */\n static *PI(): Generator<CFTerm, never, unknown> {\n yield { a: 3, b: 0 }; // a_0 = 3\n\n for (let n = 1; true; n++) {\n yield { a: 6, b: (n * 2 - 1) ** 2 };\n }\n }\n\n /**\n * Infinite generator of e = [2; 1, 2, 1, 1, 4, 1, ...].\n * Terms follow the pattern [2; (1, 2m, 1) for m = 1, 2, 3, ...].\n *\n * @yields Next continued-fraction term of e.\n */\n static *E(): Generator<number, never, unknown> {\n yield 2; // a_0 = 2\n\n for (let m = 1; true; m++) {\n yield 1;\n yield 2 * m;\n yield 1;\n }\n }\n\n /**\n * Collects at most `steps` terms from a continued fraction.\n *\n * @param source Continued-fraction term iterator or a function returning one.\n * @param steps Maximum number of terms to collect.\n * @returns Collected terms in source order.\n */\n static toArray<T>(source: ContinuedFractionSource<T>, steps = 10): T[] {\n validateSteps(steps);\n\n const iterator = iteratorFrom(source);\n const terms: T[] = [];\n\n while (terms.length < steps) {\n const result = iterator.next();\n if (result.done) break;\n terms.push(result.value);\n }\n\n return terms;\n }\n\n /**\n * Generates every convergent of a simple or generalized continued fraction.\n *\n * @param source Continued-fraction term iterator or a function returning one.\n * @yields Exact convergents as Fraction instances.\n */\n static *convergents(source: ContinuedFractionSource): Generator<FractionValue, void, unknown> {\n const iterator = iteratorFrom(source);\n const first = iterator.next();\n if (first.done) return;\n\n let currentNumerator = Fraction(coefficientOf(first.value));\n let currentDenominator = Fraction(1);\n let previousNumerator = Fraction(1);\n let previousDenominator = Fraction(0);\n\n yield currentNumerator.div(currentDenominator);\n\n while (true) {\n const result = iterator.next();\n if (result.done) return;\n\n const coefficient = Fraction(coefficientOf(result.value));\n const numerator = Fraction(numeratorOf(result.value));\n\n [previousNumerator, currentNumerator] = [\n currentNumerator,\n coefficient.mul(currentNumerator).add(numerator.mul(previousNumerator))];\n [previousDenominator, currentDenominator] = [\n currentDenominator,\n coefficient.mul(currentDenominator).add(numerator.mul(previousDenominator))];\n\n yield currentNumerator.div(currentDenominator);\n }\n }\n\n /**\n * Evaluates a simple or generalized continued fraction generator.\n * For generalized fractions terms are objects `{ a, b }`; otherwise they are coefficients.\n *\n * @param source Continued-fraction term iterator or a function returning one.\n * @param steps Number of terms to evaluate.\n * @returns Rational approximation as Fraction.\n */\n static eval(source: ContinuedFractionSource, steps = 10): FractionValue {\n validateSteps(steps);\n if (steps === 0) return Fraction(0);\n\n let approximation = Fraction(0);\n let count = 0;\n\n for (const convergent of ContinuedFraction.convergents(source)) {\n approximation = convergent;\n count++;\n if (count === steps) break;\n }\n\n return approximation;\n }\n}\n\nexport default ContinuedFraction;"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": ["FractionFactory"]
|
|
7
|
+
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
ContinuedFraction.js v0.0
|
|
3
|
-
https://github.com/rawify/ContinuedFraction.js
|
|
4
|
-
|
|
5
|
-
Copyright (c)
|
|
6
|
-
Licensed under the MIT license.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
for(let a=1;;a++)yield 1,yield 2*a,yield 1},eval:function(a,c=10){"function"===typeof a&&(a=a());var b=a.next();if(b.done)return Fraction(0);b=Fraction(b.value.a??b.value);let d=Fraction(1),f=Fraction(1),h=Fraction(0);for(let k=1;k<c;k++){const {value:g,done:p}=a.next();if(p)break;const l=Fraction(g.a??g),m=Fraction(g.b??1);[f,b]=[b,l.mul(b).add(m.mul(f))];[h,d]=[d,l.mul(d).add(m.mul(h))]}return b.div(d)}};"function"===typeof define&&define.amd?define([],function(){return e}):"object"===typeof exports?
|
|
10
|
-
(Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=e,e.ContinuedFraction=e,module.exports=e):n.ContinuedFraction=e})(this);
|
|
1
|
+
"use strict";var ContinuedFractionExports=(()=>{var S=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var A=(t,n)=>{for(var e in n)S(t,e,{get:n[e],enumerable:!0})},q=(t,n,e,i)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of R(n))!j.call(t,s)&&s!==e&&S(t,s,{get:()=>n[s],enumerable:!(i=L(n,s))||i.enumerable});return t};var z=t=>q(S({},"__esModule",{value:!0}),t);var J={};A(J,{ContinuedFraction:()=>x,default:()=>Y});typeof BigInt>"u"&&(BigInt=function(t){if(isNaN(t))throw new Error("");return t});var u=BigInt(0),f=BigInt(1),T=BigInt(2),O=BigInt(5),y=BigInt(10),Z=2e3,o={s:f,n:u,d:f};function v(t,n){try{t=BigInt(t)}catch{throw C()}return t*n}function b(t){return typeof t=="bigint"?t:Math.floor(t)}function a(t,n){if(n===u)throw D();let e=Object.create(k.prototype);e.s=t<u?-f:f,t=t<u?-t:t;let i=B(t,n);return e.n=t/i,e.d=n/i,e}function F(t){let n={},e=t,i=T,s=O-f;for(;s<=e;){for(;e%i===u;)e/=i,n[i]=(n[i]||u)+f;s+=f+T*i++}return e!==t?e>1&&(n[e]=(n[e]||u)+f):n[t]=(n[t]||u)+f,n}var m=function(t,n){let e=u,i=f,s=f;if(t!=null)if(n!==void 0){if(typeof t=="bigint")e=t;else{if(isNaN(t))throw C();if(t%1!==0)throw _();e=BigInt(t)}if(typeof n=="bigint")i=n;else{if(isNaN(n))throw C();if(n%1!==0)throw _();i=BigInt(n)}s=e*i}else if(typeof t=="object"){if("d"in t&&"n"in t)e=BigInt(t.n),i=BigInt(t.d),"s"in t&&(e*=BigInt(t.s));else if(0 in t)e=BigInt(t[0]),1 in t&&(i=BigInt(t[1]));else if(typeof t=="bigint")e=t;else throw C();s=e*i}else if(typeof t=="number"){if(isNaN(t))throw C();if(t<0&&(s=-f,t=-t),t%1===0)e=BigInt(t);else{let r=1,l=0,d=1,g=1,h=1,w=1e7;for(t>=1&&(r=10**Math.floor(1+Math.log10(t)),t/=r);d<=w&&h<=w;){let c=(l+g)/(d+h);if(t===c){d+h<=w?(e=l+g,i=d+h):h>d?(e=g,i=h):(e=l,i=d);break}else t>c?(l+=g,d+=h):(g+=l,h+=d),d>w?(e=g,i=h):(e=l,i=d)}e=BigInt(e)*BigInt(r),i=BigInt(i)}}else if(typeof t=="string"){let r=0,l=u,d=u,g=u,h=f,w=f,c=t.replace(/_/g,"").match(/\d+|./g);if(c===null)throw C();if(c[r]==="-"?(s=-f,r++):c[r]==="+"&&r++,c.length===r+1?d=v(c[r++],s):c[r+1]==="."||c[r]==="."?(c[r]!=="."&&(l=v(c[r++],s)),r++,(r+1===c.length||c[r+1]==="("&&c[r+3]===")"||c[r+1]==="'"&&c[r+3]==="'")&&(d=v(c[r],s),h=y**BigInt(c[r].length),r++),(c[r]==="("&&c[r+2]===")"||c[r]==="'"&&c[r+2]==="'")&&(g=v(c[r+1],s),w=y**BigInt(c[r+1].length)-f,r+=3)):c[r+1]==="/"||c[r+1]===":"?(d=v(c[r],s),h=v(c[r+2],f),r+=3):c[r+3]==="/"&&c[r+1]===" "&&(l=v(c[r],s),d=v(c[r+2],s),h=v(c[r+4],f),r+=5),c.length<=r)i=h*w,s=e=g+i*l+w*d;else throw C()}else if(typeof t=="bigint")e=t,s=t,i=f;else throw C();if(i===u)throw D();o.s=s<u?-f:f,o.n=e<u?-e:e,o.d=i<u?-i:i};function H(t,n,e){let i=f;for(;n>u;t=t*t%e,n>>=f)n&f&&(i=i*t%e);return i}function U(t,n){for(;n%T===u;n/=T);for(;n%O===u;n/=O);if(n===f)return u;let e=y%n,i=1;for(;e!==f;i++)if(e=e*y%n,i>Z)return u;return BigInt(i)}function W(t,n,e){let i=f,s=H(y,e,n);for(let r=0;r<300;r++){if(i===s)return BigInt(r);i=i*y%n,s=s*y%n}return 0}function B(t,n){if(!t)return n;if(!n)return t;for(;;){if(t%=n,!t)return n;if(n%=t,!n)return t}}function k(t,n){if(m(t,n),this instanceof k)t=B(o.d,o.n),this.s=o.s,this.n=o.n/t,this.d=o.d/t;else return a(o.s*o.n,o.d)}var D=function(){return new Error("Division by Zero")},C=function(){return new Error("Invalid argument")},_=function(){return new Error("Parameters must be integer")};k.prototype={s:f,n:u,d:f,abs:function(){return a(this.n,this.d)},neg:function(){return a(-this.s*this.n,this.d)},add:function(t,n){return m(t,n),a(this.s*this.n*o.d+o.s*this.d*o.n,this.d*o.d)},sub:function(t,n){return m(t,n),a(this.s*this.n*o.d-o.s*this.d*o.n,this.d*o.d)},mul:function(t,n){return m(t,n),a(this.s*o.s*this.n*o.n,this.d*o.d)},div:function(t,n){return m(t,n),a(this.s*o.s*this.n*o.d,this.d*o.n)},clone:function(){return a(this.s*this.n,this.d)},mod:function(t,n){if(t===void 0)return a(this.s*this.n%this.d,f);if(m(t,n),u===o.n*this.d)throw D();return a(this.s*(o.d*this.n)%(o.n*this.d),o.d*this.d)},gcd:function(t,n){return m(t,n),a(B(o.n,this.n)*B(o.d,this.d),o.d*this.d)},lcm:function(t,n){return m(t,n),o.n===u&&this.n===u?a(u,f):a(o.n*this.n,B(o.n,this.n)*B(o.d,this.d))},inverse:function(){return a(this.s*this.d,this.n)},pow:function(t,n){if(m(t,n),o.d===f)return o.s<u?a((this.s*this.d)**o.n,this.n**o.n):a((this.s*this.n)**o.n,this.d**o.n);if(this.s<u)return null;let e=F(this.n),i=F(this.d),s=f,r=f;for(let l in e)if(l!=="1"){if(l==="0"){s=u;break}if(e[l]*=o.n,e[l]%o.d===u)e[l]/=o.d;else return null;s*=BigInt(l)**e[l]}for(let l in i)if(l!=="1"){if(i[l]*=o.n,i[l]%o.d===u)i[l]/=o.d;else return null;r*=BigInt(l)**i[l]}return o.s<u?a(r,s):a(s,r)},log:function(t,n){if(m(t,n),this.s<=u||o.s<=u)return null;let e={},i=F(o.n),s=F(o.d),r=F(this.n),l=F(this.d);for(let h in s)i[h]=(i[h]||u)-s[h];for(let h in l)r[h]=(r[h]||u)-l[h];for(let h in i)h!=="1"&&(e[h]=!0);for(let h in r)h!=="1"&&(e[h]=!0);let d=null,g=null;for(let h in e){let w=i[h]||u,c=r[h]||u;if(w===u){if(c!==u)return null;continue}let N=c,E=w,G=B(N,E);if(N/=G,E/=G,d===null&&g===null)d=N,g=E;else if(N*g!==d*E)return null}return d!==null&&g!==null?a(d,g):null},equals:function(t,n){return m(t,n),this.s*this.n*o.d===o.s*o.n*this.d},lt:function(t,n){return m(t,n),this.s*this.n*o.d<o.s*o.n*this.d},lte:function(t,n){return m(t,n),this.s*this.n*o.d<=o.s*o.n*this.d},gt:function(t,n){return m(t,n),this.s*this.n*o.d>o.s*o.n*this.d},gte:function(t,n){return m(t,n),this.s*this.n*o.d>=o.s*o.n*this.d},compare:function(t,n){m(t,n);let e=this.s*this.n*o.d-o.s*o.n*this.d;return(u<e)-(e<u)},ceil:function(t){return t=y**BigInt(t||0),a(b(this.s*t*this.n/this.d)+(t*this.n%this.d>u&&this.s>=u?f:u),t)},floor:function(t){return t=y**BigInt(t||0),a(b(this.s*t*this.n/this.d)-(t*this.n%this.d>u&&this.s<u?f:u),t)},round:function(t){return t=y**BigInt(t||0),a(b(this.s*t*this.n/this.d)+this.s*((this.s>=u?f:u)+T*(t*this.n%this.d)>this.d?f:u),t)},roundTo:function(t,n){m(t,n);let e=this.n*o.d,i=this.d*o.n,s=e%i,r=b(e/i);return s+s>=i&&r++,a(this.s*r*o.n,o.d)},divisible:function(t,n){return m(t,n),!(!(o.n*this.d)||this.n*o.d%(o.n*this.d))},valueOf:function(){return Number(this.s*this.n)/Number(this.d)},toString:function(t){let n=this.n,e=this.d;t=t||15;let i=U(n,e),s=W(n,e,i),r=this.s<u?"-":"";if(r+=b(n/e),n%=e,n*=y,n&&(r+="."),i){for(let l=s;l--;)r+=b(n/e),n%=e,n*=y;r+="(";for(let l=i;l--;)r+=b(n/e),n%=e,n*=y;r+=")"}else for(let l=t;n&&l--;)r+=b(n/e),n%=e,n*=y;return r},toFraction:function(t){let n=this.n,e=this.d,i=this.s<u?"-":"";if(e===f)i+=n;else{let s=b(n/e);t&&s>u&&(i+=s,i+=" ",n%=e),i+=n,i+="/",i+=e}return i},toLatex:function(t){let n=this.n,e=this.d,i=this.s<u?"-":"";if(e===f)i+=n;else{let s=b(n/e);t&&s>u&&(i+=s,n%=e),i+="\\frac{",i+=n,i+="}{",i+=e,i+="}"}return i},toContinued:function(){let t=this.n,n=this.d,e=[];do{e.push(b(t/n));let i=t%n;t=n,n=i}while(t!==f);return e},simplify:function(t){let n=BigInt(1/(t||.001)|0),e=this.abs(),i=e.toContinued();for(let s=1;s<i.length;s++){let r=a(i[s-1],f);for(let d=s-2;d>=0;d--)r=r.inverse().add(i[d]);let l=r.sub(e);if(l.n*n<l.d)return r.mul(this.s)}return this}};/**
|
|
2
|
+
* @license ContinuedFraction.js v0.1.0
|
|
3
|
+
* https://github.com/rawify/ContinuedFraction.js
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2026, Robert Eisele (https://raw.org/)
|
|
6
|
+
* Licensed under the MIT license.
|
|
7
|
+
**/var I=k;function M(t){return typeof t=="function"?t():t}function P(t){return typeof t=="object"&&t!==null&&"a"in t?t.a:t}function X(t){return typeof t=="object"&&t!==null&&"b"in t?t.b:1}function V(t){if(!Number.isSafeInteger(t)||t<0)throw new RangeError("steps must be a non-negative safe integer")}var x=class t{constructor(){throw new TypeError("ContinuedFraction is a static class")}static*sqrt(n){if(!Number.isSafeInteger(n)||n<0)throw new RangeError("N must be a non-negative safe integer");let e=Math.floor(Math.sqrt(n));if(yield e,e*e===n)return;let i=0,s=1,r=e;for(;;)i=r*s-i,s=(n-i*i)/s,r=Math.floor((e+i)/s),yield r}static*fromNumber(n){yield*I(n).toContinued()}static*fromFraction(n,e){yield*(e===void 0?I(n):I(n).div(I(e))).toContinued()}static*fromTerms(n){yield*n}static*PHI(){for(;;)yield 1}static*FOUR_OVER_PI(){yield{a:1,b:0};for(let n=1;;n++)yield{a:2,b:(n*2-1)**2}}static*PI(){yield{a:3,b:0};for(let n=1;;n++)yield{a:6,b:(n*2-1)**2}}static*E(){yield 2;for(let n=1;;n++)yield 1,yield 2*n,yield 1}static toArray(n,e=10){V(e);let i=M(n),s=[];for(;s.length<e;){let r=i.next();if(r.done)break;s.push(r.value)}return s}static*convergents(n){let e=M(n),i=e.next();if(i.done)return;let s=I(P(i.value)),r=I(1),l=I(1),d=I(0);for(yield s.div(r);;){let g=e.next();if(g.done)return;let h=I(P(g.value)),w=I(X(g.value));[l,s]=[s,h.mul(s).add(w.mul(l))],[d,r]=[r,h.mul(r).add(w.mul(d))],yield s.div(r)}}static eval(n,e=10){if(V(e),e===0)return I(0);let i=I(0),s=0;for(let r of t.convergents(n))if(i=r,s++,s===e)break;return i}},Y=x;return z(J);})();
|
|
8
|
+
globalThis.ContinuedFraction = ContinuedFractionExports.default;
|