Rules / Types, classes & roles / basic

Rational Reference

Number stored as numerator and denominator

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method new #

method new(NuT:D $numerator, DeT:D $denominator --> Rational:D)

Returns Rational:D.

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:D: --> Bool:D)

Returns Bool:D.

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:D: --> Int:D)

Returns Int:D.

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:D: --> Num:D)

Returns Num:D.

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:D: --> Int:D)

Returns Int:D.

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

method floor #

method floor(Rational:D: --> Int:D)

Returns Int:D.

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

method isNaN #

method isNaN(Rational:D: --> Bool:D)

Returns Bool:D.

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:D: --> NuT:D)

Returns NuT:D.

Returns the numerator.

method denominator #

method denominator(Rational:D: --> DeT:D)

Returns DeT:D.

Returns the denominator.

method nude #

method nude(Rational:D: --> Positional)

Returns Positional.

Returns a list of the numerator and denominator.

method norm #

method norm(Rational:D: --> Rational:D)

Returns Rational:D.

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 be zero, but using it in any operation or a conversion to string will result in an exception.

method base-repeating #

method base-repeating(Rational:D: Int:D() $base = 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: 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.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

5 no-output · 1 not-runnable · 3 ok

role Rational[::NuT, ::DeT] does Real { ... }

Not executed: the documentation states no expected output for this example.

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

Neither engine can run this in isolation — the example depends on context from the surrounding text.

=begin code :preamble<subset NuT of Int; subset DeT of Int>
=end code

Not executed: the documentation states no expected output for this example.

=begin code :preamble<subset NuT of Int; subset DeT of Int>
=end code

Not executed: the documentation states no expected output for this example.

=begin code :preamble<subset NuT of Int; subset DeT of Int>
=end code

Not executed: the documentation states no expected output for this example.

use v6.c;
my Rational $by-zero = 3/0;
say $by-zero.norm.raku; # OUTPUT: «<1/0>␤»
Output
<1/0>

Documentation, Rakudo and Raku++ all agree.

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

Not executed: the documentation states no expected output for this example.

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

Documentation, Rakudo and Raku++ all agree.

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

Documentation, Rakudo and Raku++ all agree.