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

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.

Leave a Comment