1 | /* -*- C++ -*- |
---|
2 | * lemon/tolerance.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_TOLERANCE_H |
---|
18 | #define LEMON_TOLERANCE_H |
---|
19 | |
---|
20 | ///\ingroup misc |
---|
21 | ///\file |
---|
22 | ///\brief A basic tool to handle the anomalies of calculation with |
---|
23 | ///floating point numbers. |
---|
24 | /// |
---|
25 | ///\todo It should be in a module like "Basic tools" |
---|
26 | |
---|
27 | |
---|
28 | namespace lemon { |
---|
29 | |
---|
30 | /// \addtogroup misc |
---|
31 | /// @{ |
---|
32 | |
---|
33 | ///\brief A class to provide a basic way to |
---|
34 | ///handle the comparison of numbers that are obtained |
---|
35 | ///as a result of a probably inexact computation. |
---|
36 | /// |
---|
37 | ///Tolerance is a class to provide a basic way to |
---|
38 | ///handle the comparison of numbers that are obtained |
---|
39 | ///as a result of a probably inexact computation. |
---|
40 | /// |
---|
41 | ///This is an abstract class, it should be specialized for all numerical |
---|
42 | ///data types. These specialized classes like \ref Tolerance<double> |
---|
43 | ///may offer additional tuning parameters. |
---|
44 | /// |
---|
45 | ///\sa Tolerance<float> |
---|
46 | ///\sa Tolerance<double> |
---|
47 | ///\sa Tolerance<long double> |
---|
48 | ///\sa Tolerance<int> |
---|
49 | ///\sa Tolerance<long long int> |
---|
50 | |
---|
51 | template<class T> |
---|
52 | class Tolerance |
---|
53 | { |
---|
54 | public: |
---|
55 | typedef T Value; |
---|
56 | |
---|
57 | ///\name Comparisons |
---|
58 | ///The concept is that these bool functions return with \c true only if |
---|
59 | ///the related comparisons hold even if some numerical error appeared |
---|
60 | ///during the computations. |
---|
61 | |
---|
62 | ///@{ |
---|
63 | |
---|
64 | ///Returns \c true if \c a is \e surely strictly less than \c b |
---|
65 | static bool less(Value a,Value b) {return false;} |
---|
66 | ///Returns \c true if \c a is \e surely different from \c b |
---|
67 | static bool different(Value a,Value b) {return false;} |
---|
68 | ///Returns \c true if \c a is \e surely positive |
---|
69 | static bool positive(Value a) {return false;} |
---|
70 | ///Returns \c true if \c a is \e surely negative |
---|
71 | static bool negative(Value a) {return false;} |
---|
72 | ///Returns \c true if \c a is \e surely non-zero |
---|
73 | static bool nonZero(Value a) {return false;} |
---|
74 | |
---|
75 | ///@} |
---|
76 | |
---|
77 | ///Returns the zero value. |
---|
78 | static Value zero() {return T();} |
---|
79 | |
---|
80 | // static bool finite(Value a) {} |
---|
81 | // static Value big() {} |
---|
82 | // static Value negativeBig() {} |
---|
83 | }; |
---|
84 | |
---|
85 | |
---|
86 | ///Float specialization of \ref Tolerance. |
---|
87 | |
---|
88 | ///Float specialization of \ref Tolerance. |
---|
89 | ///\sa Tolerance |
---|
90 | ///\relates Tolerance |
---|
91 | template<> |
---|
92 | class Tolerance<float> |
---|
93 | { |
---|
94 | static float def_epsilon; |
---|
95 | float _epsilon; |
---|
96 | public: |
---|
97 | ///\e |
---|
98 | typedef float Value; |
---|
99 | |
---|
100 | ///Constructor setting the epsilon tolerance to the default value. |
---|
101 | Tolerance() : _epsilon(def_epsilon) {} |
---|
102 | ///Constructor setting the epsilon tolerance. |
---|
103 | Tolerance(float e) : _epsilon(e) {} |
---|
104 | |
---|
105 | ///Return the epsilon value. |
---|
106 | Value epsilon() {return _epsilon;} |
---|
107 | ///Set the epsilon value. |
---|
108 | void epsilon(Value e) {_epsilon=e;} |
---|
109 | |
---|
110 | ///Return the default epsilon value. |
---|
111 | static Value defaultEpsilon() {return def_epsilon;} |
---|
112 | ///Set the default epsilon value. |
---|
113 | static void defaultEpsilon(Value e) {def_epsilon=e;} |
---|
114 | |
---|
115 | ///\name Comparisons |
---|
116 | ///See class Tolerance for more details. |
---|
117 | |
---|
118 | ///@{ |
---|
119 | |
---|
120 | ///Returns \c true if \c a is \e surely strictly less than \c b |
---|
121 | bool less(Value a,Value b) {return a+_epsilon<b;} |
---|
122 | ///Returns \c true if \c a is \e surely different from \c b |
---|
123 | bool different(Value a,Value b) { return less(a,b)||less(b,a); } |
---|
124 | ///Returns \c true if \c a is \e surely positive |
---|
125 | bool positive(Value a) { return _epsilon<a; } |
---|
126 | ///Returns \c true if \c a is \e surely negative |
---|
127 | bool negative(Value a) { return -_epsilon>a; } |
---|
128 | ///Returns \c true if \c a is \e surely non-zero |
---|
129 | Value nonZero(Value a) { return positive(a)||negative(a); }; |
---|
130 | |
---|
131 | ///@} |
---|
132 | |
---|
133 | ///Returns zero |
---|
134 | static Value zero() {return 0;} |
---|
135 | }; |
---|
136 | |
---|
137 | ///Double specialization of \ref Tolerance. |
---|
138 | |
---|
139 | ///Double specialization of \ref Tolerance. |
---|
140 | ///\sa Tolerance |
---|
141 | ///\relates Tolerance |
---|
142 | template<> |
---|
143 | class Tolerance<double> |
---|
144 | { |
---|
145 | static double def_epsilon; |
---|
146 | double _epsilon; |
---|
147 | public: |
---|
148 | ///\e |
---|
149 | typedef double Value; |
---|
150 | |
---|
151 | ///Constructor setting the epsilon tolerance to the default value. |
---|
152 | Tolerance() : _epsilon(def_epsilon) {} |
---|
153 | ///Constructor setting the epsilon tolerance. |
---|
154 | Tolerance(double e) : _epsilon(e) {} |
---|
155 | |
---|
156 | ///Return the epsilon value. |
---|
157 | Value epsilon() {return _epsilon;} |
---|
158 | ///Set the epsilon value. |
---|
159 | void epsilon(Value e) {_epsilon=e;} |
---|
160 | |
---|
161 | ///Return the default epsilon value. |
---|
162 | static Value defaultEpsilon() {return def_epsilon;} |
---|
163 | ///Set the default epsilon value. |
---|
164 | static void defaultEpsilon(Value e) {def_epsilon=e;} |
---|
165 | |
---|
166 | ///\name Comparisons |
---|
167 | ///See class Tolerance for more details. |
---|
168 | |
---|
169 | ///@{ |
---|
170 | |
---|
171 | ///Returns \c true if \c a is \e surely strictly less than \c b |
---|
172 | bool less(Value a,Value b) {return a+_epsilon<b;} |
---|
173 | ///Returns \c true if \c a is \e surely different from \c b |
---|
174 | bool different(Value a,Value b) { return less(a,b)||less(b,a); } |
---|
175 | ///Returns \c true if \c a is \e surely positive |
---|
176 | bool positive(Value a) { return _epsilon<a; } |
---|
177 | ///Returns \c true if \c a is \e surely negative |
---|
178 | bool negative(Value a) { return -_epsilon>a; } |
---|
179 | ///Returns \c true if \c a is \e surely non-zero |
---|
180 | Value nonZero(Value a) { return positive(a)||negative(a); }; |
---|
181 | |
---|
182 | ///@} |
---|
183 | |
---|
184 | ///Returns zero |
---|
185 | static Value zero() {return 0;} |
---|
186 | }; |
---|
187 | |
---|
188 | ///Long double specialization of \ref Tolerance. |
---|
189 | |
---|
190 | ///Long double specialization of \ref Tolerance. |
---|
191 | ///\sa Tolerance |
---|
192 | ///\relates Tolerance |
---|
193 | template<> |
---|
194 | class Tolerance<long double> |
---|
195 | { |
---|
196 | static long double def_epsilon; |
---|
197 | long double _epsilon; |
---|
198 | public: |
---|
199 | ///\e |
---|
200 | typedef long double Value; |
---|
201 | |
---|
202 | ///Constructor setting the epsilon tolerance to the default value. |
---|
203 | Tolerance() : _epsilon(def_epsilon) {} |
---|
204 | ///Constructor setting the epsilon tolerance. |
---|
205 | Tolerance(long double e) : _epsilon(e) {} |
---|
206 | |
---|
207 | ///Return the epsilon value. |
---|
208 | Value epsilon() {return _epsilon;} |
---|
209 | ///Set the epsilon value. |
---|
210 | void epsilon(Value e) {_epsilon=e;} |
---|
211 | |
---|
212 | ///Return the default epsilon value. |
---|
213 | static Value defaultEpsilon() {return def_epsilon;} |
---|
214 | ///Set the default epsilon value. |
---|
215 | static void defaultEpsilon(Value e) {def_epsilon=e;} |
---|
216 | |
---|
217 | ///\name Comparisons |
---|
218 | ///See class Tolerance for more details. |
---|
219 | |
---|
220 | ///@{ |
---|
221 | |
---|
222 | ///Returns \c true if \c a is \e surely strictly less than \c b |
---|
223 | bool less(Value a,Value b) {return a+_epsilon<b;} |
---|
224 | ///Returns \c true if \c a is \e surely different from \c b |
---|
225 | bool different(Value a,Value b) { return less(a,b)||less(b,a); } |
---|
226 | ///Returns \c true if \c a is \e surely positive |
---|
227 | bool positive(Value a) { return _epsilon<a; } |
---|
228 | ///Returns \c true if \c a is \e surely negative |
---|
229 | bool negative(Value a) { return -_epsilon>a; } |
---|
230 | ///Returns \c true if \c a is \e surely non-zero |
---|
231 | Value nonZero(Value a) { return positive(a)||negative(a); }; |
---|
232 | |
---|
233 | ///@} |
---|
234 | |
---|
235 | ///Returns zero |
---|
236 | static Value zero() {return 0;} |
---|
237 | }; |
---|
238 | |
---|
239 | ///Integer specialization of \ref Tolerance. |
---|
240 | |
---|
241 | ///Integer specialization of \ref Tolerance. |
---|
242 | ///\sa Tolerance |
---|
243 | template<> |
---|
244 | class Tolerance<int> |
---|
245 | { |
---|
246 | public: |
---|
247 | ///\e |
---|
248 | typedef int Value; |
---|
249 | |
---|
250 | ///\name Comparisons |
---|
251 | ///See \ref Tolerance for more details. |
---|
252 | |
---|
253 | ///@{ |
---|
254 | |
---|
255 | ///Returns \c true if \c a is \e surely strictly less than \c b |
---|
256 | static bool less(Value a,Value b) { return a<b;} |
---|
257 | ///Returns \c true if \c a is \e surely different from \c b |
---|
258 | static bool different(Value a,Value b) { return a!=b; } |
---|
259 | ///Returns \c true if \c a is \e surely positive |
---|
260 | static bool positive(Value a) { return 0<a; } |
---|
261 | ///Returns \c true if \c a is \e surely negative |
---|
262 | static bool negative(Value a) { return 0>a; } |
---|
263 | ///Returns \c true if \c a is \e surely non-zero |
---|
264 | static Value nonZero(Value a) { return a!=0;}; |
---|
265 | |
---|
266 | ///@} |
---|
267 | |
---|
268 | ///Returns zero |
---|
269 | static Value zero() {return 0;} |
---|
270 | }; |
---|
271 | |
---|
272 | ///Long long integer specialization of \ref Tolerance. |
---|
273 | |
---|
274 | ///Long long integer specialization of \ref Tolerance. |
---|
275 | ///\sa Tolerance |
---|
276 | template<> |
---|
277 | class Tolerance<long long int> |
---|
278 | { |
---|
279 | public: |
---|
280 | ///\e |
---|
281 | typedef long long int Value; |
---|
282 | |
---|
283 | ///\name Comparisons |
---|
284 | ///See \ref Tolerance for more details. |
---|
285 | |
---|
286 | ///@{ |
---|
287 | |
---|
288 | ///Returns \c true if \c a is \e surely strictly less than \c b |
---|
289 | static bool less(Value a,Value b) { return a<b;} |
---|
290 | ///Returns \c true if \c a is \e surely different from \c b |
---|
291 | static bool different(Value a,Value b) { return a!=b; } |
---|
292 | ///Returns \c true if \c a is \e surely positive |
---|
293 | static bool positive(Value a) { return 0<a; } |
---|
294 | ///Returns \c true if \c a is \e surely negative |
---|
295 | static bool negative(Value a) { return 0>a; } |
---|
296 | ///Returns \c true if \c a is \e surely non-zero |
---|
297 | static Value nonZero(Value a) { return a!=0;}; |
---|
298 | |
---|
299 | ///@} |
---|
300 | |
---|
301 | ///Returns zero |
---|
302 | static Value zero() {return 0;} |
---|
303 | }; |
---|
304 | |
---|
305 | /// @} |
---|
306 | |
---|
307 | } //namespace lemon |
---|
308 | |
---|
309 | #endif //LEMON_TOLERANCE_H |
---|