Skip to content

Commit a95231b

Browse files
committed
initial commit
0 parents  commit a95231b

10 files changed

+891
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_
2+
.DS_Store
3+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Robert Eisele
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# ContinuedFraction.js
2+
3+
[![NPM Package](https://img.shields.io/npm/v/continuedfraction.js.svg?style=flat)](https://npmjs.org/package/continuedfraction.js "View this project on npm")
4+
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
5+
6+
7+
A lightweight JavaScript library sitting on the shoulders of [Fraction.js](https://github.com/rawify/Fraction.js) for generating and evaluating **standard** and **generalized** continued fractions. It is built with generator-based sequences and convergent recurrences for fast execution.
8+
9+
## Features
10+
11+
- Generate the simple continued‑fraction expansion of √N
12+
- Convert any real number or rational to its continued‑fraction
13+
- Infinite generators for classic constants: φ (golden ratio), e, π, 4/π
14+
- Evaluate (simple or generalized) continued fractions to a `Fraction`
15+
16+
## Example
17+
18+
```js
19+
const frac = ContinuedFraction.eval(
20+
ContinuedFraction.fromFraction(3021, 203),
21+
10 // Max Steps
22+
);
23+
console.log(frac.toString()); // → "3021/203"
24+
```
25+
26+
## Installation
27+
28+
You can install `ContinuedFraction.js` via npm:
29+
30+
```bash
31+
npm install continuedfraction.js
32+
```
33+
34+
Or with yarn:
35+
36+
```bash
37+
yarn add continuedfraction.js
38+
```
39+
40+
Alternatively, download or clone the repository:
41+
42+
```bash
43+
git clone https://github.com/rawify/ContinuedFraction.js
44+
```
45+
46+
## Usage
47+
48+
Include the `continuedfraction.min.js` file in your project:
49+
50+
```html
51+
<script src="path/to/continuedfraction.min.js"></script>
52+
<script>
53+
var x = ContinuedFraction.sqrt(2);
54+
</script>
55+
```
56+
57+
Or in a Node.js project:
58+
59+
```javascript
60+
const ContinuedFraction = require('continuedfraction.js');
61+
```
62+
63+
or
64+
65+
```javascript
66+
import ContinuedFraction from 'continuedfraction.js';
67+
```
68+
69+
## ContinuedFraction API
70+
71+
Import the static utility class and use its generators and evaluator:
72+
73+
```javascript
74+
// 1) Continued‑fraction terms of √23
75+
const sqrtGen = ContinuedFraction.sqrt(23);
76+
console.log(sqrtGen.next().value); // 4
77+
console.log(sqrtGen.next().value); // 1, 3, 1, 8, …
78+
79+
// 2) Continued‑fraction of a decimal
80+
let cnt = 0;
81+
for (let piCf of ContinuedFraction.fromNumber(Math.PI)) {
82+
console.log(piCf);
83+
if (cnt++ >= 10) break;
84+
}
85+
86+
// 3) Golden ratio φ terms
87+
const phiGen = ContinuedFraction.PHI();
88+
console.log(phiGen.next().value); // 1, and forever 1…
89+
90+
// 4) Evaluate the first 10 terms of e’s CF to a Fraction
91+
const approxE = ContinuedFraction.eval(ContinuedFraction.E, 10);
92+
console.log(approxE.toString()); // e ≈ “193/71”
93+
94+
// 5) Generalized CF for π, 4/π
95+
const genPi = ContinuedFraction.PI();
96+
console.log(genPi.next().value); // { a: 3, b: 0 }
97+
console.log(genPi.next().value); // { a: 6, b: 1 }
98+
99+
// 6) Continued‑fraction from two integers
100+
const halfCf = ContinuedFraction.fromFraction(1, 2);
101+
console.log([...halfCf]); // [0, 2]
102+
```
103+
104+
### Methods
105+
106+
| Method | Signature | Description |
107+
| ------------------------ | ----------------------------------------------------------------- | ----------------------------------------------------------------------- |
108+
| `sqrt(N: number)` | `Generator<number>` | Simple CF terms of √N |
109+
| `fromNumber(n)` | `Generator<number>` | CF terms of any real via Fraction.js |
110+
| `fromFraction(a, b)` | `Generator<number>` | CF terms of the rational a/b |
111+
| `PHI()` | `Generator<number>` | Infinite 1’s for the golden ratio |
112+
| `FOUR_OVER_PI()` | `Generator<CFTerm>` | Generalized CF terms for 4/π |
113+
| `PI()` | `Generator<CFTerm>` | Generalized CF terms for π |
114+
| `E()` | `Generator<number>` | CF expansion of e |
115+
| `eval(generator, steps)` | `(Generator|() => Generator, steps?: number) ⇒ Fraction` | Evaluate (generalized) continued fraction to a `Fraction` approximation |
116+
117+
## Coding Style
118+
119+
As every library I publish, ContinuedFraction is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.
120+
121+
## Building the library
122+
123+
After cloning the Git repository run:
124+
125+
```
126+
npm install
127+
npm run build
128+
```
129+
130+
## Copyright and Licensing
131+
132+
Copyright (c) 2025, [Robert Eisele](https://raw.org/)
133+
Licensed under the MIT license.

continuedfraction.d.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
import type Fraction from 'fraction.js';
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+
*/
24+
static sqrt(N: number): Generator<number, void, unknown>;
25+
26+
/**
27+
* Generator for continued‐fraction terms of any real number via Fraction.js.
28+
* @param n The real number to convert (as number, string, or bigint)
29+
* @yields Next continued‐fraction term of n
30+
*/
31+
static fromNumber(n: number | string | bigint): Generator<number, void, unknown>;
32+
33+
/**
34+
* Generator for continued‐fraction terms of a rational a/b via Fraction.js.
35+
* @param a Numerator (number, string, bigint, or Fraction)
36+
* @param b Denominator (number, string, or bigint)
37+
* @yields Next continued‐fraction term of a/b
38+
*/
39+
static fromFraction(
40+
a: number | string | bigint | Fraction,
41+
b: number | string | bigint
42+
): Generator<number, void, unknown>;
43+
44+
/**
45+
* Infinite generator of the golden ratio φ = [1; 1, 1, 1, …].
46+
* @yields Always 1
47+
*/
48+
static PHI(): Generator<number, void, unknown>;
49+
50+
/**
51+
* Infinite generator for the generalized continued‐fraction terms of 4/π.
52+
* @yields Term pairs { a, b } for the continued‐fraction
53+
*/
54+
static FOUR_OVER_PI(): Generator<CFTerm, void, unknown>;
55+
56+
/**
57+
* Infinite generator for the generalized continued‐fraction terms of π.
58+
* @yields Term pairs { a, b } for the continued‐fraction
59+
*/
60+
static PI(): Generator<CFTerm, void, unknown>;
61+
62+
/**
63+
* Infinite generator of e = [2; 1, 2, 1, 1, 4, 1, …].
64+
* @yields Next continued‐fraction term of e
65+
*/
66+
static E(): Generator<number, void, unknown>;
67+
68+
/**
69+
* Evaluate a (simple or generalized) continued fraction generator.
70+
* @param generator A CF term generator or a function returning one
71+
* @param steps Number of terms to include (default 10)
72+
* @returns Rational approximation as a Fraction
73+
*/
74+
static eval(
75+
generator:
76+
| Generator<number | CFTerm, any, any>
77+
| (() => Generator<number | CFTerm, any, any>),
78+
steps?: number
79+
): Fraction;
80+
}

0 commit comments

Comments
 (0)