To compare two double type variables in C, I have defined #define EQUALITY_EPSILON = 1e-8
. I am doing the comparison as follows:
if((img_score[i] - img_score[j]) >= EQUALITY_EPSILON){
// handle for ith score greater than jth score
}
else if((img_score[j] - img_score[i]) >= EQUALITY_EPSILON){
// handle for ith score smaller than jth score
}
else{
// handle for ith score equal to jth score
}
The problem I am facing is that the scores in my code are extremely small, therefore for EQUALITY_EPSILON = 1e-8
, the result of comparison turns out to be equality in some cases. My question is how small can I set EQUALITY_EPSILON
?
Floating point numbers aren't spread out evenly on the number line. They're very dense around 0, and as the magnitude increases, the 'delta' between two expressible values increases:
0
| | | | | | | ||| | | | | | | |
This means, for small numbers, you need to use a smaller 'epsilon'.
(EDIT: And the error, or epsilon you allow for should be in the same range as the error you already expect in the values you are comparing. i.e. Errors due to the floating point operations that produced them. See comments below for why. /EDIT).
One fair indication of the kind of 'epsilon' you need to use can be had from nextafter
in math.h
:
nextafter(x, y)
returns the next representable value after x
, when travelling along the number line, in the direction of y
.
Another way to do it would be to calculate the difference in magnitude between img_score[i]
and img_score[j]
, and seeing how small it is compared to the magnitude of img_score[i]
or img_score[j]
. How much smaller? You need to decide.