lemon/tolerance.h
author alpar
Mon, 04 Sep 2006 19:12:44 +0000
changeset 2194 eaf16c8f6fef
parent 2154 bbc79b698f62
child 2391 14a343be7a5a
permissions -rw-r--r--
'make doc' is now working also in case of external build.
alpar@1835
     1
/* -*- C++ -*-
alpar@1835
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1835
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1835
     8
 *
alpar@1835
     9
 * Permission to use, modify and distribute this software is granted
alpar@1835
    10
 * provided that this copyright notice appears in all copies. For
alpar@1835
    11
 * precise terms see the accompanying LICENSE file.
alpar@1835
    12
 *
alpar@1835
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1835
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1835
    15
 * purpose.
alpar@1835
    16
 *
alpar@1835
    17
 */
alpar@1835
    18
alpar@1835
    19
#ifndef LEMON_TOLERANCE_H
alpar@1835
    20
#define LEMON_TOLERANCE_H
alpar@1835
    21
alpar@1835
    22
///\ingroup misc
alpar@1835
    23
///\file
alpar@1835
    24
///\brief A basic tool to handle the anomalies of calculation with
alpar@1835
    25
///floating point numbers.
alpar@1835
    26
///
alpar@1835
    27
///\todo It should be in a module like "Basic tools"
alpar@1835
    28
alpar@1835
    29
alpar@1835
    30
namespace lemon {
alpar@1835
    31
alpar@1835
    32
  /// \addtogroup misc
alpar@1835
    33
  /// @{
alpar@1835
    34
  
alpar@1835
    35
  ///\brief A class to provide a basic way to
alpar@1835
    36
  ///handle the comparison of numbers that are obtained
alpar@1835
    37
  ///as a result of a probably inexact computation.
alpar@1835
    38
  ///
alpar@1835
    39
  ///Tolerance is a class to provide a basic way to
alpar@1835
    40
  ///handle the comparison of numbers that are obtained
alpar@1835
    41
  ///as a result of a probably inexact computation.
alpar@1835
    42
  ///
alpar@1835
    43
  ///This is an abstract class, it should be specialized for all numerical
alpar@1953
    44
  ///data types. These specialized classes like \ref Tolerance\<double\>
alpar@1835
    45
  ///may offer additional tuning parameters.
alpar@1835
    46
  ///
alpar@1835
    47
  ///\sa Tolerance<float>
alpar@1835
    48
  ///\sa Tolerance<double>
alpar@1897
    49
  ///\sa Tolerance<long double>
alpar@1835
    50
  ///\sa Tolerance<int>
alpar@1835
    51
  ///\sa Tolerance<long long int>
alpar@2154
    52
  ///\sa Tolerance<unsigned int>
alpar@2154
    53
  ///\sa Tolerance<unsigned long long int>
alpar@1835
    54
alpar@1835
    55
  template<class T>
alpar@1835
    56
  class Tolerance
alpar@1835
    57
  {
alpar@1835
    58
  public:
alpar@1835
    59
    typedef T Value;
alpar@1835
    60
alpar@1835
    61
    ///\name Comparisons
alpar@1835
    62
    ///The concept is that these bool functions return with \c true only if
alpar@1835
    63
    ///the related comparisons hold even if some numerical error appeared
alpar@1835
    64
    ///during the computations.
alpar@1835
    65
alpar@1835
    66
    ///@{
alpar@1835
    67
alpar@1835
    68
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
    69
    static bool less(Value a,Value b) {return false;}
alpar@1835
    70
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
    71
    static bool different(Value a,Value b) {return false;}
alpar@1835
    72
    ///Returns \c true if \c a is \e surely positive
alpar@1835
    73
    static bool positive(Value a) {return false;}
alpar@1835
    74
    ///Returns \c true if \c a is \e surely negative
alpar@1835
    75
    static bool negative(Value a) {return false;}
alpar@1835
    76
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
    77
    static bool nonZero(Value a) {return false;}
alpar@1835
    78
alpar@1835
    79
    ///@}
alpar@1835
    80
alpar@1835
    81
    ///Returns the zero value.
alpar@1835
    82
    static Value zero() {return T();}
alpar@1835
    83
alpar@1835
    84
    //   static bool finite(Value a) {}
alpar@1835
    85
    //   static Value big() {}
alpar@1835
    86
    //   static Value negativeBig() {}
alpar@1835
    87
  };
alpar@1835
    88
alpar@1835
    89
alpar@1897
    90
  ///Float specialization of \ref Tolerance.
alpar@1897
    91
alpar@1897
    92
  ///Float specialization of \ref Tolerance.
alpar@1897
    93
  ///\sa Tolerance
alpar@1897
    94
  ///\relates Tolerance
alpar@1897
    95
  template<>
alpar@1897
    96
  class Tolerance<float>
alpar@1897
    97
  {
alpar@1897
    98
    static float def_epsilon;
alpar@1897
    99
    float _epsilon;
alpar@1897
   100
  public:
alpar@1897
   101
    ///\e
alpar@1897
   102
    typedef float Value;
alpar@1897
   103
alpar@1897
   104
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1897
   105
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1897
   106
    ///Constructor setting the epsilon tolerance.
alpar@1897
   107
    Tolerance(float e) : _epsilon(e) {}
alpar@1897
   108
alpar@1897
   109
    ///Return the epsilon value.
alpar@2073
   110
    Value epsilon() const {return _epsilon;}
alpar@1897
   111
    ///Set the epsilon value.
alpar@1897
   112
    void epsilon(Value e) {_epsilon=e;}
alpar@1897
   113
alpar@1897
   114
    ///Return the default epsilon value.
alpar@1897
   115
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1897
   116
    ///Set the default epsilon value.
alpar@1897
   117
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1897
   118
alpar@1897
   119
    ///\name Comparisons
alpar@1897
   120
    ///See class Tolerance for more details.
alpar@1897
   121
alpar@1897
   122
    ///@{
alpar@1897
   123
alpar@1897
   124
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@2073
   125
    bool less(Value a,Value b) const {return a+_epsilon<b;}
alpar@1897
   126
    ///Returns \c true if \c a is \e surely different from \c b
alpar@2073
   127
    bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
alpar@1897
   128
    ///Returns \c true if \c a is \e surely positive
alpar@2073
   129
    bool positive(Value a) const { return _epsilon<a; }
alpar@1897
   130
    ///Returns \c true if \c a is \e surely negative
alpar@2073
   131
    bool negative(Value a) const { return -_epsilon>a; }
alpar@1897
   132
    ///Returns \c true if \c a is \e surely non-zero
alpar@2073
   133
    bool nonZero(Value a) const { return positive(a)||negative(a); };
alpar@1897
   134
alpar@1897
   135
    ///@}
alpar@1897
   136
alpar@1897
   137
    ///Returns zero
alpar@1897
   138
    static Value zero() {return 0;}
alpar@1897
   139
  };
alpar@1897
   140
alpar@1835
   141
  ///Double specialization of \ref Tolerance.
alpar@1835
   142
alpar@1835
   143
  ///Double specialization of \ref Tolerance.
alpar@1835
   144
  ///\sa Tolerance
alpar@1835
   145
  ///\relates Tolerance
alpar@1835
   146
  template<>
alpar@1835
   147
  class Tolerance<double>
alpar@1835
   148
  {
alpar@1835
   149
    static double def_epsilon;
alpar@1835
   150
    double _epsilon;
alpar@1835
   151
  public:
alpar@1835
   152
    ///\e
alpar@1835
   153
    typedef double Value;
alpar@1835
   154
alpar@1835
   155
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1835
   156
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1835
   157
    ///Constructor setting the epsilon tolerance.
alpar@1835
   158
    Tolerance(double e) : _epsilon(e) {}
alpar@1835
   159
alpar@1835
   160
    ///Return the epsilon value.
alpar@2073
   161
    Value epsilon() const {return _epsilon;}
alpar@1835
   162
    ///Set the epsilon value.
alpar@1835
   163
    void epsilon(Value e) {_epsilon=e;}
alpar@1835
   164
alpar@1835
   165
    ///Return the default epsilon value.
alpar@1835
   166
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1835
   167
    ///Set the default epsilon value.
alpar@1835
   168
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1835
   169
alpar@1835
   170
    ///\name Comparisons
alpar@1835
   171
    ///See class Tolerance for more details.
alpar@1835
   172
alpar@1835
   173
    ///@{
alpar@1835
   174
alpar@1835
   175
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@2073
   176
    bool less(Value a,Value b) const {return a+_epsilon<b;}
alpar@1835
   177
    ///Returns \c true if \c a is \e surely different from \c b
alpar@2073
   178
    bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
alpar@1835
   179
    ///Returns \c true if \c a is \e surely positive
alpar@2073
   180
    bool positive(Value a) const { return _epsilon<a; }
alpar@1835
   181
    ///Returns \c true if \c a is \e surely negative
alpar@2073
   182
    bool negative(Value a) const { return -_epsilon>a; }
alpar@1835
   183
    ///Returns \c true if \c a is \e surely non-zero
alpar@2073
   184
    bool nonZero(Value a) const { return positive(a)||negative(a); };
alpar@1835
   185
alpar@1835
   186
    ///@}
alpar@1835
   187
alpar@1835
   188
    ///Returns zero
alpar@1835
   189
    static Value zero() {return 0;}
alpar@1835
   190
  };
alpar@1835
   191
alpar@1897
   192
  ///Long double specialization of \ref Tolerance.
alpar@1835
   193
alpar@1897
   194
  ///Long double specialization of \ref Tolerance.
alpar@1835
   195
  ///\sa Tolerance
alpar@1835
   196
  ///\relates Tolerance
alpar@1835
   197
  template<>
alpar@1897
   198
  class Tolerance<long double>
alpar@1835
   199
  {
alpar@1897
   200
    static long double def_epsilon;
alpar@1897
   201
    long double _epsilon;
alpar@1835
   202
  public:
alpar@1835
   203
    ///\e
alpar@1897
   204
    typedef long double Value;
alpar@1835
   205
alpar@1835
   206
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1835
   207
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1835
   208
    ///Constructor setting the epsilon tolerance.
alpar@1897
   209
    Tolerance(long double e) : _epsilon(e) {}
alpar@1835
   210
alpar@1835
   211
    ///Return the epsilon value.
alpar@2073
   212
    Value epsilon() const {return _epsilon;}
alpar@1835
   213
    ///Set the epsilon value.
alpar@1835
   214
    void epsilon(Value e) {_epsilon=e;}
alpar@1835
   215
alpar@1835
   216
    ///Return the default epsilon value.
alpar@1835
   217
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1835
   218
    ///Set the default epsilon value.
alpar@1835
   219
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1835
   220
alpar@1835
   221
    ///\name Comparisons
alpar@1835
   222
    ///See class Tolerance for more details.
alpar@1835
   223
alpar@1835
   224
    ///@{
alpar@1835
   225
alpar@1835
   226
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@2073
   227
    bool less(Value a,Value b) const {return a+_epsilon<b;}
alpar@1835
   228
    ///Returns \c true if \c a is \e surely different from \c b
alpar@2073
   229
    bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
alpar@1835
   230
    ///Returns \c true if \c a is \e surely positive
alpar@2073
   231
    bool positive(Value a) const { return _epsilon<a; }
alpar@1835
   232
    ///Returns \c true if \c a is \e surely negative
alpar@2073
   233
    bool negative(Value a) const { return -_epsilon>a; }
alpar@1835
   234
    ///Returns \c true if \c a is \e surely non-zero
alpar@2073
   235
    bool nonZero(Value a) const { return positive(a)||negative(a); };
alpar@1835
   236
alpar@1835
   237
    ///@}
alpar@1835
   238
alpar@1835
   239
    ///Returns zero
alpar@1835
   240
    static Value zero() {return 0;}
alpar@1835
   241
  };
alpar@1835
   242
alpar@1835
   243
  ///Integer specialization of \ref Tolerance.
alpar@1835
   244
alpar@1835
   245
  ///Integer specialization of \ref Tolerance.
alpar@1835
   246
  ///\sa Tolerance
alpar@1835
   247
  template<>
alpar@1835
   248
  class Tolerance<int>
alpar@1835
   249
  {
alpar@1835
   250
  public:
alpar@1835
   251
    ///\e
alpar@1835
   252
    typedef int Value;
alpar@1835
   253
alpar@1835
   254
    ///\name Comparisons
alpar@1835
   255
    ///See \ref Tolerance for more details.
alpar@1835
   256
alpar@1835
   257
    ///@{
alpar@1835
   258
alpar@1835
   259
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   260
    static bool less(Value a,Value b) { return a<b;}
alpar@1835
   261
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   262
    static bool different(Value a,Value b) { return a!=b; }
alpar@1835
   263
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   264
    static bool positive(Value a) { return 0<a; }
alpar@1835
   265
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   266
    static bool negative(Value a) { return 0>a; }
alpar@1835
   267
    ///Returns \c true if \c a is \e surely non-zero
alpar@2073
   268
    static bool nonZero(Value a) { return a!=0; };
alpar@1835
   269
alpar@1835
   270
    ///@}
alpar@1835
   271
alpar@1835
   272
    ///Returns zero
alpar@1835
   273
    static Value zero() {return 0;}
alpar@1835
   274
  };
alpar@1835
   275
alpar@2154
   276
  ///Unsigned integer specialization of \ref Tolerance.
alpar@2154
   277
alpar@2154
   278
  ///Unsigned integer specialization of \ref Tolerance.
alpar@2154
   279
  ///\sa Tolerance
alpar@2154
   280
  template<>
alpar@2154
   281
  class Tolerance<unsigned int>
alpar@2154
   282
  {
alpar@2154
   283
  public:
alpar@2154
   284
    ///\e
alpar@2154
   285
    typedef unsigned int Value;
alpar@2154
   286
alpar@2154
   287
    ///\name Comparisons
alpar@2154
   288
    ///See \ref Tolerance for more details.
alpar@2154
   289
alpar@2154
   290
    ///@{
alpar@2154
   291
alpar@2154
   292
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@2154
   293
    static bool less(Value a,Value b) { return a<b;}
alpar@2154
   294
    ///Returns \c true if \c a is \e surely different from \c b
alpar@2154
   295
    static bool different(Value a,Value b) { return a!=b; }
alpar@2154
   296
    ///Returns \c true if \c a is \e surely positive
alpar@2154
   297
    static bool positive(Value a) { return 0<a; }
alpar@2154
   298
    ///Returns \c true if \c a is \e surely negative
alpar@2154
   299
    static bool negative(Value) { return false; }
alpar@2154
   300
    ///Returns \c true if \c a is \e surely non-zero
alpar@2154
   301
    static bool nonZero(Value a) { return a!=0; };
alpar@2154
   302
alpar@2154
   303
    ///@}
alpar@2154
   304
alpar@2154
   305
    ///Returns zero
alpar@2154
   306
    static Value zero() {return 0;}
alpar@2154
   307
  };
alpar@2154
   308
deba@2164
   309
#if defined __GNUC__ && !defined __STRICT_ANSI__
deba@1965
   310
alpar@1835
   311
  ///Long long integer specialization of \ref Tolerance.
alpar@1835
   312
alpar@1835
   313
  ///Long long integer specialization of \ref Tolerance.
alpar@2154
   314
  ///\warning This class (more exactly, type <tt>long long</tt>)
alpar@2154
   315
  ///is not ansi compatible.
alpar@1835
   316
  ///\sa Tolerance
alpar@1835
   317
  template<>
alpar@1835
   318
  class Tolerance<long long int>
alpar@1835
   319
  {
alpar@1835
   320
  public:
alpar@1835
   321
    ///\e
alpar@1835
   322
    typedef long long int Value;
alpar@1835
   323
alpar@1835
   324
    ///\name Comparisons
alpar@1835
   325
    ///See \ref Tolerance for more details.
alpar@1835
   326
alpar@1835
   327
    ///@{
alpar@1835
   328
alpar@1835
   329
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   330
    static bool less(Value a,Value b) { return a<b;}
alpar@1835
   331
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   332
    static bool different(Value a,Value b) { return a!=b; }
alpar@1835
   333
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   334
    static bool positive(Value a) { return 0<a; }
alpar@1835
   335
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   336
    static bool negative(Value a) { return 0>a; }
alpar@1835
   337
    ///Returns \c true if \c a is \e surely non-zero
alpar@2073
   338
    static bool nonZero(Value a) { return a!=0;};
alpar@1835
   339
alpar@1835
   340
    ///@}
alpar@1835
   341
alpar@1835
   342
    ///Returns zero
alpar@1835
   343
    static Value zero() {return 0;}
alpar@1835
   344
  };
alpar@1835
   345
alpar@2154
   346
  ///Unsigned long long integer specialization of \ref Tolerance.
alpar@2154
   347
alpar@2154
   348
  ///Unsigned long long integer specialization of \ref Tolerance.
alpar@2154
   349
  ///\warning This class (more exactly, type <tt>unsigned long long</tt>)
alpar@2154
   350
  ///is not ansi compatible.
alpar@2154
   351
  ///\sa Tolerance
alpar@2154
   352
  template<>
alpar@2154
   353
  class Tolerance<unsigned long long int>
alpar@2154
   354
  {
alpar@2154
   355
  public:
alpar@2154
   356
    ///\e
alpar@2154
   357
    typedef unsigned long long int Value;
alpar@2154
   358
alpar@2154
   359
    ///\name Comparisons
alpar@2154
   360
    ///See \ref Tolerance for more details.
alpar@2154
   361
alpar@2154
   362
    ///@{
alpar@2154
   363
alpar@2154
   364
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@2154
   365
    static bool less(Value a,Value b) { return a<b;}
alpar@2154
   366
    ///Returns \c true if \c a is \e surely different from \c b
alpar@2154
   367
    static bool different(Value a,Value b) { return a!=b; }
alpar@2154
   368
    ///Returns \c true if \c a is \e surely positive
alpar@2154
   369
    static bool positive(Value a) { return 0<a; }
alpar@2154
   370
    ///Returns \c true if \c a is \e surely negative
alpar@2154
   371
    static bool negative(Value) { return false; }
alpar@2154
   372
    ///Returns \c true if \c a is \e surely non-zero
alpar@2154
   373
    static bool nonZero(Value a) { return a!=0;};
alpar@2154
   374
alpar@2154
   375
    ///@}
alpar@2154
   376
alpar@2154
   377
    ///Returns zero
alpar@2154
   378
    static Value zero() {return 0;}
alpar@2154
   379
  };
alpar@2154
   380
deba@1965
   381
#endif
deba@1965
   382
alpar@1835
   383
  /// @}
alpar@1835
   384
alpar@1835
   385
} //namespace lemon
alpar@1835
   386
alpar@1835
   387
#endif //LEMON_TOLERANCE_H