aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-07-14 14:31:21 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-07-14 14:31:21 +0000
commit7f0bb427e09ab985da52217f792c292ba9d0c556 (patch)
tree8c819ec7470696280d10f72a832d7f3f6e1b0a55
parentbb44ba9b72adf41dc17714351df724bc5c21bd7a (diff)
downloadruby-7f0bb427e09ab985da52217f792c292ba9d0c556.tar.gz
* numeric.c (fix_pow): integer power calculation: 0**n => 0,
1**n => 1, -1**n => 1 (n: even) / -1 (n: odd). * test/ruby/test_fixnum.rb (TestFixnum::test_pow): update test suite. pow(-3, 2^64) gives NaN when pow(3, 2^64) gives Inf. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12789 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--numeric.c23
-rw-r--r--test/ruby/test_fixnum.rb2
3 files changed, 28 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 6c7cac6661..29bc9ca545 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Sat Jul 14 22:49:30 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * numeric.c (fix_pow): integer power calculation: 0**n => 0,
+ 1**n => 1, -1**n => 1 (n: even) / -1 (n: odd).
+
+ * test/ruby/test_fixnum.rb (TestFixnum::test_pow): update test
+ suite. pow(-3, 2^64) gives NaN when pow(3, 2^64) gives Inf.
+
Sat Jul 14 18:46:35 2007 Tanaka Akira <akr@fsij.org>
* configure.in: add --with-valgrind.
diff --git a/numeric.c b/numeric.c
index a50199dce3..9f986a7633 100644
--- a/numeric.c
+++ b/numeric.c
@@ -2326,14 +2326,21 @@ int_pow(long x, unsigned long y)
static VALUE
fix_pow(VALUE x, VALUE y)
{
+ long a = FIX2LONG(x);
+
if (FIXNUM_P(y)) {
- long a, b;
+ long b = FIX2LONG(y);
- b = FIX2LONG(y);
if (b == 0) return INT2FIX(1);
if (b == 1) return x;
- a = FIX2LONG(x);
if (a == 0) return INT2FIX(0);
+ if (a == 1) return INT2FIX(1);
+ if (a == -1) {
+ if (b % 2 == 0)
+ return INT2FIX(1);
+ else
+ return INT2FIX(-1);
+ }
if (b > 0) {
return int_pow(a, b);
}
@@ -2341,10 +2348,18 @@ fix_pow(VALUE x, VALUE y)
}
switch (TYPE(y)) {
case T_BIGNUM:
+ 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);
+ }
x = rb_int2big(FIX2LONG(x));
return rb_big_pow(x, y);
case T_FLOAT:
- return rb_float_new(pow((double)FIX2LONG(x), RFLOAT(y)->value));
+ if (a == 0) return rb_float_new(0.0);
+ if (a == 1) return rb_float_new(1.0);
+ return rb_float_new(pow((double)a, RFLOAT(y)->value));
default:
return rb_num_coerce_bin(x, y);
}
diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb
index 13281ca0ea..81a091eb40 100644
--- a/test/ruby/test_fixnum.rb
+++ b/test/ruby/test_fixnum.rb
@@ -12,7 +12,7 @@ class TestFixnum < Test::Unit::TestCase
def test_pow
[1, 2, 2**64, 2**63*3, 2**64*3].each do |y|
- [1, 3].each do |x|
+ [-1, 0, 1].each do |x|
z1 = x**y
z2 = (-x)**y
if y % 2 == 1