Template Function librapid::clamp#

Function Documentation#

template<typename X, typename Lower, typename Upper, typename std::enable_if_t<typetraits::TypeInfo<X>::type == detail::LibRapidType::Scalar && typetraits::TypeInfo<Lower>::type == detail::LibRapidType::Scalar && typetraits::TypeInfo<Upper>::type == detail::LibRapidType::Scalar, int> = 0, typename ST = typetraits::ScalarReturnType<X>>
ST librapid::clamp(X x, Lower lowerLimit, Upper upperLimit)#

Limit a value to a specified range.

\( C(x, m, M) = \left\{ \begin{align*} x & \quad m \le x \le M \\ m & \quad x < m \\ M & \quad x > M \end{align*}\right. \)

If M < m, the values are swapped to make the function valid. For example, clamp(5, 10, 0) still returns 5.

Template Parameters
  • X – Type of x

  • Lower – Type of lowerLimit

  • Upper – Type of upperLimit

Parameters
  • x – Value to limit

  • lowerLimit – Lower bound (m)

  • upperLimit – Upper bound (M)

Returns

x limited to the range [lowerLimit, upperLimit]