summaryrefslogtreecommitdiff
path: root/numpy/lib/function_base.py
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/lib/function_base.py
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/lib/function_base.py')
-rw-r--r--numpy/lib/function_base.py9
1 files changed, 7 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):