Skip to content

Commit

Permalink
Try to rework abs to return the same type passed in.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Dec 8, 2009
1 parent fcdef68 commit ed63968
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/core/Any-num.pm
@@ -1,7 +1,6 @@
augment class Any {

method abs() is export {
pir::abs__Nn(self);
multi method abs() {
self.Num.abs;
}

multi method exp() {
Expand Down Expand Up @@ -174,7 +173,8 @@ augment class Any {
}
}

our multi sub abs($x) { (+$x).abs }
our proto sub abs($x) { $x.abs }
our multi sub prefix:<abs>($x) { $x.abs }
our proto sub exp($exponent) { $exponent.exp }
our proto sub log($x) { $x.log }
our proto sub log10($x) { $x.log10 }
Expand Down
2 changes: 0 additions & 2 deletions src/core/Complex.pm
Expand Up @@ -234,8 +234,6 @@ class Complex {
}
}
multi sub abs(Complex $x) { $x.abs }
multi sub infix:<+>(Complex $a, Complex $b) {
Complex.new($a.re + $b.re, $a.im + $b.im);
}
Expand Down
4 changes: 1 addition & 3 deletions src/core/Int.pm
Expand Up @@ -15,7 +15,7 @@ our sub upgrade_to_num_if_needed($test) {

augment class Int {
multi method abs() {
pir::box__PI(pir::abs__II(self))
self < 0 ?? -self !! self;
}

our Int multi method Int() { self }
Expand Down Expand Up @@ -45,8 +45,6 @@ augment class Int {
}
}

our multi sub abs(Int $x) { $x.abs }

our multi sub infix:<+>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::add__NNN($a, $b))
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/Num.pm
Expand Up @@ -11,6 +11,10 @@ augment class Num {
multi method Num() { self; }
multi method abs() {
pir::abs__Nn(self);
}

multi method exp() {
pir::exp__Nn(self);
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/Rat.pm
Expand Up @@ -52,6 +52,10 @@ class Rat {
Rat.new($!numerator - $!denominator, $!denominator);
}

multi method abs() {
self < 0 ?? -self !! self;
}

our Int multi method sign {
# self ~~ NaN ?? NaN !! self <=> 0;
self < 0 ?? -1 !! ( self == 0 ?? 0 !! 1);
Expand Down

0 comments on commit ed63968

Please sign in to comment.