# Module math. Power and logarithmic functions

### Contents

Search other websites:

##### 1. Function math.exp(x). Exponent raised to the power x

The math.exp(x) function raises the number e to the power of x. The function returns the result of a real type. The argument x can be an integer or a real type. Exponential value: e = 2.718281… serves as the basis of the natural logarithm.

In Python, the math.exp(x) function can be replaced with other expressions

1. math.e ** x – here math.e is a constant equal to the value of the exponent.
2. pow(math.e, x) – here pow() is a built-in function of the Python language.

Example.

```# Function math.exp(x)
import math

y = math.exp(1) # y = 2.718281828459045

x = 0.0
y = math.exp(x) # y = 1.0

x = 3.85
y = math.exp(x) # y = 46.993063231579285```

### ⇑

##### 2. Function math.expm1(x). Exponent from x minus 1

The math.expm1(x) function calculates the value of the exp(x)-1 expression. When calculating the value of some y, the function call

`y = math.expm1(x)`

can be replaced by the expression

`y = math.exp(x)-1`

However, using the math.expm1(x) function will give a more accurate calculation result. This is the main purpose of this function.

Example.

```# Function math.expm1(x)
import math

x = 1.0
y = math.expm1(x) # y = 1.718281828459045

y = math.expm1(0.0) # y = 0.0```

### ⇑

##### 3. Function math.log(x). Natural logarithm

The math.log(x) function is designed to calculate the natural logarithm of a number with a given base.

The general form of the function is as follows

`math.log(x [, base])`

here

• x – the argument for which the logarithm is calculated;
• base – base of the logarithm. This function parameter is optional. If the base parameter is absent, then the number e = 2.718281…

If you try to call the log(0.0) function, the Python interpreter will throw an error

`ValueError: math domain error`

since the logarithm of zero does not exist.

Example.

```# Function math.log(x)
import math

x = 1.0
y = math.log(x) # y = 0.0```

### ⇑

##### 4. Function math.log1p(x). Logarithm for values close to zero

The log1p(x) function returns the natural logarithm of 1 + x. The basis of the logarithm is the exponent e = 2.718281… The function is necessary in cases when the value of argument x approaches zero. As you know, the logarithm of zero does not exist. To avoid an exception, this function is introduced.

Example.

```# Function math.log1p(x)
import math

x = 0.0000001
y = math.log1p(x) # y = 9.999999500000032e-08```

### ⇑

##### 5. Function math.log2(x). Logarithm with base 2

The math.log2(x) function has been introduced since Python 3.3 and returns the logarithm of the argument x with base 2. The function was introduced in order to increase the accuracy of calculations in comparison with the function math.log(x, 2). The argument x can be either an integer or a real type.

Example.

```# Function math.log2(x)
import math

x = 2
y = math.log2(x) # y = 1.0

x = 16
y = math.log2(x) # y = 4.0```

### ⇑

##### 6. Function math.log10(x). Decimal logarithm

The math.log10(x) function returns the logarithm of x with base 10 (base = 10). The function gives a more accurate result compared to calling the math.log(x, 10) function. The argument x can be either an integer or a real type.

Example.

```# Function math.log10(x)
import math

x = 10
y = math.log10(x) # y = 1.0

x = 100
y = math.log10(x) # y = 2.0

x = 10.00001
y = math.log10(x) # y = 1.0000004342942648```

### ⇑

##### 7. Function math.pow(x, y). Exponentiation

The math.pow (x, y) function raises x to the power of y. The arguments x, y can be of integer or real type. Operands of complex type are not supported.

Features of calculating the result:

• the result of pow(1.0, y) will always be 1.0;
• the result of pow (0.0, y) will always be 1.0.

In contrast to the operation ** (exponentiation), the function math.pow(x, y) integer operands converts to the real type float.

Example.

```# Function math.pow(x, y)
import math

# for integer operands
x = 3
y = 4
z = math.pow(x, y) # z = 81.0 - real result

# for floating point operands
x = 2.5
y = 1.5
z = math.pow(x, y) # z = 3.952847075210474

# negative numbers
x = -2
y = -3
z = math.pow(x, y) # z = -0.125

x = -2.0
y = 3.0
z = math.pow(x, y) # z = -8.0

# operator **
z = (-2) ** 3 # z = -8 - the result of integer type```

### ⇑

##### 8. Function math.sqrt(x). Square root

The math.sqrt(x) function computes the square root of the argument x. The function returns the result of a real type. The value of x can be positive or zero. If x is negative, the interpreter will display an error message

`math domain error`

Example.

```# Function math.sqrt(x)
import math

# for integer numbers
x = 81
y = math.sqrt(x) # y = 9.0

x = -0.0
y = math.sqrt(x) # y = -0.0

x = 2.0
y = math.sqrt(x) # y = 1.4142135623730951```