.. _sprout-algorithm-clamp: ############################################################################### clamp ############################################################################### Interface ======================================== .. sourcecode:: c++ template inline SPROUT_CONSTEXPR T const& clamp(T const& value, typename sprout::identity::type const& low, typename sprout::identity::type const& high); template inline SPROUT_CONSTEXPR T const& clamp(T const& value, typename sprout::identity::type const& low, typename sprout::identity::type const& high, Compare comp); Requires ======================================== | Type T is LessThanComparable. Returns ======================================== | low if the following corresponding conditions hold: ``value < low``, ``comp(value, low)``. | high if the following corresponding conditions hold: ``high < value``, ``comp(high, value)``. | Otherwise, returns value. Remarks ======================================== | Using clamp with floating point numbers may give unexpected results if one of the values is NaN. Complexity ======================================== | Exactly one or two comparisons. Examples ======================================== .. sourcecode:: c++ #include #include using namespace sprout; SPROUT_STATIC_CONSTEXPR auto input = array{{0, 10}}; SPROUT_STATIC_CONSTEXPR auto result = sprout::clamp(20, input[0], input[1]); static_assert(result == 10, "clamped value is 10."); Header ======================================== | ``sprout/algorithm/clamp.hpp`` | Convenience header: ``sprout/algorithm.hpp``