[前][次][番号順一覧][スレッド一覧]

ruby-changes:1691

From: ko1@a...
Date: 22 Aug 2007 10:27:13 +0900
Subject: [ruby-changes:1691] shyouhei - Ruby:r13182 (ruby_1_8_6): * numeric.c (fix_pow): integer power calculation: 0**n => 0,

shyouhei	2007-08-22 10:26:39 +0900 (Wed, 22 Aug 2007)

  New Revision: 13182

  Modified files:
    branches/ruby_1_8_6/ChangeLog
    branches/ruby_1_8_6/numeric.c
    branches/ruby_1_8_6/version.h

  Log:
    * 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.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_6/ChangeLog?r1=13182&r2=13181
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_6/version.h?r1=13182&r2=13181
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_6/numeric.c?r1=13182&r2=13181

Index: ruby_1_8_6/numeric.c
===================================================================
--- ruby_1_8_6/numeric.c	(revision 13181)
+++ ruby_1_8_6/numeric.c	(revision 13182)
@@ -2177,6 +2177,15 @@
     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
@@ -2193,23 +2202,45 @@
 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);
+    }
 }
 
 /*
Index: ruby_1_8_6/ChangeLog
===================================================================
--- ruby_1_8_6/ChangeLog	(revision 13181)
+++ ruby_1_8_6/ChangeLog	(revision 13182)
@@ -1,3 +1,11 @@
+Wed Aug 22 10:24:00 2007  Yukihiro Matsumoto  <matz@r...>
+
+	* 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.
+
 Wed Aug 22 10:23:01 2007  Yukihiro Matsumoto  <matz@r...>
 
 	* lib/base64.rb (Base64::b64encode): should not specify /o option
Index: ruby_1_8_6/version.h
===================================================================
--- ruby_1_8_6/version.h	(revision 13181)
+++ ruby_1_8_6/version.h	(revision 13182)
@@ -2,7 +2,7 @@
 #define RUBY_RELEASE_DATE "2007-08-22"
 #define RUBY_VERSION_CODE 186
 #define RUBY_RELEASE_CODE 20070822
-#define RUBY_PATCHLEVEL 67
+#define RUBY_PATCHLEVEL 68
 
 #define RUBY_VERSION_MAJOR 1
 #define RUBY_VERSION_MINOR 8

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml

[前][次][番号順一覧][スレッド一覧]