fork download
  1. #include <cassert>
  2. #include <functional>
  3.  
  4. int floorCeilImpl(const std::function<bool (int, int)> &comparator,
  5. const std::function<int (int, int)> &operation,
  6. double x) {
  7.  
  8. const int int_part = static_cast<int>(x);
  9.  
  10. if (comparator(int_part, 0)) {
  11. return int_part;
  12. } else {
  13. return operation(int_part, (x != int_part));
  14. }
  15.  
  16. }
  17.  
  18. int floor(double x) {
  19. return floorCeilImpl(std::greater_equal<int>(), std::minus<int>(), x);
  20. }
  21.  
  22. int ceil(double x) {
  23. return floorCeilImpl(std::less<int>(), std::plus<int>(), x);
  24. }
  25.  
  26. int main() {
  27. assert(ceil(0.0) == 0);
  28. assert(floor(0.0) == 0);
  29.  
  30. assert(ceil(1.0) == 1);
  31. assert(floor(1.0) == 1);
  32.  
  33. assert(ceil(-1.0) == -1);
  34. assert(floor(-1.0) == -1);
  35.  
  36. assert(ceil(1.2) == 2);
  37. assert(floor(1.2) == 1);
  38.  
  39. assert(ceil(-1.2) == -1);
  40. assert(floor(-1.2) == -2);
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
Standard output is empty