Skip to content

Commit

Permalink
Tidy Int and Rat.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Jun 12, 2010
1 parent e22e80f commit 20a512b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
9 changes: 3 additions & 6 deletions src/core/Int.pm
Expand Up @@ -33,9 +33,10 @@ augment class Int does Real {
our Num method Num() {
pir::box__PN(pir::set__NP(self));
}
}

# Next has been moved to Rat.pm for the moment.
# our Rat multi method Rat() { Rat.new(self, 1); }
our multi sub prefix:<->(Int $a) {
upgrade_to_num_if_needed(pir::neg__NN($a))
}

our multi sub infix:<+>(Int $a, Int $b) {
Expand Down Expand Up @@ -69,7 +70,3 @@ our multi sub infix:<**>(Int $a, Int $b) {
pir::pow__NNN($a, $b)
}
}

our multi sub prefix:<->(Int $a) {
upgrade_to_num_if_needed(pir::neg__NN($a))
}
12 changes: 6 additions & 6 deletions src/core/Rat.pm
Expand Up @@ -19,6 +19,8 @@ class Rat is Cool does Real {
self.bless(*, :numerator($numerator), :denominator($denominator));
}

multi method nude() { $.numerator, $.denominator; }

multi method ACCEPTS($other) {
self.Num.ACCEPTS($other);
}
Expand All @@ -39,8 +41,6 @@ class Rat is Cool does Real {
!! $!numerator.Num / $!denominator.Num;
}

multi method nude() { $.numerator, $.denominator; }

method succ {
Rat.new($!numerator + $!denominator, $!denominator);
}
Expand All @@ -50,6 +50,10 @@ class Rat is Cool does Real {
}
}

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

multi sub infix:<+>(Rat $a, Rat $b) {
my $gcd = pir::gcd__iii($a.denominator, $b.denominator);
($a.numerator * ($b.denominator div $gcd) + $b.numerator * ($a.denominator div $gcd))
Expand Down Expand Up @@ -78,10 +82,6 @@ multi sub infix:<->(Int $a, Rat $b) {
($a * $b.denominator - $b.numerator) / $b.denominator;
}

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

multi sub infix:<*>(Rat $a, Rat $b) {
($a.numerator * $b.numerator) / ($a.denominator * $b.denominator);
}
Expand Down

0 comments on commit 20a512b

Please sign in to comment.