Floating point comparisons

Detailed Description

Macros for comparing floating point numbers. It's a troublesome limitation of C and C++ that floating point comparisons are not very accurate. These macros multiply their arguments by a large number before comparing them, to improve resolution.


#define PRECISION   100000.0
#define EQ(A, B)   ((lrint(A*PRECISION))==(lrint(B*PRECISION)))
#define LT(A, B)   ((lrint(A*PRECISION))<(lrint(B*PRECISION)))
#define GT(A, B)   ((lrint(A*PRECISION))>(lrint(B*PRECISION)))
#define GTE(A, B)   ((lrint(A*PRECISION))>=(lrint(B*PRECISION)))
#define LTE(A, B)   ((lrint(A*PRECISION))<=(lrint(B*PRECISION)))

Define Documentation

#define PRECISION   100000.0

Precision of comparison. The number of zeros to the left of the decimal point determines the accuracy of the comparison in decimal places to the right of the point. E.g. precision of 100000.0 gives a comparison precision of within 0.000001

#define EQ A,
 )     ((lrint(A*PRECISION))==(lrint(B*PRECISION)))

TRUE iff A and B are equal to within PRECISION

#define LT A,
 )     ((lrint(A*PRECISION))<(lrint(B*PRECISION)))

TRUE iff A is less than B, subject to PRECISION

#define GT A,
 )     ((lrint(A*PRECISION))>(lrint(B*PRECISION)))

TRUE iff A is greater than B, subject to PRECISION

#define GTE A,
 )     ((lrint(A*PRECISION))>=(lrint(B*PRECISION)))

TRUE iff A is greater than or equal B, subject to PRECISION

#define LTE A,
 )     ((lrint(A*PRECISION))<=(lrint(B*PRECISION)))

TRUE iff A is less than or equal to B, subject to PRECISION

Generated on Thu Aug 11 13:08:10 2005 for Stage by  doxygen 1.4.0