In my program some double variables take values of the form 1.00000001. Equality check of these variables with 1 obviously fails.
I wanted to know how can I reduce the precision of double type variables in c, so that equality with integers works.
Answer
There's no portable way, no.
With the GNU C library, you can use this API to change the rounding mode.
But in general, it's better to express it with code, so that your expectations become clear and portable:
#define EQUALITY_EPSILON 1e-3 /* Or whatever. */
if(fabs(x - y) <= EQUALITY_EPSILON)
{
}
No comments:
Post a Comment