Thursday, 14 July 2016

How to set precision for double data type variables in C?











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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...