diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | numeric.c | 22 | ||||
-rw-r--r-- | test/ruby/test_fixnum.rb | 19 |
3 files changed, 36 insertions, 12 deletions
@@ -1,3 +1,10 @@ +Tue Feb 5 14:36:04 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca> + + * 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 <nobu@ruby-lang.org> * ext/io/console/console.c (rawmode_opt): use default values by `stty @@ -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 |