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

ruby-changes:42704

From: akr <ko1@a...>
Date: Tue, 26 Apr 2016 19:21:00 +0900 (JST)
Subject: [ruby-changes:42704] akr:r54778 (trunk): {Fixnum, Bignum}#bit_length is unified into Integer.

akr	2016-04-26 20:17:37 +0900 (Tue, 26 Apr 2016)

  New Revision: 54778

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

  Log:
    {Fixnum,Bignum}#bit_length is unified into Integer.
    
    * numeric.c (rb_int_bit_length): {Fixnum,Bignum}#bit_length is
      unified into Integer.
    
    * bignum.c (rb_big_bit_length): Don't define Bignum#bit_length.
    
    * internal.h (rb_big_bit_length): Declared.
    
    --This iine, and those below, will be ignored--
    
    M    ChangeLog
    M    bignum.c
    M    internal.h
    M    numeric.c

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/internal.h
    trunk/numeric.c
Index: numeric.c
===================================================================
--- numeric.c	(revision 54777)
+++ numeric.c	(revision 54778)
@@ -4121,6 +4121,15 @@ fix_size(VALUE fix) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4121
     return INT2FIX(sizeof(long));
 }
 
+static VALUE
+rb_fix_bit_length(VALUE fix)
+{
+    long v = FIX2LONG(fix);
+    if (v < 0)
+        v = ~v;
+    return LONG2FIX(bit_length(v));
+}
+
 /*
  *  call-seq:
  *     int.bit_length -> integer
@@ -4133,7 +4142,14 @@ fix_size(VALUE fix) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4142
  *  If there is no such bit (zero or minus one), zero is returned.
  *
  *  I.e. This method returns ceil(log2(int < 0 ? -int : int+1)).
- *
+
+
+ *     (-2**10000-1).bit_length  #=> 10001
+ *     (-2**10000).bit_length    #=> 10000
+ *     (-2**10000+1).bit_length  #=> 10000
+ *     (-2**1000-1).bit_length   #=> 1001
+ *     (-2**1000).bit_length     #=> 1000
+ *     (-2**1000+1).bit_length   #=> 1000
  *     (-2**12-1).bit_length     #=> 13
  *     (-2**12).bit_length       #=> 12
  *     (-2**12+1).bit_length     #=> 12
@@ -4149,6 +4165,12 @@ fix_size(VALUE fix) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4165
  *     (2**12-1).bit_length      #=> 12
  *     (2**12).bit_length        #=> 13
  *     (2**12+1).bit_length      #=> 13
+ *     (2**1000-1).bit_length    #=> 1000
+ *     (2**1000).bit_length      #=> 1001
+ *     (2**1000+1).bit_length    #=> 1001
+ *     (2**10000-1).bit_length   #=> 10000
+ *     (2**10000).bit_length     #=> 10001
+ *     (2**10000+1).bit_length   #=> 10001
  *
  *  This method can be used to detect overflow in Array#pack as follows.
  *
@@ -4160,12 +4182,15 @@ fix_size(VALUE fix) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4182
  */
 
 static VALUE
-rb_fix_bit_length(VALUE fix)
+rb_int_bit_length(VALUE num)
 {
-    long v = FIX2LONG(fix);
-    if (v < 0)
-        v = ~v;
-    return LONG2FIX(bit_length(v));
+    if (FIXNUM_P(num)) {
+	return rb_fix_bit_length(num);
+    }
+    else if (RB_TYPE_P(num, T_BIGNUM)) {
+	return rb_big_bit_length(num);
+    }
+    return Qnil;
 }
 
 static VALUE
@@ -4645,7 +4670,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4670
     rb_define_method(rb_cFixnum, ">>", rb_fix_rshift, 1);
 
     rb_define_method(rb_cFixnum, "size", fix_size, 0);
-    rb_define_method(rb_cFixnum, "bit_length", rb_fix_bit_length, 0);
+    rb_define_method(rb_cInteger, "bit_length", rb_int_bit_length, 0);
     rb_define_method(rb_cFixnum, "succ", fix_succ, 0);
 
     rb_cFloat  = rb_define_class("Float", rb_cNumeric);
Index: internal.h
===================================================================
--- internal.h	(revision 54777)
+++ internal.h	(revision 54778)
@@ -779,6 +779,7 @@ VALUE rb_integer_float_cmp(VALUE x, VALU https://github.com/ruby/ruby/blob/trunk/internal.h#L779
 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);
+VALUE rb_big_bit_length(VALUE big);
 
 /* class.c */
 VALUE rb_class_boot(VALUE);
Index: bignum.c
===================================================================
--- bignum.c	(revision 54777)
+++ bignum.c	(revision 54778)
@@ -6920,45 +6920,7 @@ rb_big_size_m(VALUE big) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6920
     return SIZET2NUM(rb_big_size(big));
 }
 
-/*
- *  call-seq:
- *     int.bit_length -> integer
- *
- *  Returns the number of bits of the value of <i>int</i>.
- *
- *  "the number of bits" means that
- *  the bit position of the highest bit which is different to the sign bit.
- *  (The bit position of the bit 2**n is n+1.)
- *  If there is no such bit (zero or minus one), zero is returned.
- *
- *  I.e. This method returns ceil(log2(int < 0 ? -int : int+1)).
- *
- *     (-2**10000-1).bit_length  #=> 10001
- *     (-2**10000).bit_length    #=> 10000
- *     (-2**10000+1).bit_length  #=> 10000
- *
- *     (-2**1000-1).bit_length   #=> 1001
- *     (-2**1000).bit_length     #=> 1000
- *     (-2**1000+1).bit_length   #=> 1000
- *
- *     (2**1000-1).bit_length    #=> 1000
- *     (2**1000).bit_length      #=> 1001
- *     (2**1000+1).bit_length    #=> 1001
- *
- *     (2**10000-1).bit_length   #=> 10000
- *     (2**10000).bit_length     #=> 10001
- *     (2**10000+1).bit_length   #=> 10001
- *
- *  This method can be used to detect overflow in Array#pack as follows.
- *
- *     if n.bit_length < 32
- *       [n].pack("l") # no overflow
- *     else
- *       raise "overflow"
- *     end
- */
-
-static VALUE
+VALUE
 rb_big_bit_length(VALUE big)
 {
     int nlz_bits;
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54777)
+++ ChangeLog	(revision 54778)
@@ -1,12 +1,21 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Apr 26 20:09:08 2016  Tanaka Akira  <akr@f...>
+
+	* numeric.c (rb_int_bit_length): {Fixnum,Bignum}#bit_length is
+	  unified into Integer.
+
+	* bignum.c (rb_big_bit_length): Don't define Bignum#bit_length.
+
+	* internal.h (rb_big_bit_length): Declared.
+
 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}.
 
+	* internal.h (rb_big_abs): Declared.
+
 Mon Apr 25 14:39:11 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/rbconfig/sizeof/extconf.rb: just check the existence of each

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

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