美文网首页
Python-math库

Python-math库

作者: JUNjianshuZHU | 来源:发表于2018-05-25 16:44 被阅读0次

    转载来源: python中math模块常用的方法整理

    • 导入函数
    import math
    

    • e
    #表示一个常量
    
    >>> math.e
    2.718281828459045
    

    • exp
    #返回math.e,也就是2.71828的x次方
    exp(x)
    
    >>> math.exp(1)
    2.718281828459045
    >>> math.exp(2)
    7.38905609893065
    >>> math.exp(3)
    20.085536923187668
    

    • pi
    #数字常量,圆周率
    
    >>> print(math.pi)
    3.141592653589793
    

    • ceil
    #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ceil(x)
    
    >>> math.ceil(4.01)
    5
    >>> math.ceil(-3.99)
    -3
    

    • floor
    #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    floor(x)
    
    >>> math.floor(4.999)
    4
    >>> math.floor(-4.01)
    -5
    

    • copysign
    #把y的正负号加到x前面,可以使用0
    copysign(x, y)
    
    >>> math.copysign(2,3)
    2.0
    >>> math.copysign(2,-3)
    -2.0
    

    • fabs
    #返回x的绝对值
    fabs(x)
    
    >>> math.fabs(-0.003)
    0.003
    >>> math.fabs(100)
    100.0
    

    • trunc
    #返回x的整数部分
    trunc(x:Real) -> Integral
    
    >>> math.trunc(6.789)
    6
    >>> math.trunc(math.pi)
    3
    

    • factorial
    #取x的阶乘的值,x为负数或非数值则报错
    factorial(x) -> Integral
    
    >>> math.factorial(1)
    1
    >>> math.factorial(2)
    2
    >>> math.factorial(3)
    6
    

    • gcd
    #返回x和y的最大公约数
    gcd(x, y) -> int
    
    >>> math.gcd(8,6)
    2
    >>> math.gcd(40,20)
    20
    

    • hypot
    #得到(x**2+y**2),平方的值的开根号
    hypot(x, y)
    
    >>> math.hypot(3,4)
    5.0
    >>> math.hypot(6,8)
    10.0
    

    • isinf
    #如果x是正无穷大或负无穷大,则返回True,否则返回False
    isinf(x) -> bool
    
    >>> math.isinf(234)
    False
    >>> math.isinf(0.1)
    False
    

    • log
    #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x) or log(base)
    
    >>> math.log(10)
    2.302585092994046
    >>> math.log(8,2)
    3.0
    

    • log10
    #返回x的以10为底的对数
    log10(x)
    
    >>> math.log10(10)
    1.0
    >>> math.log10(100)
    2.0
    

    • modf
    #返回由x的小数部分和整数部分组成的元组
    modf(x)
    
    >>> math.modf(math.pi)
    (0.14159265358979312, 3.0)
    >>> math.modf(12.34)
    (0.33999999999999986, 12.0)
    

    • sqrt
    #求x的平方根
    sqrt(x)
    
    >>> math.sqrt(100)
    10.0
    

    • pow
    #返回x的y次方,即x**y
    pow(x, y)
    
    >>> math.pow(3,4)
    81.0
    

    • degrees
    #把x从弧度转换成角度
    degrees(x)
    
    >>> math.degrees(math.pi/4)
    45.0
    >>> math.degrees(math.pi)
    180.0
    

    • radians
    #把角度x转换成弧度
    radians(x)
    
    >>> math.radians(45)
    0.7853981633974483
    

    • sin
    #求x(x为弧度)的正弦值
    sin(x)
    
    >>> math.sin(math.pi/4)
    0.7071067811865475
    >>> math.sin(math.pi/2)
    1.0
    

    • cos
    #求x的余弦,x必须是弧度
    cos(x)
    
    #math.pi/4表示弧度,转换成角度为45度
    >>> math.cos(math.pi/4)
    0.7071067811865476
    math.pi/3表示弧度,转换成角度为60度
    >>> math.cos(math.pi/3)
    0.5000000000000001
    

    • tan
    #返回x(x为弧度)的正切值
    tan(x)
    
    >>> math.tan(math.pi/4)
    0.9999999999999999
    >>> math.tan(math.pi/6)
    0.5773502691896257
    

    相关文章

      网友评论

          本文标题:Python-math库

          本文链接:https://www.haomeiwen.com/subject/dkjujftx.html