Specifying Tolerances in Numeric Tests

An approximate test needs a tolerance derived from the input units, expected scale, and error budget of the computation.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Check close and far cases
  2. Preserve a known special case
  3. Show where absolute and relative tolerances each apply
  4. Choose tolerances from the computation or required precision

Check close and far cases

If a displacement is measured to within 0.002 meters, a result differing by 0.001 meters can be acceptable. A difference of 0.02 meters fails that particular criterion. Absolute and relative tolerances compare errors at two useful scales.

Preserve a known special case

A computation may be required to return exactly zero for empty work even when other outputs are approximate. Assert exactness when that is a real part of the function contract and keep other checks within the documented numerical budget.[1]

Show where absolute and relative tolerances each apply

Against value two million, a relative tolerance of one millionth permits an error of two. At expected zero it permits zero. An absolute tolerance can account for meaningful near-zero errors. State the comparison formula used by your numeric environment. See floating-point tolerance tests.

Expected 2,000,000, actual 2,000,001 → difference 1
Expected 0, actual 0.0001 → difference 0.0001
Absolute tolerance 0.001 admits second difference

Choose tolerances from the computation or required precision

A threshold large enough to hide a unit conversion bug gives false confidence. Specify behavior for NaN and infinities according to the interface. Test one value just above and just below the permitted error.

References

  1. Python Software Foundation, unittest: Unit Testing Framework ↩