>>> 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. Maven学习 (五) Elipse中发布一个Maven项目到Tomcat

    对于maven初学者的我,经常遇到一个问题就是,maven项目创建成功后,本来已经添加了jar的依赖,但是发布到Tomcat中就是没有jar包存在, 启动Tomcat总是报没有找到jar包,可项目结构 ...

  2. 【HTML&CSS】 第二章:标准模式下的页面与怪异模式下的页面区别

    盒模型 前面提到,盒模型(box mode)是浏览器 Quirks Mode 和 Standards Mode 的主要区别. 描述 对于“盒模型”一词并没有明确的文档定义,它是开发人员描述 CSS 中 ...

  3. 【Insert Interval】cpp

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

  4. http客户端缓存

    这篇文章写得比较,点击查看

  5. JMeter学习笔记(三) 录制脚本

    jmeter测试脚本,可以通过其他工具进行录制,例如 BadBoy,我之前使用过此工具,安装以及使用都比较简单的,大家可以在网上搜索一下. 在此整理一下jmeter自带的录制功能,进行录制脚本. 1. ...

  6. 【java并发编程实战】第一章笔记

    1.线程安全的定义 当多个线程访问某个类时,不管允许环境采用何种调度方式或者这些线程如何交替执行,这个类都能表现出正确的行为 如果一个类既不包含任何域,也不包含任何对其他类中域的引用.则它一定是无状态 ...

  7. Python全栈工程师(每周总结:1)

    ParisGabriel   python今年9月份将被国家纳入计算机二级资格证 先学就是鼻祖 几年后你就是大牛      Python人工智能从入门到精通         week summer: ...

  8. diskimage-builder-command

    yum -y install python-virtualenv.noarch virtualenv ~/dib-virtualenv . ~/dib-virtualenv/bin/activate ...

  9. C# http Post与Get方法控制继电器

    ---恢复内容开始--- using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  10. PAT——甲级1046S:shortest Distance

    这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N ex ...