
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.]) Cheers, Matthew [1] https://github.com/nipy/dipy/issues/1402 [2] https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/npy_math_i... [3] https://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html

On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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. Chuck

On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com
wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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. Chuck

Hi Chuck, Thanks for the replies, they are very helpful. On Fri, Jan 19, 2018 at 1:51 PM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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. Cheers, Matthew

I am confused . Shouldn't rint round to nearest integer. http://en.cppreference.com/w/cpp/numeric/math/rint Regards Mads On Jan 19, 2018 15:50, "Matthew Brett" <matthew.brett@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@gmail.com> wrote:
On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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.
Cheers,
Matthew _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion

On Fri, Jan 19, 2018 at 11:56 PM, Mads Ipsen <madsipsen@gmail.com> wrote:
I am confused . Shouldn't rint round to nearest integer.
http://en.cppreference.com/w/cpp/numeric/math/rint It does. Matthew was asking specifically about its behavior when it is rounding numbers ending in .5, not the general case. Since that's the only real difference between rounding routines, we often recognize that from the context and speak in a somewhat elliptical way (i.e. just "round-to-even" instead of "round to the nearest integer and rounding numbers ending in .5 to the nearest even integer"). -- Robert Kern

On Fri, Jan 19, 2018 at 7:48 AM, Matthew Brett <matthew.brett@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@gmail.com> wrote:
On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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

Hi, On Fri, Jan 19, 2018 at 3:24 PM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 7:48 AM, Matthew Brett <matthew.brett@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@gmail.com> wrote:
On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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.
But I guess this could be the Python implementation of round, rather than rint and the FPU control word? I'm asking because the question arose about npy_rint at the C level ... Cheers, Matthew

On Fri, Jan 19, 2018 at 8:27 AM, Matthew Brett <matthew.brett@gmail.com> wrote:
Hi,
On Fri, Jan 19, 2018 at 3:24 PM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 7:48 AM, Matthew Brett <matthew.brett@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@gmail.com> wrote:
On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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.
But I guess this could be the Python implementation of round, rather than rint and the FPU control word? I'm asking because the question arose about npy_rint at the C level ...
I'm pretty sure Python sets the FPU control word. Note that Python itself doesn't have a public interface for setting it, nor does Java. The GNU library documentation has the following: Round to nearest. This is the default mode. It should be used unless there is a specific need for one of the others. In this mode results are rounded to the nearest representable value. If the result is midway between two representable values, the even representable is chosen. *Even* here means the lowest-order bit is zero. This rounding mode prevents statistical bias and guarantees numeric stability: round-off errors in a lengthy calculation will remain smaller than half of FLT_EPSILON. Chuck

On Fri, Jan 19, 2018 at 8:45 AM, Charles R Harris <charlesr.harris@gmail.com
wrote:
On Fri, Jan 19, 2018 at 8:27 AM, Matthew Brett <matthew.brett@gmail.com> wrote:
Hi,
On Fri, Jan 19, 2018 at 3:24 PM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 7:48 AM, Matthew Brett <matthew.brett@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@gmail.com> wrote:
On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <matthew.brett@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.
But I guess this could be the Python implementation of round, rather than rint and the FPU control word? I'm asking because the question arose about npy_rint at the C level ...
I'm pretty sure Python sets the FPU control word. Note that Python itself doesn't have a public interface for setting it, nor does Java. The GNU library documentation has the following:
Round to nearest. This is the default mode. It should be used unless there is a specific need for one of the others. In this mode results are rounded to the nearest representable value. If the result is midway between two representable values, the even representable is chosen. *Even* here means the lowest-order bit is zero. This rounding mode prevents statistical bias and guarantees numeric stability: round-off errors in a lengthy calculation will remain smaller than half of FLT_EPSILON.
A classic reference for rounding is TAOCP (section 4.2.2 in the Third Edition). I read that many years ago, so don't recall the details. Chuck

On Fri, Jan 19, 2018 at 8:24 AM, Charles R Harris <charlesr.harris@gmail.com
wrote:
On Fri, Jan 19, 2018 at 7:48 AM, Matthew Brett <matthew.brett@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@gmail.com> wrote:
On Fri, Jan 19, 2018 at 6:41 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Fri, Jan 19, 2018 at 3:30 AM, Matthew Brett <
matthew.brett@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.
Note that this applies to floating point operations in general where there are usually a couple of extra guard bits for the mantissa, and the mantissa is the result of rounding to even. The reason for this choice is that always rounding in the same direction leads to a small consistent bias, whereas rounding to even effectively randomizes the error. One grows order N, the other as the square root. Chuck
participants (4)
-
Charles R Harris
-
Mads Ipsen
-
Matthew Brett
-
Robert Kern