Skip to content

BUG: Handle large numbers and edge creations on histogram. - #31496

Open
JosephMehdiyev wants to merge 3 commits into
numpy:mainfrom
JosephMehdiyev:hist-gaps
Open

JosephMehdiyev wants to merge 3 commits into
numpy:mainfrom
JosephMehdiyev:hist-gaps

Conversation

@JosephMehdiyev

@JosephMehdiyev JosephMehdiyev commented May 24, 2026 •

Copy link
Copy Markdown
Contributor

PR summary

Fixes #8627 (wrongly tagged another issue)
If I am understanding correctly, prior to this PR, np.linspace would give equal edges because of the distances between float64 types i.e 2**52+0.5 couldn't be represented as a proper float and as a result edges would be equal and monotonic check would fail. To make sure this never happens, the _outer_bin_edge should use the number of bins and the spacing of the type of the edge.

this PR handles all 3 histogram functions.

Other comments

This is the first time I have worked on an issue about large floats/numbers, so be warned.

There are specific cases like np.histogram([2**53,2**53+2]) which is not quite possible to fix, unless bin numbers are changed undirectly. There is a issue about better default bin number selection which will fix this.
In some cases with big data values with very low spread, the similar issue I think will occur and linspace may generate equal sized edges. The first workaround would be to chose smaller bin sizes or somewhere in logic we could add spacing like this PR's case. But this is somewhat unrelated to the main issue so I might open another PR for that

AI Disclosure

Claude used to review the code.

@JosephMehdiyev

JosephMehdiyev commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor Author

hmmm, this PR looks simple enough.

I was checking and closing my old PRs as I was not planning to work on them anymore, but this one looks fine and simple?

If any maintainers think otherwise be welcome to close this, otherwise I'll forget this PR again haha

@eendebakpt

Copy link
Copy Markdown
Contributor

There are still some cases to be handled: for histogramdd the bin count is not passed. E.g. np.histogramdd([[2**53]], 10). Also the error for np.histogram([np.finfo(float).max]) is not good now.

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 in np.histogram when min == max and values are >= 2**53

3 participants