TensorFlow ops:tf.round为“Bankers Rounding”,不是什么“四舍五入”

田海立@CSDN 2020-10-11

TensorFlow里的算子round不是什么四舍五入,而是Bankers Rounding——四舍六入五取偶。

用搜索工具搜索,如果不是链接到官方网站的话,特别是到链接到中文描述,看起来还很官方的样子的地方,会把round就直接翻译成“四舍五入”,希望你再搜索的时候,看到的是本文的描述。

误导性的解释

搜索tf.round,结果第一条的误导性描述:

https://www.w3cschool.cn/tensorflow_python/tensorflow_python-cnvw2in5.html

上面这一段应该是官方描述的翻译,但这翻译也太误导了。

TF官方描述

下面看官方(也不是TF的官网,但这是能被搜索到并打开的)的描述:

https://tensorflow.google.cn/api_docs/python/tf/math/round

所以,TensorFlow里的round是Rounds half to even,也就是Bankers Rounding。

Bankers Rounding是什么

TF官网没有具体的描述Bankers Rounding是什么,但Bankers Rounding是容易找到的

http://wiki.c2.com/?BankersRounding

总结起来就是,取最近的偶数整数,具体来说:

  1. 小数部分小于0.5,则舍去;
  2. 小数部分大于0.5(注:0.5...x 也是大于0.5的),则进位;
  3. 小数部分恰好为0.5,则要看整数部分,也就是:
  •     整数为偶数则舍去小数;
  •     整数为奇数则进位到整数

            这样取得的整数结果就是偶数了。

所以Bankers Rounding也通常被称做“四舍六入五取偶”。

例子中:

  • 0.9 -> 1:case#2,进位;
  • 2.5 -> 2:case#3,2为偶数,小数舍去;
  • 2.3 -> 2:case#1,舍去;
  • 1.5 -> 2:case#3,1为奇数,小数进位;
  • -4.5 -> -4:case#3,-4为偶数,小数舍去。

猜你喜欢

转载自blog.csdn.net/thl789/article/details/109018926