Python math module
Python math moduleProvides a large number of commonly used mathematical functions, which can be used to accomplish:
- Trigonometric function calculation (sin / cos / tan)
- Logarithmic and exponential operations (log / exp / pow)
- Rounding and Truncation (ceil / floor / trunc)
- Combinations, permutations, and factorials (comb / perm / factorial)
- Distance and geometric calculation (dist / hypot / sqrt)
It is one of the most commonly used standard libraries for mathematical operations in Python.
Then it is necessary to use.mathModule completed.
mathFunctions in the module return floating-point numbers unless otherwise explicitly stated.
If you need to calculate complex numbers, please usecmathFunctions with the same name in a module.
To use math functions, you must first import them:
import math
View the contents of the math module:
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
Why use the math module?
Although Python comes with many basic operators (such as+、-、*、/), but for more complex mathematical calculations, such as:
- extract the square root
- Trigonometric functions
- natural logarithm
- Floating-point precision handling
Python math module usage examples
import math
print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0
print(math.ceil(4.2)) # 5
print(math.floor(4.9)) # 4
print(math.sin(math.pi/2))# 1.0
print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0
print(math.ceil(4.2)) # 5
print(math.floor(4.9)) # 4
print(math.sin(math.pi/2))# 1.0
The output result is:
4.0 8.0 5 4 1.0
math module constants
| constant | describe |
|---|---|
| math.e | Return Euler's number (2.7182...) |
| math.inf | Return positive infinity floating-point number. |
| math.nan | Return a floating-point value NaN (not a number). |
| math.pi | π generally refers to pi. Pi (3.1415...) |
| math.tau | The mathematical constant τ = 6.283185..., to the available precision. Tau is a circle constant equal to 2π, the ratio of a circle's circumference to its radius. |
math module functions
| function | Description |
|---|---|
| math.acos(x) | Returns the arccosine of x, with the result in the range 0 to pi. |
| math.acosh(x) | Return the inverse hyperbolic cosine of x. |
| math.asin(x) | Returns the arcsine of x, with the result in the range -pi/2 to pi/2. |
| math.asinh(x) | Returns the inverse hyperbolic sine of x. |
| math.atan(x) | Returns the arctangent of x, with the result in the range -pi/2 to pi/2. |
| math.atan2(y, x) | Returns the arctangent of the given X and Y coordinate values, with the result between -pi and pi. |
| math.atanh(x) | Returns the inverse hyperbolic tangent of x. |
| math.ceil(x) | Round x up to the nearest integer. |
| math.comb(n, k) | Return the total number of ways to choose k items from n items without repetition and without order. |
| math.copysign(x, y) | Return a floating-point number based on the absolute value of x and the sign of y. |
| math.cos() | Returns the cosine of x radians. |
| math.cosh(x) | Returns the hyperbolic cosine value of x. |
| math.degrees(x) | Convert angle x from radians to degrees. |
| math.dist(p, q) | Return the Euclidean distance between points p and q, given as a coordinate sequence (or iterable). The two points must have the same dimension. |
| math.erf(x) | Return the error function of a number. |
| math.erfc(x) | Returns the complementary error function at x. |
| math.exp(x) | Returns e to the power of x.x, where e = 2.718281... is the base of the natural logarithm. |
| math.expm1() | Return Ex- 1, e to the power of x, Ex, where e = 2.718281... is the base of the natural logarithm. This is usually more precise than math.e ** x or pow(math.e, x). |
| math.fabs(x) | Return the absolute value of x. |
| math.factorial(x) | Return the factorial of x. If x is not an integer or is negative, then a ValueError will be raised. |
| math.floor() | Round a number down to the nearest integer. |
| math.fmod(x, y) | Return the remainder of x/y. |
| math.frexp(x) | Return the mantissa and exponent of x as a pair (m, e). m is a floating-point number, e is an integer, such that x == m * 2**e exactly. If x is zero, return (0.0, 0); otherwise, 0.5 <= abs(m) < 1. |
| math.fsum(iterable) | Return the sum of elements in an iterable (tuple, array, list, etc.), as a floating-point value. |
| math.gamma(x) | Returns the value of the Gamma function at x. |
| math.gcd() | Return the greatest common divisor of the given integer parameters. |
| math.hypot() | Return the Euclidean norm, sqrt(sum(x**2 for x in coordinates)). This is the length of the vector from the origin to the point given by the coordinates. |
| math.isclose(a,b) | Check whether two values are close to each other; if the values of a and b are relatively close, return True, otherwise return False. |
| math.isfinite(x) | Determine whether x is finite; if x is neither infinity nor NaN, return True, otherwise return False. |
| math.isinf(x) | Determine whether x is infinity. If x is positive or negative infinity, return True; otherwise, return False. |
| math.isnan() | Determine whether the number is NaN. If x is NaN (not a number), return True; otherwise return False. |
| math.isqrt() | Round the square root down to the nearest integer. |
| math.ldexp(x, i) | Return x * (2**i). This is basically a function.The inverse function of math.frexp(). |
| math.lgamma() | Returns the natural logarithm of the gamma function at the absolute value of x. |
| math.log(x[, base]) | Using one parameter, returns the natural logarithm of x (base e). |
| math.log10(x) | Return the base-10 logarithm of x. |
| math.log1p(x) | Returns the natural logarithm (base e) of 1+x. |
| math.log2(x) | Return the base-2 logarithm of x. |
| math.perm(n, k=None) | Return the total number of ways to choose k items from n items without repetition and with order. |
| math.pow(x, y) | Returns x raised to the power of y. |
| math.prod(iterable) | Compute the product of all elements in the iterable. |
| math.radians(x) | Convert angle x from degrees to radians. |
| math.remainder(x, y) | Return the IEEE 754-style remainder of x divided by y. |
| math.sin(x) | Returns the sine of x radians. |
| math.sinh(x) | Return the hyperbolic sine value of x. |
| math.sqrt(x) | Return the square root of x. |
| math.tan(x) | Returns the tangent of x radians. |
| math.tanh(x) | Returns the hyperbolic tangent of x. |
| math.trunc(x) | Returns the truncated integer part of x, that is, returns the integer part and removes the fractional part. |
| math.cbrt(x) | Return the cube root of x. |
| math.exp2(x) | |
| math.lcm(*integers) | Return the least common multiple of multiple integers. |
| math.modf(x) | Return the fractional part and integer part of x, the result is a tuple (fractional, integer). |
| math.nextafter(x, y[, steps]) | Returns the value after advancing a specified floating-point number of steps from x toward y. |
| math.sumprod(p, q) | Return the sum of the products of corresponding elements of two iterables. |
| math.ulp(x) | Returns the smallest significant floating-point unit of x (Unit in the Last Place). |
Version Compatibility Notes
The following functions are new in Python 3.8+ / 3.9+ / 3.11+:
math.comb():Python 3.8+math.perm():Python 3.8+math.lcm():Python 3.9+math.cbrt():Python 3.11+math.exp2():Python 3.11+math.sumprod():Python 3.12+