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