COIN-OR::LEMON - Graph Library

source: lemon-1.2/lemon/tolerance.h @ 621:b536eaacb39b

Last change on this file since 621:b536eaacb39b was 517:2b6d5d22bb23, checked in by Alpar Juttner <alpar@…>, 15 years ago

Merge

File size: 7.4 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[7]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[7]4 *
[440]5 * Copyright (C) 2003-2009
[7]6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
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
28namespace lemon {
29
30  /// \addtogroup misc
31  /// @{
[209]32
[7]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.
36  ///
[49]37  ///\ref Tolerance is a class to provide a basic way to
[7]38  ///handle the comparison of numbers that are obtained
39  ///as a result of a probably inexact computation.
40  ///
[486]41  ///The general implementation is suitable only if the data type is exact,
42  ///like the integer types, otherwise a specialized version must be
43  ///implemented. These specialized classes like
[72]44  ///Tolerance<double> may offer additional tuning parameters.
[7]45  ///
46  ///\sa Tolerance<float>
47  ///\sa Tolerance<double>
48  ///\sa Tolerance<long double>
49
50  template<class T>
51  class Tolerance
52  {
53  public:
54    typedef T Value;
55
56    ///\name Comparisons
[49]57    ///The concept is that these bool functions return \c true only if
[7]58    ///the related comparisons hold even if some numerical error appeared
59    ///during the computations.
60
61    ///@{
62
63    ///Returns \c true if \c a is \e surely strictly less than \c b
[486]64    static bool less(Value a,Value b) {return a<b;}
[7]65    ///Returns \c true if \c a is \e surely different from \c b
[486]66    static bool different(Value a,Value b) {return a!=b;}
[7]67    ///Returns \c true if \c a is \e surely positive
[486]68    static bool positive(Value a) {return static_cast<Value>(0) < a;}
[7]69    ///Returns \c true if \c a is \e surely negative
[486]70    static bool negative(Value a) {return a < static_cast<Value>(0);}
[7]71    ///Returns \c true if \c a is \e surely non-zero
[486]72    static bool nonZero(Value a) {return a != static_cast<Value>(0);}
[7]73
74    ///@}
75
76    ///Returns the zero value.
[486]77    static Value zero() {return static_cast<Value>(0);}
[7]78
79    //   static bool finite(Value a) {}
80    //   static Value big() {}
81    //   static Value negativeBig() {}
82  };
83
84
[42]85  ///Float specialization of Tolerance.
[7]86
[42]87  ///Float specialization of Tolerance.
[7]88  ///\sa Tolerance
89  ///\relates Tolerance
90  template<>
91  class Tolerance<float>
92  {
93    static float def_epsilon;
94    float _epsilon;
95  public:
96    ///\e
97    typedef float Value;
98
99    ///Constructor setting the epsilon tolerance to the default value.
100    Tolerance() : _epsilon(def_epsilon) {}
[49]101    ///Constructor setting the epsilon tolerance to the given value.
[7]102    Tolerance(float e) : _epsilon(e) {}
103
[49]104    ///Returns the epsilon value.
[7]105    Value epsilon() const {return _epsilon;}
[49]106    ///Sets the epsilon value.
[7]107    void epsilon(Value e) {_epsilon=e;}
108
[49]109    ///Returns the default epsilon value.
[7]110    static Value defaultEpsilon() {return def_epsilon;}
[49]111    ///Sets the default epsilon value.
[7]112    static void defaultEpsilon(Value e) {def_epsilon=e;}
113
114    ///\name Comparisons
[72]115    ///See \ref lemon::Tolerance "Tolerance" for more details.
[7]116
117    ///@{
118
119    ///Returns \c true if \c a is \e surely strictly less than \c b
120    bool less(Value a,Value b) const {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) const { return less(a,b)||less(b,a); }
123    ///Returns \c true if \c a is \e surely positive
124    bool positive(Value a) const { return _epsilon<a; }
125    ///Returns \c true if \c a is \e surely negative
126    bool negative(Value a) const { return -_epsilon>a; }
127    ///Returns \c true if \c a is \e surely non-zero
[16]128    bool nonZero(Value a) const { return positive(a)||negative(a); }
[7]129
130    ///@}
131
132    ///Returns zero
133    static Value zero() {return 0;}
134  };
135
[42]136  ///Double specialization of Tolerance.
[7]137
[42]138  ///Double specialization of Tolerance.
[7]139  ///\sa Tolerance
140  ///\relates Tolerance
141  template<>
142  class Tolerance<double>
143  {
144    static double def_epsilon;
145    double _epsilon;
146  public:
147    ///\e
148    typedef double Value;
149
150    ///Constructor setting the epsilon tolerance to the default value.
151    Tolerance() : _epsilon(def_epsilon) {}
[49]152    ///Constructor setting the epsilon tolerance to the given value.
[7]153    Tolerance(double e) : _epsilon(e) {}
154
[49]155    ///Returns the epsilon value.
[7]156    Value epsilon() const {return _epsilon;}
[49]157    ///Sets the epsilon value.
[7]158    void epsilon(Value e) {_epsilon=e;}
159
[49]160    ///Returns the default epsilon value.
[7]161    static Value defaultEpsilon() {return def_epsilon;}
[49]162    ///Sets the default epsilon value.
[7]163    static void defaultEpsilon(Value e) {def_epsilon=e;}
164
165    ///\name Comparisons
[72]166    ///See \ref lemon::Tolerance "Tolerance" for more details.
[7]167
168    ///@{
169
170    ///Returns \c true if \c a is \e surely strictly less than \c b
171    bool less(Value a,Value b) const {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) const { return less(a,b)||less(b,a); }
174    ///Returns \c true if \c a is \e surely positive
175    bool positive(Value a) const { return _epsilon<a; }
176    ///Returns \c true if \c a is \e surely negative
177    bool negative(Value a) const { return -_epsilon>a; }
178    ///Returns \c true if \c a is \e surely non-zero
[16]179    bool nonZero(Value a) const { return positive(a)||negative(a); }
[7]180
181    ///@}
182
183    ///Returns zero
184    static Value zero() {return 0;}
185  };
186
[42]187  ///Long double specialization of Tolerance.
[7]188
[42]189  ///Long double specialization of Tolerance.
[7]190  ///\sa Tolerance
191  ///\relates Tolerance
192  template<>
193  class Tolerance<long double>
194  {
195    static long double def_epsilon;
196    long double _epsilon;
197  public:
198    ///\e
199    typedef long double Value;
200
201    ///Constructor setting the epsilon tolerance to the default value.
202    Tolerance() : _epsilon(def_epsilon) {}
[49]203    ///Constructor setting the epsilon tolerance to the given value.
[7]204    Tolerance(long double e) : _epsilon(e) {}
205
[49]206    ///Returns the epsilon value.
[7]207    Value epsilon() const {return _epsilon;}
[49]208    ///Sets the epsilon value.
[7]209    void epsilon(Value e) {_epsilon=e;}
210
[49]211    ///Returns the default epsilon value.
[7]212    static Value defaultEpsilon() {return def_epsilon;}
[49]213    ///Sets the default epsilon value.
[7]214    static void defaultEpsilon(Value e) {def_epsilon=e;}
215
216    ///\name Comparisons
[72]217    ///See \ref lemon::Tolerance "Tolerance" for more details.
[7]218
219    ///@{
220
221    ///Returns \c true if \c a is \e surely strictly less than \c b
222    bool less(Value a,Value b) const {return a+_epsilon<b;}
223    ///Returns \c true if \c a is \e surely different from \c b
224    bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
225    ///Returns \c true if \c a is \e surely positive
226    bool positive(Value a) const { return _epsilon<a; }
227    ///Returns \c true if \c a is \e surely negative
228    bool negative(Value a) const { return -_epsilon>a; }
229    ///Returns \c true if \c a is \e surely non-zero
[16]230    bool nonZero(Value a) const { return positive(a)||negative(a); }
[7]231
232    ///@}
233
234    ///Returns zero
235    static Value zero() {return 0;}
236  };
237
238  /// @}
239
240} //namespace lemon
241
242#endif //LEMON_TOLERANCE_H
Note: See TracBrowser for help on using the repository browser.