summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-30 03:59:02 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-30 03:59:02 +0000
commit8a2df1ce3f143b49d43b3899692c6ed559b6d53c (patch)
tree08913b0c034536b9712c9084b1531fba25e57286 /numeric.c
parentd73b431e5068f2b5a2c696dca6d84a8c394602b1 (diff)
{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#**. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/numeric.c b/numeric.c
index 5201db10fd..0ef31b7053 100644
--- a/numeric.c
+++ b/numeric.c
@@ -3557,16 +3557,22 @@ fix_divmod(VALUE x, VALUE y)
}
/*
- * 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)
}
}
+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)
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);