Home » Python

Log functions of math module in Python

Python log functions: Here, we are going to learn about the different log functions of math modules with examples in Python.
Submitted by IncludeHelp, on April 20, 2019

Python math module log functions

In python programming language, there are some of the following log functions which are defined in math module,

  1. log(x [,base])
  2. log1p(x)
  3. log2(x)
  4. log10(x)
Python log method Description Base
log(x [,base]) It returns the natural logarithm (base-e) of the number x (if we do not define the base) or returns the given base's logarithm of the number x (if we define base). base-e , base-any (given base)
log1p(x) It returns the natural logarithm (base-e) of 1 + x, where the given number is x. base-e
log2(x) It returns the base-2 logarithm of the given number x. base-2
log10(x) It returns the base-10 logarithm of the given number x. base-10

1) log(x [,base]) method

This method is used to 1) calculate natural logarithm i.e. log-e (using one parameter) or given value x, and 2) calculate any given base logarithm (using two parameters).

In this method, the second parameter (base) is optional, if we do not provide the second parameter, the logarithm will be calculated as natural logarithm (log-e).

Syntax:

    log(x, [base])

Parameter(s): x – is a number whose logarithm to be calculated, base – is an optional parameter, it can be used to define any specific base of the logarithm.

Return value: float – it returns a float value, that is the base-e or base-any (using the second parameter) logarithm of the given number x.

2) log1p(x) method

This method is used to calculate the natural logarithm of 1 + x (base e).

Syntax:

    log1p(x)

Parameter(s): x – is a number whose natural logarithm to be calculated.

Return value: float – it returns a float value that is the base-e (natural) logarithm of 1 + x.

3) log2(x) method

This method is used to calculate the base-2 logarithm of x.

Syntax:

    log2(x)

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

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

4) log10(x) method

This method is used to calculate the base-10 logarithm of x.

Syntax:

    log10(x)

Parameter(s): x is a number whose base-10 logarithm to be calculated.

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


Python code to find logarithms on different bases using python log functions

# python code to demonstrate example of 
# different math module log functions

# importing math module
import math

# number
x = 10

# natural logarithm (base-e) using log(x)
print("natural logarithm of ", x, " is = ", math.log(x))
# logarithm (base-5) using log(x, base)
print("base-5 logarithm of ", x, " is = ", math.log(x,5))

# natural logarithm (base-e) of 1+ number using log1p(x)
print("natural logarithm of 1+ ", x, " is = ", math.log(1+x))

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

# base-10 logarithm using log10(x)
print("base-10 logarithm of ", x, " is = ", math.log10(x))

Output

natural logarithm of  10  is =  2.302585092994046
base-5 logarithm of  10  is =  1.4306765580733933
natural logarithm of 1+  10  is =  2.3978952727983707
base-2 logarithm of  10  is =  3.321928094887362
base-10 logarithm of  10  is =  1.0

Exceptions or Errors with log functions

These methods return following expectations if we provide invalid values (except then the positive numbers),

  • ValueError: math domain error – if we provide the negative value.
  • TypeError: a float is required – if we provide any value except a number (like string).

Python code to demonstrate example of exceptions with log functions

# python code to demonstrate example of 
# log(x) with ValueError Exception

# importing math module
import math

# number
x = -10

print("Natural logarithm of ", x, " is = ", math.log(x))

Output

Traceback (most recent call last):
  File "/home/main.py", line 10, in <module>
    print("Natural logarithm of ", x, " is = ", math.log(x))
ValueError: math domain error
# python code to demonstrate example of 
# log(x) with TypeError Exception

# importing math module
import math

# number
x = "10"

print("Natural logarithm of ", x, " is = ", math.log(x))

Output

Traceback (most recent call last):
  File "/home/main.py", line 10, in <module>
    print("Natural logarithm of ", x, " is = ", math.log(x))
TypeError: a float is required





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.