[issue3439] create a numbits() method for int and long types

Mark Dickinson report at bugs.python.org
Tue Dec 16 22:38:30 CET 2008


Mark Dickinson <dickinsm at gmail.com> added the comment:

> Before applying, consider adding back the part of the docs with the '1 +
> floor(...' definition.

My only (minor) objection to this definition is that a straight Python
translation of it doesn't work, thanks to roundoff error and
the limited precision of floating-point:

>>> from math import floor, log
>>> n = 2**101
>>> n.bit_length()
102
>>> 1 + floor(log(n)/log(2))
101.0
>>> n = 2**80-1
>>> n.bit_length()
80
>>> 1 + floor(log(n)/log(2))
81.0

But as you say, it provides another perspective;  I'm fine with
putting it back in.

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3439>
_______________________________________


More information about the Python-bugs-list mailing list