proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:warning: Apply banker's rounding to $\frac{x}{y}$ (tools/round.hpp)

template <typename T>
T round(T x, T y);

It returns a rounded value of $\frac{x}{y}$ in the manner of banker’s rounding.

Constraints

Time Complexity

License

Author

Depends on

Code

#ifndef TOOLS_ROUND_HPP
#define TOOLS_ROUND_HPP

#include <cstdlib>
#include "tools/ceil.hpp"
#include "tools/floor.hpp"
#include "tools/mod.hpp"

namespace tools {

  template <typename T>
  constexpr T round(const T lhs, const T rhs) {
    return ::std::abs(rhs) % 2 == 0
      ? ::tools::mod(lhs, rhs) == ::std::abs(rhs) / 2
        ? ::tools::floor(lhs, rhs) % 2 == 0
          ? ::tools::floor(lhs, rhs)
          : ::tools::ceil(lhs, rhs)
        : ::tools::mod(lhs, rhs) < ::std::abs(rhs) / 2
          ? ::tools::floor(lhs, rhs)
          : ::tools::ceil(lhs, rhs)
      : ::tools::mod(lhs, rhs) <= ::std::abs(rhs) / 2
        ? ::tools::floor(lhs, rhs)
        : ::tools::ceil(lhs, rhs);
  }
}

#endif
#line 1 "tools/round.hpp"



#include <cstdlib>
#line 1 "tools/ceil.hpp"



#include <cassert>
#include <type_traits>
#line 1 "tools/is_integral.hpp"



#line 5 "tools/is_integral.hpp"

namespace tools {
  template <typename T>
  struct is_integral : ::std::is_integral<T> {};

  template <typename T>
  inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}


#line 1 "tools/is_unsigned.hpp"



#line 5 "tools/is_unsigned.hpp"

namespace tools {
  template <typename T>
  struct is_unsigned : ::std::is_unsigned<T> {};

  template <typename T>
  inline constexpr bool is_unsigned_v = ::tools::is_unsigned<T>::value;
}


#line 8 "tools/ceil.hpp"

namespace tools {
  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> ceil(const M x, const N y) noexcept {
    assert(y != 0);
    if (y >= 0) {
      if (x > 0) {
        return (x - 1) / y + 1;
      } else {
        if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
          return 0;
        } else {
          return x / y;
        }
      }
    } else {
      if (x >= 0) {
        if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
          return 0;
        } else {
          return x / y;
        }
      } else {
        return (x + 1) / y + 1;
      }
    }
  }
}


#line 1 "tools/floor.hpp"



#line 7 "tools/floor.hpp"

namespace tools {

  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> floor(const M x, const N y) noexcept {
    assert(y != 0);
    if (y >= 0) {
      if (x >= 0) {
        return x / y;
      } else {
        return (x + 1) / y - 1;
      }
    } else {
      if (x > 0) {
        return (x - 1) / y - 1;
      } else {
        return x / y;
      }
    }
  }
}


#line 1 "tools/mod.hpp"



#line 7 "tools/mod.hpp"

namespace tools {

  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> mod(const M a, const N b) noexcept {
    assert(b != 0);

    using UM = ::std::make_unsigned_t<M>;
    using UN = ::std::make_unsigned_t<N>;
    const UM ua = a >= 0 ? a : static_cast<UM>(-(a + 1)) + 1;
    const UN ub = b >= 0 ? b : static_cast<UN>(-(b + 1)) + 1;
    auto r = ua % ub;
    if (a < 0 && r > 0) {
      r = ub - r;
    }
    return r;
  }
}


#line 8 "tools/round.hpp"

namespace tools {

  template <typename T>
  constexpr T round(const T lhs, const T rhs) {
    return ::std::abs(rhs) % 2 == 0
      ? ::tools::mod(lhs, rhs) == ::std::abs(rhs) / 2
        ? ::tools::floor(lhs, rhs) % 2 == 0
          ? ::tools::floor(lhs, rhs)
          : ::tools::ceil(lhs, rhs)
        : ::tools::mod(lhs, rhs) < ::std::abs(rhs) / 2
          ? ::tools::floor(lhs, rhs)
          : ::tools::ceil(lhs, rhs)
      : ::tools::mod(lhs, rhs) <= ::std::abs(rhs) / 2
        ? ::tools::floor(lhs, rhs)
        : ::tools::ceil(lhs, rhs);
  }
}


Back to top page