Gromacs
2025.0-dev-20241009-5c23d5f
|
#include <testutils/include/testutils/testasserts.h>
Specifies a floating-point comparison tolerance and checks whether a difference is within the tolerance.
The related functions section lists methods that can be construct methods using less parameters than the full constructor, and with more obvious semantics. These should be preferred over using the constructor directly.
Several types of tolerances are possible:
0.0
and -0.0
are treated as equal for the ULP check.0.0
and -0.0
: a value with a different sign than the zero will fail the check).Either an absolute, relative, or ULP tolerance must always be specified. If several of them are specified, then the check passes if either of the tolerances is satisfied.
Any combination of absolute, relative, and ULP tolerance can be combined with the sign check. In this case, the sign check must succeed for the check to pass, even if other tolerances are satisfied.
The tolerances can be specified separately for single and double precision comparison. Different initialization functions have different semantics on how the provided tolerance values are interpreted; check their documentation.
Methods in this class do not throw, except for toString(), which may throw std::bad_alloc.
Public Member Functions | |
FloatingPointTolerance (float singleAbsoluteTolerance, double doubleAbsoluteTolerance, float singleRelativeTolerance, double doubleRelativeTolerance, uint64_t singleUlpTolerance, uint64_t doubleUlpTolerance, bool bSignMustMatch) | |
Creates a tolerance with the specified values. More... | |
bool | isWithin (const FloatingPointDifference &difference) const |
Checks whether a difference is within the specified tolerance. More... | |
std::string | toString (const FloatingPointDifference &difference) const |
Formats the tolerance as a string for assertion failure messages. | |
Related Functions | |
(Note that these are not member functions.) | |
static FloatingPointTolerance | ulpTolerance (uint64_t ulpDiff) |
Creates a tolerance that only allows a specified ULP difference. More... | |
FloatingPointTolerance | relativeToleranceAsFloatingPoint (double magnitude, double tolerance) |
Creates a tolerance that allows a difference in two compared values that is relative to the given magnitude. More... | |
FloatingPointTolerance | relativeToleranceAsPrecisionDependentFloatingPoint (double magnitude, float singleTolerance, double doubleTolerance) |
Creates a tolerance that allows a precision-dependent difference in two compared values that is relative to the given magnitude. More... | |
static FloatingPointTolerance | relativeToleranceAsPrecisionDependentUlp (double magnitude, uint64_t singleUlpDiff, uint64_t doubleUlpDiff) |
Creates a tolerance that allows a precision-dependent relative difference in a complex computation. More... | |
static FloatingPointTolerance | absoluteTolerance (double tolerance) |
Creates a tolerance that allows a specified absolute difference. | |
static FloatingPointTolerance | relativeToleranceAsUlp (double magnitude, uint64_t ulpDiff) |
Creates a tolerance that allows a relative difference in a complex computation. More... | |
static FloatingPointTolerance | defaultRealTolerance () |
Returns the default tolerance for comparing real numbers. | |
static FloatingPointTolerance | defaultFloatTolerance () |
Returns the default tolerance for comparing single-precision numbers when compared by GROMACS built in either precision mode. More... | |
|
inline |
Creates a tolerance with the specified values.
[in] | singleAbsoluteTolerance | Allowed absolute difference in a single-precision number. |
[in] | doubleAbsoluteTolerance | Allowed absolute difference in a double-precision number. |
[in] | singleRelativeTolerance | Allowed relative difference in a single-precision number. |
[in] | doubleRelativeTolerance | Allowed relative difference in a double-precision number. |
[in] | singleUlpTolerance | Allowed ULP difference in a single-precision number. |
[in] | doubleUlpTolerance | Allowed ULP difference in a double-precision number. |
[in] | bSignMustMatch | Whether sign mismatch fails the comparison. |
bool gmx::test::FloatingPointTolerance::isWithin | ( | const FloatingPointDifference & | difference | ) | const |
Checks whether a difference is within the specified tolerance.
NaNs are always treated outside the tolerance.