python3000のround
Pythonのround関数にバグらしきものを見つけたよ、という報告です。下記は僕のMacBookでの実行結果です。
$ python -c 'x=9007199254740991.0; print "%.19f\n%.19f" % (x, round(x))' 9007199254740991.0000000000000000000 9007199254740992.0000000000000000000Pythonのround関数で奇数を丸めたら偶数になった - hnwの日記
python3000ではどうなんだろうと思って実行してみた.
$ python3.0 -c 'x=9007199254740991.0; print("%.19f\n%.19f" % (x, round(x)))' 9007199254740991.0000000000000000000 9007199254740991.0000000000000000000
直っているようだ.
同じ環境でpython2.5.1(macbookairにデフォルトでインストールされていたもの)で実行すると
$ python -c 'x=9007199254740991.0; print("%.19f\n%.19f" % (x, round(x)))' 9007199254740991.0000000000000000000 9007199254740992.0000000000000000000
なので,環境のせいではないみたい.
ソースはObjects/floatobject.c:25184の辺りかなあ.bsdのやり方に近くなっている
flr = floor(x); cil = ceil(x); if (x-flr > 0.5) rounded = cil; else if (x-flr == 0.5) rounded = fmod(flr, 2) == 0 ? flr : cil; else rounded = flr;