Skip to content

Commit

Permalink
add overflow/underflow failures to **
Browse files Browse the repository at this point in the history
  • Loading branch information
TimToady committed Dec 4, 2015
1 parent 446ea7b commit 6779524
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -2018,6 +2018,14 @@ my class X::Numeric::DivideByZero is Exception {
}
}

my class X::Numeric::Overflow is Exception {
method message() { "Numeric overflow" }
}

my class X::Numeric::Underflow is Exception {
method message() { "Numeric underflow" }
}

my class X::Numeric::Confused is Exception {
has $.num;
has $.base;
Expand Down
2 changes: 2 additions & 0 deletions src/core/Int.pm
Expand Up @@ -258,10 +258,12 @@ multi sub infix:<%>(int $a, int $b) returns int {
multi sub infix:<**>(Int:D \a, Int:D \b) {
b >= 0 ?? nqp::pow_I(nqp::decont(a), nqp::decont(b), Num, Int)
!! 1 / nqp::pow_I(nqp::decont(a), nqp::decont(-b), Num, Int)
or a == 0 ?? 0 !! fail X::Numeric::Overflow.new;
}

multi sub infix:<**>(int $a, int $b) returns int {
nqp::pow_i($a, $b)
or $a == 0 ?? 0 !! fail X::Numeric::Overflow.new;
}

multi sub infix:<lcm>(Int:D \a, Int:D \b) returns Int {
Expand Down
3 changes: 3 additions & 0 deletions src/core/Num.pm
Expand Up @@ -350,11 +350,14 @@ multi sub infix:<%>(num $a, num $b) {
nqp::mod_n($a, $b)
}

# (If we get 0 here, must be underflow, since floating overflow provides Inf.)
multi sub infix:<**>(Num:D \a, Num:D \b) {
nqp::p6box_n(nqp::pow_n(nqp::unbox_n(a), nqp::unbox_n(b)))
or a == 0e0 || b.abs == Inf ?? 0e0 !! fail X::Numeric::Underflow.new;
}
multi sub infix:<**>(num $a, num $b) {
nqp::pow_n($a, $b)
or $a == 0e0 || $b.abs == Inf ?? 0e0 !! fail X::Numeric::Underflow.new;
}

# Here we sort NaN in with string "NaN"
Expand Down
1 change: 1 addition & 0 deletions src/core/Rat.pm
Expand Up @@ -213,6 +213,7 @@ multi sub infix:<**>(Rational \a, Int \b) {
a.numerator ** -b,
a,
b
or a == 0.0 ?? 0.0 !! fail X::Numeric::Overflow.new; # can this happen?
}

multi sub infix:<==>(Rational:D \a, Rational:D \b) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/core_prologue.pm
Expand Up @@ -6,6 +6,8 @@ my class WhateverCode { ... }
my class Cursor { ... }
my class Failure { ... }
my class Rakudo::Internals { ... }
my class X::Numeric::Overflow { ... }
my class X::Numeric::Underflow { ... }

# Stub these or we can't use any sigil other than $.
my role Positional { ... }
Expand Down

0 comments on commit 6779524

Please sign in to comment.