2、Python基础--除法、常用数学函数
2016-10-26 23:23
816 查看
整数与整数相除:
>>> 2/5
0
>>> 2.0/5
0.4
>>> 2/5.0
0.4
>>> 2.0/5.0
0.4
>>> 7/2
3
>>> 7.0/2
3.5
>>>
注意:整数除以整数,结果是整数(取商);
例如7/2,商是3(整数),余数是2,结果值为3。
浮点数与整数相除:
>>> 9.0/2
4.5
>>> 9/2.0
4.5
>>> 9.0/2.0
4.5
>>>
注意:不管被除数还是除数,只要有一个树是浮点数,结果就是浮点数。
浮点数与浮点数四则运算问题:(浮点数的十进制和二进制转换精度问题)
>>> 10.0/3
3.3333333333333335
>>> 0.1+0.2
0.30000000000000004
>>> 0.1+0.1+0.1-0.2
0.10000000000000003
>>> 0.1*0.1
0.010000000000000002
>>>
注意:我们输入的是十进制,计算机用的是二进制进行计算,就需要先把十进制的数转化为二进制,然后再计算。但是,在转化中,浮点数转化成二进制,出问题了。
例如:十进制的0.1,转化为二进制后,不会精准等于十进制的0.1。
这种问题不仅仅在Python语言中有,所有支持浮点数运算的变成语言都会遇到。
引用模块解决除法问题:
>>> from __future__ import division
>>> 5/2
2.5
>>> 7/3
2.3333333333333335
>>>
引用模块后,不管什么情况都能得到浮点数的结果了。
余数:
>>> 5%2
1
>>> 6%4
2
>>> 5.0%2
1.0
>>>
利用“%”符号可以得到两个数相除的余数;
内建函数divmod():
>>> divmod(5,2)
(2, 1)
>>> divmod(6,4)
(1, 2)
>>> divmod(5.0,2)
(2.0, 1.0)
>>>
函数返回了商和余数两个值。
四舍五入:
内建函数round():
>>> round(3.1415926,2)
3.14
>>> round(3.1415926,3)
3.142
>>>
求绝对值:
>>> abs(10)
10
>>> abs(-10)
10
>>> abs(-1.2)
1.2
>>>
使用math数学模块:
>>> import math
>>> math.pi
3.141592653589793
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'er
fc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gam
ma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'mod
f', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>
>>> help(math.pow)
Help on built-in function pow in module __builtin__:
pow(...)
pow(x, y[, z]) -> number
With two arguments, equivalent to x**y. With three arguments,
equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
>>>
注意两个内建函数:
dir(math):通过该指令,可以查看任何模块中所包含的工具。
help(math.pow):可以查看模块中任何一个函数的使用方法。
>>> 2/5
0
>>> 2.0/5
0.4
>>> 2/5.0
0.4
>>> 2.0/5.0
0.4
>>> 7/2
3
>>> 7.0/2
3.5
>>>
注意:整数除以整数,结果是整数(取商);
例如7/2,商是3(整数),余数是2,结果值为3。
浮点数与整数相除:
>>> 9.0/2
4.5
>>> 9/2.0
4.5
>>> 9.0/2.0
4.5
>>>
注意:不管被除数还是除数,只要有一个树是浮点数,结果就是浮点数。
浮点数与浮点数四则运算问题:(浮点数的十进制和二进制转换精度问题)
>>> 10.0/3
3.3333333333333335
>>> 0.1+0.2
0.30000000000000004
>>> 0.1+0.1+0.1-0.2
0.10000000000000003
>>> 0.1*0.1
0.010000000000000002
>>>
注意:我们输入的是十进制,计算机用的是二进制进行计算,就需要先把十进制的数转化为二进制,然后再计算。但是,在转化中,浮点数转化成二进制,出问题了。
例如:十进制的0.1,转化为二进制后,不会精准等于十进制的0.1。
这种问题不仅仅在Python语言中有,所有支持浮点数运算的变成语言都会遇到。
引用模块解决除法问题:
>>> from __future__ import division
>>> 5/2
2.5
>>> 7/3
2.3333333333333335
>>>
引用模块后,不管什么情况都能得到浮点数的结果了。
余数:
>>> 5%2
1
>>> 6%4
2
>>> 5.0%2
1.0
>>>
利用“%”符号可以得到两个数相除的余数;
内建函数divmod():
>>> divmod(5,2)
(2, 1)
>>> divmod(6,4)
(1, 2)
>>> divmod(5.0,2)
(2.0, 1.0)
>>>
函数返回了商和余数两个值。
四舍五入:
内建函数round():
>>> round(3.1415926,2)
3.14
>>> round(3.1415926,3)
3.142
>>>
求绝对值:
>>> abs(10)
10
>>> abs(-10)
10
>>> abs(-1.2)
1.2
>>>
使用math数学模块:
>>> import math
>>> math.pi
3.141592653589793
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'er
fc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gam
ma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'mod
f', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>
>>> help(math.pow)
Help on built-in function pow in module __builtin__:
pow(...)
pow(x, y[, z]) -> number
With two arguments, equivalent to x**y. With three arguments,
equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
>>>
注意两个内建函数:
dir(math):通过该指令,可以查看任何模块中所包含的工具。
help(math.pow):可以查看模块中任何一个函数的使用方法。
相关文章推荐
- Python修炼--常用字符串、数学函数 分类: python基础学习 2013-06-16 13:23 205人阅读 评论(0) 收藏
- python 常用数学函数
- Python修炼--常用字符串、数学函数
- python基础--数学函数
- python 常用的基础函数
- 【python系列】Python常用数学函数整理
- Python修炼--常用字符串、数学函数
- Python基础、函数及其相关函数库(Numpy、TensorFlow)常用函数集锦(更新中,更新日期:2017-08-03)
- python基础--字符串常用函数
- Python的math模块中的常用数学函数整理
- Python+Selenium基础1-6:Selenium常用函数
- python基础===正则表达式,常用函数re.split和re.sub
- python入门(六)-- 常用数学函数
- 【python游戏编程之旅】第四篇---pygame中加载位图与常用的数学函数。
- Python开发(基础):常用函数
- python基础学习之os、os.path模块常用函数
- R语言基础(一)-常用数学函数与定义
- python基础知识之常用函数
- python基础:os模块中关于文件/目录常用的函数使用方法
- Python基础:常用函数