summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2008-12-17 16:19:07 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2008-12-17 16:19:07 (GMT)
commit54bc1ec4c7689ceab900f453fdd4c8cf5a308e59 (patch)
tree87dd2371d042f3b8e63a32a4bda589ccce0b2303 /Doc
parent81c93fb45c4076506a5ab3d89aeec1f42e0e6be2 (diff)
downloadcpython-54bc1ec4c7689ceab900f453fdd4c8cf5a308e59.zip
cpython-54bc1ec4c7689ceab900f453fdd4c8cf5a308e59.tar.gz
cpython-54bc1ec4c7689ceab900f453fdd4c8cf5a308e59.tar.bz2
Forward merge of r67822 to py3k: add bit_length method to int.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/stdtypes.rst39
-rw-r--r--Doc/whatsnew/2.7.rst18
-rw-r--r--Doc/whatsnew/3.1.rst22
3 files changed, 78 insertions, 1 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 68e8dba..638bafd 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -419,6 +419,40 @@ Notes:
overflow check.
+Additional Methods on Integer Types
+-----------------------------------
+
+.. method:: int.bit_length()
+
+ For any integer ``x``, ``x.bit_length()`` returns the number of
+ bits necessary to represent ``x`` in binary, excluding the sign
+ and any leading zeros::
+
+ >>> n = 37
+ >>> bin(n)
+ '0b100101'
+ >>> n.bit_length()
+ 6
+ >>> n = -0b00011010
+ >>> n.bit_length()
+ 5
+
+ More precisely, if ``x`` is nonzero then ``x.bit_length()`` is the
+ unique positive integer ``k`` such that ``2**(k-1) <= abs(x) <
+ 2**k``. Equivalently, ``x.bit_length()`` is equal to ``1 +
+ floor(log(x, 2))`` [#]_ . If ``x`` is zero then ``x.bit_length()``
+ gives ``0``.
+
+ Equivalent to::
+
+ def bit_length(self):
+ 'Number of bits necessary to represent self in binary.'
+ return len(bin(self).lstrip('-0b'))
+
+
+ .. versionadded:: 3.1
+
+
Additional Methods on Float
---------------------------
@@ -2639,6 +2673,11 @@ types, where they are relevant. Some of these are not reported by the
.. [#] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
similarly for tuples.
+.. [#] Beware of this formula! It's mathematically valid, but as a
+ Python expression it will not give correct results for all ``x``,
+ as a consequence of the limited precision of floating-point
+ arithmetic.
+
.. [#] They must have since the parser can't tell the type of the operands.
.. [#] To format only a tuple you should therefore provide a singleton tuple whose only
diff --git a/Doc/whatsnew/2.7.rst b/Doc/whatsnew/2.7.rst
index a9eb0ba..61084c8 100644
--- a/Doc/whatsnew/2.7.rst
+++ b/Doc/whatsnew/2.7.rst
@@ -66,7 +66,23 @@ Other Language Changes
Some smaller changes made to the core Python language are:
-* List of changes to be written here.
+* The :func:`int` and :func:`long` types gained a ``bit_length``
+ method that returns the number of bits necessary to represent
+ its argument in binary::
+
+ >>> n = 37
+ >>> bin(37)
+ '0b100101'
+ >>> n.bit_length()
+ 6
+ >>> n = 2**123-1
+ >>> n.bit_length()
+ 123
+ >>> (n+1).bit_length()
+ 124
+
+ (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
+
.. ======================================================================
diff --git a/Doc/whatsnew/3.1.rst b/Doc/whatsnew/3.1.rst
index a244568..7df4d1e 100644
--- a/Doc/whatsnew/3.1.rst
+++ b/Doc/whatsnew/3.1.rst
@@ -66,4 +66,26 @@ This article explains the new features in Python 3.1, compared to 3.0.
.. ======================================================================
+Other Language Changes
+======================
+
+Some smaller changes made to the core Python language are:
+
+* The :func:`int` type gained a ``bit_length`` method that returns the
+ number of bits necessary to represent its argument in binary::
+
+ >>> n = 37
+ >>> bin(37)
+ '0b100101'
+ >>> n.bit_length()
+ 6
+ >>> n = 2**123-1
+ >>> n.bit_length()
+ 123
+ >>> (n+1).bit_length()
+ 124
+
+ (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
+
+
.. ======================================================================