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