... |
... |
@@ -27,51 +27,55 @@
|
27 |
27 |
///\todo It should be in a module like "Basic tools"
|
28 |
28 |
|
29 |
29 |
|
30 |
30 |
namespace lemon {
|
31 |
31 |
|
32 |
32 |
/// \addtogroup misc
|
33 |
33 |
/// @{
|
34 |
34 |
|
35 |
35 |
///\brief A class to provide a basic way to
|
36 |
36 |
///handle the comparison of numbers that are obtained
|
37 |
37 |
///as a result of a probably inexact computation.
|
38 |
38 |
///
|
39 |
39 |
///Tolerance is a class to provide a basic way to
|
40 |
40 |
///handle the comparison of numbers that are obtained
|
41 |
41 |
///as a result of a probably inexact computation.
|
42 |
42 |
///
|
43 |
43 |
///This is an abstract class, it should be specialized for all numerical
|
44 |
44 |
///data types. These specialized classes like \ref Tolerance\<double\>
|
45 |
45 |
///may offer additional tuning parameters.
|
46 |
46 |
///
|
47 |
47 |
///\sa Tolerance<float>
|
48 |
48 |
///\sa Tolerance<double>
|
49 |
49 |
///\sa Tolerance<long double>
|
50 |
50 |
///\sa Tolerance<int>
|
|
51 |
#if defined __GNUC__ && !defined __STRICT_ANSI__
|
51 |
52 |
///\sa Tolerance<long long int>
|
|
53 |
#endif
|
52 |
54 |
///\sa Tolerance<unsigned int>
|
|
55 |
#if defined __GNUC__ && !defined __STRICT_ANSI__
|
53 |
56 |
///\sa Tolerance<unsigned long long int>
|
|
57 |
#endif
|
54 |
58 |
|
55 |
59 |
template<class T>
|
56 |
60 |
class Tolerance
|
57 |
61 |
{
|
58 |
62 |
public:
|
59 |
63 |
typedef T Value;
|
60 |
64 |
|
61 |
65 |
///\name Comparisons
|
62 |
66 |
///The concept is that these bool functions return with \c true only if
|
63 |
67 |
///the related comparisons hold even if some numerical error appeared
|
64 |
68 |
///during the computations.
|
65 |
69 |
|
66 |
70 |
///@{
|
67 |
71 |
|
68 |
72 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
69 |
73 |
static bool less(Value a,Value b) {return false;}
|
70 |
74 |
///Returns \c true if \c a is \e surely different from \c b
|
71 |
75 |
static bool different(Value a,Value b) {return false;}
|
72 |
76 |
///Returns \c true if \c a is \e surely positive
|
73 |
77 |
static bool positive(Value a) {return false;}
|
74 |
78 |
///Returns \c true if \c a is \e surely negative
|
75 |
79 |
static bool negative(Value a) {return false;}
|
76 |
80 |
///Returns \c true if \c a is \e surely non-zero
|
77 |
81 |
static bool nonZero(Value a) {return false;}
|
... |
... |
@@ -109,49 +113,49 @@
|
109 |
113 |
///Return the epsilon value.
|
110 |
114 |
Value epsilon() const {return _epsilon;}
|
111 |
115 |
///Set the epsilon value.
|
112 |
116 |
void epsilon(Value e) {_epsilon=e;}
|
113 |
117 |
|
114 |
118 |
///Return the default epsilon value.
|
115 |
119 |
static Value defaultEpsilon() {return def_epsilon;}
|
116 |
120 |
///Set the default epsilon value.
|
117 |
121 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
118 |
122 |
|
119 |
123 |
///\name Comparisons
|
120 |
124 |
///See class Tolerance for more details.
|
121 |
125 |
|
122 |
126 |
///@{
|
123 |
127 |
|
124 |
128 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
125 |
129 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
126 |
130 |
///Returns \c true if \c a is \e surely different from \c b
|
127 |
131 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
128 |
132 |
///Returns \c true if \c a is \e surely positive
|
129 |
133 |
bool positive(Value a) const { return _epsilon<a; }
|
130 |
134 |
///Returns \c true if \c a is \e surely negative
|
131 |
135 |
bool negative(Value a) const { return -_epsilon>a; }
|
132 |
136 |
///Returns \c true if \c a is \e surely non-zero
|
133 |
|
bool nonZero(Value a) const { return positive(a)||negative(a); };
|
|
137 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
134 |
138 |
|
135 |
139 |
///@}
|
136 |
140 |
|
137 |
141 |
///Returns zero
|
138 |
142 |
static Value zero() {return 0;}
|
139 |
143 |
};
|
140 |
144 |
|
141 |
145 |
///Double specialization of \ref Tolerance.
|
142 |
146 |
|
143 |
147 |
///Double specialization of \ref Tolerance.
|
144 |
148 |
///\sa Tolerance
|
145 |
149 |
///\relates Tolerance
|
146 |
150 |
template<>
|
147 |
151 |
class Tolerance<double>
|
148 |
152 |
{
|
149 |
153 |
static double def_epsilon;
|
150 |
154 |
double _epsilon;
|
151 |
155 |
public:
|
152 |
156 |
///\e
|
153 |
157 |
typedef double Value;
|
154 |
158 |
|
155 |
159 |
///Constructor setting the epsilon tolerance to the default value.
|
156 |
160 |
Tolerance() : _epsilon(def_epsilon) {}
|
157 |
161 |
///Constructor setting the epsilon tolerance.
|
... |
... |
@@ -160,49 +164,49 @@
|
160 |
164 |
///Return the epsilon value.
|
161 |
165 |
Value epsilon() const {return _epsilon;}
|
162 |
166 |
///Set the epsilon value.
|
163 |
167 |
void epsilon(Value e) {_epsilon=e;}
|
164 |
168 |
|
165 |
169 |
///Return the default epsilon value.
|
166 |
170 |
static Value defaultEpsilon() {return def_epsilon;}
|
167 |
171 |
///Set the default epsilon value.
|
168 |
172 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
169 |
173 |
|
170 |
174 |
///\name Comparisons
|
171 |
175 |
///See class Tolerance for more details.
|
172 |
176 |
|
173 |
177 |
///@{
|
174 |
178 |
|
175 |
179 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
176 |
180 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
177 |
181 |
///Returns \c true if \c a is \e surely different from \c b
|
178 |
182 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
179 |
183 |
///Returns \c true if \c a is \e surely positive
|
180 |
184 |
bool positive(Value a) const { return _epsilon<a; }
|
181 |
185 |
///Returns \c true if \c a is \e surely negative
|
182 |
186 |
bool negative(Value a) const { return -_epsilon>a; }
|
183 |
187 |
///Returns \c true if \c a is \e surely non-zero
|
184 |
|
bool nonZero(Value a) const { return positive(a)||negative(a); };
|
|
188 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
185 |
189 |
|
186 |
190 |
///@}
|
187 |
191 |
|
188 |
192 |
///Returns zero
|
189 |
193 |
static Value zero() {return 0;}
|
190 |
194 |
};
|
191 |
195 |
|
192 |
196 |
///Long double specialization of \ref Tolerance.
|
193 |
197 |
|
194 |
198 |
///Long double specialization of \ref Tolerance.
|
195 |
199 |
///\sa Tolerance
|
196 |
200 |
///\relates Tolerance
|
197 |
201 |
template<>
|
198 |
202 |
class Tolerance<long double>
|
199 |
203 |
{
|
200 |
204 |
static long double def_epsilon;
|
201 |
205 |
long double _epsilon;
|
202 |
206 |
public:
|
203 |
207 |
///\e
|
204 |
208 |
typedef long double Value;
|
205 |
209 |
|
206 |
210 |
///Constructor setting the epsilon tolerance to the default value.
|
207 |
211 |
Tolerance() : _epsilon(def_epsilon) {}
|
208 |
212 |
///Constructor setting the epsilon tolerance.
|
... |
... |
@@ -211,244 +215,244 @@
|
211 |
215 |
///Return the epsilon value.
|
212 |
216 |
Value epsilon() const {return _epsilon;}
|
213 |
217 |
///Set the epsilon value.
|
214 |
218 |
void epsilon(Value e) {_epsilon=e;}
|
215 |
219 |
|
216 |
220 |
///Return the default epsilon value.
|
217 |
221 |
static Value defaultEpsilon() {return def_epsilon;}
|
218 |
222 |
///Set the default epsilon value.
|
219 |
223 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
220 |
224 |
|
221 |
225 |
///\name Comparisons
|
222 |
226 |
///See class Tolerance for more details.
|
223 |
227 |
|
224 |
228 |
///@{
|
225 |
229 |
|
226 |
230 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
227 |
231 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
228 |
232 |
///Returns \c true if \c a is \e surely different from \c b
|
229 |
233 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
230 |
234 |
///Returns \c true if \c a is \e surely positive
|
231 |
235 |
bool positive(Value a) const { return _epsilon<a; }
|
232 |
236 |
///Returns \c true if \c a is \e surely negative
|
233 |
237 |
bool negative(Value a) const { return -_epsilon>a; }
|
234 |
238 |
///Returns \c true if \c a is \e surely non-zero
|
235 |
|
bool nonZero(Value a) const { return positive(a)||negative(a); };
|
|
239 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
236 |
240 |
|
237 |
241 |
///@}
|
238 |
242 |
|
239 |
243 |
///Returns zero
|
240 |
244 |
static Value zero() {return 0;}
|
241 |
245 |
};
|
242 |
246 |
|
243 |
247 |
///Integer specialization of \ref Tolerance.
|
244 |
248 |
|
245 |
249 |
///Integer specialization of \ref Tolerance.
|
246 |
250 |
///\sa Tolerance
|
247 |
251 |
template<>
|
248 |
252 |
class Tolerance<int>
|
249 |
253 |
{
|
250 |
254 |
public:
|
251 |
255 |
///\e
|
252 |
256 |
typedef int Value;
|
253 |
257 |
|
254 |
258 |
///\name Comparisons
|
255 |
259 |
///See \ref Tolerance for more details.
|
256 |
260 |
|
257 |
261 |
///@{
|
258 |
262 |
|
259 |
263 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
260 |
264 |
static bool less(Value a,Value b) { return a<b;}
|
261 |
265 |
///Returns \c true if \c a is \e surely different from \c b
|
262 |
266 |
static bool different(Value a,Value b) { return a!=b; }
|
263 |
267 |
///Returns \c true if \c a is \e surely positive
|
264 |
268 |
static bool positive(Value a) { return 0<a; }
|
265 |
269 |
///Returns \c true if \c a is \e surely negative
|
266 |
270 |
static bool negative(Value a) { return 0>a; }
|
267 |
271 |
///Returns \c true if \c a is \e surely non-zero
|
268 |
|
static bool nonZero(Value a) { return a!=0; };
|
|
272 |
static bool nonZero(Value a) { return a!=0; }
|
269 |
273 |
|
270 |
274 |
///@}
|
271 |
275 |
|
272 |
276 |
///Returns zero
|
273 |
277 |
static Value zero() {return 0;}
|
274 |
278 |
};
|
275 |
279 |
|
276 |
280 |
///Unsigned integer specialization of \ref Tolerance.
|
277 |
281 |
|
278 |
282 |
///Unsigned integer specialization of \ref Tolerance.
|
279 |
283 |
///\sa Tolerance
|
280 |
284 |
template<>
|
281 |
285 |
class Tolerance<unsigned int>
|
282 |
286 |
{
|
283 |
287 |
public:
|
284 |
288 |
///\e
|
285 |
289 |
typedef unsigned int Value;
|
286 |
290 |
|
287 |
291 |
///\name Comparisons
|
288 |
292 |
///See \ref Tolerance for more details.
|
289 |
293 |
|
290 |
294 |
///@{
|
291 |
295 |
|
292 |
296 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
293 |
297 |
static bool less(Value a,Value b) { return a<b;}
|
294 |
298 |
///Returns \c true if \c a is \e surely different from \c b
|
295 |
299 |
static bool different(Value a,Value b) { return a!=b; }
|
296 |
300 |
///Returns \c true if \c a is \e surely positive
|
297 |
301 |
static bool positive(Value a) { return 0<a; }
|
298 |
302 |
///Returns \c true if \c a is \e surely negative
|
299 |
303 |
static bool negative(Value) { return false; }
|
300 |
304 |
///Returns \c true if \c a is \e surely non-zero
|
301 |
|
static bool nonZero(Value a) { return a!=0; };
|
|
305 |
static bool nonZero(Value a) { return a!=0; }
|
302 |
306 |
|
303 |
307 |
///@}
|
304 |
308 |
|
305 |
309 |
///Returns zero
|
306 |
310 |
static Value zero() {return 0;}
|
307 |
311 |
};
|
308 |
312 |
|
309 |
313 |
|
310 |
314 |
///Long integer specialization of \ref Tolerance.
|
311 |
315 |
|
312 |
316 |
///Long integer specialization of \ref Tolerance.
|
313 |
317 |
///\sa Tolerance
|
314 |
318 |
template<>
|
315 |
319 |
class Tolerance<long int>
|
316 |
320 |
{
|
317 |
321 |
public:
|
318 |
322 |
///\e
|
319 |
323 |
typedef long int Value;
|
320 |
324 |
|
321 |
325 |
///\name Comparisons
|
322 |
326 |
///See \ref Tolerance for more details.
|
323 |
327 |
|
324 |
328 |
///@{
|
325 |
329 |
|
326 |
330 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
327 |
331 |
static bool less(Value a,Value b) { return a<b;}
|
328 |
332 |
///Returns \c true if \c a is \e surely different from \c b
|
329 |
333 |
static bool different(Value a,Value b) { return a!=b; }
|
330 |
334 |
///Returns \c true if \c a is \e surely positive
|
331 |
335 |
static bool positive(Value a) { return 0<a; }
|
332 |
336 |
///Returns \c true if \c a is \e surely negative
|
333 |
337 |
static bool negative(Value a) { return 0>a; }
|
334 |
338 |
///Returns \c true if \c a is \e surely non-zero
|
335 |
|
static bool nonZero(Value a) { return a!=0;};
|
|
339 |
static bool nonZero(Value a) { return a!=0;}
|
336 |
340 |
|
337 |
341 |
///@}
|
338 |
342 |
|
339 |
343 |
///Returns zero
|
340 |
344 |
static Value zero() {return 0;}
|
341 |
345 |
};
|
342 |
346 |
|
343 |
347 |
///Unsigned long integer specialization of \ref Tolerance.
|
344 |
348 |
|
345 |
349 |
///Unsigned long integer specialization of \ref Tolerance.
|
346 |
350 |
///\sa Tolerance
|
347 |
351 |
template<>
|
348 |
352 |
class Tolerance<unsigned long int>
|
349 |
353 |
{
|
350 |
354 |
public:
|
351 |
355 |
///\e
|
352 |
356 |
typedef unsigned long int Value;
|
353 |
357 |
|
354 |
358 |
///\name Comparisons
|
355 |
359 |
///See \ref Tolerance for more details.
|
356 |
360 |
|
357 |
361 |
///@{
|
358 |
362 |
|
359 |
363 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
360 |
364 |
static bool less(Value a,Value b) { return a<b;}
|
361 |
365 |
///Returns \c true if \c a is \e surely different from \c b
|
362 |
366 |
static bool different(Value a,Value b) { return a!=b; }
|
363 |
367 |
///Returns \c true if \c a is \e surely positive
|
364 |
368 |
static bool positive(Value a) { return 0<a; }
|
365 |
369 |
///Returns \c true if \c a is \e surely negative
|
366 |
370 |
static bool negative(Value) { return false; }
|
367 |
371 |
///Returns \c true if \c a is \e surely non-zero
|
368 |
|
static bool nonZero(Value a) { return a!=0;};
|
|
372 |
static bool nonZero(Value a) { return a!=0;}
|
369 |
373 |
|
370 |
374 |
///@}
|
371 |
375 |
|
372 |
376 |
///Returns zero
|
373 |
377 |
static Value zero() {return 0;}
|
374 |
378 |
};
|
375 |
379 |
|
376 |
380 |
#if defined __GNUC__ && !defined __STRICT_ANSI__
|
377 |
381 |
|
378 |
382 |
///Long long integer specialization of \ref Tolerance.
|
379 |
383 |
|
380 |
384 |
///Long long integer specialization of \ref Tolerance.
|
381 |
385 |
///\warning This class (more exactly, type <tt>long long</tt>)
|
382 |
386 |
///is not ansi compatible.
|
383 |
387 |
///\sa Tolerance
|
384 |
388 |
template<>
|
385 |
389 |
class Tolerance<long long int>
|
386 |
390 |
{
|
387 |
391 |
public:
|
388 |
392 |
///\e
|
389 |
393 |
typedef long long int Value;
|
390 |
394 |
|
391 |
395 |
///\name Comparisons
|
392 |
396 |
///See \ref Tolerance for more details.
|
393 |
397 |
|
394 |
398 |
///@{
|
395 |
399 |
|
396 |
400 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
397 |
401 |
static bool less(Value a,Value b) { return a<b;}
|
398 |
402 |
///Returns \c true if \c a is \e surely different from \c b
|
399 |
403 |
static bool different(Value a,Value b) { return a!=b; }
|
400 |
404 |
///Returns \c true if \c a is \e surely positive
|
401 |
405 |
static bool positive(Value a) { return 0<a; }
|
402 |
406 |
///Returns \c true if \c a is \e surely negative
|
403 |
407 |
static bool negative(Value a) { return 0>a; }
|
404 |
408 |
///Returns \c true if \c a is \e surely non-zero
|
405 |
|
static bool nonZero(Value a) { return a!=0;};
|
|
409 |
static bool nonZero(Value a) { return a!=0;}
|
406 |
410 |
|
407 |
411 |
///@}
|
408 |
412 |
|
409 |
413 |
///Returns zero
|
410 |
414 |
static Value zero() {return 0;}
|
411 |
415 |
};
|
412 |
416 |
|
413 |
417 |
///Unsigned long long integer specialization of \ref Tolerance.
|
414 |
418 |
|
415 |
419 |
///Unsigned long long integer specialization of \ref Tolerance.
|
416 |
420 |
///\warning This class (more exactly, type <tt>unsigned long long</tt>)
|
417 |
421 |
///is not ansi compatible.
|
418 |
422 |
///\sa Tolerance
|
419 |
423 |
template<>
|
420 |
424 |
class Tolerance<unsigned long long int>
|
421 |
425 |
{
|
422 |
426 |
public:
|
423 |
427 |
///\e
|
424 |
428 |
typedef unsigned long long int Value;
|
425 |
429 |
|
426 |
430 |
///\name Comparisons
|
427 |
431 |
///See \ref Tolerance for more details.
|
428 |
432 |
|
429 |
433 |
///@{
|
430 |
434 |
|
431 |
435 |
///Returns \c true if \c a is \e surely strictly less than \c b
|
432 |
436 |
static bool less(Value a,Value b) { return a<b;}
|
433 |
437 |
///Returns \c true if \c a is \e surely different from \c b
|
434 |
438 |
static bool different(Value a,Value b) { return a!=b; }
|
435 |
439 |
///Returns \c true if \c a is \e surely positive
|
436 |
440 |
static bool positive(Value a) { return 0<a; }
|
437 |
441 |
///Returns \c true if \c a is \e surely negative
|
438 |
442 |
static bool negative(Value) { return false; }
|
439 |
443 |
///Returns \c true if \c a is \e surely non-zero
|
440 |
|
static bool nonZero(Value a) { return a!=0;};
|
|
444 |
static bool nonZero(Value a) { return a!=0;}
|
441 |
445 |
|
442 |
446 |
///@}
|
443 |
447 |
|
444 |
448 |
///Returns zero
|
445 |
449 |
static Value zero() {return 0;}
|
446 |
450 |
};
|
447 |
451 |
|
448 |
452 |
#endif
|
449 |
453 |
|
450 |
454 |
/// @}
|
451 |
455 |
|
452 |
456 |
} //namespace lemon
|
453 |
457 |
|
454 |
458 |
#endif //LEMON_TOLERANCE_H
|