Power and logarithmic functions in Python (exp, log, log10, log2)

Modified: | Tags: Python, Mathematics

In Python, you can calculate power and logarithmic functions with the math module.

All sample code in this article assumes that the math module has been imported.

import math

Euler's number (Napier's constant): math.e

Euler's number, also known as Napier's constant, is provided as a constant in the math module and is represented by math.e.

print(math.e)
# 2.718281828459045

Exponentiation: **, pow(), math.pow()

To calculate exponentiation, use the ** operator, the built-in pow() function, or the math.pow() function.

The y power of x can be obtained by x**y, pow(x, y), or math.pow(x, y).

print(2**4)
# 16

print(pow(2, 4))
# 16

print(math.pow(2, 4))
# 16.0

While math.pow() converts its arguments to float values, pow() relies on the __pow__() method defined for each data type.

For example, while pow() accepts complex, math.pow() returns an error since it cannot convert complex to float.

print(pow(1 + 1j, 2))
# 2j

# print(math.pow(1 + 1j, 2))
# TypeError: must be real number, not complex

You can also provide a third argument to pow(). While pow(x, y, z) gives the remainder when x raised to y is divided by z, it is more efficient than pow(x, y) % z.

print(pow(2, 4, 5))
# 1

Square root: math.sqrt()

To calculate the square root, use the ** operator (as in x**0.5) or the math.sqrt() function.

print(2**0.5)
# 1.4142135623730951

print(math.sqrt(2))
# 1.4142135623730951

print(2**0.5 == math.sqrt(2))
# True

Like math.pow(), math.sqrt() converts arguments to float. If you provide an argument of a data type that cannot be converted to a float, a TypeError will be raised.

print((-3 + 4j) ** 0.5)
# (1.0000000000000002+2j)

# print(math.sqrt(-3 + 4j))
# TypeError: must be real number, not complex

Also, math.sqrt() cannot process negative values, resulting in a ValueError.

print((-1) ** 0.5)
# (6.123233995736766e-17+1j)

# print(math.sqrt(-1))
# ValueError: math domain error

The ** operator might be inaccurate with complex numbers. For precision, consider the cmath module.

import cmath

print(cmath.sqrt(-3 + 4j))
# (1+2j)

print(cmath.sqrt(-1))
# 1j

Natural exponential functions: math.exp()

To calculate exponentiation using Euler's number, the base of the natural logarithm, use the math.exp() function.

math.exp(x) returns e to the power of x. math.exp(x) provides a more accurate value than using math.e ** x.

print(math.exp(2))
# 7.38905609893065

print(math.exp(2) == math.e**2)
# False

Logarithmic functions: math.log(), math.log10(), math.log2()

To calculate logarithmic functions, use the math.log(), math.log10(), and math.log2() functions.

math.log(x, y) returns the logarithm of x with y as the base.

print(math.log(25, 5))
# 2.0

If the second argument is omitted, the function defaults to calculating the natural logarithm, as demonstrated below.

Natural logarithm

The natural logarithm, which uses a base of e and is represented in mathematics by "log" or "ln", can be calculated using math.log(x).

print(math.log(math.e))
# 1.0

Common logarithm

The common logarithm, which uses a base of 10, can be calculated with math.log10(x). This gives a more accurate value than math.log(x, 10).

print(math.log10(100000))
# 5.0

Binary logarithm

The binary logarithm, which uses a base of 2, can be calculated with math.log2(x). This gives a more accurate value than math.log(x, 2).

print(math.log2(1024))
# 10.0

Related Categories

Related Articles