[::NuT, ::DeT] does Real

`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.

does Rational[UInt] ;my Positive = Positive.new(1,3);say ; # OUTPUT: «0.333333»my Positive =Positive.new(-2,3); # OUTPUT: «Type check failed in binding to parameter 'nu'; expected UInt but got Int (-2)»

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§

method new(NuT , DeT --> Rational)

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§

multi method Bool(Rational: --> 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§

method Bridge()

Returns the number, converted to `Num`

.

## method Int§

method Int(Rational: --> 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§

method Num(Rational: --> 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§

method ceiling(Rational: --> Int)

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

.

## method floor§

method floor(Rational: --> Int)

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

.

## method isNaN§

method isNaN(Rational: --> Bool)

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§

method numerator(Rational: --> NuT)

Returns the numerator.

## method denominator§

method denominator(Rational: --> DeT)

Returns the denominator.

## method nude§

method nude(Rational: --> Positional)

Returns a list of the numerator and denominator.

## method norm§

method norm(Rational: --> Rational)

**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.

use v6.c;my Rational = 3/0;say .norm.raku; # OUTPUT: «<1/0>»

say ; # OUTPUT: «Attempt to divide by zero when coercing Rational to Str

## method base-repeating§

method base-repeating(Rational: Int() = 10)

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:

my (, ) = (19/3).base-repeating(10);say ; # OUTPUT: «6.»say ; # OUTPUT: «3»printf '%s(%s)', , ; # OUTPUT: «6.(3)»

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

If no repetition occurs, the second string is empty:

say (5/2).base-repeating(10).raku; # OUTPUT: «("2.5", "")»

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.