14 #ifndef ALPS_TESTING_FP_COMPARE_HPP_161c4f3bdd53475d8c6ea28232c9653c 15 #define ALPS_TESTING_FP_COMPARE_HPP_161c4f3bdd53475d8c6ea28232c9653c 25 template <
typename>
struct fpconst {};
30 struct fpconst<double> {
31 static double epsilon() {
return DBL_EPSILON; }
32 static double min() {
return DBL_MIN; }
33 static double max() {
return DBL_MAX; }
37 struct fpconst<float> {
38 static float epsilon() {
return FLT_EPSILON; }
39 static float min() {
return FLT_MIN; }
40 static float max() {
return FLT_MAX; }
46 inline bool is_near(
const T a,
const T b,
const T eps)
49 if (a==b)
return true;
51 static const T FPMIN=detail::fpconst<T>::min();
52 static const T FPMAX=detail::fpconst<T>::max();
54 const double diff=fabs(a-b);
55 if (a==0 || b==0 || diff<FPMIN)
return diff < (eps*FPMIN);
57 const double abs_a=fabs(a);
58 const double abs_b=fabs(b);
59 const double sum = (abs_b > FPMAX-abs_a)? FPMAX : (abs_a+abs_b);
60 return diff/sum < eps;
67 static const T EPS=detail::fpconst<T>::epsilon();
bool is_near(const T a, const T b, const T eps)
Compares two floating point values, see http://floating-point-gui.de/errors/comparison/.