yetanotherfractionlibrary

1.2.2 • Public • Published

Yet Another Fraction Library

Tests License: Unlicense

This is a library for dealing with fractions directly instead of decimal numbers.

Features

  • [x] Fractions are represented as a string containing numerator and denominator.
  • [x] Fractions can be simplified.
  • [x] Fractions can be inverted.
  • [x] Fractions can be added, subtracted, multiplied, and divided.
  • [x] Fractions can be raised to an integer power.
  • [x] Fractions can be compared.
  • [x] Fractions can be converted to decimal numbers.
  • [x] Decimal numbers can be converted to fractions.
  • [ ] Fractions can be raised to a fractional power.

Installation

yafl has no production dependencies.

To install yafl, run the following command:

npm install --production yetanotherfractionlibrary

Usage

FractionString

A string representing a fraction.

'1/2'
'5/3'
'3/1'

isFraction(<FractionString>) -> Boolean

Returns true if <FractionString> is a valid fraction.

isFraction('1/2') // true
isFraction('1/0') // false
isFraction('sdf/sdf') // false

reduce(<FractionString | Number>) -> FractionString

Reduces a fraction to its simplest form.

reduce('2/4') // '1/2'

add(<FractionString | Number>, <FractionString | Number>) -> FractionString

Adds two fractions.

add('1/2', '1/4') // '3/4'

sub(<FractionString | Number>, <FractionString | Number>) -> FractionString

Subtracts two fractions.

sub('5/6', '1/2') // '2/6'

mul(<FractionString | Number>, <FractionString | Number>) -> FractionString

Multiplies two fractions.

mul('1/2', '1/4') // '1/8'

div(<FractionString | Number>, <FractionString | Number>) -> FractionString

Divides two fractions.

div('1/2', '1/4') // '2'

pow(<FractionString | Number>, <Number>) -> FractionString

Raises a fraction to a power.

pow('1/2', 2) // '1/4'

inv(<FractionString | Number>) -> FractionString

Inverts a fraction.

inv('1/2') // '2/1'

compare(<FractionString | Number>, <FractionString | Number>) -> Number

Compares two fractions, returning -1 if the first fraction is less than the second, 0 if they are equal, and 1 if the first fraction is greater than the second.

compare('1/2', '3/4') // -1
compare('1/2', '1/2') // 0
compare('1/2', '1/4') // 1

toDecimal(<FractionString>) -> Number

Converts a fraction to a decimal number.

toDecimal('1/2') // 0.5

toFraction(<Number>) -> FractionString

Converts a decimal number to a fraction.

toFraction(0.5) // '1/2'

Package Sidebar

Install

npm i yetanotherfractionlibrary

Weekly Downloads

0

Version

1.2.2

License

Unlicense

Unpacked Size

8.85 kB

Total Files

5

Last publish

Collaborators

  • susul-1312