This question is related to the epsilon- (or delta- if you prefer) test for floating point equality. But my question is not how to do it. Instead I have a related algorithm for testing equality, and I would like feedback as to what might be wrong with it.
The idea is to take the quotient of two floating point numbers and compare it to one. This eliminates the difficulty of choosing a value for epsilon. I have tested it extensively and am happy with the results. Here is a sample implementation in Java.
public class DoubleEquals
{
public static final double EPSILON = 1e-14;
public static boolean equals( double param1, double param2 )
{
// Accounts for 0, +/-INFINITY;
// if both values are NaN the result will be false
boolean result = param1 == param2;
if ( !result )
{
double quot = param1 / param2;
result = quot > 0 && (1 - quot) < EPSILON;
}
return result;
}
}
```
param1 = 1e+300
andparam2 = 1e-300
? Observe that alsoequals(param1,param2) == equals(param2,param1)
is not always true, as it happens for the previous input. $\endgroup$1e-14
? Have a look atMIN_NORMAL
andMIN_VALUE
.) $\endgroup$