lemon/tolerance.h
changeset 66 5f7a8570687d
parent 42 3a98515e9bc3
child 72 7050661bdea4
equal deleted inserted replaced
3:150880e30176 4:4965b3c79f8a
    34   
    34   
    35   ///\brief A class to provide a basic way to
    35   ///\brief A class to provide a basic way to
    36   ///handle the comparison of numbers that are obtained
    36   ///handle the comparison of numbers that are obtained
    37   ///as a result of a probably inexact computation.
    37   ///as a result of a probably inexact computation.
    38   ///
    38   ///
    39   ///Tolerance is a class to provide a basic way to
    39   ///\ref Tolerance is a class to provide a basic way to
    40   ///handle the comparison of numbers that are obtained
    40   ///handle the comparison of numbers that are obtained
    41   ///as a result of a probably inexact computation.
    41   ///as a result of a probably inexact computation.
    42   ///
    42   ///
    43   ///This is an abstract class, it should be specialized for all numerical
    43   ///This is an abstract class, it should be specialized for all numerical
    44   ///data types. These specialized classes like Tolerance<double>
    44   ///data types. These specialized classes like \ref Tolerance<double>
    45   ///may offer additional tuning parameters.
    45   ///may offer additional tuning parameters.
    46   ///
    46   ///
    47   ///\sa Tolerance<float>
    47   ///\sa Tolerance<float>
    48   ///\sa Tolerance<double>
    48   ///\sa Tolerance<double>
    49   ///\sa Tolerance<long double>
    49   ///\sa Tolerance<long double>
    50   ///\sa Tolerance<int>
    50   ///\sa Tolerance<int>
    51 #if defined __GNUC__ && !defined __STRICT_ANSI__  
       
    52   ///\sa Tolerance<long long int>
    51   ///\sa Tolerance<long long int>
    53 #endif
       
    54   ///\sa Tolerance<unsigned int>
    52   ///\sa Tolerance<unsigned int>
    55 #if defined __GNUC__ && !defined __STRICT_ANSI__  
       
    56   ///\sa Tolerance<unsigned long long int>
    53   ///\sa Tolerance<unsigned long long int>
    57 #endif
       
    58 
    54 
    59   template<class T>
    55   template<class T>
    60   class Tolerance
    56   class Tolerance
    61   {
    57   {
    62   public:
    58   public:
    63     typedef T Value;
    59     typedef T Value;
    64 
    60 
    65     ///\name Comparisons
    61     ///\name Comparisons
    66     ///The concept is that these bool functions return with \c true only if
    62     ///The concept is that these bool functions return \c true only if
    67     ///the related comparisons hold even if some numerical error appeared
    63     ///the related comparisons hold even if some numerical error appeared
    68     ///during the computations.
    64     ///during the computations.
    69 
    65 
    70     ///@{
    66     ///@{
    71 
    67 
   105     ///\e
   101     ///\e
   106     typedef float Value;
   102     typedef float Value;
   107 
   103 
   108     ///Constructor setting the epsilon tolerance to the default value.
   104     ///Constructor setting the epsilon tolerance to the default value.
   109     Tolerance() : _epsilon(def_epsilon) {}
   105     Tolerance() : _epsilon(def_epsilon) {}
   110     ///Constructor setting the epsilon tolerance.
   106     ///Constructor setting the epsilon tolerance to the given value.
   111     Tolerance(float e) : _epsilon(e) {}
   107     Tolerance(float e) : _epsilon(e) {}
   112 
   108 
   113     ///Return the epsilon value.
   109     ///Returns the epsilon value.
   114     Value epsilon() const {return _epsilon;}
   110     Value epsilon() const {return _epsilon;}
   115     ///Set the epsilon value.
   111     ///Sets the epsilon value.
   116     void epsilon(Value e) {_epsilon=e;}
   112     void epsilon(Value e) {_epsilon=e;}
   117 
   113 
   118     ///Return the default epsilon value.
   114     ///Returns the default epsilon value.
   119     static Value defaultEpsilon() {return def_epsilon;}
   115     static Value defaultEpsilon() {return def_epsilon;}
   120     ///Set the default epsilon value.
   116     ///Sets the default epsilon value.
   121     static void defaultEpsilon(Value e) {def_epsilon=e;}
   117     static void defaultEpsilon(Value e) {def_epsilon=e;}
   122 
   118 
   123     ///\name Comparisons
   119     ///\name Comparisons
   124     ///See class Tolerance for more details.
   120     ///See \ref Tolerance for more details.
   125 
   121 
   126     ///@{
   122     ///@{
   127 
   123 
   128     ///Returns \c true if \c a is \e surely strictly less than \c b
   124     ///Returns \c true if \c a is \e surely strictly less than \c b
   129     bool less(Value a,Value b) const {return a+_epsilon<b;}
   125     bool less(Value a,Value b) const {return a+_epsilon<b;}
   156     ///\e
   152     ///\e
   157     typedef double Value;
   153     typedef double Value;
   158 
   154 
   159     ///Constructor setting the epsilon tolerance to the default value.
   155     ///Constructor setting the epsilon tolerance to the default value.
   160     Tolerance() : _epsilon(def_epsilon) {}
   156     Tolerance() : _epsilon(def_epsilon) {}
   161     ///Constructor setting the epsilon tolerance.
   157     ///Constructor setting the epsilon tolerance to the given value.
   162     Tolerance(double e) : _epsilon(e) {}
   158     Tolerance(double e) : _epsilon(e) {}
   163 
   159 
   164     ///Return the epsilon value.
   160     ///Returns the epsilon value.
   165     Value epsilon() const {return _epsilon;}
   161     Value epsilon() const {return _epsilon;}
   166     ///Set the epsilon value.
   162     ///Sets the epsilon value.
   167     void epsilon(Value e) {_epsilon=e;}
   163     void epsilon(Value e) {_epsilon=e;}
   168 
   164 
   169     ///Return the default epsilon value.
   165     ///Returns the default epsilon value.
   170     static Value defaultEpsilon() {return def_epsilon;}
   166     static Value defaultEpsilon() {return def_epsilon;}
   171     ///Set the default epsilon value.
   167     ///Sets the default epsilon value.
   172     static void defaultEpsilon(Value e) {def_epsilon=e;}
   168     static void defaultEpsilon(Value e) {def_epsilon=e;}
   173 
   169 
   174     ///\name Comparisons
   170     ///\name Comparisons
   175     ///See class Tolerance for more details.
   171     ///See \ref Tolerance for more details.
   176 
   172 
   177     ///@{
   173     ///@{
   178 
   174 
   179     ///Returns \c true if \c a is \e surely strictly less than \c b
   175     ///Returns \c true if \c a is \e surely strictly less than \c b
   180     bool less(Value a,Value b) const {return a+_epsilon<b;}
   176     bool less(Value a,Value b) const {return a+_epsilon<b;}
   207     ///\e
   203     ///\e
   208     typedef long double Value;
   204     typedef long double Value;
   209 
   205 
   210     ///Constructor setting the epsilon tolerance to the default value.
   206     ///Constructor setting the epsilon tolerance to the default value.
   211     Tolerance() : _epsilon(def_epsilon) {}
   207     Tolerance() : _epsilon(def_epsilon) {}
   212     ///Constructor setting the epsilon tolerance.
   208     ///Constructor setting the epsilon tolerance to the given value.
   213     Tolerance(long double e) : _epsilon(e) {}
   209     Tolerance(long double e) : _epsilon(e) {}
   214 
   210 
   215     ///Return the epsilon value.
   211     ///Returns the epsilon value.
   216     Value epsilon() const {return _epsilon;}
   212     Value epsilon() const {return _epsilon;}
   217     ///Set the epsilon value.
   213     ///Sets the epsilon value.
   218     void epsilon(Value e) {_epsilon=e;}
   214     void epsilon(Value e) {_epsilon=e;}
   219 
   215 
   220     ///Return the default epsilon value.
   216     ///Returns the default epsilon value.
   221     static Value defaultEpsilon() {return def_epsilon;}
   217     static Value defaultEpsilon() {return def_epsilon;}
   222     ///Set the default epsilon value.
   218     ///Sets the default epsilon value.
   223     static void defaultEpsilon(Value e) {def_epsilon=e;}
   219     static void defaultEpsilon(Value e) {def_epsilon=e;}
   224 
   220 
   225     ///\name Comparisons
   221     ///\name Comparisons
   226     ///See class Tolerance for more details.
   222     ///See \ref Tolerance for more details.
   227 
   223 
   228     ///@{
   224     ///@{
   229 
   225 
   230     ///Returns \c true if \c a is \e surely strictly less than \c b
   226     ///Returns \c true if \c a is \e surely strictly less than \c b
   231     bool less(Value a,Value b) const {return a+_epsilon<b;}
   227     bool less(Value a,Value b) const {return a+_epsilon<b;}
   254   public:
   250   public:
   255     ///\e
   251     ///\e
   256     typedef int Value;
   252     typedef int Value;
   257 
   253 
   258     ///\name Comparisons
   254     ///\name Comparisons
   259     ///See Tolerance for more details.
   255     ///See \ref Tolerance for more details.
   260 
   256 
   261     ///@{
   257     ///@{
   262 
   258 
   263     ///Returns \c true if \c a is \e surely strictly less than \c b
   259     ///Returns \c true if \c a is \e surely strictly less than \c b
   264     static bool less(Value a,Value b) { return a<b;}
   260     static bool less(Value a,Value b) { return a<b;}
   287   public:
   283   public:
   288     ///\e
   284     ///\e
   289     typedef unsigned int Value;
   285     typedef unsigned int Value;
   290 
   286 
   291     ///\name Comparisons
   287     ///\name Comparisons
   292     ///See Tolerance for more details.
   288     ///See \ref Tolerance for more details.
   293 
   289 
   294     ///@{
   290     ///@{
   295 
   291 
   296     ///Returns \c true if \c a is \e surely strictly less than \c b
   292     ///Returns \c true if \c a is \e surely strictly less than \c b
   297     static bool less(Value a,Value b) { return a<b;}
   293     static bool less(Value a,Value b) { return a<b;}
   321   public:
   317   public:
   322     ///\e
   318     ///\e
   323     typedef long int Value;
   319     typedef long int Value;
   324 
   320 
   325     ///\name Comparisons
   321     ///\name Comparisons
   326     ///See Tolerance for more details.
   322     ///See \ref Tolerance for more details.
   327 
   323 
   328     ///@{
   324     ///@{
   329 
   325 
   330     ///Returns \c true if \c a is \e surely strictly less than \c b
   326     ///Returns \c true if \c a is \e surely strictly less than \c b
   331     static bool less(Value a,Value b) { return a<b;}
   327     static bool less(Value a,Value b) { return a<b;}
   354   public:
   350   public:
   355     ///\e
   351     ///\e
   356     typedef unsigned long int Value;
   352     typedef unsigned long int Value;
   357 
   353 
   358     ///\name Comparisons
   354     ///\name Comparisons
   359     ///See Tolerance for more details.
   355     ///See \ref Tolerance for more details.
   360 
   356 
   361     ///@{
   357     ///@{
   362 
   358 
   363     ///Returns \c true if \c a is \e surely strictly less than \c b
   359     ///Returns \c true if \c a is \e surely strictly less than \c b
   364     static bool less(Value a,Value b) { return a<b;}
   360     static bool less(Value a,Value b) { return a<b;}