Skip to content

Commit

Permalink
Make Real.sign always return an Int
Browse files Browse the repository at this point in the history
Except when the value was NaN, in which case it will return a NaN.
Added candidates for Num (to handle NaN) and Int (to make it almost
2x as fast).
  • Loading branch information
lizmat committed Apr 22, 2020
1 parent ec50b96 commit 42a303e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core.c/Int.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ my class Int does Real { # declared in BOOTSTRAP

method Int(--> Int) { self }

method sign(Int:D: --> Int:D) {
nqp::isgt_I(self,0) || nqp::neg_i(nqp::islt_I(self,0))
}

multi method Str(Int:D: --> Str:D) {
nqp::p6box_s(nqp::tostr_I(self));
}
Expand Down
6 changes: 6 additions & 0 deletions src/core.c/Num.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ my class Num does Real { # declared in BOOTSTRAP
!! nqp::fromnum_I(nqp::unbox_n(self),Int)
}

method sign(Num:D:) {
nqp::isnanorinf(self)
?? self == Inf ?? 1 !! self == -Inf ?? -1 !! NaN
!! self > 0 ?? 1 !! self < 0 ?? -1 !! 0
}

multi method new() { nqp::box_n(0e0, self) }
multi method new($n) { nqp::box_n($n.Num, self) }

Expand Down
2 changes: 1 addition & 1 deletion src/core.c/Real.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ my class Complex { ... }
my role Real does Numeric {
method Rat(Real:D: Real $epsilon = 1.0e-6) { self.Bridge.Rat($epsilon) }
method abs() { self < 0 ?? -self !! self }
method sign(Real:D:) { self > 0 ?? 1 !! self < 0 ?? -1 !! +self }
method sign(Real:D:) { self > 0 ?? 1 !! self < 0 ?? -1 !! 0 }
method conj(Real:D:) { self }
method sqrt() { self.Bridge.sqrt }
method rand() { self.Bridge.rand }
Expand Down

0 comments on commit 42a303e

Please sign in to comment.