首页 / 操作系统 / Linux / 有关Python里面小数点精度的控制
要求较小的精度
round()内置方法
这个是使用最多的,刚看了round()的使用解释,也不是很容易懂。round()不是简单的四舍五入的处理方式。
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, andround(1.5) is 2).>>> round(2.5)3.0>>> round(-2.5)-3.0>>> round(2.675)3.0>>> round(2.675,2)2.67round()如果只有一个数作为参数,不指定位数的时候,返回的是一个整数,而且是最靠近的整数。一般情况是使用四舍五入的规则,但是碰到舍入的后一位为5的情况,如果要取舍的位数前的数是偶数,则直接舍弃,如果奇数这向上取舍。看下面的示例:>>> round(2.555,2)2.56>>> round(2.565,2)2.56>>> round(2.575,2)2.58>>> round(2.585,2)2.58使用格式化
效果和round()是一样的。>>> a = ("%.2f" % 2.555)>>> a"2.56">>> a = ("%.2f" % 2.565)>>> a"2.56">>> a = ("%.2f" % 2.575)>>> a"2.58">>> a = ("%.2f" % 2.585)>>> a"2.58">>> a = int(2.5)>>> a2要求超过17位的精度分析
python默认的是17位精度,也就是小数点后16位,但是这里有一个问题,就是当我们的计算需要使用更高的精度(超过16位小数)的时候该怎么做呢?高精度使用decimal模块,配合getcontext
>>> from decimal import *>>> print(getcontext())Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])>>> getcontext().prec = 50>>> b = Decimal(1)/Decimal(3)>>> bDecimal("0.33333333333333333333333333333333333333333333333333")>>> c = Decimal(1)/Decimal(7)>>> cDecimal("0.14285714285714285714285714285714285714285714285714")>>> float(c)0.14285714285714285默认的context的精度是28位,可以设置为50位甚至更高,都可以。这样在分析复杂的浮点数的时候,可以有更高的自己可以控制的精度。其实可以留意下context里面的这rounding=ROUND_HALF_EVEN 参数。ROUND_HALF_EVEN, 当half的时候,靠近even.使用格式化(不推荐)
>>> a = ("%.30f" % (1.0/3))>>> a"0.333333333333333314829616256247"
可以显示,但是不准确,后面的数字基本没有意义。关于小数和取整
既然说到小数,就必然要说到整数。一般取整会用到这些函数:round()
这个不说了,前面已经讲过了。一定要注意它不是简单的四舍五入,而是ROUND_HALF_EVEN的策略。math模块的ceil(x)
取大于或者等于x的最小整数。math模块的floor(x)
去小于或者等于x的最大整数。>>> from math import ceil, floor>>> round(2.5)2>>> ceil(2.5)3>>> floor(2.5)2>>> round(2.3)2>>> ceil(2.3)3>>> floor(2.3)Ubuntu 14.04安装Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htmCentOS上源码安装Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htm本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-10/136072.htm