From 3e502fb795a93c2f56bf78034c7ff92cc116aeb6 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Mon, 16 Sep 2024 20:34:12 +0200 Subject: [PATCH] increase tolerance for certain math tests to two epsilon --- test/runtime/almost_equals.h | 9 +++++---- test/runtime/math_test.cpp | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/test/runtime/almost_equals.h b/test/runtime/almost_equals.h index 0cc03e8e..4c2153a0 100644 --- a/test/runtime/almost_equals.h +++ b/test/runtime/almost_equals.h @@ -35,7 +35,7 @@ namespace mp_units { template struct AlmostEqualsMatcher : Catch::Matchers::MatcherGenericBase { - explicit AlmostEqualsMatcher(const T& target) : target_{target} {} + explicit AlmostEqualsMatcher(const T& target, int n_eps) : target_{target}, n_eps_{n_eps} {} template U> requires std::same_as @@ -48,7 +48,7 @@ struct AlmostEqualsMatcher : Catch::Matchers::MatcherGenericBase { const auto y = common(other).numerical_value_in(common::unit); if constexpr (treat_as_floating_point) { const auto maxXYOne = std::max({rep{1}, abs(x), abs(y)}); - return abs(x - y) <= std::numeric_limits::epsilon() * maxXYOne; + return abs(x - y) <= (n_eps_ * std::numeric_limits::epsilon()) * maxXYOne; } else { if (x >= 0) { return x - 1 <= y && y - 1 <= x; @@ -71,12 +71,13 @@ struct AlmostEqualsMatcher : Catch::Matchers::MatcherGenericBase { private: const T& target_; + int n_eps_; }; template -AlmostEqualsMatcher AlmostEquals(const T& target) +AlmostEqualsMatcher AlmostEquals(const T& target, int n_eps = 1) { - return AlmostEqualsMatcher{target}; + return AlmostEqualsMatcher{target, n_eps}; } diff --git a/test/runtime/math_test.cpp b/test/runtime/math_test.cpp index 2a59135b..79018bff 100644 --- a/test/runtime/math_test.cpp +++ b/test/runtime/math_test.cpp @@ -448,7 +448,7 @@ TEST_CASE("Angle trigonometric functions", "[trig][angle]") REQUIRE_THAT(sin(0 * angle[grad]), AlmostEquals(0. * one)); REQUIRE_THAT(sin(100 * angle[grad]), AlmostEquals(1. * one)); - REQUIRE_THAT(sin(200 * angle[grad]), AlmostEquals(0. * one)); + REQUIRE_THAT(sin(200 * angle[grad]), AlmostEquals(0. * one, 2)); REQUIRE_THAT(sin(300 * angle[grad]), AlmostEquals(-1. * one)); } @@ -475,7 +475,7 @@ TEST_CASE("Angle trigonometric functions", "[trig][angle]") REQUIRE_THAT(tan(0 * angle[grad]), AlmostEquals(0. * one)); REQUIRE_THAT(tan(50 * angle[grad]), AlmostEquals(1. * one)); REQUIRE_THAT(tan(150 * angle[grad]), AlmostEquals(-1. * one)); - REQUIRE_THAT(tan(200 * angle[grad]), AlmostEquals(0. * one)); + REQUIRE_THAT(tan(200 * angle[grad]), AlmostEquals(0. * one, 2)); } }