Behavior of round(array)

Hi all, there is a discussion about how `round(array)` should behave in: https://github.com/numpy/numpy/issues/6248 There is some discussion about object arrays which should probably be fixed for `around()` in that ago. Otherwise, the is the question what to do about the fact that: * round(np.float64(2.**64)) -> 18446744073709551616 (a Python int) * round(np.array([2., 3., 2.**64]) can only return float or integer The NumPy `np.round`/`np.around` (same function) functions always return the same dtype. We can either ignore that discrepancy, or opt to raise an error, so that: >>> round(np.array([2., 3.5])) TypeError: Rounding a NumPy float array cannot return integers, use `round(arr, ndigits=0)` or `round(arr, 0) to indicate that a float result is desired. In the call today, I think we leaned a bit towards ignoring it, but if I read Aaron correctly, he prefers the error and it may be the conservative choice. We could of course do other things (i.e. return an integer `intp` array) but that would probably require a hard error for overflows (which is different from `np.rint(arr).astype(np.intp)`. Are there any small or big opinions on this? It seems useful to enable `round()` and is a bit of a shame to get caught up on the detail, but I am not sure what the right choice is :). - Sebastian

Hi Sebastian, all, I’d lean towards an error too, given that the invariants implied by the Python implementation clearly aren‘t met. No strong opinions though. Best regards, Hameer Abbasi Von meinem iPhone gesendet
Am 30.11.2022 um 18:36 schrieb Sebastian Berg <sebastian@sipsolutions.net>:
Hi all,
there is a discussion about how `round(array)` should behave in:
https://github.com/numpy/numpy/issues/6248
There is some discussion about object arrays which should probably be fixed for `around()` in that ago.
Otherwise, the is the question what to do about the fact that:
* round(np.float64(2.**64)) -> 18446744073709551616 (a Python int) * round(np.array([2., 3., 2.**64]) can only return float or integer
The NumPy `np.round`/`np.around` (same function) functions always return the same dtype.
We can either ignore that discrepancy, or opt to raise an error, so that:
round(np.array([2., 3.5])) TypeError: Rounding a NumPy float array cannot return integers, use `round(arr, ndigits=0)` or `round(arr, 0) to indicate that a float result is desired.
In the call today, I think we leaned a bit towards ignoring it, but if I read Aaron correctly, he prefers the error and it may be the conservative choice.
We could of course do other things (i.e. return an integer `intp` array) but that would probably require a hard error for overflows (which is different from `np.rint(arr).astype(np.intp)`.
Are there any small or big opinions on this? It seems useful to enable `round()` and is a bit of a shame to get caught up on the detail, but I am not sure what the right choice is :).
- Sebastian
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: einstein.edison@gmail.com

On Wed, Nov 30, 2022 at 10:39 AM Sebastian Berg <sebastian@sipsolutions.net> wrote:
Hi all,
there is a discussion about how `round(array)` should behave in:
https://github.com/numpy/numpy/issues/6248
There is some discussion about object arrays which should probably be fixed for `around()` in that ago.
Otherwise, the is the question what to do about the fact that:
* round(np.float64(2.**64)) -> 18446744073709551616 (a Python int) * round(np.array([2., 3., 2.**64]) can only return float or integer
The NumPy `np.round`/`np.around` (same function) functions always return the same dtype.
We can either ignore that discrepancy, or opt to raise an error, so that:
>>> round(np.array([2., 3.5])) TypeError: Rounding a NumPy float array cannot return integers, use `round(arr, ndigits=0)` or `round(arr, 0) to indicate that a float result is desired.
In the call today, I think we leaned a bit towards ignoring it, but if I read Aaron correctly, he prefers the error and it may be the conservative choice.
I guess I weakly prefer it, but I mostly just suggested it because it is the conservative option. Making ndigits=None just match ndigits=0 (so that round() works like around()) is also fine IMO. I don't think it would be too surprising for most people to have round() work that way on arrays. What I feel more strongly about is that there shouldn't be distinct behavior for scalars vs. arrays, and the output dtype shouldn't be value-based. Aaron Meurer
We could of course do other things (i.e. return an integer `intp` array) but that would probably require a hard error for overflows (which is different from `np.rint(arr).astype(np.intp)`.
Are there any small or big opinions on this? It seems useful to enable `round()` and is a bit of a shame to get caught up on the detail, but I am not sure what the right choice is :).
- Sebastian
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: asmeurer@gmail.com
participants (3)
-
Aaron Meurer
-
einstein.edison@gmail.com
-
Sebastian Berg