diff options
Diffstat (limited to 'zen/basic_math.h')
-rw-r--r-- | zen/basic_math.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/zen/basic_math.h b/zen/basic_math.h index 7923dc5d..bd416d19 100644 --- a/zen/basic_math.h +++ b/zen/basic_math.h @@ -32,6 +32,8 @@ const T& max(const T& a, const T& b, const T& c); template <class T> void confine(T& val, const T& minVal, const T& maxVal); //make sure minVal <= val && val <= maxVal +template <class T> +T confineCpy(const T& val, const T& minVal, const T& maxVal); template <class InputIterator> std::pair<InputIterator, InputIterator> minMaxElement(InputIterator first, InputIterator last); @@ -97,6 +99,7 @@ const double ln2 = 0.693147180559945309417; template <class T> inline T abs(T value) { + //static_assert(std::is_signed<T>::value, ""); might not compile for non-built-in arithmetic types; anyway "-value" should emit compiler error or warning for unsigned types if (value < 0) return -value; // operator "?:" caveat: may be different type than "value" else @@ -132,6 +135,17 @@ const T& max(const T& a, const T& b, const T& c) template <class T> inline +T confineCpy(const T& val, const T& minVal, const T& maxVal) +{ + assert(minVal <= maxVal); + if (val < minVal) + return minVal; + else if (val > maxVal) + return maxVal; + return val; +} + +template <class T> inline void confine(T& val, const T& minVal, const T& maxVal) //name trim? { assert(minVal <= maxVal); |