Skip to content

Commit

Permalink
increase tolerance for certain math tests to two epsilon
Browse files Browse the repository at this point in the history
  • Loading branch information
burnpanck committed Sep 16, 2024
1 parent add0a81 commit 3e502fb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions test/runtime/almost_equals.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace mp_units {

template<Quantity T>
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<std::convertible_to<T> U>
requires std::same_as<typename T::rep, typename U::rep>
Expand All @@ -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<rep>) {
const auto maxXYOne = std::max({rep{1}, abs(x), abs(y)});
return abs(x - y) <= std::numeric_limits<rep>::epsilon() * maxXYOne;
return abs(x - y) <= (n_eps_ * std::numeric_limits<rep>::epsilon()) * maxXYOne;
} else {
if (x >= 0) {
return x - 1 <= y && y - 1 <= x;
Expand All @@ -71,12 +71,13 @@ struct AlmostEqualsMatcher : Catch::Matchers::MatcherGenericBase {

private:
const T& target_;
int n_eps_;
};

template<Quantity T>
AlmostEqualsMatcher<T> AlmostEquals(const T& target)
AlmostEqualsMatcher<T> AlmostEquals(const T& target, int n_eps = 1)
{
return AlmostEqualsMatcher<T>{target};
return AlmostEqualsMatcher<T>{target, n_eps};
}


Expand Down
4 changes: 2 additions & 2 deletions test/runtime/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}
}

Expand Down

0 comments on commit 3e502fb

Please sign in to comment.