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

ruby-changes:42703

From: akr <ko1@a...>
Date: Tue, 26 Apr 2016 19:02:51 +0900 (JST)
Subject: [ruby-changes:42703] akr:r54777 (trunk): * numeric.c (int_abs): Integer#{abs, magnitude} moved from Fixnum and Bignum.

akr	2016-04-26 19:59:27 +0900 (Tue, 26 Apr 2016)

  New Revision: 54777

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

  Log:
    * 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}.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/internal.h
    trunk/numeric.c
Index: bignum.c
===================================================================
--- bignum.c	(revision 54776)
+++ bignum.c	(revision 54777)
@@ -6886,17 +6886,7 @@ rb_big_coerce(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6886
     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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L7081
     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);
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54776)
+++ ChangeLog	(revision 54777)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Apr 26 19:56:16 2016  Tanaka Akira  <akr@f...>
+
+	* 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@r...>
 
 	* ext/rbconfig/sizeof/extconf.rb: just check the existence of each
Index: internal.h
===================================================================
--- internal.h	(revision 54776)
+++ internal.h	(revision 54777)
@@ -778,6 +778,7 @@ size_t rb_big_size(VALUE); https://github.com/ruby/ruby/blob/trunk/internal.h#L778
 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);
Index: numeric.c
===================================================================
--- numeric.c	(revision 54776)
+++ numeric.c	(revision 54777)
@@ -4069,30 +4069,41 @@ int_to_f(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4069
     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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4625
     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);

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

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