Skip to content

Repository files navigation

ContinuedFraction.js

NPM Package MIT license

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.

Features

  • 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

Quick example

import ContinuedFraction from 'continuedfraction.js';

const frac = ContinuedFraction.eval(
  ContinuedFraction.fromFraction(3021, 203),
  10
);
console.log(frac.toFraction()); // "3021/203"

Installation

You can install ContinuedFraction.js via npm:

npm install continuedfraction.js

Or with yarn:

yarn add continuedfraction.js

Alternatively, download or clone the repository:

git clone https://github.com/rawify/ContinuedFraction.js

Usage and runtime

ContinuedFraction is a static class and cannot be instantiated. Use its methods directly.

CommonJS

const ContinuedFraction = require('continuedfraction.js');
const terms = ContinuedFraction.toArray(ContinuedFraction.sqrt(2), 8);

ES modules

import ContinuedFraction, { ContinuedFraction as NamedContinuedFraction } from 'continuedfraction.js';
const terms = ContinuedFraction.toArray(ContinuedFraction.sqrt(2), 8);

Standalone browser script

<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>

Native browser module

<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.

Recipes

Approximate an irrational square root

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.4142156862745099

sqrt(N) expects a non-negative safe integer and throws RangeError otherwise. Perfect squares terminate after one term; non-squares produce an infinite generator.

Inspect and reconstruct a rational value

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.

Build convergents for e and a perfect square

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.

Inspect every convergent

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"]

ContinuedFraction API

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]

Methods

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

Building the library

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 build

Run all runtime and type-level tests with:

npm test

Copyright and Licensing

Copyright (c) 2026, Robert Eisele Licensed under the MIT license.

About

Generator-based simple and generalized continued fractions with exact Fraction.js convergents

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages