[Numpy-discussion] should rint return int?
Neal Becker
ndbecker2 at gmail.com
Mon Apr 28 15:29:59 EDT 2014
Robert Kern wrote:
> On Mon, Apr 28, 2014 at 6:36 PM, Neal Becker <ndbecker2 at gmail.com> wrote:
>> I notice rint returns float. Shouldn't it return int?
>>
>> Would be useful when float is no longer acceptable as an index. I think
>> conversion to an index using rint is a common idiom.
>
> C's rint() does not:
>
> http://linux.die.net/man/3/rint
>
> This is because there are many integers that are representable as
> floats/doubles/long doubles that are well outside of the range of any
> C integer type, e.g. 1e20.
>
> Python 3's round() can return a Python int because Python ints are
> unbounded. Ours aren't.
>
> That said, typically the first thing anyone does with the result of
> rounding is to coerce it to a native int dtype without any checking.
> It would not be terrible to have a function that rounds, then coerces
> to int but checks for overflow and passes that through the numpy error
> mechanism to be controlled. But it shouldn't be called rint(), which
> is intended to be as thin a wrapper over the C function as possible.
>
Well I'd spell it nint, and it works like:
def nint (x):
return int (x + 0.5) if x >= 0 else int (x - 0.5)
More information about the NumPy-Discussion
mailing list