>>> 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. spark&dataframe

    1.今天,我们来介绍spark以及dataframe的相关的知识点,但是在此之前先说一下对以前的hadoop的一些理解 当我启动hadoop的时候,上面有hdfs的存储结构,由于这个是分布式存储,所以 ...

  2. maven打包成jar

    maven pom.xml中添加依赖 <build> <plugins> <plugin> <groupId>org.apache.maven.plug ...

  3. 有关ViewFlipper的使用及设置动画效果的讲解

    说到左右滑动,其实实现左右滑动的方式很多,有ViewPaer,自定义实现Viewgroup,gallery等都可以达到这种效果.这里做下ViewFliper实现左右滑动的效果. 会用到以下的技术: 1 ...

  4. DOS程序员手册(五)

    第8章磁           盘       学习编程语言,常常是从基本的输入和输出入手的(正如第5.6和第7章曾介绍的一 样).到目前为止,我们不仅学习了怎样输入和输出数据,还学习了如何进行数据操作 ...

  5. jwt手动生成access_token

    from rest_framework_jwt.settings import api_settings # 手动为用户生成tokenjwt_payload_handler = api_setting ...

  6. django-settings里mysql连接配置

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'dailyfresh', 'HOST': 'loca ...

  7. coreos ipa image Injection of public key

    查看readme To embed the oem/ directory into a CoreOS pxe image:   Note: In order to have the ability t ...

  8. 深入探讨ui框架

    深入探讨前端UI框架 1 前言 先说说这篇文章的由来 最近看riot的源码,发现它很像angular的dirty check,每个component ( tag )都保存一个expressions数组 ...

  9. update-database -script

    update-database -script 更新脚本生成失败? 项目选择的不对 update后面-database空格-script

  10. JVM垃圾回收机制GC

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...