Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0a8c72

Browse files
authored
Merge pull request #24161 from tylerjereddy/treddy_issue_23110
BUG: histogram small range robust
2 parents 0febb4c + 0b83726 commit d0a8c72

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
@@ -811,7 +811,8 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
811811
n = np.zeros(n_equal_bins, ntype)
812812

813813
# Pre-compute histogram scaling factor
814-
norm = n_equal_bins / _unsigned_subtract(last_edge, first_edge)
814+
norm_numerator = n_equal_bins
815+
norm_denom = _unsigned_subtract(last_edge, first_edge)
815816

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

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

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)