| Comparing Inexact Values | |||||||||||||||||||||||||||
The comparison of two objects that contain a field with the values of the types
Inexact numbers (
(Math.abs(val1 - val2) / (Math.max (Math.abs(val1), Math.abs(val2))))
< TOLERANCE;
However, if either 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. Note: It is important to note that a comparison of two numbers of the types
If the programmer expects two inexact values to be identical (as shown above) using the regular
If the comparison of any two objects involves inexact comparison, and the test method required exact
comparison, (the programmer failed to use the Here is an example of the use of the Inexact test methods:
import tester.*;
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");
}
}
The test report will look as follows:
--------------------------------- Tests for the class: basics.ExamplesBasics Found 1 test methods .... Ran 6 tests. 3 tests failed. Issued 5 warnings of inexact comparison. Failed test results: -------------- Error in test number 2 9.0/2.999 vs. 3.0 fails: should be inexact tester.ErrorReport: Error trace: at basics.ExamplesBasics.testCartPtDouble(ExamplesBasics.java:22) The comparison involved inexact numbers with relative tolerance 0.0010 actual: expected: 3.0010003334444812......................3.0 Error in test number 3 fails: inaccurate tester.ErrorReport: Error trace: at basics.ExamplesBasics.testCartPtDouble(ExamplesBasics.java:23) The comparison involved inexact numbers with relative tolerance 1.0E-6 actual: expected: 3.0010003334444812......................3.0 Error in test number 5 fails: should be inexact tester.ErrorReport: Error trace: at basics.ExamplesBasics.testCartPtDouble(ExamplesBasics.java:25) The comparison involved inexact numbers with relative tolerance 0.01 actual: expected: new basics.CartPtDouble:1( new basics.CartPtDouble:1( this.x = 3.0.......................... this.x = 3.0010003334444812 this.y = 4.0) this.y = 4.0) --- END OF TEST RESULTS --- Code sourcesHere 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. | ||||||||||||||||||||||||||||
| last updated on Mon Apr 4 15:26:25 EDT 2011 | generated with DrRacket |