Skip to content

beenotung/rational

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rational.js

rational number library

npm Package Version Minified Package Size Minified and Gzipped Package Size

Installation

## 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

> rational 3.14
157/50

Typescript Signatures

Core functions:

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): number

function normalize(x: rational): rational

// find the greatest common divisor
function gcd(a: number, b: number): number

Math functions:

function add(x: rational, y: rational): rational

function 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:

// convert rational into string in "a/b" format
function toString(rational: rational): string

License

This project is licensed with BSD-2-Clause

This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:

  • 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