blob: 0d18964fbe2d656ec6f6993de145338ae7924fd1 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2017 The Chromium Authors
Peter Kasting2202c8e2017-09-13 20:40:592// 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-Sanchez92f328e2017-10-10 22:54:318#include <cmath>
Lei Zhangcdc18bb2021-12-15 03:26:589#include <type_traits>
Peter Kasting2202c8e2017-09-13 20:40:5910
11namespace base {
12
Miguel Casas-Sanchez92f328e2017-10-10 22:54:3113template <typename T>
14constexpr bool IsApproximatelyEqual(T lhs, T rhs, T tolerance) {
Andrew Rayskiyc817c3cf2023-10-16 18:15:4015 static_assert(std::is_arithmetic_v<T>, "Argument must be arithmetic");
Miguel Casas-Sanchez92f328e2017-10-10 22:54:3116 return std::abs(rhs - lhs) <= tolerance;
17}
18
Peter Kasting2202c8e2017-09-13 20:40:5919} // namespace base
20
21#endif // BASE_NUMERICS_RANGES_H_