Skip to content

Commit 7751622

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 4a77830 commit 7751622

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

0 commit comments

Comments
 (0)