Manual for OpenFLUID 2.1.10

FloatingPoint.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file FloatingPoint.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_SCIENTIFIC_FLOATINGPOINT_HPP__
41 #define __OPENFLUID_SCIENTIFIC_FLOATINGPOINT_HPP__
42 
43 
44 #include <cmath>
45 
46 
47 namespace openfluid { namespace scientific {
48 
49 
50 /**
51  Tests equality between two floating point values, using the "close enough" method.
52  @param[in] A the first term of the equality
53  @param[in] B the second term of the equality
54  @param[in] Epsilon the comparison tolerance factor
55 
56  @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
57  @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
58 */
59 template<typename T1, typename T2>
60 inline bool isCloseEnough(const T1& A, const T2& B, const double& Epsilon = 0.00001)
61 {
62  // see Knuth section 4.2.2 pages 217-218
63  return ((std::fabs(A - B)) <= (Epsilon * std::fabs(A)));
64 }
65 
66 
67 /**
68  Tests equality between two floating point values, using the "very close" method.
69  @param[in] A the first term of the equality
70  @param[in] B the seond term of the equality
71  @param[in] Epsilon the comparison tolerance factor
72 
73  @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
74  @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
75 */
76 template<typename T1, typename T2>
77 inline bool isVeryClose(const T1& A, const T2& B, const double& Epsilon = 0.00001)
78 {
79  // see Knuth section 4.2.2 pages 217-218
80  return (((std::fabs(A - B)) <= (Epsilon * std::fabs(A))) && ((std::fabs(A - B)) <= (Epsilon * std::fabs(B))));
81 }
82 
83 
84 /**
85  @deprecated Since version 2.1.0. Use openfluid::scientific::isCloseEnough instead
86 */
87 [[deprecated]] inline bool IsCloseEnough(double A, double B, double Epsilon = 0.00001);
88 inline bool IsCloseEnough(double A, double B, double Epsilon)
89 {
90  return isCloseEnough(A,B,Epsilon);
91 }
92 
93 
94 /**
95  @deprecated Since version 2.1.0.Use openfluid::scientific::isVeryClose instead
96 */
97 [[deprecated]] inline bool IsVeryClose(double A, double B, double Epsilon = 0.00001);
98 inline bool IsVeryClose(double A, double B, double Epsilon)
99 {
100  return isVeryClose(A,B,Epsilon);
101 }
102 
103 
104 } } // namespaces
105 
106 
107 #endif /* __OPENFLUID_SCIENTIFIC_FLOATINGPOINT_HPP__ */
Definition: ApplicationException.hpp:47
bool isVeryClose(const T1 &A, const T2 &B, const double &Epsilon=0.00001)
Definition: FloatingPoint.hpp:77
bool isCloseEnough(const T1 &A, const T2 &B, const double &Epsilon=0.00001)
Definition: FloatingPoint.hpp:60
bool IsVeryClose(double A, double B, double Epsilon=0.00001)
Definition: FloatingPoint.hpp:98
bool IsCloseEnough(double A, double B, double Epsilon=0.00001)
Definition: FloatingPoint.hpp:88