{"id":19337686,"url":"https://github.com/sheetjs/frac","last_synced_at":"2025-04-06T09:08:38.956Z","repository":{"id":8966693,"uuid":"10707952","full_name":"SheetJS/frac","owner":"SheetJS","description":":heavy_division_sign: rational approximation with bounded denominator","archived":false,"fork":false,"pushed_at":"2020-05-13T21:16:57.000Z","size":33997,"stargazers_count":72,"open_issues_count":0,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-29T22:37:40.199Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://oss.sheetjs.com/frac/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SheetJS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-15T15:22:27.000Z","updated_at":"2024-05-01T05:29:16.000Z","dependencies_parsed_at":"2022-09-06T01:11:46.686Z","dependency_job_id":null,"html_url":"https://github.com/SheetJS/frac","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SheetJS%2Ffrac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SheetJS%2Ffrac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SheetJS%2Ffrac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SheetJS%2Ffrac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SheetJS","download_url":"https://codeload.github.com/SheetJS/frac/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457802,"owners_count":20941906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T03:15:10.968Z","updated_at":"2025-04-06T09:08:38.939Z","avatar_url":"https://github.com/SheetJS.png","language":"JavaScript","readme":"# frac\n\nRational approximation to a floating point number with bounded denominator.\n\nUses the [Mediant Method](https://en.wikipedia.org/wiki/Mediant_method).\n\nThis module also provides an implementation of the continued fraction method as\ndescribed by Aberth in \"A method for exact computation with rational numbers\".\nThe algorithm is used in \u003ca href=\"http://sheetjs.com\"\u003eSheetJS Libraries\u003c/a\u003e to\nreplicate fraction formats.\n\n## Installation\n\n### JS\n\nWith [`npm`](https://www.npmjs.org/package/frac):\n\n```bash\n$ npm install frac\n```\n\nIn the browser:\n\n```html\n\u003cscript src=\"frac.js\"\u003e\u003c/script\u003e\n```\n\nThe script will manipulate `module.exports` if available .  This is not always\ndesirable.  To prevent the behavior, define `DO_NOT_EXPORT_FRAC`\n\n### Python\n\nFrom [`PyPI`](https://pypi.python.org/pypi/frac):\n\n```bash\n$ pip install frac\n```\n\n## Usage\n\nIn all cases, the relevant function takes 3 arguments:\n\n - `x` the number we wish to approximate\n - `D` the maximum denominator\n - `mixed` if true, return a mixed fraction; if false, improper\n\nThe return value is an array of the form `[quot, num, den]` where `quot==0`\nfor improper fractions.  `quot \u003c= x` for mixed fractions, which may lead to some\nunexpected results when rendering negative numbers.\n\n### JS\n\nThe exported `frac` function implements the Mediant method.\n\n`frac.cont` implements the Aberth algorithm\n\nFor example:\n\n```js\n\u003e // var frac = require('frac'); // uncomment this line if in node\n\u003e frac(1.3, 9);              // [  0,  9, 7 ] //  1.3 ~       9/7\n\u003e frac(1.3, 9, true);        // [  1,  2, 7 ] //  1.3 ~  1 +  2/7\n\u003e frac(-1.3, 9);             // [  0, -9, 7 ] // -1.3 ~      -9/7\n\u003e frac(-1.3, 9, true);       // [ -2,  5, 7 ] // -1.3 ~ -2 +  5/7\n\n\u003e frac.cont(1.3, 9);         // [  0,  4, 3 ] //  1.3 ~       4/3\n\u003e frac.cont(1.3, 9, true);   // [  1,  1, 3 ] //  1.3 ~  1 +  1/3\n\u003e frac.cont(-1.3, 9);        // [  0, -4, 3 ] // -1.3 ~      -4/3\n\u003e frac.cont(-1.3, 9, true);  // [ -2,  2, 3 ] // -1.3 ~ -2 +  2/3\n```\n\n\n### Python\n\n`frac.med` implements Mediant method.\n\n`frac.cont` implements Aberth algorithm.\n\nFor example:\n\n```py\n\u003e\u003e\u003e import frac\n\u003e\u003e\u003e frac.med(1.3, 9)         ## [  0,  9, 7 ] ##  1.3 ~       9/7\n\u003e\u003e\u003e frac.med(1.3, 9, True)   ## [  1,  2, 7 ] ##  1.3 ~  1 +  2/7\n\u003e\u003e\u003e frac.med(-1.3, 9)        ## [  0, -9, 7 ] ## -1.3 ~      -9/7\n\u003e\u003e\u003e frac.med(-1.3, 9, True)  ## [ -2,  5, 7 ] ## -1.3 ~ -2 +  5/7\n\n\u003e\u003e\u003e frac.cont(1.3, 9)        ## [  0,  4, 3 ] ##  1.3 ~       4/3\n\u003e\u003e\u003e frac.cont(1.3, 9, True)  ## [  1,  1, 3 ] ##  1.3 ~  1 +  1/3\n\u003e\u003e\u003e frac.cont(-1.3, 9)       ## [  0, -4, 3 ] ## -1.3 ~      -4/3\n\u003e\u003e\u003e frac.cont(-1.3, 9, True) ## [ -2,  2, 3 ] ## -1.3 ~ -2 +  2/3\n```\n\n## Testing\n\nThe test TSV baselines in the `test_files` directory have four columns:\n\n- Column A contains the raw values\n- Column B format \"Up to one digit (1/4)\" (`denominator = 9`)\n- Column C format \"Up to two digits (21/25)\" (`denominator = 99`)\n- Column D format \"Up to three digits (312/943)\" (`denominator = 999`)\n\n`make test` will run the node-based tests.\n\n`make ctest` will use `browserify` to build a standalone script that can be run\nin the web browser.  The transform `brfs` must be installed locally.  Browser\ntest script built against `browserfy@16.5.1` and `brfs@2.0.2`.\n\n`make pytest` will run the python tests against the system Python version.\n\n`make pypytest` will run the python tests against `pypy` if installed\n\n## License\n\nPlease consult the attached LICENSE file for details.  All rights not explicitly\ngranted by the Apache 2.0 License are reserved by the Original Author.\n\n## Badges\n\n[![Build Status](https://saucelabs.com/browser-matrix/frac.svg)](https://saucelabs.com/u/frac)\n\n[![Build Status](https://travis-ci.org/SheetJS/frac.svg?branch=master)](https://travis-ci.org/SheetJS/frac)\n\n[![Coverage Status](http://img.shields.io/coveralls/SheetJS/frac/master.svg)](https://coveralls.io/r/SheetJS/frac?branch=master)\n\n[![NPM Downloads](https://img.shields.io/npm/dt/frac.svg)](https://npmjs.org/package/frac)\n\n[![Dependencies Status](https://david-dm.org/sheetjs/frac/status.svg)](https://david-dm.org/sheetjs/frac)\n\n[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/frac?pixel)](https://github.com/SheetJS/frac)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheetjs%2Ffrac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsheetjs%2Ffrac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheetjs%2Ffrac/lists"}