Skip to content

BUG,DEP: Make scalar.__round__() behave like pythons round#15840

Merged
seberg merged 2 commits intonumpy:masterfrom
hameerabbasi:fix-dunder-round
Apr 10, 2020
Merged

BUG,DEP: Make scalar.__round__() behave like pythons round#15840
seberg merged 2 commits intonumpy:masterfrom
hameerabbasi:fix-dunder-round

Conversation

@hameerabbasi
Copy link
Contributor

Fixes #15297.

@hameerabbasi
Copy link
Contributor Author

cc @mattip for review.

@hameerabbasi hameerabbasi force-pushed the fix-dunder-round branch 3 times, most recently from daa1475 to 0694334 Compare March 26, 2020 11:12
@hameerabbasi hameerabbasi force-pushed the fix-dunder-round branch 2 times, most recently from 6feb7eb to 90ebb5f Compare March 26, 2020 11:25
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting this going! Looks pretty good, actually getting it right with None being passed could be slightly annoying (i.e. require parsing – could be fast pathed, although it may be that it does not matter: parsing no arguments is pretty fast).

@hameerabbasi
Copy link
Contributor Author

Looks pretty good, actually getting it right with None being passed could be slightly annoying (i.e. require parsing – could be fast pathed, although it may be that it does not matter: parsing no arguments is pretty fast).

Yes, this was, indeed, annoying. The error checking and ndigits for round vs decimals for np.round made it impossible to pass the same arguments forward, so I had to handle that corner case, and that of course, introduced a lot of error handling.

@hameerabbasi hameerabbasi force-pushed the fix-dunder-round branch 2 times, most recently from 94dba4a to 4e28c70 Compare March 27, 2020 10:28
@hameerabbasi hameerabbasi force-pushed the fix-dunder-round branch 2 times, most recently from 3136667 to beb1abf Compare April 1, 2020 06:26
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, mainly nitpicks and a few suggestions most of which you can choose to ignore. Checking whether the warning was turned into an error in the C-code is the major thing that needs fixing.

@hameerabbasi
Copy link
Contributor Author

Travis failure is unrelated.

jobs.include.arch: unknown value aarch64
jobs.include.arch: unknown value aarch64 

@mattip
Copy link
Member

mattip commented Apr 3, 2020

That seems like a travis glitch, since the aarch64 builds have been around since Jan 2020.

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, had a quick glance over it, and I think this is in good shape now, and I will merge it soon.

This PR makes it so that round(int64) returns a python integer, and round(int64, 0) returns an int64. I am just saying that here for everyone to read. I am happy with this choice.

Could you add a deprecation fragment additionally for the complex case? I am considering if not mentioning __round__ in the title is better (since it is arguable an implementation detail that not all users have to know about), but cannot think of how to reword it concisely immediately.

@seberg seberg self-requested a review April 3, 2020 17:12
@seberg seberg changed the title Fix __round__ method for scalars. BUG,DEP: Make scalar.__round__() behave like pythons round Apr 10, 2020
@seberg
Copy link
Member

seberg commented Apr 10, 2020

Thanks @hameerabbasi, lets put this in, it seems good to me with the last modifications, thanks for staying with this, its always a bit more complicated than anticipated :).

@seberg seberg merged commit 1f36506 into numpy:master Apr 10, 2020
@WeatherGod
Copy link
Contributor

Would this change have impacted how np.round() treats scalars? I am trying to chase down a bug where np.round([x]) is producing a different value sometimes from np.round(x).

@hameerabbasi hameerabbasi deleted the fix-dunder-round branch October 21, 2020 19:18
@hameerabbasi
Copy link
Contributor Author

No, we didn't change np.round, only ndarray.__round__.

@seberg
Copy link
Member

seberg commented Oct 21, 2020

@WeatherGod do you have a reproducer or similar? One thing is that round seems to call multiple, and true_divide internally with double precision numbers. So for the scalar case, we are likely using double precision calculations, but for [x] we would be using single precision in those calculations.

@WeatherGod
Copy link
Contributor

Still waiting for the actual data from my co-worker, but we have figured out that the problem is not in np.round(), but apparently different rules for upcasting calculations involving numpy scalars versus numpy arrays. I'll dig a bit more and see if a new bug report is warranted.

fqjin added a commit to fqjin/swei-net that referenced this pull request Apr 18, 2022
Prior to numpy v1.19, using round on numpy floats did not cast them to int.
See numpy/numpy#15840.
This causes a problem when round is used on line 85 to compute tsize.
The fix is to convert dx and dt to python floats for backwards compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

01 - Enhancement 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. component: numpy._core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output type of round is inconsistent with python built-in

5 participants