Home » Python

math.log2() method with example in Python

Python math.log2() method: Here, we are going to learn about the math.log2() method with example in Python.
Submitted by IncludeHelp, on April 20, 2019

Python math.log2() method

math.log2() method is a library method of math module, it is used to get the base-2 logarithm of a number, it accepts a number and returns base-2 logarithm of the given number.

The method math.log2() gives more accurate result than the log(x, 2).

Note: If we provide anything else except a number, the method returns a TypeError – "TypeError: a float is required".

Syntax of math.log2() method:

    math.log2(x)

Parameter(s):x – is the number whose base-2 logarithm to be calculated.

Return value: float – it returns a float value that is the base-2 logarithm of the number x.

Example:

    Input:
    x = 21

    # function call
    print(math.log2(x))

    Output:
    4.392317422778761

Python code to demonstrate example of math.log2() method

# python code to demonstrate example of 
# math.log2() method

# importing math module
import math

# Base-2 logarithm
x = 21
print("Base-2 logarithm of ", x, " is = ", math.log2(x))

x = 10.23
print("Base-2 logarithm of ", x, " is = ", math.log2(x))

Output

Base-2 logarithm of  21  is =  4.392317422778761
Base-2 logarithm of  10.23  is =  3.354734239970604





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.