2013-12-14 07:11:37 +00:00
|
|
|
# frac
|
|
|
|
|
|
|
|
Rational approximation to a floating point number with bounded denominator.
|
|
|
|
|
2015-04-22 04:14:03 +00:00
|
|
|
Uses the [Mediant Method](https://en.wikipedia.org/wiki/Mediant_method).
|
2013-12-14 07:11:37 +00:00
|
|
|
|
2013-12-25 04:06:06 +00:00
|
|
|
This module also provides an implementation of the continued fraction method as
|
2015-04-22 04:14:03 +00:00
|
|
|
described by Aberth in "A method for exact computation with rational numbers".
|
2013-12-25 04:06:06 +00:00
|
|
|
|
2015-04-22 04:14:03 +00:00
|
|
|
## Installation
|
2013-12-14 07:11:37 +00:00
|
|
|
|
2015-05-05 06:19:23 +00:00
|
|
|
### JS
|
|
|
|
|
2015-04-22 04:14:03 +00:00
|
|
|
With [npm](https://www.npmjs.org/package/frac):
|
2013-12-14 07:11:37 +00:00
|
|
|
|
|
|
|
$ npm install frac
|
|
|
|
|
|
|
|
In the browser:
|
|
|
|
|
|
|
|
<script src="frac.js"></script>
|
|
|
|
|
2014-05-01 03:21:53 +00:00
|
|
|
The script will manipulate `module.exports` if available (e.g. in a CommonJS
|
|
|
|
`require` context). This is not always desirable. To prevent the behavior,
|
|
|
|
define `DO_NOT_EXPORT_FRAC`
|
|
|
|
|
2015-05-05 06:19:23 +00:00
|
|
|
### Python
|
|
|
|
|
|
|
|
From [PyPI](https://pypi.python.org/pypi/frac):
|
|
|
|
|
|
|
|
$ pip install frac
|
|
|
|
|
2014-05-01 03:21:53 +00:00
|
|
|
## Usage
|
|
|
|
|
2015-05-05 06:19:23 +00:00
|
|
|
In all cases, the relevant function takes 3 arguments:
|
2013-12-14 07:11:37 +00:00
|
|
|
|
|
|
|
- `x` the number we wish to approximate
|
|
|
|
- `D` the maximum denominator
|
2015-04-22 04:14:03 +00:00
|
|
|
- `mixed` if true, return a mixed fraction; if false, improper
|
2013-12-14 07:11:37 +00:00
|
|
|
|
|
|
|
The return value is an array of the form `[quot, num, den]` where `quot==0`
|
2015-04-22 04:14:03 +00:00
|
|
|
for improper fractions. `quot <= x` for mixed fractions, which may lead to some
|
|
|
|
unexpected results when rendering negative numbers.
|
2013-12-14 07:11:37 +00:00
|
|
|
|
2015-05-05 06:19:23 +00:00
|
|
|
### JS
|
|
|
|
|
|
|
|
The exported `frac` function implements the Mediant method.
|
|
|
|
|
|
|
|
`frac.cont` implements the Aberth algorithm
|
|
|
|
|
2013-12-14 07:11:37 +00:00
|
|
|
For example:
|
|
|
|
|
2015-05-05 06:19:23 +00:00
|
|
|
```js
|
2013-12-14 07:11:37 +00:00
|
|
|
> // var frac = require('frac'); // uncomment this line if in node
|
2015-05-05 06:19:23 +00:00
|
|
|
> 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
|
2013-12-14 07:11:37 +00:00
|
|
|
```
|
2013-12-25 04:06:06 +00:00
|
|
|
|
2015-05-05 06:19:23 +00:00
|
|
|
|
|
|
|
### Python
|
|
|
|
|
|
|
|
`frac.med` implements Mediant method.
|
|
|
|
|
|
|
|
`frac.cont` implements Aberth algorithm
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
```py
|
|
|
|
>>> import frac
|
|
|
|
>>> frac.med(1.3, 9) # [ 0, 9, 7 ]
|
|
|
|
>>> frac.med(1.3, 9, True) # [ 1, 2, 7 ]
|
|
|
|
>>> frac.med(-1.3, 9) # [ 0, -9, 7 ]
|
|
|
|
>>> frac.med(-1.3, 9, True) # [ -2, 5, 7 ]
|
|
|
|
|
|
|
|
>>> frac.cont(1.3, 9) # [ 0, 4, 3 ]
|
|
|
|
>>> frac.cont(1.3, 9, True) # [ 1, 1, 3 ]
|
|
|
|
>>> frac.cont(-1.3, 9) # [ 0, -4, 3 ]
|
|
|
|
>>> frac.cont(-1.3, 9, True) # [ -2, 2, 3 ]
|
|
|
|
```
|
2014-01-09 09:01:35 +00:00
|
|
|
|
2015-04-22 04:14:03 +00:00
|
|
|
## Testing
|
2014-05-01 03:21:53 +00:00
|
|
|
|
2015-04-22 04:14:03 +00:00
|
|
|
`make test` will run the node-based tests.
|
2014-01-09 09:01:35 +00:00
|
|
|
|
|
|
|
Tests generated from Excel have 4 columns. To produce a similar test:
|
|
|
|
|
|
|
|
- Column A contains the raw values
|
|
|
|
- Column B format "Up to one digit (1/4)"
|
|
|
|
- Column C format "Up to two digits (21/25)"
|
|
|
|
- Column D format "Up to three digits (312/943)"
|
|
|
|
|
2015-04-22 04:14:03 +00:00
|
|
|
## License
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
## Badges
|
|
|
|
|
2014-05-01 02:32:25 +00:00
|
|
|
[![Build Status](https://travis-ci.org/SheetJS/frac.svg?branch=master)](https://travis-ci.org/SheetJS/frac)
|
|
|
|
|
2015-04-22 04:14:03 +00:00
|
|
|
[![Coverage Status](http://img.shields.io/coveralls/SheetJS/frac/master.svg)](https://coveralls.io/r/SheetJS/frac?branch=master)
|
2014-05-01 02:32:25 +00:00
|
|
|
|
2015-05-05 06:19:23 +00:00
|
|
|
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/frac?pixel)](https://github.com/SheetJS/frac)
|