Tests that compare two objects that contain a field with values of types double or float (or their wrapper classes).
The comparison of two objects that contain a field with the values of the types double or float(or their wrapper classes) invokes the appropriate Inexact variant of the test method, and must provide the desired relative TOLERANCE to determine the desired accuracy of the comparison. Inexact numbers (double and Double or float and Float) are considered the same if their values val1 and val2 satisfy the formula:
(Math.abs(val1 - val2) / (Math.max (Math.abs(val1), Math.abs(val2)))) < TOLERANCE
However, if either val1 or val2 is exact zero, the test checks that the absolute value of the other data is less that TOLERANCE. Otherwise, the test would always fail.
If a comparison between two objects involves inexact comparison of any two fields, the test case report, whether successful or not, includes a warning that an inexact comparison has been involved in evaluating this test case.
The class ExamplesInexact contains all test cases.
Here is the complete source code for this test suite.
You can also download the entire souce code as a zip file.
Complete test results are shown here.
Here is an example of the use of the Inexact test methods:
class CartPtDouble {
double x;
double y;
CartPtDouble(double x, double y) { this.x = x; this.y = y; }
double distTo0() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
class Examples {
Examples() {}
CartPtDouble pt = new CartPtDouble(3.0, 4.0);
void testCartPtDouble(Tester t) {
t.checkExpect(pt.distTo0(), 5.0, "exact - will succeed");
t.checkExpect(9.0/2.999, 3.0, "fails: should be inexact");
t.checkInexact(9.0/2.999, 3.0, 0.000001, "fails: inaccurate");
t.checkInexact(9.0/2.999, 3.0, 0.01, "9.0/2.999 vs. 3.0 - success");
t.checkExpect(pt, new CartPtDouble(9.0/2.999, 4.0), "fails: should be inexact");
t.checkInexact(pt, new CartPtDouble(9.0/2.999, 4.0), 0.001, "will succeed");
}
}