Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Python3, round with one argument should return an integer type. #5700

anntzer opened this issue Mar 20, 2015 · 3 comments

On Python3, round with one argument should return an integer type. #5700

anntzer opened this issue Mar 20, 2015 · 3 comments


Copy link

@anntzer anntzer commented Mar 20, 2015

Unlike Python2, Python3's (builtin) round function is documented to return an integer when called with a single argument. However, numpy float dtypes do not satisfy this, returning a float instead.


This comment has been minimized.

Copy link
Contributor Author

@anntzer anntzer commented May 11, 2016

The discussion at #6248 suggested that one issue is that floats have a larger range than (fixed-size) ints. I this it may be reasonable to raise an overflow warning/error (depending on the errstate) and set the value to either MAX_INT or zero, just like integer division by zero does.

In fact, now that I think of it, it may be worth having an extra flag in the errstate about integer overflows: I'd guess one will usually want to ignore floating point overflows (because infs are "not that bad") while not losing track of integer overflows (because they actually give wrong results). Thoughts?

@mihaic mihaic mentioned this issue Feb 28, 2017

This comment has been minimized.

Copy link

@pepalogik pepalogik commented Aug 25, 2017

No thoughts here for more than a year, so I'm going to write something, although I use NumPy rather marginally.

My first point is that this issue is a duplicate of #3511.

Now to the desired round() behavior. I personally wouldn't mind if round() returned Python's ints for NumPy floats, but I understand that NumPy functions are wanted to return NumPy types. To get some inspiration, let's take a look at the float.__round__() behavior in Python 3 (inspired by @anntzer in #6248):

>>> round(1e308)
>>> round(1e309)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: cannot convert float infinity to integer

(1e309 overflows to infinity)

As demonstrated, even the built-in float.__round__() has its limit: It raises an OverflowError if the argument is an infinity. Therefore, I think it will be OK if NumPy's round() also has a limit, such as the highest available integer (or possibly the highest float which has enough accuracy for conversion to an integer). If the argument is too big, an OverflowError can simply be raised.

For the sake of completeness, I'm reminding that the behavior is quite different if the second argument is given (and not None):

>>> round(1e308, 0)
>>> round(1e309, 0)

This comment has been minimized.

Copy link

@eric-wieser eric-wieser commented Sep 29, 2017

Related to the return type int/float thing is #9068, for ceil and floor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
5 participants
You can’t perform that action at this time.