I am trying to check whether one value equals + or – 2 the other value.
This is what i have come across.
for (int lineNumber : errorLineList) {
if (lineNumber == startLineNumber
|| lineNumber == (startLineNumber + 1)
|| lineNumber == (startLineNumber + 2)
|| lineNumber == (startLineNumber - 1)
|| lineNumber == (startLineNumber - 2)) {
lineFound = true;
break;
}
}
But here if i want the tolerance to be 5, then i have to add 10 checks.
Is there any other elegant way to do this in java.
This is often need when using doubles as you can get small rounding errors.
You can change 2 to 5. This is says; if the difference between two values is less than 2. i.e it could be -2, -1, 0, 1 or 2.
For
doublea similar solution is to test “equality” usingWhat is a good value of ERR to use depends on the situation.