summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorJulian Taylor <jtaylor.debian@googlemail.com>2014-06-23 22:44:05 +0200
committerJulian Taylor <jtaylor.debian@googlemail.com>2014-06-23 22:49:33 +0200
commite20d4b91d00cea1cf495d4cb85ee9bc2b7930a3d (patch)
treec4d8f06f3ce273ca93b6abd92cf52bde06477968 /numpy
parentfe3410f7380c06adc81fb8e097c96dc51a42e0f3 (diff)
downloadnumpy-e20d4b91d00cea1cf495d4cb85ee9bc2b7930a3d.tar.gz
BUG: handle rounding issue with histogram edges on float32 data
Following inequality causes wrong counting at the edges and can be avoided by making the edge array of the same type as the input data. In [1]: np.around(np.float64(6010.36962890625), 5) Out[1]: 6010.3696300000001 In [2]: np.around(np.float32(6010.36962890625), 5) Out[2]: 6010.3701 Closes gh-4799
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/function_base.py9
-rw-r--r--numpy/lib/tests/test_function_base.py7
2 files changed, 14 insertions, 2 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 00bfab6ba..f625bcb90 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -336,6 +336,11 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
smin[i] = smin[i] - .5
smax[i] = smax[i] + .5
+ # avoid rounding issues for comparisons when dealing with inexact types
+ if np.issubdtype(sample.dtype, np.inexact):
+ edge_dt = sample.dtype
+ else:
+ edge_dt = float
# Create edge arrays
for i in arange(D):
if isscalar(bins[i]):
@@ -344,9 +349,9 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
"Element at index %s in `bins` should be a positive "
"integer." % i)
nbin[i] = bins[i] + 2 # +2 for outlier bins
- edges[i] = linspace(smin[i], smax[i], nbin[i]-1)
+ edges[i] = linspace(smin[i], smax[i], nbin[i]-1, dtype=edge_dt)
else:
- edges[i] = asarray(bins[i], float)
+ edges[i] = asarray(bins[i], edge_dt)
nbin[i] = len(edges[i]) + 1 # +1 for outlier bins
dedges[i] = diff(edges[i])
if np.any(np.asarray(dedges[i]) <= 0):
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index ee38b3573..ac677a308 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -1070,6 +1070,13 @@ class TestHistogram(TestCase):
h, b = histogram(a, weights=np.ones(10, float))
assert_(issubdtype(h.dtype, float))
+ def test_f32_rounding(self):
+ # gh-4799, check that the rounding of the edges works with float32
+ x = np.array([276.318359 , -69.593948 , 21.329449], dtype=np.float32)
+ y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32)
+ counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100)
+ assert_equal(counts_hist.sum(), 3.)
+
def test_weights(self):
v = rand(100)
w = np.ones(100) * 5