Search code examples
pythonbinarylogarithm

Precise math.log(x, 2) in Python


In python, I need to get the rounded down logarithm of positive integers for base 2, including big numbers.

However, since floating point math is used, I might get bad results, for example:

>>> import math
>>> int(math.log(281474976710655, 2))
48

However:

>>> 2 ** 48
281474976710656

So the correct result, rounded down, should be 47.

How can I get the correct value?


Solution

  • In Python 3, integers have a .bit_length method, so you should use that to get your rounded down base 2 logarithm.

    Here's a short demo:

    m = 2 ** 1000
    for n in (281474976710655, m-1, m, m+1):
        a = n.bit_length() - 1
        b = 2 ** a
        print(a, b <= n < 2 * b)
    

    output

    47 True
    999 True
    1000 True
    1000 True