Re: [請益] 如何直接判斷浮點數運算時有誤差(贈P幣)
你已經知道答案了,但你可能還不知道原理。
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。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.115.126.26
※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1543757742.A.357.html
推
12/02 22:15,
5年前
, 1F
12/02 22:15, 1F
念計組應該要知道。寫金融數字的也需要知道bankers rounding.
推
12/03 00:13,
5年前
, 2F
12/03 00:13, 2F
→
12/03 00:14,
5年前
, 3F
12/03 00:14, 3F
→
12/03 00:14,
5年前
, 4F
12/03 00:14, 4F
→
12/03 00:14,
5年前
, 5F
12/03 00:14, 5F
推
12/03 03:47,
5年前
, 6F
12/03 03:47, 6F
→
12/03 03:47,
5年前
, 7F
12/03 03:47, 7F
如g大所講。(1234)(5)(6789)這樣才是平衡。
※ 編輯: oopFoo (36.231.141.178), 12/03/2018 10:30:29
推
12/03 16:29,
5年前
, 8F
12/03 16:29, 8F
→
12/03 18:20,
5年前
, 9F
12/03 18:20, 9F
推
12/04 19:49,
5年前
, 10F
12/04 19:49, 10F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):