Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Peter Kasting | 2202c8e | 2017-09-13 20:40:59 | [diff] [blame] | 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 | |
Miguel Casas-Sanchez | 92f328e | 2017-10-10 22:54:31 | [diff] [blame] | 8 | #include <cmath> |
Lei Zhang | cdc18bb | 2021-12-15 03:26:58 | [diff] [blame] | 9 | #include <type_traits> |
Peter Kasting | 2202c8e | 2017-09-13 20:40:59 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | |
Miguel Casas-Sanchez | 92f328e | 2017-10-10 22:54:31 | [diff] [blame] | 13 | template <typename T> |
| 14 | constexpr bool IsApproximatelyEqual(T lhs, T rhs, T tolerance) { |
Andrew Rayskiy | c817c3cf | 2023-10-16 18:15:40 | [diff] [blame] | 15 | static_assert(std::is_arithmetic_v<T>, "Argument must be arithmetic"); |
Miguel Casas-Sanchez | 92f328e | 2017-10-10 22:54:31 | [diff] [blame] | 16 | return std::abs(rhs - lhs) <= tolerance; |
| 17 | } |
| 18 | |
Peter Kasting | 2202c8e | 2017-09-13 20:40:59 | [diff] [blame] | 19 | } // namespace base |
| 20 | |
| 21 | #endif // BASE_NUMERICS_RANGES_H_ |