diff options
author | Raymond Hettinger <python@rcn.com> | 2008-12-19 09:06:07 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-12-19 09:06:07 +0000 |
commit | ba666a59db2e90fec979e40789d70ba774f38ab6 (patch) | |
tree | 08fe65ff83231207c2bea162f539e5040dc4358f | |
parent | 2b97b71b6f22abe4e4208226580eb2da403a66ca (diff) | |
download | cpython-git-ba666a59db2e90fec979e40789d70ba774f38ab6.tar.gz |
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.
-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 be684ed6c9..1dde919c0c 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -453,31 +453,27 @@ Additional Methods on Integer Types .. 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 @@ 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 |