Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

rawify/ContinuedFraction.js

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ContinuedFraction.js

NPM Package MIT license

A lightweight JavaScript library sitting on the shoulders of Fraction.js for generating and evaluating standard and generalized continued fractions. It is built with generator-based sequences and convergent recurrences for fast execution.

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

Example

const frac = ContinuedFraction.eval(
  ContinuedFraction.fromFraction(3021, 203),
  10 // Max Steps
);
console.log(frac.toString()); // → "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

Include the continuedfraction.min.js file in your project:

<script src="path/to/continuedfraction.min.js"></script>
<script>
  var x = ContinuedFraction.sqrt(2);
</script>

Or in a Node.js project:

const ContinuedFraction = require('continuedfraction.js');

or

import ContinuedFraction from 'continuedfraction.js';

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.toString()); // e ≈ “193/71”
// 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]); // [0, 2]

Methods

Method Signature Description
sqrt(N: number) Generator<number> Simple CF terms of √N
fromNumber(n) Generator<number> CF terms of any real via Fraction.js
fromFraction(a, b) Generator<number> CF terms of the rational a/b
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
eval(generator, steps) Generator (() => Generator, steps?: number) ⇒ Fraction Evaluate (generalized) continued fraction to a Fraction approximation

Coding Style

Like all my libraries, ContinuedFraction.js is written to minimize size after compression with Google Closure Compiler in advanced mode. The code style is optimized to maximize compressibility. If you extend the library, please preserve this style.

Building the library

After cloning the Git repository run:

npm install
npm run build

Copyright and Licensing

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

About

The RAW continued fractions library

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.