Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CompareFloatingPoint.cpp
1 #include "W32Compat.hh"
2 #include <Base/Common/CompareFloatingPoint.hh>
3 
4 #include <cmath>
5 
6 using namespace BIAS;
7 using namespace std;
8 
9 namespace BIAS {
10  template <typename T>
11  T Cast(const double input)
12  { return static_cast<T>(rint(input)); }
13 
14  template <> BIASCommon_EXPORT
15  double Cast<double>(const double input)
16  { return input; }
17 
18  template <> BIASCommon_EXPORT
19  float Cast<float>(const double input)
20  { return static_cast<float>(input); }
21 
22  template <typename T>
23  bool Equal(const T left, const T right, const T eps)
24  {
25  return (left==right);
26  }
27 
28  template <> BIASCommon_EXPORT
29  bool Equal<double>(const double left, const double right,
30  const double eps)
31  {
32  // cover the trivial case
33  if (left==right) return true;
34  // the code below does not work when one value is zero
35  if (left==0. || right==0.){
36  return (fabs(left-right) <= eps);
37  }
38  // compare two non-zero values
39  return ( fabs(left-right) <= eps * fabs(right) ||
40  fabs(left-right) <= eps * fabs(left) );
41  }
42 
43  template <> BIASCommon_EXPORT
44  bool Equal<float>(const float left, const float right,
45  const float eps)
46  {
47  // cover the trivial case
48  if (left==right) return true;
49  // the code below does not work when one value is zero
50  if (left==0. || right==0.){
51  return (fabs(left-right) <= eps);
52  }
53  // compare two non-zero values
54  return (fabs(left-right) <= eps * fabs(right) ||
55  fabs(left-right) <= eps * fabs(left) );
56  }
57 
58 
59 }
60 
61 
62 /////////////////////////////////////////////////////////////////////
63 // instantiation
64 
65 #define INST(type)\
66 template BIASCommon_EXPORT type BIAS::Cast<type>(const double);\
67 template BIASCommon_EXPORT bool BIAS::Equal<type>(const type left, const type right, const type eps);
68 
69 INST(unsigned char)
70 INST(int)
71 INST(char)
72 INST(short)
73 INST(unsigned short)
74 INST(long int)
75 INST(unsigned int)
76 #ifndef WIN32
77 INST(float)
78 INST(double)
79 #endif
T Cast(const double input)
correct casting to storage type, incorporates rint for integer values and simple casting for floating...
BIASCommon_EXPORT bool Equal< double >(const double left, const double right, const double eps)
BIASCommon_EXPORT float Cast< float >(const double input)
BIASCommon_EXPORT double Cast< double >(const double input)
BIASCommon_EXPORT bool Equal< float >(const float left, const float right, const float eps)
INST(unsigned char)
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...