diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-26 10:59:27 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-26 10:59:27 +0000 |
commit | cfaf65847ba0ea7a1bfeee8c40b8c1416cbaebb9 (patch) | |
tree | 5704820d15803a3973b5ec867d53ff8328d9fed6 | |
parent | d4b26a44f8a1c03bb903ba9de0e2eceef505c9ec (diff) | |
download | ruby-cfaf65847ba0ea7a1bfeee8c40b8c1416cbaebb9.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-- | ChangeLog | 9 | ||||
-rw-r--r-- | bignum.c | 14 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | numeric.c | 37 |
4 files changed, 35 insertions, 26 deletions
@@ -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 @@ -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); @@ -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); |