https://github.com/beenotung/rational
rational number library
https://github.com/beenotung/rational
calculations fraction math rational-numbers typescript-library
Last synced: 2 months ago
JSON representation
rational number library
- Host: GitHub
- URL: https://github.com/beenotung/rational
- Owner: beenotung
- License: bsd-2-clause
- Created: 2018-10-08T16:05:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-14T07:26:27.000Z (over 1 year ago)
- Last Synced: 2025-07-02T01:16:21.451Z (3 months ago)
- Topics: calculations, fraction, math, rational-numbers, typescript-library
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/rational.js
- Size: 24.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rational.js
rational number library
[](https://www.npmjs.com/package/rational.js)
[](https://bundlephobia.com/package/rational.js)
[](https://bundlephobia.com/package/rational.js)## Installation
```bash
## for cli
npm install --global rational.js## for library usage
npm install rational.js
```## Functions
- rational :: f -> a/b where f is floating number, a,b are integers
- number :: rational -> number
- add
- minus
- multiply
- divide
- pow## Command Line Usage
```bash
> rational 3.14
157/50
```## Typescript Signatures
Core functions:
```typescript
type rational = [number, number]let MAX_ERROR: number // 1e-12
// convert floating point number into rational
function rational(f: number, maxError?: number): rational// convert rational into floating point number
function number(x: rational): numberfunction normalize(x: rational): rational
// find the greatest common divisor
function gcd(a: number, b: number): number
```Math functions:
```typescript
function add(x: rational, y: rational): rationalfunction minus(x: rational, y: rational): rational
function multiply(x: rational, y: rational): rational
function divide(x: rational, y: rational): rational
function pow(x: rational, y: rational): rational
```Format function:
```typescript
// convert rational into string in "a/b" format
function toString(rational: rational): string
```## License
This project is licensed with [BSD-2-Clause](./LICENSE)
This is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):
- The freedom to run the program as you wish, for any purpose
- The freedom to study how the program works, and change it so it does your computing as you wish
- The freedom to redistribute copies so you can help others
- The freedom to distribute copies of your modified versions to others