fork download
  1. #include <iostream>
  2. #include <climits>
  3. using namespace std;
  4.  
  5. bool isInteger1(float f)
  6. {
  7. return f == (long)f;
  8. }
  9.  
  10. bool isInteger2(float f)
  11. {
  12. return f <= LONG_MIN || f >= LONG_MAX || f == (long)f;
  13. }
  14.  
  15. bool isInteger3(float f)
  16. {
  17. return f >= LONG_MIN && f <= LONG_MAX && f == (long)f;
  18. }
  19.  
  20. void check(float f)
  21. {
  22. cout << f << ": " << std::boolalpha << isInteger1(f) << ", "
  23. << isInteger2(f) << ", "
  24. << isInteger3(f) << "\n";
  25. }
  26.  
  27. int main() {
  28. check(0.0f);
  29. check(42.0f);
  30. check(-1.2e20f);
  31. check(-2147483647.0f); // LONG_MIN
  32. check(-2147483648.0f); // LONG_MIN - 1
  33. check(-2147483649.0f); // LONG_MIN - 2
  34. check(2147483647.0f); // LONG_MAX
  35. check(2147483648.0f); // LONG_MAX + 1
  36. check(2147483647.1f); // LONG_MAX + 0.1
  37. return 0;
  38. }
Success #stdin #stdout 0s 4320KB
stdin
Standard input is empty
stdout
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