Python. Module math. Special Functions and Constants




Module math. Special Functions and Constants


Contents


Search other websites:

1. Function math.erf(x). Error function

The math.erf(x) function in Python is designed to calculate the error function of the argument x. The error function is also called the Gaussian error function and is determined by the formula

Python. Gaussian error function. Formula

You can learn more about the features of using the error function from other sources.

Example.

# Function math.erf(x)
import math

x = 1.5
y = math.erf(x) # y = 0.9661051464753108

x = 0
y = math.erf(x) # y = 0.0

  

2. Function math.erfc(x). Complementary error function

The math.erfc(x) function is used to compute an complementary error function at point x. An additional error function is defined as

1.0 - math.erf(x)

The math.erfc(x) function is used when x is large. For large x values, loss of significance may occur. To avoid this, this function is used.
The math.erfc(x) function is used in Python since version 3.2.

Example.

# Function math.erfc(x)
import math

x = 1.5
y = math.erfc(x) # y = 0.033894853524689274

x = 0
y = math.erfc(x) # y = 1.0

  

3. Function math.gamma(x). Gamma function

The math.gamma(x) function returns the gamma function of argument x. The gamma function is calculated by the formula:

Python. Gamma function. Formula

More information on using the Gamma function can be found in other sources.
The math.gamma (x) function has been introduced in Python since version 3.2.

Example.

# Function math.gamma(x)
import math

x = 1.0
y = math.gamma(x) # y = 1.0

x = -2.2
y = math.gamma(x) # y = -2.2049805184191333

x = 3.8
y = math.gamma(x) # y = 4.694174205740421

  

4. Function math.lgamma(x). Natural logarithm of Gamma function

The math.lgamma(x) function returns the natural logarithm of the absolute value of the Gamma function of the argument x. This function has been introduced in Python since version 3.2.

Example.

# Function math.lgamma(x)
import math

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

x = 2.7
y = math.lgamma(x) # y = 0.4348205536551042


  

5. Constant math.pi. Number π

The constant math.pi defines the number π to available precision.

Example.

# Constant math.pi
import math

y = math.pi # y = 3.141592653589793

# Calculating the area of a circle
r = 2.0
s = math.pi*r*r # s = 12.566370614359172

  

6. Constant math.e. Exponent

The constant math.e defines the value of the exponent to available precision.

Example.

# Constant math.e - exponent
import math

y = math.e # y = 2.718281828459045

x = 1.5
y = math.e**x # y = 4.4816890703380645

  

7. Constant math.tau. Number 2·π

The math.tau constant defines a 2·π number to available precision. The value of math.tau is equal to the ratio of the circumference of a circle to its radius. The constant has been used in Python since version 3.6.

Example.

# Constant math.tau
import math

y = math.tau # y = 6.283185307179586

# Calculation of the circumference of radius r = 2
r = 2.0
length = math.tau*r # length = 12.566370614359172

  

8. Constant math.inf. Positive infinity

The math.inf constant defines positive floating point infinity. To determine negative infinity, use –math.inf.

The constant math.inf is equal to

float('inf')

Constant introduced in Python since version 3.5.

Example.

# Constant math.inf
import math
y = math.inf # y = inf - positive infinity
print('y = ', y)

y = float('inf') # y = inf
print('y = ', y)

After starting the program, the following result will be obtained

y = inf
y = inf

  

9. Constant math.nan. NaN value (not a number)

The math.nan constant was introduced in Python version 3.6 and is equal to the value of NaN with a floating point. The value of NaN may occur in cases where the result of the calculation is undefined. An example of such a calculation would be dividing zero by zero, multiplying zero by infinity.

You can also determine if the result is equal a NaN value using the math.isnan(x) function.

The math.nan constant has been introduced in Python since version 3.5. The value of the constant is equivalent to the value

float('nan')

Example.

# Constant math.nan
import math

y = math.nan # y = nan
print('y = ', y)

y = float('nan') # y = nan
print('y = ', y)

The result of the program

y = nan
y = nan

  


Related topics