楼主:
oopFoo (3d)
2018-12-02 21:35:39你已经知道答案了,但你可能还不知道原理。
round "half" to nearest even. 又称gaussian/banker's rounding.
例子
round[{0.5, 1.5, 2.5, 3.5, .5}] =>{0, 2, 2, 4, 4}
为什么呢?因为四舍五入是biased. (.5)是刚好在(0,1)的一半(half)。
要公平,不累进error,其实要一半舍,一半入。最简单的方法就是odd或even入,有一半的机会。
banker's rounding 其实蛮常见的。
.net Math.round的 default 就是banker's rounding.
java也可以设定
https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html
floating point 的话就
010....(不到一半,舍)
101....(超过一半,入)
100....(刚好一半,用banker's rounding)看要round的bit是1或0.
硬件的floating point会有3个extra bits. guard, round, sticky. sticky bit 是or所有多出的bits.
2个bits不够,因为做完arithmatic还需要normalized 回来。
请参考
http://pages.cs.wisc.edu/~david/courses/cs552/S12/handouts/guardbits.pdf
金融业,应该都知道这个吧。
PS: "What Every Computer Scientist Should Know About Floating-Point Arithmetic"
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
主要在在讨论rounding error。
作者:
Muscovy (三分熟的闹钟)
2018-12-03 00:13:0001234 舍, 56789 入, 一半一半看起来很公平啊.考虑连续区间也是, [0, 5) 舍, [5, 10) 入, 也是很公平.为什么会说“四舍五入是 biased”? 是有什么特别原因吗?路过纯好奇.