Repository navigation
BUG: Round integers exactly in np.round with negative decimals - #33544
Open
ChenyangLi4288 wants to merge 1 commit into
Open
ChenyangLi4288 wants to merge 1 commit into
ChenyangLi4288 wants to merge 1 commit into
Conversation
yimingdx
reviewed
Dec 6, 2026
Comment on lines
+115
to
+124
| /* p - r rather than 2 * r, which can overflow */ | ||
| rest = ufunc_call(n_ops->subtract, p, r); | ||
| if (rest == NULL) { | ||
| goto finish; | ||
| } | ||
| up = ufunc_call(n_ops->greater, r, rest); | ||
| if (up == NULL) { | ||
| goto finish; | ||
| } | ||
| tie = ufunc_call(n_ops->equal, r, rest); |
Contributor
There was a problem hiding this comment.
Below takes about 3 ms per call on the base and 13 ms with the patch, producing the same result.
import numpy as np
a = np.arange(1_000_000, dtype=np.int64)
np.round(a, -3)Could we try to reduce the overhead here?
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
Closes #33461
What problem does this PR solve: To round a big whole number like 1,700,000,000,123,456,789 to the nearest thousand, NumPy previously converted it to a float that keeps only about 16 significant digits, blurring the last few digits of a 19-digit number before rounding. The answer came out slightly wrong: …457,024 instead of …457,000. More details in BUG: np.round on int64/uint64 with negative decimals loses precision #33461.
Fix: Whole numbers now stay whole the entire time. To round to the nearest thousand, it now rounds the way peopel would on paper: First split the number into "how many thousands" (1,700,000,000,123,456) and "what's left over" (789). Then if the leftover is more than half a thousand, round up. If it's less, round down. If it's exactly half, round to the even choice. Finally multiply back: 1,700,000,000,123,457 × 1000 = 1,700,000,000,123,457,000. i.e.
q = a // 10k, r = a % 10k, bump q if r > p - r or tie with q odd (half-to-even, same as floats / Python round), result q * p.
Why are you interested in working on this PR: I raised the issue of ns timestamps -> rounding to microseconds for dedup in postgres.
How does the proposed change help you: To solve the issue I encountered above.
First time contributor introduction
I'm a data scientist working at Columbia University. I use np for both work and personal purposes. I have been using np for more than 5 years because I appreciate high-performance code.
AI Disclosure
Test are completely generated by Opus 5.5, I reviewed them and checked against Python's round().
Initial implement done by me manually, Opus 5.5 helped debug my code.