Socket
Book a DemoInstallSign in
Socket

fractions-math

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fractions-math

Implementing fractions module from The Python Standard Library on TypeScript

1.3.2
latest
Source
npmnpm
Version published
Weekly downloads
111
-20.71%
Maintainers
1
Weekly downloads
 
Created
Source

fractions-math

version size downloads license donate

Implementing fractions module from The Python Standard Library on TypeScript.

Install

npm i fractions-math
yarn add fractions-math

Usage

import { Fraction, fraq } from "fractions-math"

const f1 = new Fraction(1, 2)
const f2 = fraq(1.5)
const f3 = f1.add(f2).toString() // -> "2" (0.5 + 1.5)
const f4 = f3.mul(f2).toString() // -> "3" (2 * 1.5)
const f5 = f4.div(f1).toString() // -> "6" (3 / 0.5)

API

type Fraq = Fraction | [number, number] | number | string

fraq(val: Fraq)

Helper function to create fraction from various inputs.

fraq(2).toPair() // -> [2, 1]
fraq([2, 1]).toPair() // -> [2, 1]
fraq(0.5).toPair() // -> [1, 2]
fraq("1/2").toPair() // -> [1, 2]

new Fraction(n: number, d = 1, reduce = false)

Creates a fraction taking two numbers – numenator and denominator. Denominator has default value 1. By default, the fraction is reduced to the minimum form.

new Fraction(1, 2).toPair() // -> [1, 2]
new Fraction(2).toPair() // -> [2, 1]
new Fraction(5, 10).toPair() // -> [5, 10]
new Fraction(5, 10, true).toPair() // -> [1, 2]

.toString()

fraq(1, 2).toString() // -> "1/2"
fraq(3, 3).toString() // -> "3"
fraq(-1).toString() // -> "-1"
fraq(1, -2).toString() // -> "-1/2"

.toNumber()

fraq(1, 2).toString() // -> 0.5

.reduce()

fraq(5, 10).reduce().toPair() // -> [1, 2]

.limit(max: number = 10_000)

Finds the closest Fraction that has denominator at most max.

fraq(Math.PI).limit(1000).toString() // -> "355/113"
fraq(1.1).limit().toString() // -> "11/10"

.add(b: Fraq)

fraq(1, 2).add([-1, 2]).toString() // -> "0"
fraq(1, 2).add(1).toString() // -> "3/2"

.sub(b: Fraq)

fraq(1, 2).sub([1, 2]).toString() // -> "0"
fraq(1, 2).sub(1).toString() // -> "-1/2"

.mul(b: Fraq)

fraq(1, 2).mul([1, 2]).toString() // -> "1/4"
fraq(1, 2).mul(1.5).toString() // -> "3/4"

.div(b: Fraq)

fraq(1, 2).div([1, 2]).toString() // -> "1"
fraq(1, 2).div(1.5).toString() // -> "1/3"
fraq(1, 2).div(0).toString() // throws Error

.eq(b: Fraq)

fraq(1, 2).eq(0.5) // -> true
fraq(1, 2).eq([1, 3]) // -> false

.lt(b: Fraq) -> boolean

.lte(b: Fraq) -> boolean

.gt(b: Fraq) -> boolean

.gte(b: Fraq) -> boolean

.toAscii(limit=16)

fraq(0.5).toUnicode() // -> "1/2"
fraq([1, 64]).toUnicode() // -> "0"
fraq([1, 64]).toUnicode(64) // -> "1/64"

.toUnicode()

fraq(0.5).toUnicode() // -> "½"
fraq([1, 64]).toUnicode() // -> "0"
fraq([1, 64]).toUnicode(64) // -> "¹⁄₆₄"

Keywords

arithmetic

FAQs

Package last updated on 09 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.