scenario_simulator_v2 C++ API
comparison.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ARITHMETIC__FLOATING_POINT__COMPARISON_HPP_
16 #define ARITHMETIC__FLOATING_POINT__COMPARISON_HPP_
17 
18 #include <algorithm>
19 #include <limits>
20 
21 namespace math
22 {
23 namespace arithmetic
24 {
25 template <typename T>
26 auto isApproximatelyEqualTo(T a, T b)
27 {
28  return std::abs(a - b) <=
29  (std::numeric_limits<T>::epsilon() * std::max(std::abs(a), std::abs(b)));
30 }
31 
32 template <typename T>
33 auto isEssentiallyEqualTo(T a, T b)
34 {
35  return std::abs(a - b) <=
36  (std::numeric_limits<T>::epsilon() * std::min(std::abs(a), std::abs(b)));
37 }
38 
39 template <typename T, typename... Ts>
40 auto isDefinitelyLessThan(T a, T b, Ts... xs)
41 {
42  auto compare = [](T a, T b) {
43  return (b - a) > (std::numeric_limits<T>::epsilon() * std::max(std::abs(a), std::abs(b)));
44  };
45 
46  if constexpr (0 < sizeof...(Ts)) {
47  return compare(a, b) and compare(b, xs...);
48  } else {
49  return compare(a, b);
50  }
51 }
52 
53 template <typename T>
55 {
56  return (a - b) > (std::numeric_limits<T>::epsilon() * std::max(std::abs(a), std::abs(b)));
57 }
58 } // namespace arithmetic
59 } // namespace math
60 
61 #endif // ARITHMETIC__FLOATING_POINT__COMPARISON_HPP_
auto isDefinitelyGreaterThan(T a, T b)
Definition: comparison.hpp:54
auto isApproximatelyEqualTo(T a, T b)
Definition: comparison.hpp:26
auto isEssentiallyEqualTo(T a, T b)
Definition: comparison.hpp:33
auto isDefinitelyLessThan(T a, T b, Ts... xs)
Definition: comparison.hpp:40
Definition: bounding_box.hpp:32
Definition: junit5.hpp:25