Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
conversions between Rat and FatRat
  • Loading branch information
moritz committed Feb 13, 2012
1 parent bdf0434 commit 85196e7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/core/Rat.pm
Expand Up @@ -40,7 +40,6 @@ my role Rational is Real {
method Int() { $!numerator div $!denominator }

method Bridge() { self.Num }
method Rat(Rat:D: Real $?) { self }
multi method Str(Rat:D:) {
self.Num.Str
}
Expand All @@ -56,9 +55,20 @@ my role Rational is Real {
}
}

my class FatRat { ... }
# XXX: should also be Cool
my class Rat does Rational { }
my class FatRat does Rational { }
my class Rat does Rational {
method Rat (Rat:D: Real $?) { self }
method FatRat(Rat:D: Real $?) { FatRat.new($.numerator, $.denominator); }
}
my class FatRat does Rational {
method FatRat(FatRat:D: Real $?) { self }
method Rat (FatRat:D: Real $?) {
$.denominator < $UINT64_UPPER
?? Rat.new($.numerator, $.denominator)
!! fail "Cannot convert from FatRat to Rat because denominator is too big";
}
}

sub DIVIDE_NUMBERS(Int:D \$nu, Int:D \$de, $t1, $t2) {
my Int $gcd := $nu gcd $de;
Expand All @@ -79,6 +89,7 @@ sub DIVIDE_NUMBERS(Int:D \$nu, Int:D \$de, $t1, $t2) {
nqp::bindattr($r, Rat, '$!denominator', nqp::p6decont($denominator));
$r;
} else {
# TODO: be smarter here if both integers are big
nqp::p6box_n(nqp::div_n(
nqp::tonum_I(nqp::p6decont $numerator),
nqp::tonum_I(nqp::p6decont $denominator)
Expand Down

0 comments on commit 85196e7

Please sign in to comment.