There's also modf. It breaks a floating-point number into an integral and a fractional part. I'm not sure how efficient it is, but it's perfect for this situation.
Sorry for the C example code; I didn't notice what forum I was in. [edit] See the C++ code below. [/edit]
Code:
#include <stdio.h>
#include <math.h>
void check(double x) {
double fractional, integral;
fractional = modf(x, &fractional);
printf(fractional == 0.0 ? "an integer" : "not an integer");
}
int main() {
printf("3.0 is ");
check(3.0);
putchar('\n');
printf("3.5 is ");
check(3.5);
putchar('\n');
return 0;
}
[edit] In C++:
Code:
#include <cmath>
bool is_integer(double number) {
double fractional; // ignored
return modf(number, &fractional) == 0.0;
}
I used the dummy variable fractional since modf doesn't support passing NULL for the integral part parameter.
I know I'm comparing floating point numbers with == here, but since I'm comparing with 0.0 it should be okay in most cases.
[/edit]