Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Numeric.Real, Numeric.Int, Numeric.Rat, Numeric.Num, and Real.Rea…
…l. Change Complex.Num to Complex.Real.
  • Loading branch information
colomon committed Jun 10, 2010
1 parent b461fd2 commit 16d9cb0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/core/Complex.pm
Expand Up @@ -13,22 +13,30 @@ class Complex does Numeric is Cool {
($topic.Num ~~ $.re) && ($.im == 0);
}

multi method Complex() { self }

method reals() {
(self.re, self.im);
}

method Real() {
if $!im == 0 {
$!re;
} else {
fail "You can only coerce a Complex to Real if the imaginary part is zero"
}
}

our Bool multi method Bool() { ( $!re != 0 || $!im != 0 ) ?? Bool::True !! Bool::False }

multi method perl() {
"Complex.new($.re, $.im)";
}
multi method Complex() { self }

method Str() {
"$.re + {$.im}i";
}

multi method perl() {
"Complex.new($.re, $.im)";
}

method abs(Complex $x:) {
($x.re * $x.re + $x.im * $x.im).sqrt
}
Expand Down Expand Up @@ -198,14 +206,6 @@ class Complex does Numeric is Cool {
%r = $P1
}
}
multi method Num {
if $!im == 0 {
$!re;
} else {
fail "You can only coerce a Complex to Num if the imaginary part is zero"
}
}
}
multi sub infix:<+>(Complex $a, Complex $b) {
Expand Down
20 changes: 20 additions & 0 deletions src/core/Numeric.pm
Expand Up @@ -3,6 +3,26 @@ role Numeric {
self;
}

# NOTE: Real is defined as failing if the number in
# question is not convertable to Real, eg a Complex
# with a non-zero imaginary part.
method Real() {
note "$.WHAT() needs a version of .Real";
fail "$.WHAT() needs a version of .Real";
}

method Int() {
self.Real.Int;
}

method Rat(::Real $epsilon = 1.0e-6) {
self.Real.Rat($epsilon);
}

method Num() {
self.Real.Num;
}

method reals() {
note "$.WHAT() needs a version of .reals";
fail "$.WHAT() needs a version of .reals";
Expand Down
4 changes: 4 additions & 0 deletions src/core/Real.pm
Expand Up @@ -5,6 +5,10 @@ role Real does Numeric {
fail "Bridge must be defined for the Real type " ~ self.WHAT;
}

method Real() {
self;
}

method Bool() {
self != 0 ?? Bool::True !! Bool::False;
}
Expand Down

0 comments on commit 16d9cb0

Please sign in to comment.