|
| 1 | +# ContinuedFraction.js |
| 2 | + |
| 3 | +[](https://npmjs.org/package/continuedfraction.js "View this project on npm") |
| 4 | +[](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. |
0 commit comments