zoukankan      html  css  js  c++  java
  • Python的math模块

    常用模块之math模块

    Python官方提供了众多模块,这里介绍一些常用模块,不常用的可以去查询Python官方的API文档


    Python官方提供math模块进行数学运算,如指数对数平方根三角函数等运算。math模块中只包括整数浮点数,不包括复数,复数需要用到cmath

    舍入函数

    math模块提供的舍入函数有math.ceil(a)math.floor(a),math.ceil(a)用来返回大于或等于a的最小整数,math.floor(a)返回小于或等于a的最大整数

    >>> from math import *
    >>> ceil(1.4)
    2
    >>> floor(1.4)
    1
    >>> round(1.4)
    1
    
    
    >>> ceil(1.5)
    2
    >>> floor(1.5)
    1
    >>> round(1.5)
    2
    
    
    >>> ceil(1.6)
    2
    >>> floor(1.6)
    1
    >>> round(1.6)
    2
    

    幂和对数函数

    math模块提供的对数函数如下所示。

    • 对数运算:math.log(a[,base])返回以base为a的对数,省略底数base,是a的自然对数
    • 平方根:math.sqrt(a)返回a的平方根
    • 幂运算:math.pow(a, b)返回a的b次幂
      在PythonShell
    >>> from math import *
    >>> log(8, 2)
    3.0
    >>> pow(8, 2)
    64.0
    >>> log(8)
    2.0794415416798357
    >>> sqrt(1.6)
    1.2649110640673518
    

    三角函数

    为了简单,于是我省略了math.

    • sin(a):返回弧度a的三角正弦
    • cos(a):返回弧度a的三角余弦
    • tan(a):返回弧度a的三角正切
    • asin(a):返回弧度a的反正弦
    • acos(a):返回弧度a的反余弦
    • atan(a):返回弧度a的反正切
    • degrees(a):将弧度a转换为角度
    • radians(a):将角度a转换为弧度
  • 相关阅读:
    kubectl 命令详解
    codeforce344 C report
    poj3041 建图+最小顶点覆盖(最大匹配数)
    poj1637 混合欧拉回路的判定
    poj1149 最大流好题 难在建图 好题
    targan 算法模板
    poj2186 强连通分量 targan算法的应用
    poj2723 2分 + 2-sat
    poj3061 尺取法
    poj3207 2-sat基础题
  • 原文地址:https://www.cnblogs.com/coding365/p/12872233.html
Copyright © 2011-2022 走看看