[Numpy-discussion] Behavior of rint?
Charles R Harris
charlesr.harris at gmail.com
Fri Jan 19 10:24:29 EST 2018
On Fri, Jan 19, 2018 at 7:48 AM, Matthew Brett <matthew.brett at gmail.com>
wrote:
> Hi Chuck,
>
> Thanks for the replies, they are very helpful.
>
> On Fri, Jan 19, 2018 at 1:51 PM, Charles R Harris
> <charlesr.harris at gmail.com> wrote:
> >
> >
> > On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris
> > <charlesr.harris at gmail.com> wrote:
> >>
> >>
> >>
> >> On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett at gmail.com
> >
> >> wrote:
> >>>
> >>> Hi,
> >>>
> >>> Sorry for my confusion, but I noticed (as a result of the discussion
> >>> here [1]) that np.rint and the fallback C function [2] seem to round
> >>> to even. But - my impression was that C rint, by default, rounds down
> >>> [3]. Is numpy rint not behaving the same way as the GNU C library
> >>> rint?
> >>>
> >>> In [4]: np.rint(np.arange(0.5, 11))
> >>> Out[4]: array([ 0., 2., 2., 4., 4., 6., 6., 8., 8., 10., 10.])
> >>>
> >>> In [5]: np.round(np.arange(0.5, 11))
> >>> Out[5]: array([ 0., 2., 2., 4., 4., 6., 6., 8., 8., 10., 10.])
> >>
> >>
> >> The GNU C documentation says that rint "round(s) x to an integer value
> >> according to the current rounding mode." The rounding mode is
> determined by
> >> settings in the FPU control word. Numpy runs with it set to round to
> even,
> >> although, IIRC, there is a bug on windows where the library is not
> setting
> >> those bits correctly.
> >
> >
> > Round to even is also the Python default rounding mode.
>
> Do you mean that it is Python setting the FPU control word? Or do we
> set it? Do you happen to know where that is in the source? I did a
> quick grep just now without anything obvious.
>
I can't find official (PEP) documentation, but googling indicates that in
Python 3, `round` rounds to even, and in Python 2 it rounds up. See also
https://docs.python.org/3/whatsnew/3.0.html.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180119/8d493653/attachment.html>
More information about the NumPy-Discussion
mailing list