1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
marcandre 2013-02-05 05:39:49 +00:00
parent 8797ebd662
commit d22ce4a522
3 changed files with 36 additions and 12 deletions

View file

@ -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

View file

@ -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:

View file

@ -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