summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-26 11:17:37 (GMT)
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-26 11:17:37 (GMT)
commitbcc17969832e8f40200231f1a925c8f2318aa706 (patch)
treee2caeb465d38e523ed8e819c9f2ecf2a5c7528da /numeric.c
parent9368d7515b9886267ebe7602218e4ac2ea494074 (diff)
{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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54778 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c39
1 files changed, 32 insertions, 7 deletions
diff --git a/numeric.c b/numeric.c
index 76a64ca..7d3ad34 100644
--- a/numeric.c
+++ b/numeric.c
@@ -4121,6 +4121,15 @@ fix_size(VALUE fix)
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)
* 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)
* (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)
*/
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)
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);