Skip to content

Commit

Permalink
WIP: Implement approx equal floating point comparison method
Browse files Browse the repository at this point in the history
Uses scaled machine epsilon to compare two floats to detertmine if spatial env width is a multiple of the radius.

Only in a test suite so far, with some tests showing it seems to work for previously bad cases.

Todo: Move into the spatial messaging classes, compute on the host, copy if wrapped is available to device, query at runtime in seatbelts mode.
  • Loading branch information
ptheywood committed Feb 29, 2024
1 parent 4a77830 commit 7751622
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/test_cases/runtime/messaging/test_spatial_2d.cu
Original file line number Diff line number Diff line change
Expand Up @@ -951,14 +951,49 @@ void wrapped_2d_test_bounds(std::array<float, 2> lower, std::array<float, 2> upp
EXPECT_NO_THROW(c.simulate());
}
}

template <typename T>
bool approxExactlyDivisible(T a, T b) {
// Scale machine epsilon by the magnitude of the larger value
T scaledEpsilon = std::max(std::abs(a), std::abs(b)) * std::numeric_limits<T>::epsilon();
// Compute the remainder
T v = std::fmod(a, b);
// approx equal if the remainder is within scaledEpsilon of 0 or b (fmod(1, 0.05f) returns ~0.05f)
return v <= scaledEpsilon || v > b - scaledEpsilon;
}

bool wrappedCompatible(float lower, float upper, float radius) {
// @todo - validate that upper is > lower?, upper != lower etc, radius != 0 && radius < upper-lower
return approxExactlyDivisible<float>(upper - lower, radius);
}


TEST(Spatial2DMessageTest, Wrapped_EnvDimsNotFactor) {
// This tests that bug #1157 is fixed
// When the interaction radius is not a factor of the width
// that agent's near the max env bound all have the full interaction radius
wrapped_2d_test_bounds({0, 0}, {50.1f, 50.1f}, 10, true);
// also includes a number of potential edge cases to ensure that no false positives are included (#1177)

wrappedCompatible(0, 1, 0.05f);
wrappedCompatible(0, 1, 0.04f);
wrappedCompatible(0, 2, 0.05f);
wrappedCompatible(0, 1, 0.005f);
wrappedCompatible(0, 1, 0.005f);

wrappedCompatible(0, 100000, 0.05f);
wrappedCompatible(0, 100000, 0.03f);


wrappedCompatible(0, 1, 0.03f);

wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.05f, false);
wrapped_2d_test_bounds({0, 0}, {2, 1}, 0.05f, false);
wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.04f, false);

wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.03f, true);


}
#else
TEST(Spatial2DMessageTest, DISABLED_Wrapped_OutOfBounds) { }
Expand Down

0 comments on commit 7751622

Please sign in to comment.