proconlib

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

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: Sign function (tools/signum.hpp)

template <typename T>
constexpr int signum(T x) noexcept;

It returns

\[\begin{align*} \left\{\begin{array}{ll} -1 & \text{(if $x < 0$)}\\ 0 & \text{(if $x = 0$)}\\ 1 & \text{(if $x > 0$)} \end{array}\right.& \end{align*}\]

Constraints

Time Complexity

License

Author

Depends on

Required by

Verified with

Code

#ifndef TOOLS_SIGNUM_HPP
#define TOOLS_SIGNUM_HPP

#include "tools/is_unsigned.hpp"

namespace tools {

  template <typename T>
  constexpr int signum(const T x) noexcept {
    if constexpr (::tools::is_unsigned_v<T>) {
      return T(0) < x;
    } else {
      return (T(0) < x) - (x < T(0));
    }
  }
}

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



#line 1 "tools/is_unsigned.hpp"



#include <type_traits>

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

namespace tools {

  template <typename T>
  constexpr int signum(const T x) noexcept {
    if constexpr (::tools::is_unsigned_v<T>) {
      return T(0) < x;
    } else {
      return (T(0) < x) - (x < T(0));
    }
  }
}


Back to top page