Permalink
Browse files

Implement Complex.ceiling, .floor, .round to fix inf recursion

This seemed better than trying to work around the lack of these methods
in Complex.  This works identically to Mathematica, (other than midpoint
rounding, where they round down).
  • Loading branch information...
1 parent b9869c2 commit 7192039c5674dd69c348374867127fa826971f25 @labster labster committed Jun 25, 2013
Showing with 16 additions and 2 deletions.
  1. +14 −0 src/core/Complex.pm
  2. +1 −1 src/core/Int.pm
  3. +1 −1 src/core/Real.pm
View
@@ -186,6 +186,20 @@ my class Complex is Cool does Numeric {
method acotanh(Complex:D:) {
(1 / self).atanh;
}
+
+ method floor(Complex:D:) {
+ Complex.new( self.re.floor, self.im.floor );
+ }
+
+ method ceiling(Complex:D:) {
+ Complex.new( self.re.ceiling, self.im.ceiling );
+ }
+
+ proto method round(|) {*}
+ multi method round(Complex:D: Real $scale = 1) {
+ Complex.new( self.re.round($scale), self.im.round($scale) );
+ }
+
}
multi sub prefix:<->(Complex:D \a) returns Complex:D {
View
@@ -66,7 +66,7 @@ my class Int does Real {
method ceiling(Int:D:) { self }
proto method round(|) {*}
multi method round(Int:D:) { self }
- multi method round(Int:D: $scale) { (self / $scale + 1/2).floor * $scale }
+ multi method round(Int:D: Real $scale) { (self / $scale + 1/2).floor * $scale }
method lsb(Int:D:) {
return Nil if self == 0;
View
@@ -39,7 +39,7 @@ my role Real does Numeric {
method floor() { self.Bridge.floor }
method ceiling() { self.Bridge.ceiling }
# cannot use '0.5' here, because Rat isn't initialized yet
- method round($scale = 1) { (self / $scale + 1/2).floor * $scale }
+ method round(Real $scale = 1) { (self / $scale + 1/2).floor * $scale }
method unpolar(Real $angle) {
Complex.new(self * $angle.cos, self * $angle.sin);
}

0 comments on commit 7192039

Please sign in to comment.