How can I write a 'clamp' / 'clip' / 'bound' macro for returning a value in a given range?
This is without side effects and works for any primitive number:
#define MIN(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })#define MAX(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })#define CLAMP(x, low, high) ({\ __typeof__(x) __x = (x); \ __typeof__(low) __low = (low);\ __typeof__(high) __high = (high);\ __x > __high ? __high : (__x < __low ? __low : __x);\ })
Can be used like so
int clampedInt = CLAMP(computedValue, 3, 7);double clampedDouble = CLAMP(computedValue, 0.5, 1.0);
Other suggested names instead of CLAMP
can be VALUE_CONSTRAINED_LOW_HIGH
, BOUNDS
, CLIPPED
.
Taken from this site http://developer.gnome.org/glib/2.34/glib-Standard-Macros.html#CLAMP:CAPS
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
Maybe you want to try it like that:
template <class T> const T& clamp(const T& value, const T& low, const T& high) { return value < low ? low: value > high? high: value;}