>>> 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. struct2 命名空间

    转自http://blog.csdn.net/carefree31441/article/details/4857546 使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s: ...

  2. 10 class封装 ORM

    1.版本1:初始化 # -*- coding:utf-8 -*- from MySQLdb import * class MysqlHelper: def __init__(self,host,por ...

  3. CSAcademy Palindromic Concatenation 字符串哈希

    题意: 题目链接 给出\(n\)个字符串,求有多少对\((i,j),i \neq j\)使得\(s_i\)与\(s_j\)拼起来是回文串 分析: 设\(s_i,s_j\)的长度分别为\(L_i, L_ ...

  4. 解决Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0

    解决办法是你需要将工程根目录build.gradle中的 classpath 'com.android.tools.build:gradle:2.2.0' 更改成 classpath 'com.and ...

  5. string函数Contains()实例

    public bool Contains(string value)如果值参数出现在此字符串内,或者值为空字符串(“”),则为true; 否则为false using System; class Ex ...

  6. 【Theory of Generalization】林轩田机器学习基石

    紧接上一讲的Break Point of H.有一个非常intuition的结论,如果break point在k取到了,那么k+1, k+2,... 都是break point. 那么除此之外,我们还 ...

  7. iframe 如何让它展现内容自适应高度

    引用: <iframe id="ifm1" runat="server" src="/comment/page1?id=@productId&q ...

  8. Hastable和Dictionary以及ArrayList和(List,LinkedList,数组)的区别

    Hastable和Dictionary的区别:(键值对) 1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分. 2:多线程程序中推荐使用 Hashtabl ...

  9. install.cloudinit.qga.bat

    @echo off title Auto Install color 1F ::CloudBase-Init echo. msiexec /i \\192.168.122.47\cloudbase\C ...

  10. 第三方库的安装:Pangolin

    Pangolin: 一款开源的OPENGL显示库,可以用来视频显示.而且开发容易. 代码我们可以从Github 进行下载:https://github.com/stevenlovegrove/Pang ...