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

ruby-changes:42757

From: akr <ko1@a...>
Date: Sat, 30 Apr 2016 12:02:26 +0900 (JST)
Subject: [ruby-changes:42757] akr:r54831 (trunk): {Fixnum, Bignum}#** is unified into Integer.

akr	2016-04-30 12:59:02 +0900 (Sat, 30 Apr 2016)

  New Revision: 54831

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54831

  Log:
    {Fixnum,Bignum}#** is unified into Integer.
    
    * numeric.c (rb_int_pow): {Fixnum,Bignum}#** is unified into
      Integer.
    
    * bignum.c (rb_big_pow): Don't define Bignum#**.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/numeric.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54830)
+++ ChangeLog	(revision 54831)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr 30 12:25:43 2016  Tanaka Akira  <akr@f...>
+
+	* numeric.c (rb_int_pow): {Fixnum,Bignum}#** is unified into
+	  Integer.
+
+	* bignum.c (rb_big_pow): Don't define Bignum#**.
+
 Sat Apr 30 12:28:59 2016  Tanaka Akira  <akr@f...>
 
 	* bignum.c (rb_big_comp): Renamed from rb_big_neg.
Index: bignum.c
===================================================================
--- bignum.c	(revision 54830)
+++ bignum.c	(revision 54831)
@@ -6296,19 +6296,6 @@ rb_big_fdiv(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6296
     return DBL2NUM(dx / dy);
 }
 
-/*
- *  call-seq:
- *     big ** exponent   -> numeric
- *
- *  Raises _big_ to the _exponent_ power (which may be an integer, float,
- *  or anything that will coerce to a number). The result may be
- *  a Fixnum, Bignum, or Float
- *
- *    123456789 ** 2      #=> 15241578750190521
- *    123456789 ** 1.2    #=> 5126464716.09932
- *    123456789 ** -2     #=> 6.5610001194102e-17
- */
-
 VALUE
 rb_big_pow(VALUE x, VALUE y)
 {
@@ -6950,7 +6937,6 @@ Init_Bignum(void) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6937
     rb_define_method(rb_cBignum, "modulo", rb_big_modulo, 1);
     rb_define_method(rb_cBignum, "remainder", rb_big_remainder, 1);
     rb_define_method(rb_cBignum, "fdiv", rb_big_fdiv, 1);
-    rb_define_method(rb_cBignum, "**", rb_big_pow, 1);
 
     rb_define_method(rb_cBignum, "==", rb_big_eq, 1);
     rb_define_method(rb_cBignum, ">", big_gt, 1);
Index: numeric.c
===================================================================
--- numeric.c	(revision 54830)
+++ numeric.c	(revision 54831)
@@ -3557,16 +3557,22 @@ fix_divmod(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3557
 }
 
 /*
- *  Document-method: Fixnum#**
+ *  Document-method: Integer#**
  *  call-seq:
- *    fix ** numeric  ->  numeric_result
+ *    integer ** numeric  ->  numeric_result
  *
- *  Raises +fix+ to the power of +numeric+, which may be negative or
+ *  Raises +integer+ to the power of +numeric+, which may be negative or
  *  fractional.
+ *  The result may be a Fixnum, Bignum, or Float
  *
  *    2 ** 3      #=> 8
  *    2 ** -1     #=> (1/2)
  *    2 ** 0.5    #=> 1.4142135623731
+ *
+ *    123456789 ** 2      #=> 15241578750190521
+ *    123456789 ** 1.2    #=> 5126464716.09932
+ *    123456789 ** -2     #=> 6.5610001194102e-17
+ *
  */
 
 static VALUE
@@ -3666,6 +3672,18 @@ fix_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3672
     }
 }
 
+static VALUE
+rb_int_pow(VALUE x, VALUE y)
+{
+    if (FIXNUM_P(x)) {
+	return fix_pow(x, y);
+    }
+    else if (RB_TYPE_P(x, T_BIGNUM)) {
+	return rb_big_pow(x, y);
+    }
+    return Qnil;
+}
+
 /*
  * Document-method: Fixnum#==
  * call-seq:
@@ -4799,7 +4817,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4817
     rb_define_method(rb_cFixnum, "modulo", fix_mod, 1);
     rb_define_method(rb_cFixnum, "divmod", fix_divmod, 1);
     rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1);
-    rb_define_method(rb_cFixnum, "**", fix_pow, 1);
+    rb_define_method(rb_cInteger, "**", rb_int_pow, 1);
 
     rb_define_method(rb_cInteger, "abs", int_abs, 0);
     rb_define_method(rb_cInteger, "magnitude", int_abs, 0);

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

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