⊕
⊖
Contents
Python math.log2()
math.log2(x) function returns the logarithm of x to base 2.
Syntax
The syntax to call log2() function is
math.log2(x)
where
Parameter | Required | Description |
---|---|---|
x | Yes | A numeric value. The value must be greater than 0. |
If x is an invalid value, such as a negative value, then log2() raises ValueError.
Examples
In the following program, we find the logarithm of 8 to base 2, using log2() function of math module.
Output
log2(x) : 3.0
Logarithm of a negative number to base 2.
Output
ValueError: math domain error
Logarithm of infinity.
Output
log2(x) : inf
Logarithm of 1024 to base 2.
Output
log2(x) : 10.0
Summary
In this Python Examples tutorial, we learned the syntax of, and examples for math.log2() function.