log2()
log2(x) returns logarithm of x with base 2
import math
print(math.log2(2)) # 1.0
print(math.log2(4)) # 2.0
print(math.log2(5)) # 2.321928094887362
Using negative number
For any value less than or equal to 0 , we will get ValueError
import math
print(math.log2(-2))
Above code will generate error.
Use Case: Data Size Calculation: To calculate how many bits are needed to store a number:
print(math.log2(1024) + 1) # Output: 11.0
« log
modf()
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com