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>
48 ///\sa Tolerance<long long int>
57 ///The concept is that these bool functions return with \c true only if
58 ///the related comparisons hold even if some numerical error appeared
59 ///during the computations.
63 ///Returns \c true if \c a is \e surely strictly less than \c b
64 static bool less(Value a,Value b) {return false;}
65 ///Returns \c true if \c a is \e surely different from \c b
66 static bool different(Value a,Value b) {return false;}
67 ///Returns \c true if \c a is \e surely positive
68 static bool positive(Value a) {return false;}
69 ///Returns \c true if \c a is \e surely negative
70 static bool negative(Value a) {return false;}
71 ///Returns \c true if \c a is \e surely non-zero
72 static bool nonZero(Value a) {return false;}
76 ///Returns the zero value.
77 static Value zero() {return T();}
79 // static bool finite(Value a) {}
80 // static Value big() {}
81 // static Value negativeBig() {}
85 ///Double specialization of \ref Tolerance.
87 ///Double specialization of \ref Tolerance.
91 class Tolerance<double>
93 static double def_epsilon;
99 ///Constructor setting the epsilon tolerance to the default value.
100 Tolerance() : _epsilon(def_epsilon) {}
101 ///Constructor setting the epsilon tolerance.
102 Tolerance(double e) : _epsilon(e) {}
104 ///Return the epsilon value.
105 Value epsilon() {return _epsilon;}
106 ///Set the epsilon value.
107 void epsilon(Value e) {_epsilon=e;}
109 ///Return the default epsilon value.
110 static Value defaultEpsilon() {return def_epsilon;}
111 ///Set the default epsilon value.
112 static void defaultEpsilon(Value e) {def_epsilon=e;}
115 ///See class Tolerance for more details.
119 ///Returns \c true if \c a is \e surely strictly less than \c b
120 bool less(Value a,Value b) {return a+_epsilon<b;}
121 ///Returns \c true if \c a is \e surely different from \c b
122 bool different(Value a,Value b) { return less(a,b)||less(b,a); }
123 ///Returns \c true if \c a is \e surely positive
124 bool positive(Value a) { return _epsilon<a; }
125 ///Returns \c true if \c a is \e surely negative
126 bool negative(Value a) { return -_epsilon>a; }
127 ///Returns \c true if \c a is \e surely non-zero
128 Value nonZero(Value a) { return positive(a)||negative(a); };
133 static Value zero() {return 0;}
136 ///Float specialization of \ref Tolerance.
138 ///Float specialization of \ref Tolerance.
140 ///\relates Tolerance
142 class Tolerance<float>
144 static float def_epsilon;
150 ///Constructor setting the epsilon tolerance to the default value.
151 Tolerance() : _epsilon(def_epsilon) {}
152 ///Constructor setting the epsilon tolerance.
153 Tolerance(float e) : _epsilon(e) {}
155 ///Return the epsilon value.
156 Value epsilon() {return _epsilon;}
157 ///Set the epsilon value.
158 void epsilon(Value e) {_epsilon=e;}
160 ///Return the default epsilon value.
161 static Value defaultEpsilon() {return def_epsilon;}
162 ///Set the default epsilon value.
163 static void defaultEpsilon(Value e) {def_epsilon=e;}
166 ///See class Tolerance for more details.
170 ///Returns \c true if \c a is \e surely strictly less than \c b
171 bool less(Value a,Value b) {return a+_epsilon<b;}
172 ///Returns \c true if \c a is \e surely different from \c b
173 bool different(Value a,Value b) { return less(a,b)||less(b,a); }
174 ///Returns \c true if \c a is \e surely positive
175 bool positive(Value a) { return _epsilon<a; }
176 ///Returns \c true if \c a is \e surely negative
177 bool negative(Value a) { return -_epsilon>a; }
178 ///Returns \c true if \c a is \e surely non-zero
179 Value nonZero(Value a) { return positive(a)||negative(a); };
184 static Value zero() {return 0;}
187 ///Integer specialization of \ref Tolerance.
189 ///Integer specialization of \ref Tolerance.
199 ///See \ref Tolerance for more details.
203 ///Returns \c true if \c a is \e surely strictly less than \c b
204 static bool less(Value a,Value b) { return a<b;}
205 ///Returns \c true if \c a is \e surely different from \c b
206 static bool different(Value a,Value b) { return a!=b; }
207 ///Returns \c true if \c a is \e surely positive
208 static bool positive(Value a) { return 0<a; }
209 ///Returns \c true if \c a is \e surely negative
210 static bool negative(Value a) { return 0>a; }
211 ///Returns \c true if \c a is \e surely non-zero
212 static Value nonZero(Value a) { return a!=0;};
217 static Value zero() {return 0;}
220 ///Long long integer specialization of \ref Tolerance.
222 ///Long long integer specialization of \ref Tolerance.
225 class Tolerance<long long int>
229 typedef long long int Value;
232 ///See \ref Tolerance for more details.
236 ///Returns \c true if \c a is \e surely strictly less than \c b
237 static bool less(Value a,Value b) { return a<b;}
238 ///Returns \c true if \c a is \e surely different from \c b
239 static bool different(Value a,Value b) { return a!=b; }
240 ///Returns \c true if \c a is \e surely positive
241 static bool positive(Value a) { return 0<a; }
242 ///Returns \c true if \c a is \e surely negative
243 static bool negative(Value a) { return 0>a; }
244 ///Returns \c true if \c a is \e surely non-zero
245 static Value nonZero(Value a) { return a!=0;};
250 static Value zero() {return 0;}
257 #endif //LEMON_TOLERANCE_H