# frac **Repository Path**: ArkTSCentralRepository/frac ## Basic Information - **Project Name**: frac - **Description**: frac 是一个用于将浮点数近似为有界分母的有理数的库,提供了通过中介法和Aberth的连续分数算法进行有理数近似的功能。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-11-21 - **Last Updated**: 2024-11-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # frac 基于[frac](https://www.npmjs.com/package/frac)原库1.1.2版本进行适配, 所有功能代码已经转换为`ArkTS`文件 ## Install ```sh ohpm install frac ``` ## Description Rational approximation to a floating point number with bounded denominator. Uses the [Mediant Method](https://en.wikipedia.org/wiki/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. ## 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. ### Example The exported `frac` function implements the Mediant method. `cont` implements the Aberth algorithm For example: ```typescript > // 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 > cont(1.3, 9); // [ 0, 4, 3 ] // 1.3 ~ 4/3 > cont(1.3, 9, true); // [ 1, 1, 3 ] // 1.3 ~ 1 + 1/3 > cont(-1.3, 9); // [ 0, -4, 3 ] // -1.3 ~ -4/3 > cont(-1.3, 9, true); // [ -2, 2, 3 ] // -1.3 ~ -2 + 2/3 ``` ## 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](https://saucelabs.com/browser-matrix/frac.svg)](https://saucelabs.com/u/frac) [![Build Status](https://travis-ci.org/SheetJS/frac.svg?branch=master)](https://travis-ci.org/SheetJS/frac) [![Coverage Status](http://img.shields.io/coveralls/SheetJS/frac/master.svg)](https://coveralls.io/r/SheetJS/frac?branch=master) [![NPM Downloads](https://img.shields.io/npm/dt/frac.svg)](https://npmjs.org/package/frac) [![Dependencies Status](https://david-dm.org/sheetjs/frac/status.svg)](https://david-dm.org/sheetjs/frac) [![ghit.me](https://ghit.me/badge.svg?repo=sheetjs/js-xlsx)](https://ghit.me/repo/sheetjs/js-xlsx) [![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/frac?pixel)](https://github.com/SheetJS/frac)