- tolerance() added.
- better doc.
2 * lemon/tolerance.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
17 #ifndef LEMON_TOLERANCE_H
18 #define LEMON_TOLERANCE_H
22 ///\brief A basic tool to handle the anomalies of calculation with
23 ///floating point numbers.
25 ///\todo It should be in a module like "Basic tools"
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.
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.
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.
45 ///\sa Tolerance<float>
46 ///\sa Tolerance<double>
47 ///\sa Tolerance<long double>
49 ///\sa Tolerance<long long int>
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.
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;}
77 ///Returns the zero value.
78 static Value zero() {return T();}
80 // static bool finite(Value a) {}
81 // static Value big() {}
82 // static Value negativeBig() {}
86 ///Float specialization of \ref Tolerance.
88 ///Float specialization of \ref Tolerance.
92 class Tolerance<float>
94 static float def_epsilon;
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) {}
105 ///Return the epsilon value.
106 Value epsilon() {return _epsilon;}
107 ///Set the epsilon value.
108 void epsilon(Value e) {_epsilon=e;}
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;}
116 ///See class Tolerance for more details.
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); };
134 static Value zero() {return 0;}
137 ///Double specialization of \ref Tolerance.
139 ///Double specialization of \ref Tolerance.
141 ///\relates Tolerance
143 class Tolerance<double>
145 static double def_epsilon;
149 typedef double Value;
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) {}
156 ///Return the epsilon value.
157 Value epsilon() {return _epsilon;}
158 ///Set the epsilon value.
159 void epsilon(Value e) {_epsilon=e;}
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;}
167 ///See class Tolerance for more details.
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); };
185 static Value zero() {return 0;}
188 ///Long double specialization of \ref Tolerance.
190 ///Long double specialization of \ref Tolerance.
192 ///\relates Tolerance
194 class Tolerance<long double>
196 static long double def_epsilon;
197 long double _epsilon;
200 typedef long double Value;
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) {}
207 ///Return the epsilon value.
208 Value epsilon() {return _epsilon;}
209 ///Set the epsilon value.
210 void epsilon(Value e) {_epsilon=e;}
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;}
218 ///See class Tolerance for more details.
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); };
236 static Value zero() {return 0;}
239 ///Integer specialization of \ref Tolerance.
241 ///Integer specialization of \ref Tolerance.
251 ///See \ref Tolerance for more details.
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;};
269 static Value zero() {return 0;}
272 ///Long long integer specialization of \ref Tolerance.
274 ///Long long integer specialization of \ref Tolerance.
277 class Tolerance<long long int>
281 typedef long long int Value;
284 ///See \ref Tolerance for more details.
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;};
302 static Value zero() {return 0;}
309 #endif //LEMON_TOLERANCE_H