Number stored as numerator and denominator
[::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)␤»
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 depending on whether the original is negative, zero, or positive, respectively.
multi method Bool(Rational: --> Bool)
Returns the number, converted to
method Int(Rational: --> Int)
method Num(Rational: --> Num)
method ceiling(Rational: --> Int)
method floor(Rational: --> Int)
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(Rational: --> NuT)
Returns the numerator.
method denominator(Rational: --> DeT)
Returns the denominator.
method nude(Rational: --> Positional)
Returns a list of the numerator and denominator.
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(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
Returns a Range object that represents the range of values supported.