对于一些貌似很简单常见的函数,最好还是去读一下Python文档,否则当你被某个BUG折磨得死去活来时,还不知根源所在.尤其是Python这种不断更新的语言.(python 2.7 的round和3.3.2不一样)

  • 3.3.2官方文档对round的定义

round(number[, ndigits])

Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. Delegates to number.__round__(ndigits).

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, and round(1.5) is 2). The return value is an integer if called with one argument, otherwise of the same type as number.

  • 举例查看:
>>> round(1.5)
2
>>> round(2.5)
2
>>> round(3.5)
4
>>> round(4.5)
4
>>> round(5.5)
6
>>> round(6.5)
6

分析定义中的关键语句:

values are rounded to the closest multiple of 10 to the power minus ndigits
  • 假设round(123.45, 1)=A,那么A在数轴上是离123.45最近的点(the closest),且A保留1位小数.
  • "10 to the power minus ndigits"意思是"10的负ndigits次方",即pow(10, -ndigits).
  • "multiple of n" 表示n的倍数.倍数只能是整数,这个是关键.(可参考维基百科:http://zh.wikipedia.org/wiki/%E5%9B%A0%E6%95%B8)

那么,例如round(123.45, 1)就表示数轴上离123.45最近的"0.1的X倍".

X=1时,"0.1的X倍"(即0.1)是离123.45最近的值吗?显然不是,因为当X=1000时,"0.1的X倍"是100,比0.1更靠近123.45.

按照这个原理进行推导,容易知道当X=1234或1235时,"0.1的X倍"最靠近123.45.那么该取哪个X作为最终结果呢?

if two multiples are equally close, rounding is done toward the even choice 

"toward the even choice"意思就是说取偶数.即"0.1的1234倍",即123.4.

然而,当你测试round(123.45, 1):

>>> round(123.45, 1)
123.5

为什么取偶数呢?是BUG吗?非也,官方文档继续解释:

Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information

意思就是说计算机需要先将十进制123.45转换为二进制,这个过程会导致二进制的值比123.45略大(比如123.45000001之类的),那么自然就得到123.5这个值了.

这种情形非常普遍:

>>> round(0.05, 1) #should return 0.0
0.1
>>> round(0.15, 1) #should return 0.2
0.1
>>> round(0.25, 1)
0.2
>>> round(0.35, 1) #should return 0.4
0.3
>>> round(0.45, 1) #should return 0.4
0.5

Python 3.3.2 round函数并非"四舍五入"的更多相关文章

  1. 【转载】Sqlserver中使用Round函数对计算结果四舍五入

    在实际应用的计算中,很多时候我们需要对最后计算结果四舍五入,其实在Sqlserver中也有对应的四舍五入函数,就是Round函数,Round函数的格式为Round(column_name,decima ...

  2. python教程(三)·函数与模块

    函数,这和数学中的函数有点关联,但又不是完全等价 概念 不说的这么官方,我就已自己的理解来表达 ^_^ 在数学中,把一个或多个值(输入x)进行一定的计算或者映射,得到一个值(输出y),这个计算或者映射 ...

  3. Math.Round函数详解

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  4. floor()函数 和round()函数的区别

    floor()函数 和round()函数的区别 2018-08-17  09:40:00 1.floor()函数:取整,保留整数部分,舍弃小数部分. 2.round()函数:四舍五入.round(x, ...

  5. 一场Math.Round函数的误解

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  6. msyql round函数隐藏问题

    1.背景 在用mysql round进行四舍五入计算的时候如果参与计算的字段为float,则最终计算出的四舍五入效果会有很大出入.例子我就不列举了 2.原因 mysql官方文档中关于ROUND函数的部 ...

  7. Math.round真的是四舍五入吗?我不这么认为

    public static long round(double a) 返回最接近参数的 long.结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 long 类型.换句话 ...

  8. 改写python round()函数,解决四舍五入问题 round(1.365,2)=1.36

    round()函数四舍五入存在一个问题,遇到5不一定进一.如下图所示: print(round(1.365,2)) #1.36 没进一 print('%.2f'%1.365) print(round( ...

  9. Math.Round函数四舍五入

    Math.Round函数四舍五入的问题   今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...

随机推荐

  1. 【vuejs深入一】深入学习vue指令,自定义指令解决开发痛点

    写在前面  一个好的架构需要经过血与火的历练,一个好的工程师需要经过无数项目的摧残. 最近博主我沉淀了几个月,或者说懒了几个月.然而大佬的指点总是一针见血,能够让人看到方向.所以我现在有觉得,一个好的 ...

  2. for/range/break/continue

    #for智能循环 members=['武林至尊','宝刀屠龙','号令天下','莫敢不从'] for every in members:     print(every,len(every)) 武林至 ...

  3. MySQL实现全关联 full outer join

    SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行. LEFT JOIN 关键 ...

  4. HDFS- High Availability

    NameNode High Availability Background Hadoop2.0.0之前,NameNode存在单点失败(single point of failure (SPOF) )问 ...

  5. [LeetCode] Accounts Merge 账户合并

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  6. Discuz利用UC_KEY进行前台getshell

    来源:http://wooyun.jozxing.cc/static/bugs/wooyun-2015-0137991.html 先通过uc_key把恶意代码保存在/uc_client/data/ca ...

  7. 以技术面试官的经验分享毕业生和初级程序员通过面试的技巧(Java后端方向)

    本来想分享毕业生和初级程序员如何进大公司的经验,但后来一想,人各有志,有程序员或许想进成长型或创业型公司或其它类型的公司,所以就干脆来分享些提升技能和通过面试的技巧,技巧我讲,公司你选,两厢便利. 毕 ...

  8. POJ2135:Farm Tour

    题意:给定一个无向图,从1走到n再从n走回1,每个边只能走一遍,求最短路 题解:可以定义一个源点s,和一个汇点t s和1相连容量为2,费用为0, t和n相连容量为2,费用为0 然后所用的边的容量都定为 ...

  9. salt基本使用之一(1)

    1.帮助文档 以cmd函数为例子说下salt中的一些帮助信息 # 查看指定modules用法 salt '*' sys.doc cmd # 查看所有的modules列表 salt '*' sys.li ...

  10. React 深入系列4:组件的生命周期

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列4:组件的生命周期 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助 ...