Cgl 0.60.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
68 CoinAbsFltEq()
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
86 CoinAbsFltEq(const CoinAbsFltEq &src)
87 : epsilon_(src.epsilon_)
88 {
89 }
90
92
93 CoinAbsFltEq &operator=(const CoinAbsFltEq &rhs)
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
120 class CoinRelFltEq {
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
147 CoinRelFltEq()
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
173 CoinRelFltEq(const CoinRelFltEq &src)
174 : epsilon_(src.epsilon_)
175 {
176 }
177
179
180 CoinRelFltEq &operator=(const CoinRelFltEq &rhs)
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 */
CoinIsnan
bool CoinIsnan(double val)
checks if a double value is not a number
CoinRelFltEq::epsilon_
double epsilon_
Base equality tolerance.
Definition: CoinFloatEqual.hpp:196
CoinFinite.hpp
CoinRelFltEq::CoinRelFltEq
CoinRelFltEq(const double epsilon)
Alternate constructor with epsilon as a parameter.
Definition: CoinFloatEqual.hpp:162
CoinRelFltEq::operator()
bool operator()(const double f1, const double f2) const
Compare function.
Definition: CoinFloatEqual.hpp:124
CoinAbsFltEq::CoinAbsFltEq
CoinAbsFltEq(const double epsilon)
Alternate constructor with epsilon as a parameter.
Definition: CoinFloatEqual.hpp:75
CoinRelFltEq
Equality to a scaled tolerance.
Definition: CoinFloatEqual.hpp:120
CoinAbsFltEq::CoinAbsFltEq
CoinAbsFltEq()
Default constructor.
Definition: CoinFloatEqual.hpp:68
CoinAbsFltEq::epsilon_
double epsilon_
Equality tolerance.
Definition: CoinFloatEqual.hpp:109
CoinRelFltEq::CoinRelFltEq
CoinRelFltEq()
Default constructor.
Definition: CoinFloatEqual.hpp:147
CoinRelFltEq::operator=
CoinRelFltEq & operator=(const CoinRelFltEq &rhs)
Assignment.
Definition: CoinFloatEqual.hpp:180
CoinAbsFltEq
Equality to an absolute tolerance.
Definition: CoinFloatEqual.hpp:46
CoinRelFltEq::CoinRelFltEq
CoinRelFltEq(const CoinRelFltEq &src)
Copy constructor.
Definition: CoinFloatEqual.hpp:173
CoinAbsFltEq::~CoinAbsFltEq
virtual ~CoinAbsFltEq()
Destructor.
Definition: CoinFloatEqual.hpp:82
CoinAbsFltEq::operator=
CoinAbsFltEq & operator=(const CoinAbsFltEq &rhs)
Assignment.
Definition: CoinFloatEqual.hpp:93
CoinAbsFltEq::operator()
bool operator()(const double f1, const double f2) const
Compare function.
Definition: CoinFloatEqual.hpp:50
CoinFinite
bool CoinFinite(double val)
checks if a double value is finite (not infinity and not NaN)
CoinRelFltEq::~CoinRelFltEq
virtual ~CoinRelFltEq()
Destructor.
Definition: CoinFloatEqual.hpp:169
CoinAbsFltEq::CoinAbsFltEq
CoinAbsFltEq(const CoinAbsFltEq &src)
Copy constructor.
Definition: CoinFloatEqual.hpp:86