Nektar++
RealComparison.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File RealComparison.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: simple routines to compare 2 real
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKTAR_LIB_UTILITIES_REALCOMPARISON_H
36 #define NEKTAR_LIB_UTILITIES_REALCOMPARISON_H
37 
40 #include <limits>
41 #include <type_traits>
42 #include <boost/math/special_functions/relative_difference.hpp>
43 
44 namespace Nektar
45 {
46 namespace LibUtilities
47 {
48 /// compare reals of same type with relative tolerance
49 template
50 <
51  class T1, class T2,
52  class = typename std::enable_if
53  <
54  std::is_floating_point
55  <
56  typename std::remove_cv<
57  typename std::remove_reference<T1>::type>::type
58  >::value &&
59  std::is_same
60  <
61  typename std::remove_cv<
62  typename std::remove_reference<T1>::type>::type,
63  typename std::remove_cv<
64  typename std::remove_reference<T2>::type>::type
65  >::value
66  >::type
67 >
68 inline bool IsRealEqual(T1&& lhs, T2&& rhs,
69  const unsigned int factor = NekConstants::kNekFloatCompFact)
70 {
71  // Check precondition in debug mode
72  ASSERTL1(factor >= 1, "real comparison factor needs to be >= 1");
73  // Relative distance normalized by machine epsilon
74  return boost::math::epsilon_difference(lhs, rhs) < factor;
75 }
76 
77 /// compare reals of same type with absolute tolerance
78 template
79 <
80  class T1, class T2,
81  class = typename std::enable_if
82  <
83  std::is_floating_point
84  <
85  typename std::remove_cv<
86  typename std::remove_reference<T1>::type>::type
87  >::value &&
88  std::is_same
89  <
90  typename std::remove_cv<
91  typename std::remove_reference<T1>::type>::type,
92  typename std::remove_cv<
93  typename std::remove_reference<T2>::type>::type
94  >::value
95  >::type
96 >
97 inline bool IsRealClose(T1&& lhs, T2&& rhs,
99 {
100  // Check if tolerance is positive
101  ASSERTL1(tol >= 0, "real comparison tolerance needs to be >= 0");
102  // Check if distance is within tolerance
103  return std::abs(lhs-rhs) < tol;
104 }
105 
106 }
107 }
108 
109 #endif
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:250
bool IsRealClose(T1 &&lhs, T2 &&rhs, const NekDouble tol=NekConstants::kNekMachineEpsilon)
compare reals of same type with absolute tolerance
bool IsRealEqual(T1 &&lhs, T2 &&rhs, const unsigned int factor=NekConstants::kNekFloatCompFact)
compare reals of same type with relative tolerance
static const unsigned int kNekFloatCompFact
static const NekDouble kNekMachineEpsilon
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
double NekDouble
scalarT< T > abs(scalarT< T > in)
Definition: scalar.hpp:272