1 // 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>
9 #include <cmath>
10
11 namespace base
12 {
13
14 // DO NOT USE THIS FUNCTION. IT IS DUE TO BE REMOVED. https://crbug.com/1231569
15 // Please use base::clamp() from base/cxx17_backports.h instead.
16 //
17 // This function, unlike base::clamp(), does not check if `min` is greater than
18 // `max`, and returns a bogus answer if it is. Please migrate all code that
19 // calls this function to use base::clamp() instead.
20 //
21 // If, for some reason the broken behavior is required, please re-create this
22 // min/max nesting inline in the host code and explain with a comment why it
23 // is needed.
24 template <class T>
BrokenClampThatShouldNotBeUsed(const T & value,const T & min,const T & max)25 constexpr const T &BrokenClampThatShouldNotBeUsed(const T &value, const T &min, const T &max)
26 {
27 return std::min(std::max(value, min), max);
28 }
29
30 template <typename T>
IsApproximatelyEqual(T lhs,T rhs,T tolerance)31 constexpr bool IsApproximatelyEqual(T lhs, T rhs, T tolerance)
32 {
33 static_assert(std::is_arithmetic<T>::value, "Argument must be arithmetic");
34 return std::abs(rhs - lhs) <= tolerance;
35 }
36
37 } // namespace base
38
39 #endif // BASE_NUMERICS_RANGES_H_
40