-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
round() returns floating point, not int, for some numpy floats when no second arg #11810
Comments
what type is being returned?
Your examples with |
You're explaining what the code does. I have to agree: yes, that's what it does. |
NumPy round applied to numpy floats does not return integers. It is a feature, not a bug. The Python behavior you illustrate is new in Python 3. |
Just to elaborate a little more: the problem is with very large numbers; in python, one can return a long integer, but in numpy we cannot (for the general case of arrays). We have had some discussion, however, whether this should change at least for |
@charris: I don't think we're talking about |
I get the situation: Python's It's just confusing to have code like: Regards, |
After we drop Python 2.7 we might want to take a second look at this. However, backwards compatibility is always a consideration. Although in this case I expect people do want an integer, especially for indexing. There has been a similar discussion about |
I stand corrected--- |
From PEP3141:
A quick test (Python 3.7.0) shows:
If I got it right: current I think that it would be sensible to adhere immediately to the PEP3141 calling signature. When
EDIT: I just noticed that this has already been discussed in #11557, #5700, #3511. Sorry for adding noise to the discussion, but I feel that a ref to PEP3141 is important. |
Another thought on this issue: since The problem is that one has a lot of paths (sometimes unexpected) in which |
I've encountered this issue as well. At least once my own code has broken since |
https://docs.python.org/3/library/functions.html#round If ndigits is omitted or is None, it returns the nearest integer to its input. Otherwise keep int() due to: numpy/numpy#11810
Workaround for me: width = round(float(np.sqrt(x))) |
Hey Daniel :). The workaround is good, closing the issue since this will now return a python integer for version NumPy 1.19 and later (fixed in gh-15840). |
Maybe I should pull it with pip then. 😀 |
The semantics of round() changed in Python 3:
This works incorrectly in the case of np.float64, which returns a float. I believe the __round__ method is calling __rint__, which should return an integer but doesn't.
Reproducing code example:
This behavior is the same for float16, float32, and float128.
The text was updated successfully, but these errors were encountered: