Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
preliminary work to get both Rat and FatRat working
Fails just one test that does (2/3) but SomeRole
  • Loading branch information
moritz committed Feb 12, 2012
1 parent cd5136e commit 2685a77
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/core/Rat.pm
@@ -1,5 +1,7 @@
# XXX: should also be Cool, but attributes and MI don't seem to mix yet
my class Rat is Real {
my Int $UINT64_UPPER = nqp::pow_I(2, 64, Num, Int);
subset UInt64 of Int where { 0 <= $_ < $UINT64_UPPER }

my role Rational is Real {
has Int $.numerator;
has Int $.denominator;

Expand Down Expand Up @@ -55,6 +57,31 @@ my class Rat is Real {
}
}

# XXX: should also be Cool
my class Rat does Rational { }
my class FatRat does Rational { }

sub DIVIDE_NUMBERS(Int:D \$nu, Int:D \$de, $t1, $t2) {
my Int $gcd := $nu gcd $de;
my Int $numerator = $nu div $gcd;
my Int $denominator = $de div $gcd;
if $denominator < 0 {
$numerator = -$numerator;
$denominator = -$denominator;
}
if nqp::istype($nu, FatRat) || nqp::istype($de, FatRat) {
my $r := nqp::create(FatRat);
nqp::bindattr($r, FatRat, '$!numerator', nqp::p6decont($numerator));
nqp::bindattr($r, FatRat, '$!denominator', nqp::p6decont($denominator));
} elsif $denominator <= $UINT64_UPPER {
my $r := nqp::create(Rat);
nqp::bindattr($r, Rat, '$!numerator', nqp::p6decont($numerator));
nqp::bindattr($r, Rat, '$!denominator', nqp::p6decont($denominator));
} else {

}
}

multi prefix:<->(Rat \$a) {
Rat.new(-$a.numerator, $a.denominator);
}
Expand Down

0 comments on commit 2685a77

Please sign in to comment.