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 | 9368d7515b9886267ebe7602218e4ac2ea494074 (patch) | |
tree | 5704820d15803a3973b5ec867d53ff8328d9fed6 /numeric.c | |
parent | 9223fe4fca4b3a6a46d2ae9fcc9d2ec6b01478db (diff) | |
download | ruby-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
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 37 |
1 files changed, 24 insertions, 13 deletions
@@ -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); |