Options
All
  • Public
  • Public/Protected
  • All
Menu

A Rational number

This implementation uses bigint internally for the numerator and denominator.

A rational is immutable.

const x = Rational.of(2, 3);
x.mul(Rational.nat(3)).toDecimal(); // => "2.0"

Hierarchy

  • Rational

Index

Methods

add

asTuple

  • asTuple(): [bigint, bigint]
  • returns a tuple of the numerator and denominator

    Returns [bigint, bigint]

div

inverse

mul

negate

normalize

sub

toDecimal

  • toDecimal(__namedParameters?: { decimals: number; round: boolean; separator: string }): string
  • formats this rational as decimal string

    Parameters

    • Default value __namedParameters: { decimals: number; round: boolean; separator: string } = {}
      • decimals: number
      • round: boolean
      • separator: string

    Returns string

toNumber

  • toNumber(): number
  • converts this rational to a float

    Returns number

toString

  • toString(): string
  • formats this rational as string

    Returns string

Static fromDecimal

  • creates a new rational from the given decimal string

    const x = Rational.fromDecimal("13.37")

    Parameters

    • s: string

      the decimal string

    Returns Rational

Static nat

  • creates a new rational representing the given natural number

    const x = Rational.nat(42)

    Parameters

    • n: bigint | number

      the natural to represent as rational

    Returns Rational

Static of

  • of(n: bigint | number, d: bigint | number): Rational
  • creates a new rational with the given numerator and denominator

    Parameters

    • n: bigint | number

      the numerator

    • d: bigint | number

      the denominator

    Returns Rational

Legend

Generated using TypeDoc