From ea59abfa2bfa8054554b54667e04119ae6773e40 Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Mon, 17 Feb 2025 13:06:38 +0000 Subject: [PATCH 1/4] tests: Remove floating point equality assertions --- tests/test_symbolics.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/test_symbolics.py b/tests/test_symbolics.py index 2bae5679c8..0a6badd56f 100644 --- a/tests/test_symbolics.py +++ b/tests/test_symbolics.py @@ -1,5 +1,6 @@ from ctypes import c_void_p +import math import sympy import pytest import numpy as np @@ -620,8 +621,8 @@ def test_minmax_precision(dtype, expected): # Check generated code -- ensure it's using the fp64 versions of min/max, # that is fminf/fmaxf assert all(i in str(op) for i in expected) - - assert np.all(f.data == 6.0) + tolerance = 1e-8 if dtype is np.float32 else 1e-16 + assert np.allclose(f.data, 6.0, rtol=tolerance) @pytest.mark.parametrize('dtype,expected', [ @@ -643,7 +644,8 @@ def test_pow_precision(dtype, expected): op.apply() assert expected in str(op) - assert np.all(f.data == 8.0) + tolerance = 1e-8 if dtype is np.float32 else 1e-16 + assert np.allclose(f.data, 8.0, rtol=tolerance) @pytest.mark.parametrize('dtype,expected', [ @@ -665,7 +667,8 @@ def test_abs_precision(dtype, expected): op.apply() assert expected in str(op) - assert np.all(f.data == 1.0) + tolerance = 1e-8 if dtype is np.float32 else 1e-16 + assert np.allclose(f.data, 1.0, rtol=tolerance) class TestRelationsWithAssumptions: @@ -705,7 +708,7 @@ def test_multibounds_op(self): op.apply(time_M=5) fnorm2 = norm(f) - assert fnorm == fnorm2 + assert math.isclose(fnorm, fnorm2, rel_tol=1e-8) @pytest.mark.parametrize('op, expr, assumptions, expected', [ ([min, '[a, b, c, d]', '[]', 'Min(a, Min(b, Min(c, d)))']), From 8411f26d408865f57c7b329baeab00aae1d9b38f Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Mon, 17 Feb 2025 14:26:52 +0000 Subject: [PATCH 2/4] tests: Loosen tolerances --- tests/test_symbolics.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_symbolics.py b/tests/test_symbolics.py index 0a6badd56f..4da6724aae 100644 --- a/tests/test_symbolics.py +++ b/tests/test_symbolics.py @@ -621,7 +621,7 @@ def test_minmax_precision(dtype, expected): # Check generated code -- ensure it's using the fp64 versions of min/max, # that is fminf/fmaxf assert all(i in str(op) for i in expected) - tolerance = 1e-8 if dtype is np.float32 else 1e-16 + tolerance = 1e-7 if dtype is np.float32 else 1e-15 assert np.allclose(f.data, 6.0, rtol=tolerance) @@ -644,7 +644,7 @@ def test_pow_precision(dtype, expected): op.apply() assert expected in str(op) - tolerance = 1e-8 if dtype is np.float32 else 1e-16 + tolerance = 1e-7 if dtype is np.float32 else 1e-15 assert np.allclose(f.data, 8.0, rtol=tolerance) @@ -667,7 +667,7 @@ def test_abs_precision(dtype, expected): op.apply() assert expected in str(op) - tolerance = 1e-8 if dtype is np.float32 else 1e-16 + tolerance = 1e-7 if dtype is np.float32 else 1e-15 assert np.allclose(f.data, 1.0, rtol=tolerance) @@ -708,7 +708,7 @@ def test_multibounds_op(self): op.apply(time_M=5) fnorm2 = norm(f) - assert math.isclose(fnorm, fnorm2, rel_tol=1e-8) + assert math.isclose(fnorm, fnorm2, rel_tol=1e-7) @pytest.mark.parametrize('op, expr, assumptions, expected', [ ([min, '[a, b, c, d]', '[]', 'Min(a, Min(b, Min(c, d)))']), From 70983780bf7095cd31083a68d0e2b70f7033a5fc Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Mon, 17 Feb 2025 14:40:49 +0000 Subject: [PATCH 3/4] tests: Use numpy machinary to determine tolerance --- tests/test_symbolics.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/test_symbolics.py b/tests/test_symbolics.py index 4da6724aae..35a36993cb 100644 --- a/tests/test_symbolics.py +++ b/tests/test_symbolics.py @@ -1,6 +1,5 @@ from ctypes import c_void_p -import math import sympy import pytest import numpy as np @@ -621,8 +620,7 @@ def test_minmax_precision(dtype, expected): # Check generated code -- ensure it's using the fp64 versions of min/max, # that is fminf/fmaxf assert all(i in str(op) for i in expected) - tolerance = 1e-7 if dtype is np.float32 else 1e-15 - assert np.allclose(f.data, 6.0, rtol=tolerance) + assert np.allclose(f.data, 6.0, rtol=np.finfo(dtype).eps) @pytest.mark.parametrize('dtype,expected', [ @@ -644,8 +642,7 @@ def test_pow_precision(dtype, expected): op.apply() assert expected in str(op) - tolerance = 1e-7 if dtype is np.float32 else 1e-15 - assert np.allclose(f.data, 8.0, rtol=tolerance) + assert np.allclose(f.data, 8.0, rtol=np.finfo(dtype).eps) @pytest.mark.parametrize('dtype,expected', [ @@ -667,8 +664,7 @@ def test_abs_precision(dtype, expected): op.apply() assert expected in str(op) - tolerance = 1e-7 if dtype is np.float32 else 1e-15 - assert np.allclose(f.data, 1.0, rtol=tolerance) + assert np.allclose(f.data, 1.0, np.finfo(dtype).eps) class TestRelationsWithAssumptions: @@ -708,7 +704,7 @@ def test_multibounds_op(self): op.apply(time_M=5) fnorm2 = norm(f) - assert math.isclose(fnorm, fnorm2, rel_tol=1e-7) + assert np.allclose(fnorm, fnorm2, rtol=np.finfo(fnorm.dtype).eps) @pytest.mark.parametrize('op, expr, assumptions, expected', [ ([min, '[a, b, c, d]', '[]', 'Min(a, Min(b, Min(c, d)))']), From bcef07cd6c18032c9a3d6fc91a81e399af708158 Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Mon, 17 Feb 2025 14:43:37 +0000 Subject: [PATCH 4/4] tests: Use isclose rather than allclose --- tests/test_symbolics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_symbolics.py b/tests/test_symbolics.py index 35a36993cb..48909939b8 100644 --- a/tests/test_symbolics.py +++ b/tests/test_symbolics.py @@ -704,7 +704,7 @@ def test_multibounds_op(self): op.apply(time_M=5) fnorm2 = norm(f) - assert np.allclose(fnorm, fnorm2, rtol=np.finfo(fnorm.dtype).eps) + assert np.isclose(fnorm, fnorm2, rtol=np.finfo(fnorm.dtype).eps) @pytest.mark.parametrize('op, expr, assumptions, expected', [ ([min, '[a, b, c, d]', '[]', 'Min(a, Min(b, Min(c, d)))']),