Skip to content

Commit 29eb33d

Browse files
committed
WIP: Implement approx equal floating point comparison method
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.
1 parent 8b241c6 commit 29eb33d

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

tests/test_cases/runtime/messaging/test_spatial_2d.cu

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,14 +952,49 @@ void wrapped_2d_test_bounds(std::array<float, 2> lower, std::array<float, 2> upp
952952
EXPECT_NO_THROW(c.simulate());
953953
}
954954
}
955+
956+
template <typename T>
957+
bool approxExactlyDivisible(T a, T b) {
958+
// Scale machine epsilon by the magnitude of the larger value
959+
T scaledEpsilon = std::max(std::abs(a), std::abs(b)) * std::numeric_limits<T>::epsilon();
960+
// Compute the remainder
961+
T v = std::fmod(a, b);
962+
// approx equal if the remainder is within scaledEpsilon of 0 or b (fmod(1, 0.05f) returns ~0.05f)
963+
return v <= scaledEpsilon || v > b - scaledEpsilon;
964+
}
965+
966+
bool wrappedCompatible(float lower, float upper, float radius) {
967+
// @todo - validate that upper is > lower?, upper != lower etc, radius != 0 && radius < upper-lower
968+
return approxExactlyDivisible<float>(upper - lower, radius);
969+
}
970+
971+
955972
TEST(Spatial2DMessageTest, Wrapped_EnvDimsNotFactor) {
956973
// This tests that bug #1157 is fixed
957974
// When the interaction radius is not a factor of the width
958975
// that agent's near the max env bound all have the full interaction radius
959976
wrapped_2d_test_bounds({0, 0}, {50.1f, 50.1f}, 10, true);
960977
// also includes a number of potential edge cases to ensure that no false positives are included (#1177)
978+
979+
wrappedCompatible(0, 1, 0.05f);
980+
wrappedCompatible(0, 1, 0.04f);
981+
wrappedCompatible(0, 2, 0.05f);
982+
wrappedCompatible(0, 1, 0.005f);
983+
wrappedCompatible(0, 1, 0.005f);
984+
985+
wrappedCompatible(0, 100000, 0.05f);
986+
wrappedCompatible(0, 100000, 0.03f);
987+
988+
989+
wrappedCompatible(0, 1, 0.03f);
990+
961991
wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.05f, false);
962992
wrapped_2d_test_bounds({0, 0}, {2, 1}, 0.05f, false);
993+
wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.04f, false);
994+
995+
wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.03f, true);
996+
997+
963998
}
964999
#else
9651000
TEST(Spatial2DMessageTest, DISABLED_Wrapped_OutOfBounds) { }

0 commit comments

Comments
 (0)