EVA 5e91fae1cd 图标数据 | hace 1 año | |
---|---|---|
.. | ||
types | hace 1 año | |
LICENSE | hace 1 año | |
README.md | hace 1 año | |
frac.js | hace 1 año | |
package.json | hace 1 año |
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". The algorithm is used in SheetJS Libraries to replicate fraction formats.
With npm
:
$ npm install frac
In the browser:
<script src="frac.js"></script>
The script will manipulate module.exports
if available . This is not always
desirable. To prevent the behavior, define DO_NOT_EXPORT_FRAC
From PyPI
:
$ pip install frac
In all cases, the relevant function takes 3 arguments:
x
the number we wish to approximateD
the maximum denominatormixed
if true, return a mixed fraction; if false, improperThe return value is an array of the form [quot, num, den]
where quot==0
for improper fractions. quot <= x
for mixed fractions, which may lead to some
unexpected results when rendering negative numbers.
The exported frac
function implements the Mediant method.
frac.cont
implements the Aberth algorithm
For example:
> // var frac = require('frac'); // uncomment this line if in node
> frac(1.3, 9); // [ 0, 9, 7 ] // 1.3 ~ 9/7
> frac(1.3, 9, true); // [ 1, 2, 7 ] // 1.3 ~ 1 + 2/7
> frac(-1.3, 9); // [ 0, -9, 7 ] // -1.3 ~ -9/7
> frac(-1.3, 9, true); // [ -2, 5, 7 ] // -1.3 ~ -2 + 5/7
> frac.cont(1.3, 9); // [ 0, 4, 3 ] // 1.3 ~ 4/3
> frac.cont(1.3, 9, true); // [ 1, 1, 3 ] // 1.3 ~ 1 + 1/3
> frac.cont(-1.3, 9); // [ 0, -4, 3 ] // -1.3 ~ -4/3
> frac.cont(-1.3, 9, true); // [ -2, 2, 3 ] // -1.3 ~ -2 + 2/3
frac.med
implements Mediant method.
frac.cont
implements Aberth algorithm.
For example:
>>> import frac
>>> frac.med(1.3, 9) ## [ 0, 9, 7 ] ## 1.3 ~ 9/7
>>> frac.med(1.3, 9, True) ## [ 1, 2, 7 ] ## 1.3 ~ 1 + 2/7
>>> frac.med(-1.3, 9) ## [ 0, -9, 7 ] ## -1.3 ~ -9/7
>>> frac.med(-1.3, 9, True) ## [ -2, 5, 7 ] ## -1.3 ~ -2 + 5/7
>>> frac.cont(1.3, 9) ## [ 0, 4, 3 ] ## 1.3 ~ 4/3
>>> frac.cont(1.3, 9, True) ## [ 1, 1, 3 ] ## 1.3 ~ 1 + 1/3
>>> frac.cont(-1.3, 9) ## [ 0, -4, 3 ] ## -1.3 ~ -4/3
>>> frac.cont(-1.3, 9, True) ## [ -2, 2, 3 ] ## -1.3 ~ -2 + 2/3
The test TSV baselines in the test_files
directory have four columns:
denominator = 9
)denominator = 99
)denominator = 999
)make test
will run the node-based tests.
make pytest
will run the python tests against the system Python version.
make pypytest
will run the python tests against pypy
if installed
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.