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

Money and Business

Using math, Python's standard module for mathematical functions, you can calculate exponential and logarithmic functions (natural logarithm, ordinary logarithm, and binary logarithm).

The following is explained here, along with sample code.

  • Base of natural logarithm (Napier number):math.e
  • Power::**operator,pow(),math.pow()
  • Square root (root):math.sqrt()
  • Exponential function (natural exponential function):math.exp()
  • a logarithmic function:math.log(),math.log10(),math.log2()

Base of natural logarithm (Napier number): math.e

The base of the natural logarithm (Napier number) is provided as a constant in the math module, denoted by math.e.

import math

print(math.e)
# 2.718281828459045

Power: ** operator, pow(), math.pow(): **operator, pow(), math.pow()

To compute powers, use either the ** operator, the built-in function pow(), or math.pow().

The y-square of x is obtained as follows

  • x**y
  • pow(x, y)
  • math.pow(x, y)
print(2**4)
# 16

print(pow(2, 4))
# 16

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

math.pow() converts the argument to a floating-point type. On the other hand, Python's built-in function pow() uses __pow()__ defined for each type.

For example, pow() allows complex types to be specified as arguments, but math.pow() cannot convert complex types to float types, resulting in an error.

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

# print(math.pow(1 + 1j, 2))
# TypeError: can't convert complex to float

The Python built-in function pow() also allows a third argument, pow(x, y, z), which returns the remainder (remainder) of z to the y-power of x. It is the same calculation as pow(x, y) % z, but pow(x, y, z) is more efficient.

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

Square root (root): math.sqrt()

The square root (root) can be set to **0.5 using ** or math.sqrt().

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 floating-point types for processing, so specifying a type that cannot be converted to a float type will result in a TypeError.

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

# print(math.sqrt(-3 + 4j))
# TypeError: can't convert complex to float

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

Note that when dealing with complex numbers, the example using the ** operator shows an error, but the cmath module provides a more accurate value. Negative values can also be handled.

import cmath

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

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

Exponential function (natural exponential function): math.exp()

To calculate the power of the base of the natural logarithm (Napier number) e, use math.exp().

math.exp(x) returns x squared of e.
math.exp(x) is not equivalent to “math.e ** x” and math.exp(x) is more accurate.

print(math.exp(2))
# 7.38905609893065

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

a logarithmic function: math.log(), math.log10(), math.log2()

To compute the logarithmic function, use math.log(),math.log10(),math.log2().

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 natural logarithm is shown below.

logarithm

In mathematics, the natural logarithm (logarithm with Napier number e as the base), represented by log or ln, can be calculated by math.log(x).

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

logarithm (base 10)

The ordinary logarithm (logarithm with base 10) can be calculated with math.log10(x), which is more accurate than math.log(x, 10).

print(math.log10(100000))
# 5.0

binary logarithm

The binary logarithm (logarithm with base 2) can be calculated with math.log2(x), which is more accurate than math.log(x, 2).

print(math.log2(1024))
# 10.0