Home  >  Article  >  Backend Development  >  Python's Math library: usage and introduction to common functions

Python's Math library: usage and introduction to common functions

WBOY
WBOYforward
2023-04-24 23:10:069866browse

    Math library overview

    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.

    The first one: import math Use math.83ebb1fdced5fe59333b6148bf2f4ee2() for the functions in the math library Use the form

    for example:

    import math
    a=math.ceil(10.2)		#向上取整
    print(a)

    11

    The second type: from math import 788a85521940291aa43ea2004446ddaa Right math Functions in the library can be used directly in the form of 788a85521940291aa43ea2004446ddaa()

    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 library analysis

    math Mathematical constants of the library (4 in total)

    ##math.infPositive infinity , negative infinity is math.nannon-floating point number mark,
    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
    - math.inf
    NaN (Not a Number)

    math Numeric representation functions of the library (16 in total)

    function Descriptionmath.fabs(x)Returns the absolute value of x##math. fmod(x,y)math.fsum([x,y,…])math.ceil(x)math.floor(x)math.factorial(x)math.gcd(a,b)math.frexp(x)math.ldexp(x,i)math.modf(x)math.trunc(x)math.copysign(x,y)math.isclose(a,b)##math.isfinite(x)When x is not infinity or NaN, return True; otherwise, return Falsemath.isinf(x)When x is positive or negative infinity, return True; otherwise , returns Falsemath.isnan(x)When x is NaN, returns True; otherwise returns False

    math.fsum([x,y,…]) 函数在数学求和运算中十分有用,例如:

    a=0.1 + 0.2 + 0.3
    print(a)

    0.6000000000000001

    import math
    a=math.fsum([0.1, 0.2, 0.3])
    print(a)

    0.6

    浮点数,如 0.1、0.2 和 0.3,在 Python 解释器内部表示时存在一个小数点后若干位的精度尾数,当浮点数进行运算时,这个精度尾数可能会影响输出结果。因此,在涉及浮点数运算及结果比较时,建议采用 math 库提供的函数,而不直接使用 Python 提供的运算符。

    math 库的幂对数函数(共 8 个)

    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.pow(x,y) xy 返回 x 的 y 次幂
    math.exp(x) ex 返回 e 的 x 次幂,e 是自然对数
    math.expml(x) ex-1 返回 ex 次幂减 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 返回 x2 对数值
    math.log10(x) log10x 返回 x10 对数值

    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!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete