From ab01b9ee59eb8855c2a98af1fa6896d27f0c0fb4 Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Sat, 18 Oct 2014 20:24:32 +0200 Subject: [PATCH] Make .round (no parameters) about 3x as fast --- src/core/Int.pm | 1 + src/core/Real.pm | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/core/Int.pm b/src/core/Int.pm index ad618ccde76..b0a67c30abb 100644 --- a/src/core/Int.pm +++ b/src/core/Int.pm @@ -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:) { diff --git a/src/core/Real.pm b/src/core/Real.pm index a9fcf72b7e8..02a198c38ff 100644 --- a/src/core/Real.pm +++ b/src/core/Real.pm @@ -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); }