OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
VcstFloatCompare.h
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * NAME: VcstFloatCompare
4  *
5  * DESCRIPTION: Compare two floating point values for equality. Two param
6  * method uses system defined epsilon. Three param method uses
7  * a user provided error delta value. This class addresses
8  * SSPM E.4.1.3.5 Never compare 2 float or double values using
9  * the == operator. Class is templated on type T, being either
10  * Float32 (float) or Float64 (double). Typedefs are provided
11  * for convenience.
12  *
13  * Adapted directly from the IDPS file FloatCompare.h published
14  * by Raytheon Company
15  *
16  **************************************************************************/
17 
18 #ifndef FloatCompare_h
19 #define FloatCompare_h
20 
21 #include <limits>
22 #include <cmath>
23 
24 template<typename T>
25 class FloatCompare {
26 public:
27 
28  static bool equal(T firstValue, T secondValue);
29  static bool equal(T firstValue, T secondValue, T errorDelta);
30 
31  static bool notequal(T firstValue, T secondValue);
32  static bool notequal(T firstValue, T secondValue, T errorDelta);
33 };
34 
37 
38 template<typename T>
39 inline bool FloatCompare<T>::equal(T firstValue, T secondValue) {
40  const T EPSILON = std::numeric_limits < T > ::epsilon();
41 
42  if ((fabs(firstValue - secondValue)) <= EPSILON)
43  return true;
44  else
45  return false;
46 }
47 
48 template<typename T>
49 inline bool FloatCompare<T>::equal(T firstValue, T secondValue, T errorDelta) {
50  if ((fabs(firstValue - secondValue)) <= errorDelta)
51  return true;
52  else
53  return false;
54 }
55 
56 template<typename T>
57 inline bool FloatCompare<T>::notequal(T firstValue, T secondValue) {
58  return !equal(firstValue, secondValue);
59 }
60 
61 template<typename T>
62 inline bool FloatCompare<T>::notequal(T firstValue, T secondValue,
63  T errorDelta) {
64  return !equal(firstValue, secondValue, errorDelta);
65 }
66 
67 #endif
FloatCompare< double > Float64Compare
static bool equal(T firstValue, T secondValue)
static bool notequal(T firstValue, T secondValue)
#define EPSILON
Definition: regen_attr.h:50
#define fabs(a)
Definition: misc.h:93
FloatCompare< float > Float32Compare