Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient/elegant way to clip a number?

Given a real (n), a maximum value this real can be (upper), and a minimum value this real can be (lower), how can we most efficiently clip n, such that it remains between lower and upper?

Of course, using a bunch of if statements can do this, but that's boring! What about more compact and elegant/fun solutions?

My own quick attempt (C/C++):

float clip( float n, float lower, float upper )
{
    n = ( n > lower ) * n + !( n > lower ) * lower;
    return ( n < upper ) * n + !( n < upper ) * upper;
}

I'm sure there are other, better ways to do this, that's why I'm putting this out there..!

like image 258
Alex Z Avatar asked Feb 17 '12 06:02

Alex Z


3 Answers

What about boring, old, readable, and shortest yet:

float clip(float n, float lower, float upper) {
  return std::max(lower, std::min(n, upper));
}

?

This expression could also be 'genericized' like so:

template <typename T>
T clip(const T& n, const T& lower, const T& upper) {
  return std::max(lower, std::min(n, upper));
}

Update

Billy ONeal added:

Note that on windows you might have to define NOMINMAX because they define min and max macros which conflict

like image 163
justin Avatar answered Nov 05 '22 10:11

justin


Why rewrite something that's already been written for you?

#include <boost/algorithm/clamp.hpp>
boost::algorithm::clamp(n, lower, upper);

As of C++17, this is now part of the STL:

#include <algorithm>
std::clamp(n, lower, upper);
like image 63
Riot Avatar answered Nov 05 '22 08:11

Riot


C++17 is expected to add a clamp function. Courtesy of cppreference.com:

template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi );

template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp );
like image 24
Josh Kelley Avatar answered Nov 05 '22 09:11

Josh Kelley