Hi, I'm trying to write a macro for the relative difference function which is used to check the close enough floating point values. This is the relative difference function. I want to write a macro instead :
Code:
#define Abs(x) ((x < 0 ? -(x) : (x))
#define Max(a,b) ((a) > (b) ? (a) : (b))
double reldif( double a, double b)
{
double c = Abs(a);
double d = Abs(b);
d = Max(c,d);
return d == 0.0 ? 0.0 : Abs(a-b)/d;
}
Then for checking equality of two floating points :
#define EPSILON 0.000001
.....
if ( reldif(a, b) <= TOLERANCE)
And here's my macro eq(a,b):
Code:
#define max(x, y) ((x) > (y) ? (x) : (y))
#define eq(a, b) max(fabs(a), fabs(b)) == 0.0 ? 0.0 : fabs(a - b) / (max(fabs(a), fabs(b)))
So we can simply write eq(a,b) <= EPSILON
can some one please tell me if this is correct or not ?