summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-15 21:25:07 +0000
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-15 21:25:07 +0000
commita406475bcbecae14693201adafa2ad2e0cdfc8fc (patch)
tree9ddd7b27865737bc3bcee9797f5b2eaabd8b8a8d
parent033020007206ce9e6ffa89aa33a092fc88ddd840 (diff)
* 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/branches/ruby_1_8_5@13015 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--numeric.c41
-rw-r--r--version.h2
3 files changed, 42 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 62151d6..3d98a63 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Aug 16 06:24:39 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).
+
Thu Aug 16 06:11:34 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/base64.rb (Base64::b64encode): should not specify /o option
diff --git a/numeric.c b/numeric.c
index 23362a0..f4132fd 100644
--- a/numeric.c
+++ b/numeric.c
@@ -2179,6 +2179,15 @@ fix_divmod(x, y)
return rb_num_coerce_bin(x, y);
}
+static VALUE
+int_even_p(VALUE num)
+{
+ if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
/*
* call-seq:
* fix ** other => Numeric
@@ -2195,23 +2204,45 @@ static VALUE
fix_pow(x, y)
VALUE x, y;
{
+ long a = FIX2LONG(x);
+
if (FIXNUM_P(y)) {
- long a, b;
+ long b;
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 rb_big_pow(rb_int2big(a), y);
}
return rb_float_new(pow((double)a, (double)b));
- } else if (TYPE(y) == T_FLOAT) {
- long a = FIX2LONG(x);
- return rb_float_new(pow((double)a, RFLOAT(y)->value));
}
- return rb_num_coerce_bin(x, 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:
+ 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/version.h b/version.h
index 3ae04d2..7b433e7 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@
#define RUBY_RELEASE_DATE "2007-08-16"
#define RUBY_VERSION_CODE 185
#define RUBY_RELEASE_CODE 20070816
-#define RUBY_PATCHLEVEL 76
+#define RUBY_PATCHLEVEL 77
#define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 8