3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_TOLERANCE_H
20 #define LEMON_TOLERANCE_H
24 ///\brief A basic tool to handle the anomalies of calculation with
25 ///floating point numbers.
27 ///\todo It should be in a module like "Basic tools"
35 ///\brief A class to provide a basic way to
36 ///handle the comparison of numbers that are obtained
37 ///as a result of a probably inexact computation.
39 ///Tolerance is a class to provide a basic way to
40 ///handle the comparison of numbers that are obtained
41 ///as a result of a probably inexact computation.
43 ///This is an abstract class, it should be specialized for all numerical
44 ///data types. These specialized classes like \ref Tolerance\<double\>
45 ///may offer additional tuning parameters.
47 ///\sa Tolerance<float>
48 ///\sa Tolerance<double>
49 ///\sa Tolerance<long double>
51 #if defined __GNUC__ && !defined __STRICT_ANSI__
52 ///\sa Tolerance<long long int>
54 ///\sa Tolerance<unsigned int>
55 #if defined __GNUC__ && !defined __STRICT_ANSI__
56 ///\sa Tolerance<unsigned long long int>
66 ///The concept is that these bool functions return with \c true only if
67 ///the related comparisons hold even if some numerical error appeared
68 ///during the computations.
72 ///Returns \c true if \c a is \e surely strictly less than \c b
73 static bool less(Value a,Value b) {return false;}
74 ///Returns \c true if \c a is \e surely different from \c b
75 static bool different(Value a,Value b) {return false;}
76 ///Returns \c true if \c a is \e surely positive
77 static bool positive(Value a) {return false;}
78 ///Returns \c true if \c a is \e surely negative
79 static bool negative(Value a) {return false;}
80 ///Returns \c true if \c a is \e surely non-zero
81 static bool nonZero(Value a) {return false;}
85 ///Returns the zero value.
86 static Value zero() {return T();}
88 // static bool finite(Value a) {}
89 // static Value big() {}
90 // static Value negativeBig() {}
94 ///Float specialization of \ref Tolerance.
96 ///Float specialization of \ref Tolerance.
100 class Tolerance<float>
102 static float def_epsilon;
108 ///Constructor setting the epsilon tolerance to the default value.
109 Tolerance() : _epsilon(def_epsilon) {}
110 ///Constructor setting the epsilon tolerance.
111 Tolerance(float e) : _epsilon(e) {}
113 ///Return the epsilon value.
114 Value epsilon() const {return _epsilon;}
115 ///Set the epsilon value.
116 void epsilon(Value e) {_epsilon=e;}
118 ///Return the default epsilon value.
119 static Value defaultEpsilon() {return def_epsilon;}
120 ///Set the default epsilon value.
121 static void defaultEpsilon(Value e) {def_epsilon=e;}
124 ///See class Tolerance for more details.
128 ///Returns \c true if \c a is \e surely strictly less than \c b
129 bool less(Value a,Value b) const {return a+_epsilon<b;}
130 ///Returns \c true if \c a is \e surely different from \c b
131 bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
132 ///Returns \c true if \c a is \e surely positive
133 bool positive(Value a) const { return _epsilon<a; }
134 ///Returns \c true if \c a is \e surely negative
135 bool negative(Value a) const { return -_epsilon>a; }
136 ///Returns \c true if \c a is \e surely non-zero
137 bool nonZero(Value a) const { return positive(a)||negative(a); }
142 static Value zero() {return 0;}
145 ///Double specialization of \ref Tolerance.
147 ///Double specialization of \ref Tolerance.
149 ///\relates Tolerance
151 class Tolerance<double>
153 static double def_epsilon;
157 typedef double Value;
159 ///Constructor setting the epsilon tolerance to the default value.
160 Tolerance() : _epsilon(def_epsilon) {}
161 ///Constructor setting the epsilon tolerance.
162 Tolerance(double e) : _epsilon(e) {}
164 ///Return the epsilon value.
165 Value epsilon() const {return _epsilon;}
166 ///Set the epsilon value.
167 void epsilon(Value e) {_epsilon=e;}
169 ///Return the default epsilon value.
170 static Value defaultEpsilon() {return def_epsilon;}
171 ///Set the default epsilon value.
172 static void defaultEpsilon(Value e) {def_epsilon=e;}
175 ///See class Tolerance for more details.
179 ///Returns \c true if \c a is \e surely strictly less than \c b
180 bool less(Value a,Value b) const {return a+_epsilon<b;}
181 ///Returns \c true if \c a is \e surely different from \c b
182 bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
183 ///Returns \c true if \c a is \e surely positive
184 bool positive(Value a) const { return _epsilon<a; }
185 ///Returns \c true if \c a is \e surely negative
186 bool negative(Value a) const { return -_epsilon>a; }
187 ///Returns \c true if \c a is \e surely non-zero
188 bool nonZero(Value a) const { return positive(a)||negative(a); }
193 static Value zero() {return 0;}
196 ///Long double specialization of \ref Tolerance.
198 ///Long double specialization of \ref Tolerance.
200 ///\relates Tolerance
202 class Tolerance<long double>
204 static long double def_epsilon;
205 long double _epsilon;
208 typedef long double Value;
210 ///Constructor setting the epsilon tolerance to the default value.
211 Tolerance() : _epsilon(def_epsilon) {}
212 ///Constructor setting the epsilon tolerance.
213 Tolerance(long double e) : _epsilon(e) {}
215 ///Return the epsilon value.
216 Value epsilon() const {return _epsilon;}
217 ///Set the epsilon value.
218 void epsilon(Value e) {_epsilon=e;}
220 ///Return the default epsilon value.
221 static Value defaultEpsilon() {return def_epsilon;}
222 ///Set the default epsilon value.
223 static void defaultEpsilon(Value e) {def_epsilon=e;}
226 ///See class Tolerance for more details.
230 ///Returns \c true if \c a is \e surely strictly less than \c b
231 bool less(Value a,Value b) const {return a+_epsilon<b;}
232 ///Returns \c true if \c a is \e surely different from \c b
233 bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
234 ///Returns \c true if \c a is \e surely positive
235 bool positive(Value a) const { return _epsilon<a; }
236 ///Returns \c true if \c a is \e surely negative
237 bool negative(Value a) const { return -_epsilon>a; }
238 ///Returns \c true if \c a is \e surely non-zero
239 bool nonZero(Value a) const { return positive(a)||negative(a); }
244 static Value zero() {return 0;}
247 ///Integer specialization of \ref Tolerance.
249 ///Integer specialization of \ref Tolerance.
259 ///See \ref Tolerance for more details.
263 ///Returns \c true if \c a is \e surely strictly less than \c b
264 static bool less(Value a,Value b) { return a<b;}
265 ///Returns \c true if \c a is \e surely different from \c b
266 static bool different(Value a,Value b) { return a!=b; }
267 ///Returns \c true if \c a is \e surely positive
268 static bool positive(Value a) { return 0<a; }
269 ///Returns \c true if \c a is \e surely negative
270 static bool negative(Value a) { return 0>a; }
271 ///Returns \c true if \c a is \e surely non-zero
272 static bool nonZero(Value a) { return a!=0; }
277 static Value zero() {return 0;}
280 ///Unsigned integer specialization of \ref Tolerance.
282 ///Unsigned integer specialization of \ref Tolerance.
285 class Tolerance<unsigned int>
289 typedef unsigned int Value;
292 ///See \ref Tolerance for more details.
296 ///Returns \c true if \c a is \e surely strictly less than \c b
297 static bool less(Value a,Value b) { return a<b;}
298 ///Returns \c true if \c a is \e surely different from \c b
299 static bool different(Value a,Value b) { return a!=b; }
300 ///Returns \c true if \c a is \e surely positive
301 static bool positive(Value a) { return 0<a; }
302 ///Returns \c true if \c a is \e surely negative
303 static bool negative(Value) { return false; }
304 ///Returns \c true if \c a is \e surely non-zero
305 static bool nonZero(Value a) { return a!=0; }
310 static Value zero() {return 0;}
314 ///Long integer specialization of \ref Tolerance.
316 ///Long integer specialization of \ref Tolerance.
319 class Tolerance<long int>
323 typedef long int Value;
326 ///See \ref Tolerance for more details.
330 ///Returns \c true if \c a is \e surely strictly less than \c b
331 static bool less(Value a,Value b) { return a<b;}
332 ///Returns \c true if \c a is \e surely different from \c b
333 static bool different(Value a,Value b) { return a!=b; }
334 ///Returns \c true if \c a is \e surely positive
335 static bool positive(Value a) { return 0<a; }
336 ///Returns \c true if \c a is \e surely negative
337 static bool negative(Value a) { return 0>a; }
338 ///Returns \c true if \c a is \e surely non-zero
339 static bool nonZero(Value a) { return a!=0;}
344 static Value zero() {return 0;}
347 ///Unsigned long integer specialization of \ref Tolerance.
349 ///Unsigned long integer specialization of \ref Tolerance.
352 class Tolerance<unsigned long int>
356 typedef unsigned long int Value;
359 ///See \ref Tolerance for more details.
363 ///Returns \c true if \c a is \e surely strictly less than \c b
364 static bool less(Value a,Value b) { return a<b;}
365 ///Returns \c true if \c a is \e surely different from \c b
366 static bool different(Value a,Value b) { return a!=b; }
367 ///Returns \c true if \c a is \e surely positive
368 static bool positive(Value a) { return 0<a; }
369 ///Returns \c true if \c a is \e surely negative
370 static bool negative(Value) { return false; }
371 ///Returns \c true if \c a is \e surely non-zero
372 static bool nonZero(Value a) { return a!=0;}
377 static Value zero() {return 0;}
380 #if defined __GNUC__ && !defined __STRICT_ANSI__
382 ///Long long integer specialization of \ref Tolerance.
384 ///Long long integer specialization of \ref Tolerance.
385 ///\warning This class (more exactly, type <tt>long long</tt>)
386 ///is not ansi compatible.
389 class Tolerance<long long int>
393 typedef long long int Value;
396 ///See \ref Tolerance for more details.
400 ///Returns \c true if \c a is \e surely strictly less than \c b
401 static bool less(Value a,Value b) { return a<b;}
402 ///Returns \c true if \c a is \e surely different from \c b
403 static bool different(Value a,Value b) { return a!=b; }
404 ///Returns \c true if \c a is \e surely positive
405 static bool positive(Value a) { return 0<a; }
406 ///Returns \c true if \c a is \e surely negative
407 static bool negative(Value a) { return 0>a; }
408 ///Returns \c true if \c a is \e surely non-zero
409 static bool nonZero(Value a) { return a!=0;}
414 static Value zero() {return 0;}
417 ///Unsigned long long integer specialization of \ref Tolerance.
419 ///Unsigned long long integer specialization of \ref Tolerance.
420 ///\warning This class (more exactly, type <tt>unsigned long long</tt>)
421 ///is not ansi compatible.
424 class Tolerance<unsigned long long int>
428 typedef unsigned long long int Value;
431 ///See \ref Tolerance for more details.
435 ///Returns \c true if \c a is \e surely strictly less than \c b
436 static bool less(Value a,Value b) { return a<b;}
437 ///Returns \c true if \c a is \e surely different from \c b
438 static bool different(Value a,Value b) { return a!=b; }
439 ///Returns \c true if \c a is \e surely positive
440 static bool positive(Value a) { return 0<a; }
441 ///Returns \c true if \c a is \e surely negative
442 static bool negative(Value) { return false; }
443 ///Returns \c true if \c a is \e surely non-zero
444 static bool nonZero(Value a) { return a!=0;}
449 static Value zero() {return 0;}
458 #endif //LEMON_TOLERANCE_H