 role Rational
ErrorsCollection

# role Rational

Number stored as numerator and denominator

`Rational` is the common role for numbers that are stored as pairs of numerator and denominator. It is parameterized by the types of the numerator (`NuT`) and denominator (`DeT`). By default, these are `Int`, but other types of `Rational` are possible by using a different parameterization. In addition, `Rational` objects are immutable throughout their life.

Please note that, since `DeT` is by default equal to `NuT`, in this case both are instantiated to `UInt`. Built into Raku are Rat and FatRat, which both do the `Rational` role.

# Methods

## method new

Creates a new rational object from numerator and denominator, which it normalizes to the lowest terms. The `\$denominator` can be zero, in which case the numerator is normalized to `-1`, `0`, or `1` depending on whether the original is negative, zero, or positive, respectively.

## method Bool

Returns `False` if numerator is `0`, otherwise returns `True`. This applies for `<0/0>` zero-denominator Rational as well, despite `?<0/0>.Num` being `True`.

## method Bridge

Returns the number, converted to `Num`.

## method Int

Coerces the invocant to Int by truncating non-whole portion of the represented number, if any. If the denominator is zero, will fail with `X::Numeric::DivideByZero`.

## method Num

Coerces the invocant to Num by dividing numerator by denominator. If denominator is `0`, returns `Inf`, `-Inf`, or `NaN`, based on whether numerator is a positive number, negative number, or `0`, respectively.

## method ceiling

Return the smallest integer not less than the invocant. If denominator is zero, fail with `X::Numeric::DivideByZero`.

## method floor

Return the largest integer not greater than the invocant. If denominator is zero, fail with `X::Numeric::DivideByZero`.

## method isNaN

Tests whether the invocant's Num value is a NaN, an acronym for Not available Number. That is both its numerator and denominator are zero.

## method numerator

Returns the numerator.

## method denominator

Returns the denominator.

## method nude

Returns a list of the numerator and denominator.

## method norm

DEPRECATED as of 6.d. The method is no longer needed, because as of 6.d language version, it's required for `Rational` type to be normalized on creation.

Returns a normalized Rational object, i.e. with positive denominator, and numerator and denominator coprime. The denominator can also by zero, but using it in any operation or a conversion to string will result in an exception.

## method base-repeating

Returns a list of two strings that, when concatenated, represent the number in base `\$base`. The second element is the one that repeats. For example:

19/3 is 6.333333... with the 3 repeating indefinitely.

If no repetition occurs, the second string is empty:

The precision for determining the repeating group is limited to 1000 characters, above that, the second string is `???`.

`\$base` defaults to `10`.

## method Range

Returns a Range object that represents the range of values supported.