GithubHelp home page GithubHelp logo

vladkens / fractions-math Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 43 KB

๐Ÿ๐Ÿ”ข Implementing fractions module from The Python Standard Library on TypeScript.

Home Page: https://npm.im/fractions-math

License: MIT License

TypeScript 100.00%
cjs esm fractions npm-modules rational-numbers printer unicode math

fractions-math's Introduction

fractions-math

Implementing fractions module from The Python Standard Library on TypeScript.

Install

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) // -> "ยนโ„โ‚†โ‚„"

fractions-math's People

Contributors

vladkens avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.