| 6 | #define BASE_NUMERICS_RANGES_H_ |
| 7 |
| 8 | #include <algorithm> |
| 9 | #include <cmath> |
| 10 |
| 11 | namespace base { |
| 12 |
| 13 | // To be replaced with std::clamp() from C++17, someday. |
| 14 | template <class T> |
| 15 | constexpr const T& ClampToRange(const T& value, const T& min, const T& max) { |
| 16 | return std::min(std::max(value, min), max); |
| 17 | } |
| 18 |
| 19 | template <typename T> |
| 20 | constexpr bool IsApproximatelyEqual(T lhs, T rhs, T tolerance) { |
| 21 | static_assert(std::is_arithmetic<T>::value, "Argument must be arithmetic"); |
| 22 | return std::abs(rhs - lhs) <= tolerance; |
| 23 | } |
| 24 |
| 25 | } // namespace base |
| 26 |
| 27 | #endif // BASE_NUMERICS_RANGES_H_ |
| 28 |