Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Python math.log2() Method

  • 01 Aug, 2025
  • Com 0
Python math.log2()

Python math.log2() calculates the base-2 logarithm of a given number. It is helpful in computer science and information theory, offering better performance and accuracy.

math.log2() Method in Python

Let’s calculate the base-2 log of positive numbers.

import math

print(math.log2(1))  # Log base 2 of 1
# Output: 0.0

print(math.log2(8))  # Log base 2 of 8
# Output: 3.0

print(math.log2(4.49))  # Log base 2 of 4.49
# Output: 2.1667154449664223

The above output shows that even after having an integer input, it returns a floating-point output.

Syntax

import math 

math.log2(num)

To use the log2() method, we need to import the built-in math module.

Parameter

Argument Description
num (float or int) It represents a positive float or int value, for which the base-2 logarithm is calculated.

It should not be zero or negative.

Non-positive or 0 inputs

math.log2() throws ValueError

What if you pass a negative number to the log2() method? Well, it raises the ValueError: math domain error.

If the input is 0, it still raises a ValueError because it should not be zero either.

import math

try:
    x = 0
    math.log2(x)      # x ≤ 0
except ValueError as e:
    print(e)
    # Output: math domain error

try:
    y = -10
    math.log2(y)    # Negative input
except ValueError as e:
    print(e)
    # Output: math domain error

Special values

The log2() of infinity is infinity, and NaN is NaN.

import math

print(math.log2(float('inf')))
# Output: inf (log₂(∞) = ∞)

print(math.log2(float('nan')))
# Output: nan (Undefined)

Non-numeric Input

If you attempt to find log2() of a string or characters, it raises ValueError: could not convert string to float: ”.

import math

print(math.log2(float('Krunal')))

# ValueError: could not convert string to float: 'Krunal'

Don’t pass variables other than float or integer.

Post Views: 24
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.rot90() Method
JavaScript Array push() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend