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@supagro.inra.fr>
37 */
38 
39 
40 
41 #ifndef __OPENFLUID_SCIENTIFIC_FLOATINGPOINT_HPP__
42 #define __OPENFLUID_SCIENTIFIC_FLOATINGPOINT_HPP__
43 
44 
45 #include <cmath>
47 
48 
49 namespace openfluid { namespace scientific {
50 
51 
52 /**
53  Tests equality between two floating point values, using the "close enough" method.
54  @param[in] A the first term of the equality
55  @param[in] B the second term of the equality
56  @param[in] Epsilon the comparison tolerance factor
57 
58  @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
59  @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
60 */
61 template<typename T1, typename T2>
62 inline bool isCloseEnough(const T1& A, const T2& B, const double& Epsilon = 0.00001)
63 {
64  // see Knuth section 4.2.2 pages 217-218
65  return ((std::fabs(A - B)) <= (Epsilon * std::fabs(A)));
66 }
67 
68 
69 /**
70  Tests equality between two floating point values, using the "very close" method.
71  @param[in] A the first term of the equality
72  @param[in] B the seond term of the equality
73  @param[in] Epsilon the comparison tolerance factor
74 
75  @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
76  @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
77 */
78 template<typename T1, typename T2>
79 inline bool isVeryClose(const T1& A, const T2& B, const double& Epsilon = 0.00001)
80 {
81  // see Knuth section 4.2.2 pages 217-218
82  return (((std::fabs(A - B)) <= (Epsilon * std::fabs(A))) && ((std::fabs(A - B)) <= (Epsilon * std::fabs(B))));
83 }
84 
85 
86 
87 /**
88  @deprecated Since version 2.1.0. Use openfluid::scientific::isCloseEnough instead
89 */
90 inline bool IsCloseEnough(double A, double B, double Epsilon = 0.00001) OPENFLUID_DEPRECATED;
91 inline bool IsCloseEnough(double A, double B, double Epsilon)
92 {
93  return isCloseEnough(A,B,Epsilon);
94 }
95 
96 
97 /**
98  @deprecated Since version 2.1.0.Use openfluid::scientific::isVeryClose instead
99 */
100 inline bool IsVeryClose(double A, double B, double Epsilon = 0.00001) OPENFLUID_DEPRECATED;
101 inline bool IsVeryClose(double A, double B, double Epsilon)
102 {
103  return isVeryClose(A,B,Epsilon);
104 }
105 
106 
107 } } // namespaces
108 
109 
110 #endif /* __OPENFLUID_SCIENTIFIC_FLOATINGPOINT_HPP__ */
#define OPENFLUID_DEPRECATED
Definition: deprecation.hpp:54
bool IsVeryClose(double A, double B, double Epsilon=0.00001) OPENFLUID_DEPRECATED
Definition: FloatingPoint.hpp:101
bool isVeryClose(const T1 &A, const T2 &B, const double &Epsilon=0.00001)
Definition: FloatingPoint.hpp:79
Definition: ApplicationException.hpp:47
bool isCloseEnough(const T1 &A, const T2 &B, const double &Epsilon=0.00001)
Definition: FloatingPoint.hpp:62
bool IsCloseEnough(double A, double B, double Epsilon=0.00001) OPENFLUID_DEPRECATED
Definition: FloatingPoint.hpp:91