Skip to content

Commit

Permalink
Compare float values with a tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddieAkeroyd authored and mdavidsaver committed Feb 14, 2020
1 parent aca717a commit 2c199fe
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion testApp/remote/channelAccessIFTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const std::string ChannelAccessIFTest::TEST_ARRAY_CHANNEL_NAME = "testArray1";
#define EXTRA_STRESS_TESTS 0
#endif

/// test if values are equal within a given tolerance
template <typename T>
inline bool compareWithTol(const T v1, const T v2, const T tol)
{
return (std::fabs(v1 - v2) <= tol);
}

int ChannelAccessIFTest::runAllTest() {

testPlan(152+EXTRA_STRESS_TESTS);
Expand Down Expand Up @@ -1924,7 +1931,7 @@ void ChannelAccessIFTest::test_channelArray() {
//checking 1.1 2.2 3.3 4.4 5.5
for (unsigned i = 0; i < numOfElements; i++) {
int ii = i + 1;
testOk(data[i] == (ii + ii*0.1), "%s: data slot %d should be %f not %f", CURRENT_FUNCTION, i, (ii+ii*0.1), data[i]);
testOk(compareWithTol(data[i], ii + ii*0.1, 1e-5), "%s: data slot %d should be %f not %f", CURRENT_FUNCTION, i, (ii+ii*0.1), data[i]);
}


Expand Down

0 comments on commit 2c199fe

Please sign in to comment.