Skip to content

Commit

Permalink
Make .round (no parameters) about 3x as fast
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Oct 18, 2014
1 parent 3448cb1 commit ab01b9e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/core/Int.pm
Expand Up @@ -69,6 +69,7 @@ my class Int does Real { # declared in BOOTSTRAP
method ceiling(Int:D:) { self }
proto method round(|) {*}
multi method round(Int:D:) { self }
multi method round(Int:D: 1) { self }
multi method round(Int:D: $scale as Real) { (self / $scale + 1/2).floor * $scale }

method lsb(Int:D:) {
Expand Down
14 changes: 12 additions & 2 deletions src/core/Real.pm
Expand Up @@ -38,8 +38,18 @@ my role Real does Numeric {
method acotanh() { self.Bridge.acotanh }
method floor() { self.Bridge.floor }
method ceiling() { self.Bridge.ceiling }
# cannot use '0.5' here, because Rat isn't initialized yet
method round($scale as Real = 1) { (self / $scale + 1/2).floor * $scale }

proto method round(|) { * }
multi method round(Real:D:) {
(self + 1/2).floor; # Rat NYI here, so no .5
}
multi method round(Real:D: 1) {
(self + 1/2).floor;
}
multi method round(Real:D: $scale as Real) {
(self / $scale + 1/2).floor * $scale;
}

method unpolar(Real $angle) {
Complex.new(self * $angle.cos, self * $angle.sin);
}
Expand Down

0 comments on commit ab01b9e

Please sign in to comment.