Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make Inf|-Inf|NaN.Rat.Num round-trippable
- Inf.Rat gives 1/0
- -Inf.Rat gives -1/0
- NaN.Rat gives 0/0
- (1/0).Num gives Inf
- (-1/0).Num gives -Inf
- (0/0).Num gives NaN

As proposed at http://irclog.perlgeek.de/perl6-dev/2016-05-21#i_12521558
  • Loading branch information
lizmat committed May 22, 2016
1 parent 947ed63 commit 1f3ca64
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/core/Num.pm
Expand Up @@ -36,9 +36,8 @@ my class Num does Real { # declared in BOOTSTRAP
}

method Rat(Num:D: Real $epsilon = 1.0e-6, :$fat) {
if nqp::isnanorinf(nqp::unbox_n(self)) {
return self;
}
return self == Inf ?? 1/0 !! self == -Inf ?? -1/0 !! 0/0
if nqp::isnanorinf(nqp::unbox_n(self));

my Num $num = self;
$num = -$num if (my int $signum = $num < 0);
Expand Down
2 changes: 1 addition & 1 deletion src/core/Rational.pm
Expand Up @@ -32,7 +32,7 @@ my role Rational[::NuT, ::DeT] does Real {
method nude() { self.REDUCE-ME; $!numerator, $!denominator }
method Num() {
$!denominator == 0
?? ($!numerator < 0 ?? -Inf !! Inf)
?? ($!numerator == 0 ?? NaN !! $!numerator < 0 ?? -Inf !! Inf)
!! nqp::p6box_n(nqp::div_In(
nqp::decont($!numerator),
nqp::decont($!denominator)
Expand Down

0 comments on commit 1f3ca64

Please sign in to comment.