frac
Rational approximation with bounded denominator
Last updated 7 years ago by sheetjs .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install frac 
SYNC missed versions from official npm registry.

frac

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.

Installation

JS

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

Python

From PyPI:

$ pip install frac

Usage

In all cases, the relevant function takes 3 arguments:

  • x the number we wish to approximate
  • D the maximum denominator
  • mixed if true, return a mixed fraction; if false, improper

The 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.

JS

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

Python

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

Testing

The test TSV baselines in the test_files directory have four columns:

  • Column A contains the raw values
  • Column B format "Up to one digit (1/4)" (denominator = 9)
  • Column C format "Up to two digits (21/25)" (denominator = 99)
  • Column D format "Up to three digits (312/943)" (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

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

Build Status

Build Status

Coverage Status

NPM Downloads

Dependencies Status

ghit.me

Analytics

Current Tags

  • 1.1.2                                ...           latest (7 years ago)

12 Versions

  • 1.1.2                                ...           7 years ago
  • 1.1.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.6                                ...           8 years ago
  • 1.0.5                                ...           8 years ago
  • 1.0.4                                ...           9 years ago
  • 1.0.2                                ...           10 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           11 years ago
  • 0.3.1                                ...           11 years ago
  • 0.3.0                                ...           11 years ago
  • 0.2.1                                ...           11 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (8)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |