fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. #ifdef CPP11
  5. #define ROUND(x) std::round(x)
  6. #else /* CPP11 */
  7. inline double myRound(double x) {
  8. return (x >= 0.0 ? std::floor(x + 0.5) : std::ceil(x - 0.5));
  9. }
  10.  
  11. #define ROUND(x) myRound(x)
  12. #endif /* CPP11 */
  13.  
  14.  
  15. int main()
  16. {
  17. std::cout << "0.3 rounds to " << ROUND(0.3) << std::endl;
  18. std::cout << "With std::round(), it's " << std::round(0.3) << std::endl;
  19. std::cout << "-0.3 rounds to " << ROUND(-0.3) << std::endl;
  20. std::cout << "With std::round(), it's " << std::round(-0.3) << std::endl;
  21. std::cout << "12.8 rounds to " << ROUND(12.8) << std::endl;
  22. std::cout << "With std::round(), it's " << std::round(12.8) << std::endl;
  23. std::cout << "-31.6 rounds to " << ROUND(-31.6) << std::endl;
  24. std::cout << "With std::round(), it's " << std::round(-31.6) << std::endl;
  25.  
  26. return 0;
  27. }
  28.  
  29.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0.3 rounds to 0
With std::round(), it's 0
-0.3 rounds to -0
With std::round(), it's -0
12.8 rounds to 13
With std::round(), it's 13
-31.6 rounds to -32
With std::round(), it's -32