Created
September 24, 2015 23:44
-
-
Save sfaleron/1f958c6e37f4f235d5c8 to your computer and use it in GitHub Desktop.
Quick ln2 function for Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil | |
# for example, inc=4 gives ln16, or the number of hexadecimal digits | |
# required to represent n. | |
def ln2(n, inc=1): | |
if n<0: | |
raise ValueError('math domain error') | |
i = 0 | |
n = int(ceil(n)) | |
while n: | |
i += 1 | |
n >>= inc | |
return i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment