at master 1.5 kB view raw
1From 75b2156a6414e2464eb15663004b8ab928374135 Mon Sep 17 00:00:00 2001 2From: Sveinung Rundhovde <ssru@equinor.com> 3Date: Tue, 30 Jul 2024 08:32:56 +0200 4Subject: [PATCH] Fix test failing due to Numpy 2.0 promotion rules 5 6From Numpy 2.0 adding a numpy.float32 and a Python numeric type returns 7a numy.float32 when it previously returned a numpy.float64. This changes 8the behavior when using the Python builtin sum function on a 9numpy.float32 array as the internal computations now will be performed 10as numpy.float32 additions when it used to be numpy.float64. 11 12Passing a numpy.double(0) as a start value to the innermost sum forces 13the old behavior and provides consistent results for Numpy 1 and 2. 14--- 15 python/test/segyio_c.py | 4 ++-- 16 1 file changed, 2 insertions(+), 2 deletions(-) 17 18diff --git a/python/test/segyio_c.py b/python/test/segyio_c.py 19index 45fe95d89..b1e144d9d 100644 20--- a/python/test/segyio_c.py 21+++ b/python/test/segyio_c.py 22@@ -540,10 +540,10 @@ def read_line(f, metrics, iline_idx, xline_idx): 23 buf = numpy.zeros((len(iline_idx), samples), dtype=numpy.single) 24 25 f.getline(xline_trace0, len(iline_idx), xline_stride, offsets, buf) 26- assert sum(sum(buf)) == approx(800.061169624, abs=1e-6) 27+ assert sum(sum(buf), numpy.double(0)) == approx(800.061169624, abs=1e-6) 28 29 f.getline(iline_trace0, len(xline_idx), iline_stride, offsets, buf) 30- assert sum(sum(buf)) == approx(305.061146736, abs=1e-6) 31+ assert sum(sum(buf), numpy.double(0)) == approx(305.061146736, abs=1e-6) 32 33 f.close() 34