lemon/tolerance.h
changeset 1899 2d4835f5a86a
parent 1875 98698b69a902
child 1953 d4f411003580
equal deleted inserted replaced
1:00d9b761251b 2:b58228839da0
    42   ///data types. These specialized classes like \ref Tolerance<double>
    42   ///data types. These specialized classes like \ref Tolerance<double>
    43   ///may offer additional tuning parameters.
    43   ///may offer additional tuning parameters.
    44   ///
    44   ///
    45   ///\sa Tolerance<float>
    45   ///\sa Tolerance<float>
    46   ///\sa Tolerance<double>
    46   ///\sa Tolerance<double>
       
    47   ///\sa Tolerance<long double>
    47   ///\sa Tolerance<int>
    48   ///\sa Tolerance<int>
    48   ///\sa Tolerance<long long int>
    49   ///\sa Tolerance<long long int>
    49 
    50 
    50   template<class T>
    51   template<class T>
    51   class Tolerance
    52   class Tolerance
    79     //   static bool finite(Value a) {}
    80     //   static bool finite(Value a) {}
    80     //   static Value big() {}
    81     //   static Value big() {}
    81     //   static Value negativeBig() {}
    82     //   static Value negativeBig() {}
    82   };
    83   };
    83 
    84 
       
    85 
       
    86   ///Float specialization of \ref Tolerance.
       
    87 
       
    88   ///Float specialization of \ref Tolerance.
       
    89   ///\sa Tolerance
       
    90   ///\relates Tolerance
       
    91   template<>
       
    92   class Tolerance<float>
       
    93   {
       
    94     static float def_epsilon;
       
    95     float _epsilon;
       
    96   public:
       
    97     ///\e
       
    98     typedef float Value;
       
    99 
       
   100     ///Constructor setting the epsilon tolerance to the default value.
       
   101     Tolerance() : _epsilon(def_epsilon) {}
       
   102     ///Constructor setting the epsilon tolerance.
       
   103     Tolerance(float e) : _epsilon(e) {}
       
   104 
       
   105     ///Return the epsilon value.
       
   106     Value epsilon() {return _epsilon;}
       
   107     ///Set the epsilon value.
       
   108     void epsilon(Value e) {_epsilon=e;}
       
   109 
       
   110     ///Return the default epsilon value.
       
   111     static Value defaultEpsilon() {return def_epsilon;}
       
   112     ///Set the default epsilon value.
       
   113     static void defaultEpsilon(Value e) {def_epsilon=e;}
       
   114 
       
   115     ///\name Comparisons
       
   116     ///See class Tolerance for more details.
       
   117 
       
   118     ///@{
       
   119 
       
   120     ///Returns \c true if \c a is \e surely strictly less than \c b
       
   121     bool less(Value a,Value b) {return a+_epsilon<b;}
       
   122     ///Returns \c true if \c a is \e surely different from \c b
       
   123     bool different(Value a,Value b) { return less(a,b)||less(b,a); }
       
   124     ///Returns \c true if \c a is \e surely positive
       
   125     bool positive(Value a) { return _epsilon<a; }
       
   126     ///Returns \c true if \c a is \e surely negative
       
   127     bool negative(Value a) { return -_epsilon>a; }
       
   128     ///Returns \c true if \c a is \e surely non-zero
       
   129     Value nonZero(Value a) { return positive(a)||negative(a); };
       
   130 
       
   131     ///@}
       
   132 
       
   133     ///Returns zero
       
   134     static Value zero() {return 0;}
       
   135   };
    84 
   136 
    85   ///Double specialization of \ref Tolerance.
   137   ///Double specialization of \ref Tolerance.
    86 
   138 
    87   ///Double specialization of \ref Tolerance.
   139   ///Double specialization of \ref Tolerance.
    88   ///\sa Tolerance
   140   ///\sa Tolerance
   131 
   183 
   132     ///Returns zero
   184     ///Returns zero
   133     static Value zero() {return 0;}
   185     static Value zero() {return 0;}
   134   };
   186   };
   135 
   187 
   136   ///Float specialization of \ref Tolerance.
   188   ///Long double specialization of \ref Tolerance.
   137 
   189 
   138   ///Float specialization of \ref Tolerance.
   190   ///Long double specialization of \ref Tolerance.
   139   ///\sa Tolerance
   191   ///\sa Tolerance
   140   ///\relates Tolerance
   192   ///\relates Tolerance
   141   template<>
   193   template<>
   142   class Tolerance<float>
   194   class Tolerance<long double>
   143   {
   195   {
   144     static float def_epsilon;
   196     static long double def_epsilon;
   145     float _epsilon;
   197     long double _epsilon;
   146   public:
   198   public:
   147     ///\e
   199     ///\e
   148     typedef float Value;
   200     typedef long double Value;
   149 
   201 
   150     ///Constructor setting the epsilon tolerance to the default value.
   202     ///Constructor setting the epsilon tolerance to the default value.
   151     Tolerance() : _epsilon(def_epsilon) {}
   203     Tolerance() : _epsilon(def_epsilon) {}
   152     ///Constructor setting the epsilon tolerance.
   204     ///Constructor setting the epsilon tolerance.
   153     Tolerance(float e) : _epsilon(e) {}
   205     Tolerance(long double e) : _epsilon(e) {}
   154 
   206 
   155     ///Return the epsilon value.
   207     ///Return the epsilon value.
   156     Value epsilon() {return _epsilon;}
   208     Value epsilon() {return _epsilon;}
   157     ///Set the epsilon value.
   209     ///Set the epsilon value.
   158     void epsilon(Value e) {_epsilon=e;}
   210     void epsilon(Value e) {_epsilon=e;}