diff options
| author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-03-12 14:18:02 +0000 |
|---|---|---|
| committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-03-12 14:18:02 +0000 |
| commit | 0179055d2d146d495b93c148e431cf103b55208f (patch) | |
| tree | 8823f2dafecd4990a45b9979ac4202d4c94c7dc4 | |
| parent | 398098555620384bc2796dbb3f977a81f640af39 (diff) | |
merge revision(s) 57688,57689: [Backport #13242]
rational.c: infinity in power
* rational.c (nurat_expt): return Infinity due to overflow.
[ruby-core:79686] [Bug #13242]:
rational.c: infinity in power
* rational.c (nurat_expt): return 0 due to overflow.
[ruby-core:79686] [Bug #13242]:
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@57911 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
| -rw-r--r-- | numeric.c | 2 | ||||
| -rw-r--r-- | rational.c | 8 | ||||
| -rw-r--r-- | test/ruby/test_rational.rb | 8 | ||||
| -rw-r--r-- | version.h | 2 |
4 files changed, 19 insertions, 1 deletions
@@ -3904,6 +3904,8 @@ int_pow(long x, unsigned long y) VALUE v; bignum: 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); return v; } diff --git a/rational.c b/rational.c index 8d05342ed5..460f82cfeb 100644 --- a/rational.c +++ b/rational.c @@ -1039,6 +1039,14 @@ nurat_expt(VALUE self, VALUE other) num = ONE; den = ONE; } + if (RB_FLOAT_TYPE_P(num)) { /* infinity due to overflow */ + if (RB_FLOAT_TYPE_P(den)) return DBL2NUM(NAN); + return num; + } + if (RB_FLOAT_TYPE_P(den)) { /* infinity due to overflow */ + num = ZERO; + den = ONE; + } return f_rational_new2(CLASS_OF(self), num, den); } } diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index e40de141b5..20989a572e 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -955,6 +955,14 @@ class Rational_Test < Test::Unit::TestCase assert_raise(ZeroDivisionError, bug5713) { Rational(0, 1) ** Rational(-2,3) } 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 + x = EnvUtil.suppress_warning {(1/4r)**40000000} + assert_equal 0, x, bug + end + def test_positive_p assert_predicate(1/2r, :positive?) assert_not_predicate(-1/2r, :positive?) @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.4.0" #define RUBY_RELEASE_DATE "2017-03-12" -#define RUBY_PATCHLEVEL 67 +#define RUBY_PATCHLEVEL 68 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 3 |
