From d22ce4a522141c2f6e847846944ddc1ec3a949f2 Mon Sep 17 00:00:00 2001 From: marcandre Date: Tue, 5 Feb 2013 05:39:49 +0000 Subject: * numeric.c (fix_pow): Handle special cases when base is 0, -1 or +1 [Bug #5713] [Bug #5715] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39064 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ numeric.c | 22 ++++++++++------------ test/ruby/test_fixnum.rb | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index d22edf6211..80ab00e595 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Feb 5 14:36:04 2013 Marc-Andre Lafortune + + * numeric.c (fix_pow): Handle special cases when base is 0, -1 or +1 + [Bug #5713] [Bug #5715] + + * rational.c (nurat_expt): ditto + Tue Feb 5 13:27:53 2013 Nobuyoshi Nakada * ext/io/console/console.c (rawmode_opt): use default values by `stty diff --git a/numeric.c b/numeric.c index 4ae5948cf0..cacccb9df6 100644 --- a/numeric.c +++ b/numeric.c @@ -2951,6 +2951,13 @@ fix_pow(VALUE x, VALUE y) if (FIXNUM_P(y)) { long b = FIX2LONG(y); + if (a == 1) return INT2FIX(1); + if (a == -1) { + if (b % 2 == 0) + return INT2FIX(1); + else + return INT2FIX(-1); + } if (b < 0) return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); @@ -2960,27 +2967,18 @@ fix_pow(VALUE x, VALUE y) if (b > 0) return INT2FIX(0); return DBL2NUM(INFINITY); } - if (a == 1) return INT2FIX(1); - if (a == -1) { - if (b % 2 == 0) - return INT2FIX(1); - else - return INT2FIX(-1); - } return int_pow(a, b); } switch (TYPE(y)) { case T_BIGNUM: - - if (negative_int_p(y)) - return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); - - if (a == 0) return INT2FIX(0); if (a == 1) return INT2FIX(1); if (a == -1) { if (int_even_p(y)) return INT2FIX(1); else return INT2FIX(-1); } + if (negative_int_p(y)) + return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); + if (a == 0) return INT2FIX(0); x = rb_int2big(FIX2LONG(x)); return rb_big_pow(x, y); case T_FLOAT: diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb index 4567da0a60..85c8bdfa7b 100644 --- a/test/ruby/test_fixnum.rb +++ b/test/ruby/test_fixnum.rb @@ -279,4 +279,23 @@ class TestFixnum < Test::Unit::TestCase def test_frozen assert_equal(true, 1.frozen?) end + + def assert_eql(a, b, mess) + assert a.eql?(b), "expected #{a} & #{b} to be eql? #{mess}" + end + + def test_power_of_1_and_minus_1 + bug5715 = '[ruby-core:41498]' + big = 1 << 66 + assert_eql 1, 1 ** -big , bug5715 + assert_eql 1, (-1) ** -big , bug5715 + assert_eql -1, (-1) ** -(big+1) , bug5715 + end + + def test_power_of_0 + bug5713 = '[ruby-core:41494]' + big = 1 << 66 + assert_raise(ZeroDivisionError, bug5713) { 0 ** -big } + assert_raise(ZeroDivisionError, bug5713) { 0 ** Rational(-2,3) } + end end -- cgit v1.2.3