diff options
author | Malik Woods <mkowoods@gmail.com> | 2014-10-21 15:24:50 -0700 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-10-27 20:27:47 +0100 |
commit | 3d5f499fd6807186baf669eec491034766caab34 (patch) | |
tree | a340d4d57ab3daefd9c2b1d8316ce1e623cbb8f0 /numpy/lib/function_base.py | |
parent | 76fc3e7df95779b2a67cf40210585cc1bb776591 (diff) | |
download | numpy-3d5f499fd6807186baf669eec491034766caab34.tar.gz |
BUG: upcast weights to average result type to avoid inaccuracies
closes gh-5202
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r-- | numpy/lib/function_base.py | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 0a1d05f77..aba18f799 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -510,8 +510,7 @@ def average(a, axis=None, weights=None, returned=False): scl = avg.dtype.type(a.size/avg.size) else: a = a + 0.0 - wgt = np.array(weights, dtype=a.dtype, copy=0) - + wgt = np.asarray(weights) # Sanity checks if a.shape != wgt.shape: if axis is None: @@ -528,7 +527,7 @@ def average(a, axis=None, weights=None, returned=False): # setup wgt to broadcast along axis wgt = np.array(wgt, copy=0, ndmin=a.ndim).swapaxes(-1, axis) - scl = wgt.sum(axis=axis) + scl = wgt.sum(axis=axis, dtype=np.result_type(a.dtype, wgt.dtype)) if (scl == 0.0).any(): raise ZeroDivisionError( "Weights sum to zero, can't be normalized") |