Skip to content

Commit

Permalink
Minor tweak to Complex.abs, added Real.exp and Real.infix:<**>, remov…
Browse files Browse the repository at this point in the history
…ed Num.exp.
  • Loading branch information
colomon committed Apr 24, 2010
1 parent a1159c7 commit 543d672
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/core/Complex.pm
Expand Up @@ -13,8 +13,8 @@ class Complex does Numeric is Cool {
($topic.Num ~~ $.re) && ($.im == 0);
}

method abs() {
($!re * $!re + $!im * $!im).sqrt
method abs(Complex $x:) {
($x.re * $x.re + $x.im * $x.im).sqrt
}

multi method Complex() { self }
Expand Down
6 changes: 3 additions & 3 deletions src/core/Num.pm
Expand Up @@ -67,9 +67,9 @@ augment class Num does Real {
($signum * $b) / $d;
}
multi method exp() {
pir::exp__Nn(self);
}
# multi method exp() {
# pir::exp__Nn(self);
# }

multi method log() {
pir::ln__Nn(self);
Expand Down
26 changes: 18 additions & 8 deletions src/core/Numeric.pm
Expand Up @@ -3,20 +3,30 @@ role Numeric {
self;
}

method abs(Numeric $x:) {
note "$.WHAT() needs a version of .abs";
fail "$.WHAT() needs a version of .abs";
}

multi method exp(Numeric $exponent: Numeric $base = e) {
note "$.WHAT() needs a version of .exp";
fail "$.WHAT() needs a version of .exp";
}

method log10(Numeric $x:) {
self.log(10);
}

INIT {
our @trig-base-conversions = (1.0, pi / 180.0, pi / 200.0, 2.0 * pi);
}

# Used by the :Trig subs and methods in the Int and Num classes.
method to-radians($base) {
self * pir::get_global__Ps('@trig-base-conversions')[$base];
}

method from-radians($base) {
self / pir::get_global__Ps('@trig-base-conversions')[$base];
method to-radians(Numeric $x: $base) {
$x * pir::get_global__Ps('@trig-base-conversions')[$base];
}

method log10(Numeric $x:) {
self.log(10);
method from-radians(Numeric $x: $base) {
$x / pir::get_global__Ps('@trig-base-conversions')[$base];
}
}
12 changes: 12 additions & 0 deletions src/core/Real.pm
Expand Up @@ -9,6 +9,10 @@ role Real does Numeric {
$x < 0 ?? -$x !! $x;
}

method exp(Real $exponent: Real $base = e) {
$base ** $exponent;
}

method sign(Real $x:) {
$x.notdef ?? Mu
!! ($x ~~ NaN ?? NaN !! $x <=> 0);
Expand Down Expand Up @@ -96,3 +100,11 @@ multi sub infix:</>(Real $a, Real $b) {
multi sub infix:</>(Num $a, Num $b) {
pir::div__NNN($a, $b)
}

multi sub infix:<**>(Real $a, Real $b) {
$a.Bridge ** $b.Bridge;
}

multi sub infix:<**>(Num $a, Num $b) {
pir::pow__NNN($a, $b)
}

0 comments on commit 543d672

Please sign in to comment.