Skip to content

Commit

Permalink
Make Range.sum sensible for -Inf/Inf endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Nov 1, 2017
1 parent 87e8720 commit 5eeb72a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/core/Range.pm
Expand Up @@ -391,7 +391,7 @@ my class Range is Cool does Iterable does Positional {
($from = $!min.floor + $!excludes-min),
($to = $!max.floor - ($!excludes-max && $!max.Int == $!max))
),
(die "Cannot determine integer bounds")
Failure.new("Cannot determine integer bounds")
)
)
}
Expand Down Expand Up @@ -649,9 +649,15 @@ my class Range is Cool does Iterable does Positional {
}

method sum() is nodal {
my ($start,$stop) = self.int-bounds || nextsame;
my $elems = 0 max $stop - $start + 1;
($start + $stop) * $elems div 2;
self.int-bounds(my $start, my $stop)
?? ($start + $stop) * (0 max $stop - $start + 1) div 2
!! $!min == -Inf
?? $!max == Inf
?? 0
!! -Inf
!! $!max == Inf
?? Inf
!! nextsame
}

method rand() {
Expand Down

0 comments on commit 5eeb72a

Please sign in to comment.