aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-26 10:59:27 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-26 10:59:27 +0000
commit9368d7515b9886267ebe7602218e4ac2ea494074 (patch)
tree5704820d15803a3973b5ec867d53ff8328d9fed6
parent9223fe4fca4b3a6a46d2ae9fcc9d2ec6b01478db (diff)
downloadruby-9368d7515b9886267ebe7602218e4ac2ea494074.tar.gz
* numeric.c (int_abs): Integer#{abs,magnitude} moved from Fixnum and Bignum.
* internal.h (rb_big_abs): Declared. * bignum.c (rb_big_abs): Don't define Bignum#{abs,magnitude}. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54777 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog9
-rw-r--r--bignum.c14
-rw-r--r--internal.h1
-rw-r--r--numeric.c37
4 files changed, 35 insertions, 26 deletions
diff --git a/ChangeLog b/ChangeLog
index fce9bfc147..c7afccf614 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Tue Apr 26 19:56:16 2016 Tanaka Akira <akr@fsij.org>
+
+ * numeric.c (int_abs): Integer#{abs,magnitude} moved from
+ Fixnum and Bignum.
+
+ * internal.h (rb_big_abs): Declared.
+
+ * bignum.c (rb_big_abs): Don't define Bignum#{abs,magnitude}.
+
Mon Apr 25 14:39:11 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/rbconfig/sizeof/extconf.rb: just check the existence of each
diff --git a/bignum.c b/bignum.c
index e46f845c57..6fcbc74783 100644
--- a/bignum.c
+++ b/bignum.c
@@ -6886,17 +6886,7 @@ rb_big_coerce(VALUE x, VALUE y)
return rb_assoc_new(y, x);
}
-/*
- * call-seq:
- * big.abs -> aBignum
- * big.magnitude -> aBignum
- *
- * Returns the absolute value of <i>big</i>.
- *
- * -1234567890987654321.abs #=> 1234567890987654321
- */
-
-static VALUE
+VALUE
rb_big_abs(VALUE x)
{
if (BIGNUM_NEGATIVE_P(x)) {
@@ -7091,8 +7081,6 @@ Init_Bignum(void)
rb_define_method(rb_cBignum, "<", big_lt, 1);
rb_define_method(rb_cBignum, "<=", big_le, 1);
rb_define_method(rb_cBignum, "===", rb_big_eq, 1);
- rb_define_method(rb_cBignum, "abs", rb_big_abs, 0);
- rb_define_method(rb_cBignum, "magnitude", rb_big_abs, 0);
rb_define_method(rb_cBignum, "size", rb_big_size_m, 0);
rb_define_method(rb_cBignum, "bit_length", rb_big_bit_length, 0);
diff --git a/internal.h b/internal.h
index a9f50a5cdc..5f79d4eb4b 100644
--- a/internal.h
+++ b/internal.h
@@ -778,6 +778,7 @@ size_t rb_big_size(VALUE);
VALUE rb_integer_float_cmp(VALUE x, VALUE y);
VALUE rb_integer_float_eq(VALUE x, VALUE y);
VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base);
+VALUE rb_big_abs(VALUE x);
/* class.c */
VALUE rb_class_boot(VALUE);
diff --git a/numeric.c b/numeric.c
index 5366d06139..76a64ca9cc 100644
--- a/numeric.c
+++ b/numeric.c
@@ -4069,30 +4069,41 @@ int_to_f(VALUE num)
return DBL2NUM(val);
}
+static VALUE
+fix_abs(VALUE fix)
+{
+ long i = FIX2LONG(fix);
+
+ if (i < 0) i = -i;
+
+ return LONG2NUM(i);
+}
+
/*
* call-seq:
- * fix.abs -> integer
- * fix.magnitude -> integer
+ * int.abs -> integer
+ * int.magnitude -> integer
*
- * Returns the absolute value of +fix+.
+ * Returns the absolute value of +int+.
*
* -12345.abs #=> 12345
* 12345.abs #=> 12345
+ * -1234567890987654321.abs #=> 1234567890987654321
*
*/
static VALUE
-fix_abs(VALUE fix)
+int_abs(VALUE num)
{
- long i = FIX2LONG(fix);
-
- if (i < 0) i = -i;
-
- return LONG2NUM(i);
+ if (FIXNUM_P(num)) {
+ return fix_abs(num);
+ }
+ else if (RB_TYPE_P(num, T_BIGNUM)) {
+ return rb_big_abs(num);
+ }
+ return Qnil;
}
-
-
/*
* call-seq:
* fix.size -> fixnum
@@ -4614,8 +4625,8 @@ Init_Numeric(void)
rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1);
rb_define_method(rb_cFixnum, "**", fix_pow, 1);
- rb_define_method(rb_cFixnum, "abs", fix_abs, 0);
- rb_define_method(rb_cFixnum, "magnitude", fix_abs, 0);
+ rb_define_method(rb_cInteger, "abs", int_abs, 0);
+ rb_define_method(rb_cInteger, "magnitude", int_abs, 0);
rb_define_method(rb_cFixnum, "==", fix_equal, 1);
rb_define_method(rb_cFixnum, "===", fix_equal, 1);