There is no widely used class or library for this because it is a poor programming technique, and, to the extent it provides any benefit, is not easily adaptable to different situations.
Accepting unequal numbers as equal reduces false negatives (results that are not accepted as equal due to rounding in floating-point operations but that would be equal if calculated exactly) but increases false positives (results that are accepted as equal but that are not equal if calculated exactly). There is no general principle that balances reduction of false negatives against increase of false positives or vice-versa, so there is no general solution for this problem.
Additionally, a number of different situations arise: The magnitude of floating-point errors may be relative to the magnitude of the results, so a tolerance for error would have to be proportional to the results. Or the magnitude may be largely absolute (with respect to the results, perhaps relative to some other aspect of the problem), so a tolerance for error would have to be a fixed number. Errors on one side of a threshold may be tolerable while errors on the other side might not be (as when taking an arcsine near one or a square root near zero).
If you do not care about false positives, then you can simply replace your test with true
, and that will eliminate all false negatives. If that is not acceptable to you, then you need to determine what false positives are unacceptable.
This determination is highly dependent on the calculations you are performing and their purpose. It is important to determine what rounding errors may occur in your calculations, how they will affect your results, and whether the errors are tolerable in your application.