3

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?

0

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

2

In python 3 ints even have an efficient .bit_length() method!

>>> (281474976710655).bit_length()
48
>>> (281474976710656).bit_length()
49

In python 2, instead of using floating point math, count the number of bits:

def log2(n):
    assert n >= 1
    return len(bin(n)) - 3  # bin() returns a string starting with '0b'

(Edited following this comment)

1 Comment

That's fine for Python 2, but in Python 3 .bit_length is much more efficient than converting the number to a binary string. BTW, in Python 2.6+ format(n, 'b') is more convenient for this than bin, since you don't get that pesky '0b'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.