diff options
-rw-r--r-- | numpy/core/src/multiarray/convert_datatype.c | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 14 |
2 files changed, 15 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index bb01d875b..f0f18eead 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -1353,7 +1353,7 @@ static int min_scalar_type_num(char *valueptr, int type_num, case NPY_UINT: { npy_uint value = *(npy_uint *)valueptr; if (value <= NPY_MAX_UBYTE) { - if (value < NPY_MAX_BYTE) { + if (value <= NPY_MAX_BYTE) { *is_small_unsigned = 1; } return NPY_UBYTE; diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index b53c4338e..f1133b8c9 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -875,6 +875,20 @@ class TestTypes(object): # Also test keyword arguments assert_(np.can_cast(from_=np.int32, to=np.int64)) + def test_can_cast_values(self): + # gh-5917 + for dt in np.sctypes['int'] + np.sctypes['uint']: + ii = np.iinfo(dt) + assert_(np.can_cast(ii.min, dt)) + assert_(np.can_cast(ii.max, dt)) + assert_(not np.can_cast(ii.min - 1, dt)) + assert_(not np.can_cast(ii.max + 1, dt)) + + for dt in np.sctypes['float']: + fi = np.finfo(dt) + assert_(np.can_cast(fi.min, dt)) + assert_(np.can_cast(fi.max, dt)) + # Custom exception class to test exception propagation in fromiter class NIterError(Exception): |