Rounding to nearest int with numpy.rint() not consistent for .5

后端 未结 7 1400
余生分开走
余生分开走 2020-12-20 11:37

numpy\'s round int doesn\'t seem to be consistent with how it deals with xxx.5

In [2]: np.rint(1.5)
Out[2]: 2.0

In [3]: np.rint(10.5)
Out[3]: 10.0
<         


        
7条回答
  •  -上瘾入骨i
    2020-12-20 12:08

    Not sure its the most efficient solution but it works:

    signs = np.sign(arr)
    tmp = signs * arr
    arr = np.floor(tmp + 0.5)
    arr = arr * signs
    

提交回复
热议问题