8000 BUG: histogram small range robust by tylerjereddy · Pull Request #24161 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: h 8000 istogram small range robust #24161

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

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions numpy/lib/histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,8 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
n = np.zeros(n_equal_bins, ntype)

# Pre-compute histogram scaling factor
norm = n_equal_bins / _unsigned_subtract(last_edge, first_edge)
norm_numerator = n_equal_bins
norm_denom = _unsigned_subtract(last_edge, first_edge)

# We iterate over blocks here for two reasons: the first is that for
# large arrays, it is actually faster (for example for a 10^8 array it
Expand Down Expand Up @@ -839,7 +840,8 @@ def histogram(a, bins=10, range=None, density=None, weights=None):

# Compute the bin indices, and for values that lie exactly on
# last_edge we need to subtract one
f_indices = _unsigned_subtract(tmp_a, first_edge) * norm
f_indices = ((_unsigned_subtract(tmp_a, first_edge) / norm_denom)
* norm_numerator)
indices = f_indices.astype(np.intp)
indices[indices == n_equal_bins] -= 1

Expand Down
7 changes: 7 additions & 0 deletions numpy/lib/tests/test_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ def test_big_arrays(self):
hist = np.histogramdd(sample=sample, bins=(xbins, ybins, zbins))
assert_equal(type(hist), type((1, 2)))

def test_gh_23110(self):
hist, e = np.histogram(np.array([-0.9e-308], dtype='>f8'),
bins=2,
range=(-1e-308, -2e-313))
expected_hist = np.array([1, 0])
assert_array_equal(hist, expected_hist)


class TestHistogramOptimBinNums:
"""
Expand Down
0