>>> import math
>>>
>>> # ceil,取大于等于x的最小的整数值
>>> math.ceil(4)
4
>>> math.ceil(4.1)
5
>>> math.ceil(-3)
-3
>>> math.ceil(-3.1)
-3
>>> math.ceil(-2.9)
-2
>>>
>>>
>>> #copysign(x, y),把y的符号加到x上面去
>>> math.copysign(1, 2)
1.0
>>> math.copysign(1, -2)
-1.0
>>> math.copysign(-1, -2)
-1.0
>>> math.copysign(-1, 2)
1.0
>>> # 所以很明显,符号不存在叠加,否则math.copysign(-1, -2)就变成1了
>>> # math.copysign(x, y),伪代码大概就相当于 |x| if y为正 else -|x|
>>>
>>>
>>> # cos(x),sin(x),tan(x),求x的余弦,正弦和正切
>>> # 这里的x必须是弧度
>>> math.cos(math.pi / 3)
0.5000000000000001
>>> math.sin(math.pi / 6)
0.49999999999999994
>>> math.tan(math.pi / 4)
0.9999999999999999
>>>
>>>
>>> # degrees(x),将x从弧度转成角度
>>> math.degrees(math.pi / 4)
45.0
>>>
>>>
>>> # e,表示自然对数的底
>>> math.e
2.718281828459045
>>>
>>>
>>> # exp(x),返回e的x次方
>>> math.exp(1)
2.718281828459045
>>> math.exp(2)
7.38905609893065
>>>
>>>
>>> # expm1(x),返回e的x次方减1
>>> math.expm1(1)
1.718281828459045
>>>
>>>
>>> # fabs(x),返回x的绝对值
>>> math.fabs(-0.5)
0.5
>>>
>>> math.fabs(-100)
100.0
>>> abs(-0.5)
0.5
>>> abs(-100)
100
>>> # 和内置函数abs类似,前者返回浮点,后者返回整型
>>>
>>>
>>>
>>> # factorial(x),取x的阶乘
>>> math.factorial(10)
3628800
>>>
>>>
>>> # floor(x),返回小于等于x的最大整数,和ceil相反
>>> math.floor(2.7)
2
>>> math.floor(-1.8)
-2
>>>
>>>
>>> # fmod(x, y),返回x除以y的余数
>>> math.fmod(3, 2)
1.0
>>> divmod(3, 2)
(1, 1)
>>>
>>>
>>> # frexp(x),个人觉得没什么乱用的函数。首先让x分别除以0.5和1(不包括两端)
>>> # 得到一个范围,然后找到满足2**n位于这个范围内最大的n
>>> # 然后m = x / 2**n,最终返回(m, n)
>>> math.frexp(20)
(0.625, 5)
>>> # (20, 40),所以是n = 5,然后20 / 2**5
>>>
>>>
>>> # fsum(iterable),和sum类似
>>> math.fsum([1, 2, 3, 4])
10.0
>>> sum([1, 2, 3, 4])
10
>>>
>>>
>>>
>>> # gcd(x, y),返回x和y的最大公约数
>>> math.gcd(15, 12)
3
>>> math.gcd(24, 12)
12
>>> math.gcd(13, 7)
1
>>>
>>> >>> # hypot(x, y),得到x**2 + y**2的平方根
>>> math.hypot(3, 4)
5.0
>>>
>>>
>>> # isfinite(x),如果x不是无穷大的数字,返回True,否则返回False
>>> math.isfinite(100)
True
>>>
>>>
>>> # isinf(x),如果x是无穷大,返回True,否则返回False
>>> math.isinf(234)
False
>>>
>>>
>>> # ldexp(x, i),返回x*(2**i)
>>> math.ldexp(5, 5)
160.0
>>>
>>>
>>> # log(x),返回x的对数,底默认为e,base参数可以指定底数
>>> math.log(math.e)
1.0
>>> math.log(9, 3)
2.0
>>>
>>>
>>> # modf(x),返回由小数部分和整数部分组成的元组
>>> math.modf(3.14)
(0.14000000000000012, 3.0)
>>> math.modf(3)
(0.0, 3.0)
>>>
>>>
>>> # pi,圆周率
>>> math.pi
3.141592653589793
>>>
>>>
>>> # pow(x, y),返回x的y次方
>>> math.pow(3, 4)
81.0
>>> # 和pow()类似
>>> pow(3, 4)
81
>>> pow(3, 4, 2)
1
>>> # pow还可以有第三个参数
>>> pow(2, 4, 11)
5
>>> # pow(x, y, z) == x ** y % z
>>>
>>>
>>> # radians,把角度x转换成弧度
>>> math.radians(45)
0.7853981633974483
>>>
>>>
>>> # sqrt(x),求x的平方根
>>> math.sqrt(16)
4.0
>>>
>>>
>>> # trunc(x),返回x的整数部分
>>> math.trunc(math.pi)
3
>>> math.trunc(4.4444)
4

  

python--math的更多相关文章

  1. Note of Python Math

    Note of Python Math math 库是Python 提供的内置数学类函数库,而其中复数类型常用于科学计算,一般计算并不常用,因此math 库不支持复数类型.math 库一共提供4个数学 ...

  2. Python math 模块、cmath 模块

    Python math 模块.cmath 模块 Python 中数学运算常用的函数基本都在 math 模块.cmath 模块中.高佣联盟 www.cgewang.com Python math 模块提 ...

  3. python math详解(1)

    python math详解(1) 一.导入 python要调用math要进行导入 import math 二.返回值 math包里有一些值 比如 math.pi 返回pi的值 约为3.14 math. ...

  4. python math random

    很有用个的工具 值得好好看看,这是作者(python发明者)对于工作中使用到的大多数场景的提炼 //test.py 1 import math 2 3 print abs(-10) 4 print m ...

  5. python math.asin

    import mathmath.asin(x) x : -1 到 1 之间的数值.如果 x 是大于 1,会产生一个错误. #!/usr/bin/pythonimport math print &quo ...

  6. 第12.3节 Python math模块导览

    math 模块提供对浮点数学的底层C库函数的访问,常用的成员包括: math.ceil(x):返回 x 的上限,即大于或者等于 x 的最小整数 math.floor(x):返回 x 的向下取整,小于或 ...

  7. Python math库常用函数

    math库常用函数及举例: 注意:使用math库前,用import导入该库>>> import math 取大于等于x的最小的整数值,如果x是一个整数,则返回x>>> ...

  8. python math模块

    import math math. ceil:取大于等于x的最小的整数值,如果x是一个整数,则返回x copysign:把y的正负号加到x前面,可以使用0 cos:求x的余弦,x必须是弧度 degre ...

  9. python math 模块

    数学模块 引入模块:import math 注意: 使用某个模块下的函数,必须先引入这个模块,否则无法正常使用. ceil() 向上取整操作 格式:math.ceil(数值) 返回值:整型 floor ...

  10. Python math库和random库

    1.math库 >>> from math import * >>> 2*pi 6.283185307179586 >>> e 2.7182818 ...

随机推荐

  1. Hive 压缩技术Data Compression

    Mapreducwe 执行流程 :input > map > shuffle > reduce > output 压缩执行时间,map 之后,压缩,数据存储在本地磁盘,减少磁盘 ...

  2. saltstack执行远程命令

    目录 Remote Execution salt state salt state 系统 salt state 系统流程 Runner salt runner Orchestrate Runner S ...

  3. [Jenkins]持续集成环境下fingbug插件的安装使用与配置

    参考:https://wiki.jenkins.io/display/JENKINS/FindBugs+Plugin 突然,天降杂事.我是想安安静静的做个美丽的测试...但是事与愿违,项目经理叫我帮忙 ...

  4. JAVA中使用AES加密解密

    技术交流群: 233513714 /** * AES加密测试 * * @param str 加密参数 */ public void aesTest(String str) { log.info(&qu ...

  5. nodejs安装&bower 安装

    1.进入官网下载:https://nodejs.org/en/ 2.直接进行安装,可以将安装路径设置为:D:\nodejs 3.进入node.js command prompt 命令窗口 4.检测是否 ...

  6. 《Cracking the Coding Interview》——第8章:面向对象设计——题目2

    2014-04-23 17:45 题目:假设有个呼叫中心,有接线员.经理.主管三种角色.如果接线员无法处理呼叫,就上传给经理:如果仍无法处理,则上传给主管.请用代码描述这一过程. 解法:第一眼觉得这题 ...

  7. 【Insert Interval】cpp

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  8. paramiko类Fabric主机管理

    环境:Linux python3.5 要求:类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/ ...

  9. Learn the shell

    learn the shell what is the shell? when we speak of the command line,we are really to the shell.Actu ...

  10. Java性能监控之Java程序执行解析

    大家好,最近接触javassist技术,研究过程中对Java程序执行过程进行了一系列探索,弄清楚了几个盲区(仅针对个人而言),现将经验与大家分享. 1.编码->.java 通常指写代码的过程,最 ...