CoinFloatEqual.hpp
Go to the documentation of this file.
1 /* $Id: CoinFloatEqual.hpp 1416 2011-04-17 09:57:29Z stefan $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 // This code is licensed under the terms of the Eclipse Public License (EPL).
5 
6 #ifndef CoinFloatEqual_H
7 #define CoinFloatEqual_H
8 
9 #include <algorithm>
10 #include <cmath>
11 
12 #include "CoinFinite.hpp"
13 
47 {
48  public:
49 
51 
52  inline bool operator() (const double f1, const double f2) const
53 
54  { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
55  if (f1 == f2) return true ;
56  return (fabs(f1-f2) < epsilon_) ; }
57 
60 
66  CoinAbsFltEq () : epsilon_(1.e-10) {}
67 
69 
70  CoinAbsFltEq (const double epsilon) : epsilon_(epsilon) {}
71 
73 
74  virtual ~CoinAbsFltEq () {}
75 
77 
78  CoinAbsFltEq (const CoinAbsFltEq& src) : epsilon_(src.epsilon_) {}
79 
81 
83 
84  { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
85  return (*this) ; }
86 
88 
89  private:
90 
93 
95 
96  double epsilon_ ;
97 
99 
100 } ;
101 
102 
103 
111 {
112  public:
113 
115 
116  inline bool operator() (const double f1, const double f2) const
117 
118  { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
119  if (f1 == f2) return true ;
120  if (!CoinFinite(f1) || !CoinFinite(f2)) return false ;
121 
122  double tol = (fabs(f1)>fabs(f2))?fabs(f1):fabs(f2) ;
123 
124  return (fabs(f1-f2) <= epsilon_*(1+tol)) ; }
125 
128 
129 #ifndef COIN_FLOAT
130 
134  CoinRelFltEq () : epsilon_(1.e-10) {}
135 #else
136 
140  CoinRelFltEq () : epsilon_(1.e-6) {} ; // as float
141 #endif
142 
144 
145  CoinRelFltEq (const double epsilon) : epsilon_(epsilon) {}
146 
148 
149  virtual ~CoinRelFltEq () {}
150 
152 
153  CoinRelFltEq (const CoinRelFltEq & src) : epsilon_(src.epsilon_) {}
154 
156 
158 
159  { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
160  return (*this) ; }
161 
163 
164 private:
165 
168 
170 
171  double epsilon_ ;
172 
174 
175 } ;
176 
177 #endif
Equality to a scaled tolerance.
CoinAbsFltEq(const CoinAbsFltEq &src)
Copy constructor.
Equality to an absolute tolerance.
CoinRelFltEq(const double epsilon)
Alternate constructor with epsilon as a parameter.
virtual ~CoinAbsFltEq()
Destructor.
CoinAbsFltEq()
Default constructor.
bool CoinFinite(double val)
checks if a double value is finite (not infinity and not NaN)
CoinRelFltEq & operator=(const CoinRelFltEq &rhs)
Assignment.
CoinAbsFltEq(const double epsilon)
Alternate constructor with epsilon as a parameter.
bool CoinIsnan(double val)
checks if a double value is not a number
CoinRelFltEq(const CoinRelFltEq &src)
Copy constructor.
double epsilon_
Base equality tolerance.
CoinAbsFltEq & operator=(const CoinAbsFltEq &rhs)
Assignment.
bool operator()(const double f1, const double f2) const
Compare function.
double epsilon_
Equality tolerance.
bool operator()(const double f1, const double f2) const
Compare function.
virtual ~CoinRelFltEq()
Destructor.