[Python-checkins] r67850 - python/trunk/Doc/library/stdtypes.rst

raymond.hettinger python-checkins at python.org
Fri Dec 19 10:06:07 CET 2008


Author: raymond.hettinger
Date: Fri Dec 19 10:06:07 2008
New Revision: 67850

Log:
Fix-up and clean-up docs for int.bit_length().

* Replace dramatic footnote with in-line comment about possible round-off errors in logarithms of large numbers.
* Add comments to the pure python code equivalent.
* replace floor() with int() in the mathematical equivalent so the type is correct (should be an int, not a float).
* add abs() to the mathematical equivalent so that it matches the previous line that it is supposed to be equivalent to.
* make one combined example with a negative input.



Modified:
   python/trunk/Doc/library/stdtypes.rst

Modified: python/trunk/Doc/library/stdtypes.rst
==============================================================================
--- python/trunk/Doc/library/stdtypes.rst	(original)
+++ python/trunk/Doc/library/stdtypes.rst	Fri Dec 19 10:06:07 2008
@@ -453,31 +453,27 @@
 .. method:: int.bit_length()
 .. method:: long.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::
+    Return the number of bits necessary to represent an integer in binary,
+    excluding the sign and leading zeros::
 
-        >>> n = 37
+        >>> n = -37
         >>> bin(n)
-        '0b100101'
+        '-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``.
+    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, when ``abs(x)`` is small enough to have a correctly
+    rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
+    If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
 
     Equivalent to::
 
         def bit_length(self):
-            'Number of bits necessary to represent self in binary.'
-            return len(bin(self).lstrip('-0b'))
-
+            s = bin(x)          # binary representation:  bin(-37) --> '-0b100101'
+            s = s.lstrip('-0b') # remove leading zeros and minus sign
+            return len(s)       # len('100101') --> 6
 
     .. versionadded:: 2.7
 
@@ -2683,11 +2679,6 @@
 .. [#] 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


More information about the Python-checkins mailing list