mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rational.c: infinity in power
* rational.c (nurat_expt): return Infinity due to overflow. [ruby-core:79686] [Bug #13242]: git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57688 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3f6aeba69a
commit
06010b2b05
3 changed files with 12 additions and 0 deletions
|
@ -3913,6 +3913,8 @@ int_pow(long x, unsigned long y)
|
||||||
VALUE v;
|
VALUE v;
|
||||||
bignum:
|
bignum:
|
||||||
v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
|
v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
|
||||||
|
if (RB_FLOAT_TYPE_P(v)) /* infinity due to overflow */
|
||||||
|
return v;
|
||||||
if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
|
if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1042,6 +1042,10 @@ nurat_expt(VALUE self, VALUE other)
|
||||||
num = ONE;
|
num = ONE;
|
||||||
den = ONE;
|
den = ONE;
|
||||||
}
|
}
|
||||||
|
if (RB_FLOAT_TYPE_P(num)) { /* infinity due to overflow */
|
||||||
|
if (RB_FLOAT_TYPE_P(den)) return DBL2NUM(NAN);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
return f_rational_new2(CLASS_OF(self), num, den);
|
return f_rational_new2(CLASS_OF(self), num, den);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -955,6 +955,12 @@ class Rational_Test < Test::Unit::TestCase
|
||||||
assert_raise(ZeroDivisionError, bug5713) { Rational(0, 1) ** Rational(-2,3) }
|
assert_raise(ZeroDivisionError, bug5713) { Rational(0, 1) ** Rational(-2,3) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_power_overflow
|
||||||
|
bug = '[ruby-core:79686] [Bug #13242]: Infinity due to overflow'
|
||||||
|
x = EnvUtil.suppress_warning {4r**40000000}
|
||||||
|
assert_predicate x, :infinite?, bug
|
||||||
|
end
|
||||||
|
|
||||||
def test_positive_p
|
def test_positive_p
|
||||||
assert_predicate(1/2r, :positive?)
|
assert_predicate(1/2r, :positive?)
|
||||||
assert_not_predicate(-1/2r, :positive?)
|
assert_not_predicate(-1/2r, :positive?)
|
||||||
|
|
Loading…
Add table
Reference in a new issue