>>> 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. 理解JAVA与C的运行机制

    1.java的运行机制 java的编译过程,将java的源程序(扩展名为.java的文件),由java编译程序将java的字节码文件(.class文件)在jvm上运行,机器码有cpu运行, jvm编译 ...

  2. J.U.C 系列 Tools之Executors

    上个章节说了Tools中的其他四个工具类,本节我们来看一看工具类中的老大Executors,为什么说它是老大,肯定是因为他的功能最多最强大. 一 Executors是什么 Executors 是一个线 ...

  3. 1864: [Zjoi2006]三色二叉树

    1864: [Zjoi2006]三色二叉树 链接 分析: 做得最智障的一题了... 首先中间输出两个数之间没空格(换行居然也过了...), 写了dp[i][0/1/2],后来知道其实dp[i][0/1 ...

  4. Go实现try-catch-finally机制

    前言 许多主流语言诸如:Java.Python都实现了try-catch-finally机制,而Go处理错误的方式却与前两种语言不同.关于Go处理异常的方式是好是坏仁者见仁智者见智,笔者还是更喜欢tr ...

  5. laravel - ReflectionException in Container.php, Class not found?

    SIGN UPSIGN IN CATALOG SERIES PODCAST DISCUSSIONS ReflectionException in Container.php, Class not fo ...

  6. Centos 7.X 安装JDK1.8

    一.查看本机jdk版本并卸载原有openjdk        查看       # java -version       openjdk version "1.8.0_144"  ...

  7. pip 代理设置,坑爹的代理继续

    Linux ubuntu 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Lin ...

  8. 《Cracking the Coding Interview》——第18章:难题——题目2

    2014-04-29 00:59 题目:设计一个洗牌算法,效率尽量快点,必须等概率. 解法:每次随机抽一张牌出来,最后都抽完了,也就洗好了.时间复杂度O(n^2),请看代码. 代码: // 18.2 ...

  9. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目1

    2014-03-20 02:55 题目:小朋友跳台阶,每次跳1层或2层,那么跳N层总共有多少种跳法. 解法:斐波那契数列. 代码: // 9.1 A child can run up the stai ...

  10. ajax向Asp.NET后端传递数组型数据

    近日,在开发一个组件的过程中,需要通过Ajax对象向Asp.NET后端传递一个比较复杂的表单,表单中的一个字段是数组类型,我能想到的办法是用JSON.stringify将前端的数组对象序列化成字符串, ...