Rat Reference
Rational number (limited-precision)
What it is #
Position in the hierarchy #
| Inherits from | — |
| Does | — |
| Inherited by | RatStr |
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 raku #
multi method raku(Rat:D: --> Str:D)
Returns Str:D.
Returns an implementation-specific string that produces an equivalent object when given to EVAL.
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.
1 no-output · 4 ok
class Rat is Cool does Rational[Int, uint64] { }Not executed: the documentation states no expected output for this example.
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)»
3.1
Rat
(31 10)
Documentation, Rakudo and Raku++ all agree.
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)»
0.5
Rat
(1 2)
Documentation, Rakudo and Raku++ all agree.
sub approx-sqrt($n, $iterations) {
my $x = $n;
$x = ($x + $n / $x) / 2 for ^$iterations;
return $x;
}
say approx-sqrt(2, 5).^name; # OUTPUT: «Rat»
say approx-sqrt(2, 10).^name; # OUTPUT: «Num»Rat
Num
Documentation, Rakudo and Raku++ all agree.
say (1/3).raku; # OUTPUT: «<1/3>» say (2/4).raku; # OUTPUT: «0.5»
<1/3>
0.5
Documentation, Rakudo and Raku++ all agree.