On Apr 23, 5:56 pm, xz <zhang.xi...@gmail.com> wrote:
> In a program, if a double is within 0.0001 from an int, then I treat
> it as int. For example, 10.00005 will be treated as int, while 10.001
> is not.
>
> I am looking for an algorithm to implement this checking. Anybody
> gives an idea?
>
> Thanks.
Something like this? but see also
http://www.parashift.com/c++-faq-lit...html#faq-29.17
bool close_to_int (double d, double tolerance) {
double cdiff = std::fabs (std::ceil (d) - d);
double fdiff = std::fabs (std::floor (d) - d);
if (cdiff < fdiff)
return cdiff <= tolerance;
return fdiff <= tolerance;
}