diff options
author | Raymond Hettinger <python@rcn.com> | 2008-12-19 09:11:49 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-12-19 09:11:49 (GMT) |
commit | d3e18b7113a6704950d7657134d770066d9542c5 (patch) | |
tree | eadec4f0c5604068201ad8dd81a2004b97ff06cb /Doc | |
parent | 09027aac9d5101d0e15eac8ba3a5d60e5298f26c (diff) | |
download | cpython-d3e18b7113a6704950d7657134d770066d9542c5.zip cpython-d3e18b7113a6704950d7657134d770066d9542c5.tar.gz cpython-d3e18b7113a6704950d7657134d770066d9542c5.tar.bz2 |
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.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/stdtypes.rst | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 638bafd..de00c49 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -424,31 +424,27 @@ 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:: + 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:: 3.1 @@ -2673,11 +2669,6 @@ 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 |