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 <boost/math/special_functions/relative_difference.hpp>
41#include <limits>
42#include <type_traits>
43
44namespace Nektar
45{
46namespace LibUtilities
47{
48/// compare reals of same type with relative tolerance
49template <
50 class T1, class T2,
51 class = typename std::enable_if<
52 std::is_floating_point<typename std::remove_cv<
53 typename std::remove_reference<T1>::type>::type>::value &&
54 std::is_same<typename std::remove_cv<
55 typename std::remove_reference<T1>::type>::type,
56 typename std::remove_cv<typename std::remove_reference<
57 T2>::type>::type>::value>::type>
58inline bool IsRealEqual(
59 T1 &&lhs, T2 &&rhs,
60 const unsigned int factor = NekConstants::kNekFloatCompFact)
61{
62 // Check precondition in debug mode
63 ASSERTL1(factor >= 1, "real comparison factor needs to be >= 1");
64 // Relative distance normalized by machine epsilon
65 return boost::math::epsilon_difference(lhs, rhs) < factor;
66}
67
68/// compare reals of same type with absolute tolerance
69template <
70 class T1, class T2,
71 class = typename std::enable_if<
72 std::is_floating_point<typename std::remove_cv<
73 typename std::remove_reference<T1>::type>::type>::value &&
74 std::is_same<typename std::remove_cv<
75 typename std::remove_reference<T1>::type>::type,
76 typename std::remove_cv<typename std::remove_reference<
77 T2>::type>::type>::value>::type>
78inline bool IsRealClose(T1 &&lhs, T2 &&rhs,
80{
81 // Check if tolerance is positive
82 ASSERTL1(tol >= 0, "real comparison tolerance needs to be >= 0");
83 // Check if distance is within tolerance
84 return std::abs(lhs - rhs) < tol;
85}
86
87} // namespace LibUtilities
88} // namespace Nektar
89
90#endif
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
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:2
double NekDouble
scalarT< T > abs(scalarT< T > in)
Definition: scalar.hpp:298