[Numpy-discussion] why numpy.round get a different result from python round function?

Stéfan van der Walt stefan@sun.ac...
Tue Mar 10 09:50:01 CDT 2009


Hi David

2009/3/10 shuwj5460@163.com <shuwj5460@163.com>:
> I read the doc for numpy about round function:
>
> -----
> 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. Results may also be surprising due to the inexact
> representation of decimal fractions in the IEEE floating point standard
> [16] and errors introduced when scaling by powers of ten.
> ----
>
> why not numpy round  keep the same with python round function? or
> provide anothor function to do so?

In Python 2.6 and 3.0, rounding is also to the nearest even number.
You can round to nearest using something like

np.floor(np.abs(x) + 0.5) * np.sign(x)

Regards
Stéfan


More information about the Numpy-discussion mailing list