## Comparing Inexact Values

A pedagogical guide.

In the past we used a default tolerance to compare any two inexact values. This was a source of a major flaw in making the meaning of every test independent of all other tests. If a test expected the default tolerance to be 0.001 and someone added a new test before this that set the tolerance to 0.0001, our original test could now fail, even though it has succeeded in an earlier run. This is a bad policy overall. We want to teach students to write every test in the manner that will make it independent of any other tests the program may run.

When trying to resolve this problem it became clear that the programmer must set the tolerance for every test explicitly. Of course, now our shortcuts of providing a default tolerance became not only useless, but also dangerous.

But the next problem now was the fact that if the comparison of two objects involved inexact values and we did not do anything about it, we could again come up with a situation where at one time the test would succeed and at a later time it would fail.

To make it clear that a method involves inxeact comparison, the new method names mimic the old ones with the word `Inexact` added after the `check`. The arguments are the same as for the matching exact version, with the new ```double tolerance``` argument added in what was in each case the most appropriate place.

We expect students to stumble here and forget that some object may contain inexact values. To make sure it is clear why a test failed, every test result that involved inexact comparison in its evaluation (regardless of whether you are printing all test cases or only those that failed) shows a warning that inexact comparison has been done. If the two values are inexact, but both are identical (i.e. the test ```a == b``` succeeds), the comparison is not marked as inexact.

We have chosen to use the relative tolerance, rather than the absolute one used in ProfessorJ. We provide the following justification for this choice. Suppose we have the class `CityPair` that contains a field that represents the location given as the latitude and longitude. These are inexact values of the magnitude between 0 and 180. At the same time the class conatins the field that represents the distance between the two cities in miles, also given as an inexact value, which can be anything between 30 and 3000. Using the absolute tolerance for both comparisons would make one of the comparisons either meaningless or faulty. We know this is a far-fetched example, but the possibility of defining a class with two inexact fields with substantially different expected magnitude of values is a problem that we have chosen to address here.

Posted: May 20, 2009 8:15 am

Last modified: Wed May 20 8:16:18 EDT 2009