Nektar++
Loading...
Searching...
No Matches
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 <algorithm>
41#include <cmath>
42#include <limits>
43#include <type_traits>
44
46{
47namespace detail
48{
49template <class T> inline T EpsilonDifference(T lhs, T rhs)
50{
51 if (lhs == rhs)
52 {
53 return T(0);
54 }
55
56 const T diff = std::abs(lhs - rhs);
57 const T scale = (std::min)(std::abs(lhs), std::abs(rhs));
58 const T eps = std::numeric_limits<T>::epsilon();
59
60 if (scale == T(0))
61 {
62 return diff / eps;
63 }
64
65 return diff / (scale * eps);
66}
67} // namespace detail
68
69/// compare reals of same type with relative tolerance
70template <
71 class T1, class T2,
72 class = std::enable_if_t<
73 std::is_floating_point_v<typename std::remove_cv<
74 typename std::remove_reference<T1>::type>::type> &&
75 std::is_same_v<typename std::remove_cv<
76 typename std::remove_reference<T1>::type>::type,
77 typename std::remove_cv<
78 typename std::remove_reference<T2>::type>::type>>>
79inline bool IsRealEqual(
80 T1 &&lhs, T2 &&rhs,
81 const unsigned int factor = NekConstants::kNekFloatCompFact)
82{
83 // Check precondition in debug mode
84 ASSERTL1(factor >= 1, "real comparison factor needs to be >= 1");
85 // Relative distance normalized by machine epsilon
86 return detail::EpsilonDifference(lhs, rhs) < factor;
87}
88
89/// compare reals of same type with absolute tolerance
90template <
91 class T1, class T2,
92 class = std::enable_if_t<
93 std::is_floating_point_v<typename std::remove_cv<
94 typename std::remove_reference<T1>::type>::type> &&
95 std::is_same_v<typename std::remove_cv<
96 typename std::remove_reference<T1>::type>::type,
97 typename std::remove_cv<
98 typename std::remove_reference<T2>::type>::type>>>
99inline bool IsRealClose(T1 &&lhs, T2 &&rhs,
101{
102 // Check if tolerance is positive
103 ASSERTL1(tol >= 0, "real comparison tolerance needs to be >= 0");
104 // Check if distance is within tolerance
105 return std::abs(lhs - rhs) < tol;
106}
107
108} // namespace Nektar::LibUtilities
109
110#endif
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
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