blob: f19320ceddac2265a18924b174749bd202285ede [file] [log] [blame]
Peter Kasting2202c8e2017-09-13 20:40:591// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_NUMERICS_RANGES_H_
6#define BASE_NUMERICS_RANGES_H_
7
8#include <algorithm>
Miguel Casas-Sanchez92f328e2017-10-10 22:54:319#include <cmath>
Peter Kasting2202c8e2017-09-13 20:40:5910
11namespace base {
12
Lei Zhange4839b62017-10-28 07:28:3913// To be replaced with std::clamp() from C++17, someday.
14template <class T>
15constexpr const T& ClampToRange(const T& value, const T& min, const T& max) {
Peter Kasting2202c8e2017-09-13 20:40:5916 return std::min(std::max(value, min), max);
17}
18
Miguel Casas-Sanchez92f328e2017-10-10 22:54:3119template <typename T>
20constexpr 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
Peter Kasting2202c8e2017-09-13 20:40:5925} // namespace base
26
27#endif // BASE_NUMERICS_RANGES_H_