1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/tolerance.h Tue Nov 29 08:40:03 2005 +0000
1.3 @@ -0,0 +1,257 @@
1.4 +/* -*- C++ -*-
1.5 + * lemon/tolerance.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_TOLERANCE_H
1.21 +#define LEMON_TOLERANCE_H
1.22 +
1.23 +///\ingroup misc
1.24 +///\file
1.25 +///\brief A basic tool to handle the anomalies of calculation with
1.26 +///floating point numbers.
1.27 +///
1.28 +///\todo It should be in a module like "Basic tools"
1.29 +
1.30 +
1.31 +namespace lemon {
1.32 +
1.33 + /// \addtogroup misc
1.34 + /// @{
1.35 +
1.36 + ///\brief A class to provide a basic way to
1.37 + ///handle the comparison of numbers that are obtained
1.38 + ///as a result of a probably inexact computation.
1.39 + ///
1.40 + ///Tolerance is a class to provide a basic way to
1.41 + ///handle the comparison of numbers that are obtained
1.42 + ///as a result of a probably inexact computation.
1.43 + ///
1.44 + ///This is an abstract class, it should be specialized for all numerical
1.45 + ///data types. These specialized classes like \ref Tolerance<double>
1.46 + ///may offer additional tuning parameters.
1.47 + ///
1.48 + ///\sa Tolerance<float>
1.49 + ///\sa Tolerance<double>
1.50 + ///\sa Tolerance<int>
1.51 + ///\sa Tolerance<long long int>
1.52 +
1.53 + template<class T>
1.54 + class Tolerance
1.55 + {
1.56 + public:
1.57 + typedef T Value;
1.58 +
1.59 + ///\name Comparisons
1.60 + ///The concept is that these bool functions return with \c true only if
1.61 + ///the related comparisons hold even if some numerical error appeared
1.62 + ///during the computations.
1.63 +
1.64 + ///@{
1.65 +
1.66 + ///Returns \c true if \c a is \e surely strictly less than \c b
1.67 + static bool less(Value a,Value b) {return false;}
1.68 + ///Returns \c true if \c a is \e surely different from \c b
1.69 + static bool different(Value a,Value b) {return false;}
1.70 + ///Returns \c true if \c a is \e surely positive
1.71 + static bool positive(Value a) {return false;}
1.72 + ///Returns \c true if \c a is \e surely negative
1.73 + static bool negative(Value a) {return false;}
1.74 + ///Returns \c true if \c a is \e surely non-zero
1.75 + static bool nonZero(Value a) {return false;}
1.76 +
1.77 + ///@}
1.78 +
1.79 + ///Returns the zero value.
1.80 + static Value zero() {return T();}
1.81 +
1.82 + // static bool finite(Value a) {}
1.83 + // static Value big() {}
1.84 + // static Value negativeBig() {}
1.85 + };
1.86 +
1.87 +
1.88 + ///Double specialization of \ref Tolerance.
1.89 +
1.90 + ///Double specialization of \ref Tolerance.
1.91 + ///\sa Tolerance
1.92 + ///\relates Tolerance
1.93 + template<>
1.94 + class Tolerance<double>
1.95 + {
1.96 + static double def_epsilon;
1.97 + double _epsilon;
1.98 + public:
1.99 + ///\e
1.100 + typedef double Value;
1.101 +
1.102 + ///Constructor setting the epsilon tolerance to the default value.
1.103 + Tolerance() : _epsilon(def_epsilon) {}
1.104 + ///Constructor setting the epsilon tolerance.
1.105 + Tolerance(double e) : _epsilon(e) {}
1.106 +
1.107 + ///Return the epsilon value.
1.108 + Value epsilon() {return _epsilon;}
1.109 + ///Set the epsilon value.
1.110 + void epsilon(Value e) {_epsilon=e;}
1.111 +
1.112 + ///Return the default epsilon value.
1.113 + static Value defaultEpsilon() {return def_epsilon;}
1.114 + ///Set the default epsilon value.
1.115 + static void defaultEpsilon(Value e) {def_epsilon=e;}
1.116 +
1.117 + ///\name Comparisons
1.118 + ///See class Tolerance for more details.
1.119 +
1.120 + ///@{
1.121 +
1.122 + ///Returns \c true if \c a is \e surely strictly less than \c b
1.123 + bool less(Value a,Value b) {return a+_epsilon<b;}
1.124 + ///Returns \c true if \c a is \e surely different from \c b
1.125 + bool different(Value a,Value b) { return less(a,b)||less(b,a); }
1.126 + ///Returns \c true if \c a is \e surely positive
1.127 + bool positive(Value a) { return _epsilon<a; }
1.128 + ///Returns \c true if \c a is \e surely negative
1.129 + bool negative(Value a) { return -_epsilon>a; }
1.130 + ///Returns \c true if \c a is \e surely non-zero
1.131 + Value nonZero(Value a) { return positive(a)||negative(a); };
1.132 +
1.133 + ///@}
1.134 +
1.135 + ///Returns zero
1.136 + static Value zero() {return 0;}
1.137 + };
1.138 +
1.139 + ///Float specialization of \ref Tolerance.
1.140 +
1.141 + ///Float specialization of \ref Tolerance.
1.142 + ///\sa Tolerance
1.143 + ///\relates Tolerance
1.144 + template<>
1.145 + class Tolerance<float>
1.146 + {
1.147 + static float def_epsilon;
1.148 + float _epsilon;
1.149 + public:
1.150 + ///\e
1.151 + typedef float Value;
1.152 +
1.153 + ///Constructor setting the epsilon tolerance to the default value.
1.154 + Tolerance() : _epsilon(def_epsilon) {}
1.155 + ///Constructor setting the epsilon tolerance.
1.156 + Tolerance(float e) : _epsilon(e) {}
1.157 +
1.158 + ///Return the epsilon value.
1.159 + Value epsilon() {return _epsilon;}
1.160 + ///Set the epsilon value.
1.161 + void epsilon(Value e) {_epsilon=e;}
1.162 +
1.163 + ///Return the default epsilon value.
1.164 + static Value defaultEpsilon() {return def_epsilon;}
1.165 + ///Set the default epsilon value.
1.166 + static void defaultEpsilon(Value e) {def_epsilon=e;}
1.167 +
1.168 + ///\name Comparisons
1.169 + ///See class Tolerance for more details.
1.170 +
1.171 + ///@{
1.172 +
1.173 + ///Returns \c true if \c a is \e surely strictly less than \c b
1.174 + bool less(Value a,Value b) {return a+_epsilon<b;}
1.175 + ///Returns \c true if \c a is \e surely different from \c b
1.176 + bool different(Value a,Value b) { return less(a,b)||less(b,a); }
1.177 + ///Returns \c true if \c a is \e surely positive
1.178 + bool positive(Value a) { return _epsilon<a; }
1.179 + ///Returns \c true if \c a is \e surely negative
1.180 + bool negative(Value a) { return -_epsilon>a; }
1.181 + ///Returns \c true if \c a is \e surely non-zero
1.182 + Value nonZero(Value a) { return positive(a)||negative(a); };
1.183 +
1.184 + ///@}
1.185 +
1.186 + ///Returns zero
1.187 + static Value zero() {return 0;}
1.188 + };
1.189 +
1.190 + ///Integer specialization of \ref Tolerance.
1.191 +
1.192 + ///Integer specialization of \ref Tolerance.
1.193 + ///\sa Tolerance
1.194 + template<>
1.195 + class Tolerance<int>
1.196 + {
1.197 + public:
1.198 + ///\e
1.199 + typedef int Value;
1.200 +
1.201 + ///\name Comparisons
1.202 + ///See \ref Tolerance for more details.
1.203 +
1.204 + ///@{
1.205 +
1.206 + ///Returns \c true if \c a is \e surely strictly less than \c b
1.207 + static bool less(Value a,Value b) { return a<b;}
1.208 + ///Returns \c true if \c a is \e surely different from \c b
1.209 + static bool different(Value a,Value b) { return a!=b; }
1.210 + ///Returns \c true if \c a is \e surely positive
1.211 + static bool positive(Value a) { return 0<a; }
1.212 + ///Returns \c true if \c a is \e surely negative
1.213 + static bool negative(Value a) { return 0>a; }
1.214 + ///Returns \c true if \c a is \e surely non-zero
1.215 + static Value nonZero(Value a) { return a!=0;};
1.216 +
1.217 + ///@}
1.218 +
1.219 + ///Returns zero
1.220 + static Value zero() {return 0;}
1.221 + };
1.222 +
1.223 + ///Long long integer specialization of \ref Tolerance.
1.224 +
1.225 + ///Long long integer specialization of \ref Tolerance.
1.226 + ///\sa Tolerance
1.227 + template<>
1.228 + class Tolerance<long long int>
1.229 + {
1.230 + public:
1.231 + ///\e
1.232 + typedef long long int Value;
1.233 +
1.234 + ///\name Comparisons
1.235 + ///See \ref Tolerance for more details.
1.236 +
1.237 + ///@{
1.238 +
1.239 + ///Returns \c true if \c a is \e surely strictly less than \c b
1.240 + static bool less(Value a,Value b) { return a<b;}
1.241 + ///Returns \c true if \c a is \e surely different from \c b
1.242 + static bool different(Value a,Value b) { return a!=b; }
1.243 + ///Returns \c true if \c a is \e surely positive
1.244 + static bool positive(Value a) { return 0<a; }
1.245 + ///Returns \c true if \c a is \e surely negative
1.246 + static bool negative(Value a) { return 0>a; }
1.247 + ///Returns \c true if \c a is \e surely non-zero
1.248 + static Value nonZero(Value a) { return a!=0;};
1.249 +
1.250 + ///@}
1.251 +
1.252 + ///Returns zero
1.253 + static Value zero() {return 0;}
1.254 + };
1.255 +
1.256 + /// @}
1.257 +
1.258 +} //namespace lemon
1.259 +
1.260 +#endif //LEMON_TOLERANCE_H