#include <iostream> #include <climits> using namespace std; bool isInteger1(float f) { return f == (long)f; } bool isInteger2(float f) { return f <= LONG_MIN || f >= LONG_MAX || f == (long)f; } bool isInteger3(float f) { return f >= LONG_MIN && f <= LONG_MAX && f == (long)f; } void check(float f) { cout << f << ": " << std::boolalpha << isInteger1(f) << ", " << isInteger2(f) << ", " << isInteger3(f) << "\n"; } int main() { check(0.0f); check(42.0f); check(-1.2e20f); check(-2147483647.0f); // LONG_MIN check(-2147483648.0f); // LONG_MIN - 1 check(-2147483649.0f); // LONG_MIN - 2 check(2147483647.0f); // LONG_MAX check(2147483648.0f); // LONG_MAX + 1 check(2147483647.1f); // LONG_MAX + 0.1 return 0; }
Standard input is empty
0: true, true, true 42: true, true, true -1.2e+20: false, true, false -2.14748e+09: true, true, true -2.14748e+09: true, true, true -2.14748e+09: true, true, true 2.14748e+09: true, true, true 2.14748e+09: true, true, true 2.14748e+09: true, true, true