class Rat is Cool does Rational[Intuint64{ }

Rat objects store rational numbers as a pair of a numerator and denominator. Number literals with a dot but without exponent produce Rats.

say 3.1;          # OUTPUT: «3.1␤»      (same as: Rat.new(31, 10)) 
say 3.1.^name;    # OUTPUT: «Rat␤» 
say 3.1.nude;     # OUTPUT: «(31 10)␤» 
 
say <1/2>;        # OUTPUT: «0.5␤»      (same as: Rat.new(1, 2)) 
say <1/2>.^name;  # OUTPUT: «Rat␤» 
say <1/2>.nude;   # OUTPUT: «(1 2)␤»

Thus arithmetic with short dotted-decimal numbers does not suffer from floating point errors.

To prevent the numerator and denominator from becoming pathologically large, the denominator is limited to 64 bit storage. On overflow of the denominator during an arithmetic operation a Num (floating-point number) is returned instead. You can tune this behavior by setting the $*RAT-OVERFLOW dynamic variable.

For example this function crudely approximates a square root, and overflows the denominator quickly:

sub approx-sqrt($n$iterations{
    my $x = $n;
    $x = ($x + $n / $x/ 2 for ^$iterations;
    return $x;
}
say approx-sqrt(25).^name;     # OUTPUT: «Rat␤» 
say approx-sqrt(210).^name;    # OUTPUT: «Num␤»

If you want arbitrary precision arithmetic with rational numbers, use the FatRat type instead.

Rat objects are immutable.

Methods§

method raku§

multi method raku(Rat:D: --> Str:D)

Returns an implementation-specific string that produces an equivalent object when given to EVAL.

say (1/3).raku;                # OUTPUT: «<1/3>␤» 
say (2/4).raku;                # OUTPUT: «0.5␤» 

Typegraph§

Type relations for Rat
raku-type-graph Rat Rat Cool Cool Rat->Cool Rational Rational Rat->Rational Mu Mu Any Any Any->Mu Cool->Any Numeric Numeric Real Real Real->Numeric Rational->Real Stringy Stringy Str Str Str->Cool Str->Stringy Allomorph Allomorph Allomorph->Str RatStr RatStr RatStr->Rat RatStr->Allomorph

Expand chart above