Home > Article > Backend Development > Python's Math library: usage and introduction to common functions
math The library is a built-in mathematical function library provided by Python, because complex numbers Types are often used in scientific calculations, but not in general calculations. Therefore, the math library does not support complex number types and only supports integer and floating point number operations.
math The library provides a total of 4 mathematical constants and 44 functions. 44 functions are divided into 4 categories, including 16 numerical representation functions, 8 power logarithmic functions, 16 trigonometric functions and 4 advanced special functions.
math There are a large number of functions in the library. During the learning process, we only need to understand the functions one by one and remember some commonly used functions. In actual programming, if you need to use the math library, you can check the math library quick reference at any time.
math The functions in the library cannot be used directly. You need to use the reserved words import to reference the library first. The reference method is as follows.
for example:
import math a=math.ceil(10.2) #向上取整 print(a)
11
For example:
from math import floor a=floor(10.2) #向下取整 print(a)
10
Another form of the second is from math import *. If the math library is introduced in this way, all functions in the math library can be used directly in the form 788a85521940291aa43ea2004446ddaa().
math References to libraries and other function libraries can be freely selected from these two methods.
math Mathematical constants of the library (4 in total)
Constant | Description |
math.pi | Pi, the value is 3.141 592 653 589 793 |
math.e | Natural logarithm, the value is 2.718 281 828 459 045 |
Positive infinity , negative infinity is | - math.inf |
non-floating point number mark, | NaN (Not a Number) |
math Numeric representation functions of the library (16 in total)
Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns the absolute value of x | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return the modulus of x and y | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Float Accurate sum of points | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Round up and return the smallest integer that is not less than x | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Round down and return the largest integer not greater than x | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return Factorial of x, if x is a decimal or negative number, return ValueError | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return the greatest common divisor of a and b | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
means x = m*2e, return (m,e), when x =0, return (0.0, 0) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return x*2i operation value, the inverse operation of math.frexp(x) function | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return the decimal and integer parts of x | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return the integer part of x | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Replace the sign of value x with the sign of value y | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compare the similarity between a and b, return True or False | ##math.isfinite(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
math.isinf(x) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
math.isnan(x) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
函数 | 数学表示 | 描述 |
math.pow(x,y) | xy | 返回 x 的 y 次幂 |
math.exp(x) | ex | 返回 e 的 x 次幂,e 是自然对数 |
math.expml(x) | ex-1 | 返回 e 的 x 次幂减 1 |
math.sqrt(x) | √x | 返回 x 的平方根 |
math.log(x[,base]) | logbasex | 返回 x 的对数值,只输入 x 时,返回自然对数,即 ln x |
math.log1p(x) | ln(1+x) | 返回 1+x 的自然对数值 |
math.log2(x) | log2x | 返回 x 的 2 对数值 |
math.log10(x) | log10x | 返回 x 的 10 对数值 |
math 库的三角运算函数(共 16 个)
函数 | 数学表示 | 描述 |
math.degrees(x) | 无 | 角度 x 的弧度值转角度值 |
math.radians(x) | 无 | 角度 x 的角度值转弧度值 |
math.hypot(x,y) | √x2+y2 | 返回 (x,y) 坐标到原点的距离 |
math.sin(x) | sin x | 返回 x 的正弦函数值,x 是弧度值 |
math.cos(x) | cos x | 返回 x 的余弦函数值,x 是弧度值 |
math.tan(x) | tan x | 返回 x 的正切函数值,x 是弧度值 |
math.asin(x) | arcsin x | 返回 x 的反正弦函数值,x 是弧度值 |
math.acos(x) | arccos x | 返回 x 的反余弦函数值,x 是弧度值 |
math.atan(x) | arctan x | 返回 x 的反正切函数值,x 是弧度值 |
math.atan2(x,y) | arctan y/x | 返回 y/x 的反正切函数值,x 是弧度值 |
math.sinh(x) | sinh x | 返回 x 的双曲正弦函数值 |
math.cosh(x) | cosh x | 返回 x 的双曲余弦函数值 |
math.tanh(x) | tanh x | 返回 x 的双曲正切函数值 |
math.asinh(x) | atcsinh x | 返回 x 的反双曲正弦函数值 |
math.acosh(x) | arccosh x | 返回 x 的反双曲余弦函数值 |
math.atanh(x) | arctanh x | 返回 x 的反双曲正切函数值 |
math 库的高等特殊函数(共 4 个)
math.erf(x) | 高斯误差函数,应用于概率论、统计学等领域 |
math.erfc(x) | 余补高斯误差函数,math.erfc(x)= 1 - math.erf(x) |
math.gamma(x) | 伽玛(Gamma)函数,也叫欧拉第二积分函数 |
math.lgamma(x) | 伽玛函数的自然对数 |
The above is the detailed content of Python's Math library: usage and introduction to common functions. For more information, please follow other related articles on the PHP Chinese website!