Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3e18b7

Browse files
committed
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.
1 parent 09027aa commit d3e18b7

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -424,31 +424,27 @@ Additional Methods on Integer Types
424424

425425
.. method:: int.bit_length()
426426

427-
For any integer ``x``, ``x.bit_length()`` returns the number of
428-
bits necessary to represent ``x`` in binary, excluding the sign
429-
and any leading zeros::
427+
Return the number of bits necessary to represent an integer in binary,
428+
excluding the sign and leading zeros::
430429

431-
>>> n = 37
430+
>>> n = -37
432431
>>> bin(n)
433-
'0b100101'
432+
'-0b100101'
434433
>>> n.bit_length()
435434
6
436-
>>> n = -0b00011010
437-
>>> n.bit_length()
438-
5
439435

440-
More precisely, if ``x`` is nonzero then ``x.bit_length()`` is the
441-
unique positive integer ``k`` such that ``2**(k-1) <= abs(x) <
442-
2**k``. Equivalently, ``x.bit_length()`` is equal to ``1 +
443-
floor(log(x, 2))`` [#]_ . If ``x`` is zero then ``x.bit_length()``
444-
gives ``0``.
436+
More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the
437+
unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``.
438+
Equivalently, when ``abs(x)`` is small enough to have a correctly
439+
rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
440+
If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
445441

446442
Equivalent to::
447443

448444
def bit_length(self):
449-
'Number of bits necessary to represent self in binary.'
450-
return len(bin(self).lstrip('-0b'))
451-
445+
s = bin(x) # binary representation: bin(-37) --> '-0b100101'
446+
s = s.lstrip('-0b') # remove leading zeros and minus sign
447+
return len(s) # len('100101') --> 6
452448

453449
.. versionadded:: 3.1
454450

@@ -2673,11 +2669,6 @@ types, where they are relevant. Some of these are not reported by the
26732669
.. [#] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
26742670
similarly for tuples.
26752671
2676-
.. [#] Beware of this formula! It's mathematically valid, but as a
2677-
Python expression it will not give correct results for all ``x``,
2678-
as a consequence of the limited precision of floating-point
2679-
arithmetic.
2680-
26812672
.. [#] They must have since the parser can't tell the type of the operands.
26822673
26832674
.. [#] To format only a tuple you should therefore provide a singleton tuple whose only

0 commit comments

Comments
 (0)