diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index 8048b62029a0..57a1cf1e5d11 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -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 @@ -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 diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py index 87e6e1d41c4a..38b3d3dcbf3f 100644 --- a/numpy/lib/tests/test_histograms.py +++ b/numpy/lib/tests/test_histograms.py @@ -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: """