Permalink
Browse files

* numeric.c (fix_pow): Handle special cases when base is 0, 1 or +1

  [Bug #5713] [Rubyspec:7af675cde9f]
  • Loading branch information...
1 parent 550773d commit 015ff0137e2cf33e329d081eb0bf953e0b01d3ae @marcandre committed Apr 1, 2012
Showing with 10 additions and 11 deletions.
  1. +10 −11 numeric.c
View
@@ -2840,6 +2840,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);
@@ -2849,27 +2856,19 @@ 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 (rb_funcall(y, '<', 1, INT2FIX(0)))
- 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 (rb_funcall(y, '<', 1, INT2FIX(0)))
+ 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:

0 comments on commit 015ff01

Please sign in to comment.