JavaLib
 
Tester
 
Overview
Basic Types
User Types
Inexact Values
Sets
Iterable
Traversals
Maps
Random Choice
Range Check
Equivalence
ISame
Exceptions
Constructors
Methods
CheckFail
 
Sources
Downloads
 
TSRJ Home
TSRJ Local

Comparing Inexact Values

The comparison of two objects that contain a field with the values of the types double or float (or their wrapper classes) must invoke 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.

Note: It is important to note that a comparison of two numbers of the types double or float could succeed through direct comparison. For example new Double(5.0) == 5.0 and in that case the comparison succeeds and no warning of inexact comparison is issued. This test is performed before proceeding with the inexct comparison.

If the programmer expects two inexact values to be identical (as shown above) using the regular checkExpect variant will succeed with no warning.

If the comparison of any two objects involves inexact comparison, and the test method required exact comparison, (the programmer failed to use the Inexact variant) the test case fails and the warning is displayed as well.


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 sources

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.


last updated on Mon Apr 4 15:26:25 EDT 2011generated with DrRacket