numpy.round的奇怪行为 [英] Strange behavior of numpy.round
本文介绍了numpy.round的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
面对x.5数字,Python的round()
似乎总是四舍五入:
Python's round()
seems to always round up when faced with x.5 numbers:
print round(1.5),round(2.5),round(3.5),round(4.5)
>>> 2.0 3.0 4.0 5.0
但是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
在某些情况下,这可能会导致错误.这是错误还是我错过了什么?
This can introduce errors in certain cases. Is this a bug or am I missing something?
推荐答案
numpy舍入到最接近的偶数值:
numpy rounds to the nearest even value:
https://docs.scipy .org/doc/numpy/reference/generated/numpy.around.html#numpy.around
对于恰好位于四舍五入的十进制值中间的值,NumPy会四舍五入为最接近的偶数.因此1.5和2.5四舍五入为2.0,-0.5和0.5四舍五入为0.0,等等.
For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 round to 0.0, etc.
这篇关于numpy.round的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文