As I understand the values of floating point types must be compared carefully to avoid issues with inherent floating point errors. This can be improved by comparing values with an error threshold.
For example, the following solution is more usable than a straightforward x == y
test:
static float CompareRelativeError(float x, float y) {
return Math.Abs(x - y) / Math.Max(Math.Abs(x), Math.Abs(y));
}
static bool CompareAlmostEqual(float x, float y, float delta) {
return x == y || CompareRelativeError(x, y) < delta;
}
// apologies if this is a poor example
if (CompareAlmostEqual(1f/10f, 0.1f)) { ... }
The above solution was derived from the following resource: Is it safe when compare 2 float/double directly in Java?
Whilst I haven't been able to find any literature to confirm this, to me it seems that the same must hold true for comparisons like x > y
. For example, if x
and y
are essentially equal, how can one be greater than the other...
static bool CompareGreater(float x, float y, float delta) {
return x > y && !CompareAlmostEqual(x, y, delta);
}
And thus the following would be valid for x >= y
:
static bool CompareGreaterOrEqual(float x, float y) {
return x >= y;
}
Are my assumptions correct?