diff --git a/numcodecs/delta.py b/numcodecs/delta.py index f6307312..a76ce483 100644 --- a/numcodecs/delta.py +++ b/numcodecs/delta.py @@ -67,7 +67,7 @@ def encode(self, buf): if arr.dtype == bool: np.not_equal(arr[1:], arr[:-1], out=enc[1:]) else: - np.subtract(arr[1:], arr[:-1], out=enc[1:]) + np.subtract(arr[1:], arr[:-1], out=enc[1:], casting='unsafe') return enc diff --git a/numcodecs/tests/test_delta.py b/numcodecs/tests/test_delta.py index 9664efee..21c49988 100644 --- a/numcodecs/tests/test_delta.py +++ b/numcodecs/tests/test_delta.py @@ -13,19 +13,24 @@ # mix of dtypes: integer, float # mix of shapes: 1D, 2D, 3D # mix of orders: C, F +# mix of encoding types: All available types for each arrays arrays = [ - np.random.randint(0, 1, size=110, dtype='?').reshape(10, 11), - np.arange(1000, dtype=' integer types, check numpy/numpy#8987. +oveflow_proned_float_float_pairs = [ + ('f4', 'f2'), + ('f8', 'f4'), +] + + +def test_oveflow_proned_float_float_encode(): + for dtype, astype in oveflow_proned_float_float_pairs: + codec = Delta(dtype=dtype, astype=astype) + arr = np.array([0, np.finfo(astype).max.astype(dtype) * 2], dtype=dtype) + with pytest.warns(RuntimeWarning, match=r"overflow encountered"): + codec.encode(arr) + + +overflow_proned_integer_float_paris = [ + ('i4', 'f2'), + ('i8', 'f2'), + ('u4', 'f2'), + ('u8', 'f2'), +] + + +def test_oveflow_proned_integer_float_encode(): + for dtype, astype in overflow_proned_integer_float_paris: + codec = Delta(dtype=dtype, astype=astype) + arr = np.array([0, int(np.rint(np.finfo(astype).max)) * 2], dtype=dtype) + with pytest.warns(RuntimeWarning, match=r"overflow encountered"): + codec.encode(arr)