Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

37 lines (25 sloc) 1.01 KB
=begin pod
=TITLE class Rat
class Rat is Cool does Rational[Int, UInt64] { ... }
C<Rat> objects store rational numbers as a pair of a numerator and
denominator. Number literals with a dot but without exponent produce
3.1; #, 10)
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 denomniator
a C<Num> (floating-poing number) is returned instead.
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(2, 5).WHAT; # Rat()
say approx-sqrt(2, 10).WHAT; # Num()
If you want arbitrary precision arithmetic with rational numbers, use the
C<FatFat> type instead.
C<Rat> objects are immutable.
=end pod
Jump to Line
Something went wrong with that request. Please try again.