Skip to content

BUG: Round integers exactly in np.round with negative decimals - #33544

Open
ChenyangLi4288 wants to merge 1 commit into
numpy:mainfrom
ChenyangLi4288:bug/round-int-negative-decimals
Open

ChenyangLi4288 wants to merge 1 commit into
numpy:mainfrom
ChenyangLi4288:bug/round-int-negative-decimals

Conversation

@ChenyangLi4288

Copy link
Copy Markdown

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.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: np.round on int64/uint64 with negative decimals loses precision

2 participants