1.2.2 • Published 2 years ago

yetanotherfractionlibrary v1.2.2

Weekly downloads
-
License
Unlicense
Repository
github
Last release
2 years ago

Yet Another Fraction Library

Tests License: Unlicense

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

Features

  • Fractions are represented as a string containing numerator and denominator.
  • Fractions can be simplified.
  • Fractions can be inverted.
  • Fractions can be added, subtracted, multiplied, and divided.
  • Fractions can be raised to an integer power.
  • Fractions can be compared.
  • Fractions can be converted to decimal numbers.
  • 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'
1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago