diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2014-02-12 11:57:27 +0100 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2014-02-16 00:00:22 +0100 |
commit | ab04e1ae0e8eca717bc7e42f3b0a60c9ff764289 (patch) | |
tree | 49ea02f820c4ee3eb484578abd0078f543ef4898 /numpy/testing/utils.py | |
parent | 58e9e27c0c110f9be1558a53fb547dc1abc76fa4 (diff) | |
download | numpy-ab04e1ae0e8eca717bc7e42f3b0a60c9ff764289.tar.gz |
BUG: Force allclose logic to use inexact type
Casting y to an inexact type fixes problems such as
abs(MIN_INT) < 0, and generally makes sense since the allclose
logic is inherently for float types.
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r-- | numpy/testing/utils.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 82aa1e39c..97908c7e8 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -793,7 +793,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): y: array([ 1. , 2.33333, 5. ]) """ - from numpy.core import around, number, float_ + from numpy.core import around, number, float_, result_type, array from numpy.core.numerictypes import issubdtype from numpy.core.fromnumeric import any as npany def compare(x, y): @@ -811,17 +811,21 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): except (TypeError, NotImplementedError): pass - if x.dtype.kind == 'b' and y.dtype.kind == 'b': - z = x ^ y - else: - z = abs(x-y) + # make sure y is an inexact type to avoid abs(MIN_INT); will cause + # casting of x later. + dtype = result_type(y, 1.) + y = array(y, dtype=dtype, copy=False) + z = abs(x-y) if not issubdtype(z.dtype, number): z = z.astype(float_) # handle object arrays + return around(z, decimal) <= 10.0**(-decimal) + assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose, header=('Arrays are not almost equal to %d decimals' % decimal)) + def assert_array_less(x, y, err_msg='', verbose=True): """ Raise an assertion if two array_like objects are not ordered by less than. |