rational approximation with bounded denominator
Go to file
2014-04-30 20:21:53 -07:00
dist version bump 1.0.0 2014-04-30 20:21:53 -07:00
test_files version bump 0.3.1: handling numbers >= 2**32 2014-01-12 01:44:07 -05:00
.npmignore version bump 0.3.1-a: coverage testing 2014-04-30 19:32:25 -07:00
.travis.yml version bump 0.3.1-a: coverage testing 2014-04-30 19:32:25 -07:00
frac.js version bump 1.0.0 2014-04-30 20:21:53 -07:00
frac.md version bump 1.0.0 2014-04-30 20:21:53 -07:00
LICENSE version bump 1.0.0 2014-04-30 20:21:53 -07:00
Makefile version bump 1.0.0 2014-04-30 20:21:53 -07:00
package.json version bump 1.0.0 2014-04-30 20:21:53 -07:00
README.md version bump 1.0.0 2014-04-30 20:21:53 -07:00
test.js version bump 0.3.1-a: coverage testing 2014-04-30 19:32:25 -07:00

frac

Rational approximation to a floating point number with bounded denominator.

Uses the Mediant Method https://en.wikipedia.org/wiki/Mediant_(mathematics)

This module also provides an implementation of the continued fraction method as described by Aberth in "A method for exact computation with rational numbers", which appears to be used by spreadsheet programs for displaying fractions

Setup

In node:

$ npm install frac

In the browser:

<script src="frac.js"></script>

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

Usage

The exported frac function takes three arguments:

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

The return value is an array of the form [quot, num, den] where quot==0 for improper fractions.

For example:

> // 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.cont implements the Aberth algorithm (input and output specifications match the original frac function)

License

Apache 2.0

Tests

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)"

Build Status

Coverage Status

githalytics.com alpha