Rational approximation with bounded denominator
Rational approximation to a floating point number with bounded denominator.
Uses the Mediant Method.
This module also provides an implementation of the continued fraction method as described by Aberth in "A method for exact computation with rational numbers".
$ npm install frac
In the browser:
The script will manipulate
module.exports if available (e.g. in a CommonJS
require context). This is not always desirable. To prevent the behavior,
frac function takes three arguments:
xthe number we wish to approximate
Dthe maximum denominator
mixedif true, return a mixed fraction; if false, improper
The return value is an array of the form
[quot, num, den] where
for improper fractions.
quot <= x for mixed fractions, which may lead to some
unexpected results when rendering negative numbers.
> // var frac = require('frac'); // uncomment this line if in node> frac(Math.PI,100); // [ 0, 22, 7 ]> frac(Math.PI,100,true); // [ 3, 1, 7 ]> frac(-Math.PI,100); // [ 0, -22, 7 ]> frac(-Math.PI,100,true); // [ -4, 6, 7 ] // the approximation is (-4) + (6/7)
frac.cont implements the Aberth algorithm (input and output specifications
match the original
make test will run the node-based tests.
Tests generated from Excel have 4 columns. To produce a similar test:
Please consult the attached LICENSE file for details. All rights not explicitly granted by the Apache 2.0 license are reserved by the Original Author.