Log2 function of Python math module

Overview:

  • The function log2(num), computes the binary logarithm of a given number. The Base-2 logarithm of a given number “n” is the power to which, number 2 must be raised to obtain the number “n”.

Example:

# Example Python program that computes
# the base-2 logarithm of a number
# using the function log2(x)

import math

# Compute binary logrithm for 10
num = 10
base2log = math.log2(num)
print("Base-2 logarithm of %d:%d"%(num, base2log))

# Compute binary logrithm for 128
num = 128
base2log = math.log2(num)
print("Base-2 logarithm of %d:%d"%(num, base2log))

Output:

Base-2 logarithm of 10:3

Base-2 logarithm of 128:7

 


Copyright 2024 © pythontic.com