[Numpy-discussion] Changing the behavior of (builtins.)round (via the __round__ dunder) to return an integer

Stephan Hoyer shoyer at gmail.com
Wed Apr 13 12:35:19 EDT 2016


On Wed, Apr 13, 2016 at 8:06 AM, <josef.pktd at gmail.com> wrote:
>
> The difference is that Python 3 has looooong ints, (and doesn't have to
> overflow, AFAICS)
>

This is a good point. But if your float is so big that rounding it to an
integer would overflow int64, rounding is already a no-op. I'm sure this
has been done before but I would guess it's quite rare. I would be OK
raising in this situation, especially because np.around will still be
around returning floats.


> what happens with nan?
> I guess inf would overflow?
>

builtins.round raises for both of these (in Python 3) and I would propose
copying this behavior:

In [52]: round(float('inf'))
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-52-798e0e9243d6> in <module>()
----> 1 round(float('inf'))

OverflowError: cannot convert float infinity to integer

In [53]: round(float('nan'))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-53-e989485df64c> in <module>()
----> 1 round(float('nan'))

ValueError: cannot convert float NaN to integer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160413/0e81b8c0/attachment.html>


More information about the NumPy-Discussion mailing list