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