ContinuedFraction.js is published as continuedfraction.js. It generates simple and generalized continued fractions and evaluates a bounded number of terms to an exact Fraction.js convergent.
Use it to inspect continued-fraction terms or construct rational approximations of square roots, real values, rational values, φ, e, π, and 4/π. Use Fraction.js directly when the input is already rational and no term sequence is needed. It is not an arbitrary-precision transcendental-function package: each result is a rational convergent chosen by the requested term count.
- Generate the simple continued‑fraction expansion of √N
- Convert any real number or rational to its continued‑fraction
- Infinite generators for classic constants: φ (golden ratio), e, π, 4/π
- Evaluate (simple or generalized) continued fractions to a
Fraction - Enumerate exact convergents or collect a bounded number of terms
- Strict TypeScript types for terms, sources, and coefficients
import ContinuedFraction from 'continuedfraction.js';
const frac = ContinuedFraction.eval(
ContinuedFraction.fromFraction(3021, 203),
10
);
console.log(frac.toFraction()); // "3021/203"You can install ContinuedFraction.js via npm:
npm install continuedfraction.jsOr with yarn:
yarn add continuedfraction.jsAlternatively, download or clone the repository:
git clone https://github.com/rawify/ContinuedFraction.jsContinuedFraction is a static class and cannot be instantiated. Use its methods directly.
const ContinuedFraction = require('continuedfraction.js');
const terms = ContinuedFraction.toArray(ContinuedFraction.sqrt(2), 8);import ContinuedFraction, { ContinuedFraction as NamedContinuedFraction } from 'continuedfraction.js';
const terms = ContinuedFraction.toArray(ContinuedFraction.sqrt(2), 8);<script src="https://cdn.jsdelivr.net/npm/continuedfraction.js@0.1.0/dist/continuedfraction.min.js"></script>
<script>
const terms = ContinuedFraction.toArray(ContinuedFraction.sqrt(2), 8);
</script><script type="module">
import ContinuedFraction from 'https://cdn.jsdelivr.net/npm/continuedfraction.js@0.1.0/dist/continuedfraction.mjs';
const terms = ContinuedFraction.toArray(ContinuedFraction.sqrt(2), 8);
</script>The package supports Node.js 20 or newer. Its CommonJS build uses the declared Fraction.js dependency; the ESM and standalone browser builds are self-contained. CommonJS also exposes .default and .ContinuedFraction aliases for compatibility.
sqrt() generates the periodic simple continued fraction; eval() consumes at most the requested number of terms.
import ContinuedFraction from 'continuedfraction.js';
const approximation = ContinuedFraction.eval(
ContinuedFraction.sqrt(2),
8
);
console.log(approximation.toFraction()); // "577/408"
console.log(approximation.valueOf()); // 1.4142156862745099sqrt(N) expects a non-negative safe integer and throws RangeError otherwise. Perfect squares terminate after one term; non-squares produce an infinite generator.
Rational expansions terminate. Fraction.js represents their terms as BigInt, so convert them before JSON serialization when the values are known to fit safely in Number.
import ContinuedFraction from 'continuedfraction.js';
const terms = [...ContinuedFraction.fromFraction(415, 93)];
const restored = ContinuedFraction.eval(
ContinuedFraction.fromFraction(415, 93),
10
);
console.log(terms.map(Number)); // [4, 2, 6, 7]
console.log(restored.toFraction()); // "415/93"Do not convert large BigInt terms to Number unless they are within the safe-integer range.
Generator functions can be passed directly to eval(). This creates a new generator for each evaluation.
import ContinuedFraction from 'continuedfraction.js';
const e = ContinuedFraction.eval(ContinuedFraction.E, 10);
const squareTerms = [...ContinuedFraction.sqrt(49)];
console.log(e.toFraction()); // "1457/536"
console.log(squareTerms.map(Number)); // [7]Always bound iteration over E(), PHI(), PI(), FOUR_OVER_PI(), or a non-square sqrt() generator. Spreading an infinite generator never completes.
convergents() yields exact Fraction.js values lazily. fromTerms() turns an existing sequence into a generator, while toArray() safely collects a bounded prefix.
import ContinuedFraction from 'continuedfraction.js';
const convergents = ContinuedFraction.toArray(
ContinuedFraction.convergents(
ContinuedFraction.fromTerms([1, 2, 2, 2])
),
4
);
console.log(convergents.map((value) => value.toFraction()));
// ["1", "3/2", "7/5", "17/12"]Import the static utility class and use its generators and evaluator:
// 1) Continued‑fraction terms of √23
const sqrtGen = ContinuedFraction.sqrt(23);
console.log(sqrtGen.next().value); // 4
console.log(sqrtGen.next().value); // 1, 3, 1, 8, …// 2) Continued‑fraction of a decimal
let cnt = 0;
for (let piCf of ContinuedFraction.fromNumber(Math.PI)) {
console.log(piCf);
if (cnt++ >= 10) break;
}// 3) Golden ratio φ terms
const phiGen = ContinuedFraction.PHI();
console.log(phiGen.next().value); // 1, and forever 1…// 4) Evaluate the first 10 terms of e’s CF to a Fraction
const approxE = ContinuedFraction.eval(ContinuedFraction.E, 10);
console.log(approxE.toFraction()); // "1457/536"// 5) Generalized CF for π, 4/π
const genPi = ContinuedFraction.PI();
console.log(genPi.next().value); // { a: 3, b: 0 }
console.log(genPi.next().value); // { a: 6, b: 1 }// 6) Continued‑fraction from two integers
const halfCf = ContinuedFraction.fromFraction(1, 2);
console.log([...halfCf]); // [0n, 2n]| Method | Signature | Description |
|---|---|---|
sqrt(N: number) |
Generator<number> |
Simple CF terms of √N |
fromNumber(n) |
Generator<bigint> |
CF terms of any real via Fraction.js |
fromFraction(a, b?) |
Generator<bigint> |
CF terms of a rational value or a/b |
fromTerms(terms) |
Generator<Term> |
Yield an existing finite or infinite term sequence |
PHI() |
Generator<number> |
Infinite 1’s for the golden ratio |
FOUR_OVER_PI() |
Generator<CFTerm> |
Generalized CF terms for 4/π |
PI() |
Generator<CFTerm> |
Generalized CF terms for π |
E() |
Generator<number> |
CF expansion of e |
toArray(source, steps) |
Term[] |
Collect at most steps terms; the default is 10 |
convergents(source) |
Generator<Fraction> |
Yield every exact convergent lazily |
eval(source, steps) |
Fraction |
Evaluate up to steps terms; the default is 10 |
The source is strict TypeScript. The build emits CommonJS, ESM, a standalone browser bundle, source maps, and format-specific declarations.
After cloning the Git repository, run:
npm install
npm run buildRun all runtime and type-level tests with:
npm testCopyright (c) 2026, Robert Eisele Licensed under the MIT license.