Clp  1.17.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoinFloatEqual.hpp
Go to the documentation of this file.
1 /* $Id: CoinFloatEqual.hpp 2083 2019-01-06 19:38:09Z unxusr $ */
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 
46 class CoinAbsFltEq {
47 public:
49 
50  inline bool operator()(const double f1, const double f2) const
51 
52  {
53  if (CoinIsnan(f1) || CoinIsnan(f2))
54  return false;
55  if (f1 == f2)
56  return true;
57  return (fabs(f1 - f2) < epsilon_);
58  }
59 
62 
69  : epsilon_(1.e-10)
70  {
71  }
72 
74 
75  CoinAbsFltEq(const double epsilon)
76  : epsilon_(epsilon)
77  {
78  }
79 
81 
82  virtual ~CoinAbsFltEq() {}
83 
85 
87  : epsilon_(src.epsilon_)
88  {
89  }
90 
92 
94 
95  {
96  if (this != &rhs)
97  epsilon_ = rhs.epsilon_;
98  return (*this);
99  }
100 
102 
103 private:
106 
108 
109  double epsilon_;
110 
112 };
113 
121 public:
123 
124  inline bool operator()(const double f1, const double f2) const
125 
126  {
127  if (CoinIsnan(f1) || CoinIsnan(f2))
128  return false;
129  if (f1 == f2)
130  return true;
131  if (!CoinFinite(f1) || !CoinFinite(f2))
132  return false;
133 
134  double tol = (fabs(f1) > fabs(f2)) ? fabs(f1) : fabs(f2);
135 
136  return (fabs(f1 - f2) <= epsilon_ * (1 + tol));
137  }
138 
141 
142 #ifndef COIN_FLOAT
143 
148  : epsilon_(1.e-10)
149  {
150  }
151 #else
152 
156  CoinRelFltEq()
157  : epsilon_(1.e-6) {}; // as float
158 #endif
159 
161 
162  CoinRelFltEq(const double epsilon)
163  : epsilon_(epsilon)
164  {
165  }
166 
168 
169  virtual ~CoinRelFltEq() {}
170 
172 
174  : epsilon_(src.epsilon_)
175  {
176  }
177 
179 
181 
182  {
183  if (this != &rhs)
184  epsilon_ = rhs.epsilon_;
185  return (*this);
186  }
187 
189 
190 private:
193 
195 
196  double epsilon_;
197 
199 };
200 
201 #endif
202 
203 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
204 */
bool CoinIsnan(double val)
checks if a double value is not a number
double epsilon_
Base equality tolerance.
CoinRelFltEq(const double epsilon)
Alternate constructor with epsilon as a parameter.
bool operator()(const double f1, const double f2) const
Compare function.
CoinAbsFltEq(const double epsilon)
Alternate constructor with epsilon as a parameter.
Equality to a scaled tolerance.
CoinAbsFltEq()
Default constructor.
double epsilon_
Equality tolerance.
CoinRelFltEq()
Default constructor.
CoinRelFltEq & operator=(const CoinRelFltEq &rhs)
Assignment.
Equality to an absolute tolerance.
CoinRelFltEq(const CoinRelFltEq &src)
Copy constructor.
virtual ~CoinAbsFltEq()
Destructor.
CoinAbsFltEq & operator=(const CoinAbsFltEq &rhs)
Assignment.
bool operator()(const double f1, const double f2) const
Compare function.
bool CoinFinite(double val)
checks if a double value is finite (not infinity and not NaN)
virtual ~CoinRelFltEq()
Destructor.
CoinAbsFltEq(const CoinAbsFltEq &src)
Copy constructor.