Python math.log2() – Logarithm to Base 2

Python supports various kinds of inbuilt modules and functions that perform basic to advanced tasks. One of the modules, named “math” is used in Python to perform different mathematical tasks. The “math” module has a variety of functions that can solve several specific tasks. One such function of the “math” module is “math.log2()”; this function returns the logarithm of a number to base 2.

This Python write-up provided a detailed overview of Python math.log2() function with numerous examples. The following outcomes are discussed in this Python guide:

So let’s begin!

What is math.log2() in Python?

The inbuilt “math” module provides a function “math.log2()” to return the logarithm of a number to base 2. The syntax of this function “math.log2()” is shown below:

math.log2(x)
How to install Kali Linux in Window...

In the above syntax:

  • The parameter “x” must be a positive numeric value.
  • If the parameter value is negative or zero, the “value error” is returned on the screen.
  • If the parameter value is other than the number, then the “type error” is returned on the screen.

Let’s understand this function by performing multiple examples.

Example 1: Finding Logarithm to Base 2 of Positive Number

In the following code example, the “math.log2()” function is used to find the logarithm of the positive numbers to base 2.

Code:

import math

Number = 88
Output = math.log2(Number)
print(Output)

In the above code, firstly, the “math” module is imported. The function named “math.log2()” takes the variable value as an argument and returns the logarithm of 88 to base 2.

Output:

The output shows the value of the logarithm to base 2 of the number “88”.

Example 2: Finding Logarithm to Base 2 of Negative Number

In the following code example, the “math.log2()” function is used to find the logarithm of the negative numbers to base 2.

Code:

import math

Number = -20
Output = math.log2(Number)
print(Output)

In the above code, the “math” module is imported. The function named “math.log2()” takes the variable value as an argument and returns “math domain error” on the output screen. This function only returns the logarithm to base 2 of a positive number.

Output:

The output shows the “math domain error” while executing the negative number.

Example 3: Finding Logarithm to Base 2 of Infinity

In the following code example, the “math.log2()” function is used to find the logarithm of the infinite numbers to base 2.

Code:

import math

Number = math.inf
Output = math.log2(Number)
print(Output)

In the following code, The “math.inf” is used to create an infinite number and store it in a variable named “Number”. The “math.log2()” takes the variable value and returns the logarithm of an infinite number to base 2.

Output:

The output verified that the logarithm of infinite to base 2 is “infinite”.

This was all about this Python guide!

Conclusion

In Python, the “math.log2()” function of the “math” module returns the logarithm of the number to base 2. The input number must be greater than zero. The “math.log2()” function only returns the logarithm of the positive number to base 2. The negative number and zero value will give “ValueError”. If the parameter values are other than numbers, “math.log2()” function returns a TypeError. This Python guide provides all details related to Python “math.log2()” function.