8000 Merge pull request #24185 from charris/backport-24161 · numpy/numpy@c300e10 · GitHub
[go: up one dir, main page]

Skip to content

Commit c300e10

Browse files
authored
Merge pull request #24185 from charris/backport-24161
BUG: histogram small range robust
2 parents b92248a + 78137ba commit c300e10

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

numpy/lib/histograms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,8 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
807807
n = np.zeros(n_equal_bins, ntype)
808808

809809
# Pre-compute histogram scaling factor
810-
norm = n_equal_bins / _unsigned_subtract(last_edge, first_edge)
810+
norm_numerator = n_equal_bins
811+
norm_denom = _unsigned_subtract(last_edge, first_edge)
811812

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

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

numpy/lib/tests/test_histograms.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,13 @@ def test_big_arrays(self):
408408
hist = np.histogramdd(sample=sample, bins=(xbins, ybins, zbins))
409409
assert_equal(type(hist), type((1, 2)))
410410

411+
def test_gh_23110(self):
412+
hist, e = np.histogram(np.array([-0.9e-308], dtype='>f8'),
413+
bins=2,
414+
range=(-1e-308, -2e-313))
415+
expected_hist = np.array([1, 0])
416+
assert_array_equal(hist, expected_hist)
417+
411418

412419
class TestHistogramOptimBinNums:
413420
"""

0 commit comments

Comments
 (0)
0