当Python 3中的尾随小数点> = 0.5时,math.ceil()和round()之间的算法区别是什么?

3 python algorithm math python-3.x

当Python 3中的尾随小数点> = 0.5时,math.ceil()和round()之间的算法区别是什么?

例如,

round(9.5) = 10
round(9.67) = 10
math.ceil(9.5) = 10
math.ceil(9.5) = 10
Run Code Online (Sandbox Code Playgroud)

Ziy*_*her 5

文档中,

[...]如果两个倍数相等,则向均匀选择进行舍入(例如,圆形(0.5)和圆形(-0.5)均为0,圆形(1.5)为2).

但是,math.ceil总会"圆"起来.即大于或等于输入的最小整数.

此外,roundmath.ceil执行对负数时有很大的不同.

>>> math.ceil(-2.8)
-2
>>> round(-2.8)
-3
Run Code Online (Sandbox Code Playgroud)