use*_*975 2 python numpy rounding python-2.7
round()
面对x.5数字,Python 似乎总是四舍五入:
print round(1.5),round(2.5),round(3.5),round(4.5)
>>> 2.0 3.0 4.0 5.0
Run Code Online (Sandbox Code Playgroud)
但是numpy.round()
似乎不一致:
import numpy as np
print np.round(1.5),np.round(2.5),np.round(3.5),np.round(4.5)
>>> 2.0 2.0 4.0 4.0
Run Code Online (Sandbox Code Playgroud)
在某些情况下,这可能会导致错误。这是错误还是我错过了什么?
小智 5
numpy舍入到最接近的偶数值:
https://docs.scipy.org/doc/numpy/reference/generation/numpy.around.html#numpy.around
对于恰好介于四舍五入的十进制值中间的值,NumPy会四舍五入为最接近的偶数值。因此1.5和2.5四舍五入为2.0,-0.5和0.5四舍五入为0.0,依此类推。