number/rational#

Classes#

Rational#

Defined in: number/rational.ts:52

An exact fraction with a positive denominator, in lowest terms.

Properties#

denominator#

readonly denominator: bigint

Defined in: number/rational.ts:54

numerator#

readonly numerator: bigint

Defined in: number/rational.ts:53

Methods#

add()#

add(other): Rational

Defined in: number/rational.ts:91

Parameters#
other#

Rational

Returns#

Rational

compare()#

compare(other): -1 | 0 | 1

Defined in: number/rational.ts:129

-1, 0 or 1 as this value is below, equal to or above other.

Parameters#
other#

Rational

Returns#

-1 | 0 | 1

divide()#

divide(other): Rational

Defined in: number/rational.ts:113

Parameters#
other#

Rational

Returns#

Rational

Throws#

if other is zero.

equals()#

equals(other): boolean

Defined in: number/rational.ts:134

Parameters#
other#

Rational

Returns#

boolean

floor()#

floor(): bigint

Defined in: number/rational.ts:121

The largest integer not above this value.

Returns#

bigint

multiply()#

multiply(other): Rational

Defined in: number/rational.ts:105

Parameters#
other#

Rational

Returns#

Rational

subtract()#

subtract(other): Rational

Defined in: number/rational.ts:98

Parameters#
other#

Rational

Returns#

Rational

toFraction()#

toFraction(): readonly [bigint, bigint]

Defined in: number/rational.ts:139

The Fraction pair [numerator, denominator].

Returns#

readonly [bigint, bigint]

toString()#

toString(): string

Defined in: number/rational.ts:143

Returns#

string

fromParts()#

static fromParts(numerator, denominator): Rational

Defined in: number/rational.ts:84

Normalize a numerator and denominator.

Parameters#
numerator#

bigint

denominator#

bigint

Returns#

Rational

of()#

static of(value): Rational

Defined in: number/rational.ts:68

Build a normalized rational from a Fraction pair, an integer or a decimal integer string.

Parameters#
value#

RationalInput

Returns#

Rational

Throws#

if a component is a JavaScript number that is not a safe integer, or the string is not a decimal integer.

Throws#

if the denominator is zero.

Type Aliases#

IntegerInput#

IntegerInput = bigint | number

Defined in: number/rational.ts:10

An integer a Rational may be built from: a bigint or a safe integer.


RationalInput#

RationalInput = IntegerInput | string | readonly [IntegerInput, IntegerInput] | Rational

Defined in: number/rational.ts:13

Anything a Rational may be built from.

Functions#

gcd()#

gcd(left, right): bigint

Defined in: number/rational.ts:44

The greatest common divisor of two integers, never negative; 0 for two zeros.

Parameters#

left#

bigint

Returns#

bigint


roundHalfEven()#

roundHalfEven(numerator, denominator): bigint

Defined in: number/rational.ts:34

The integer nearest to numerator / denominator, a tie going to the even one, as Python’s round.

Parameters#

numerator#

bigint

denominator#

bigint

Returns#

bigint

Throws#

if denominator is not positive.


toBigInt()#

toBigInt(value): bigint

Defined in: number/rational.ts:22

Read an integer, refusing every number that is not a safe integer.

Parameters#

value#

IntegerInput

Returns#

bigint