Check double variable if it contains an integer, and not floating point

What I mean is the following:

double d1 = 555;
double d2 = 55.343;

I want to be able to tell that d1 is an integer while d2 is not. Is there an easy way to do it in C/C++?


  • Use std::modf:

    double intpart;
    modf(value, &intpart) == 0.0

    Don't convert to int! The number 1.0e+300 is an integer too you know.

    Edit: As Pete Kirkham points out, passing 0 as the second argument is not guaranteed by the standard to work, requiring the use of a dummy variable and, unfortunately, making the code a lot less elegant.