proconlib

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

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: std::midpoint(a, b) extended for my library (tools/midpoint.hpp)

template <tools::non_bool_integral T>
constexpr T midpoint(T a, T b) noexcept;

It returns $\frac{a + b}{2}$ rounded towards $a$.

Constraints

Time Complexity

License

Author

Depends on

Verified with

Code

#ifndef TOOLS_MIDPOINT_HPP
#define TOOLS_MIDPOINT_HPP

#include "tools/make_unsigned.hpp"
#include "tools/non_bool_integral.hpp"

namespace tools {
  template <tools::non_bool_integral T>
  constexpr T midpoint(const T a, const T b) noexcept {
    using U = tools::make_unsigned_t<T>;
    if (a < b) {
      return a + T((U(b) - U(a)) / U(2));
    } else {
      return a - T((U(a) - U(b)) / U(2));
    }
  }
}

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



#line 1 "tools/make_unsigned.hpp"



#include <type_traits>

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

  template <typename T>
  using make_unsigned_t = typename tools::make_unsigned<T>::type;
}


#line 1 "tools/non_bool_integral.hpp"



#include <concepts>
#line 1 "tools/integral.hpp"



#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 5 "tools/integral.hpp"

namespace tools {
  template <typename T>
  concept integral = tools::is_integral_v<T>;
}


#line 7 "tools/non_bool_integral.hpp"

namespace tools {
  template <typename T>
  concept non_bool_integral = tools::integral<T> && !std::same_as<std::remove_cv_t<T>, bool>;
}


#line 6 "tools/midpoint.hpp"

namespace tools {
  template <tools::non_bool_integral T>
  constexpr T midpoint(const T a, const T b) noexcept {
    using U = tools::make_unsigned_t<T>;
    if (a < b) {
      return a + T((U(b) - U(a)) / U(2));
    } else {
      return a - T((U(a) - U(b)) / U(2));
    }
  }
}


Back to top page