lemon/tolerance.h
author klao
Thu, 16 Mar 2006 13:52:12 +0000
changeset 2009 de287863cc98
parent 1956 a055123339d5
child 2073 d886e4b131e6
permissions -rw-r--r--
rethrow fix
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@1835
    52
alpar@1835
    53
  template<class T>
alpar@1835
    54
  class Tolerance
alpar@1835
    55
  {
alpar@1835
    56
  public:
alpar@1835
    57
    typedef T Value;
alpar@1835
    58
alpar@1835
    59
    ///\name Comparisons
alpar@1835
    60
    ///The concept is that these bool functions return with \c true only if
alpar@1835
    61
    ///the related comparisons hold even if some numerical error appeared
alpar@1835
    62
    ///during the computations.
alpar@1835
    63
alpar@1835
    64
    ///@{
alpar@1835
    65
alpar@1835
    66
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
    67
    static bool less(Value a,Value b) {return false;}
alpar@1835
    68
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
    69
    static bool different(Value a,Value b) {return false;}
alpar@1835
    70
    ///Returns \c true if \c a is \e surely positive
alpar@1835
    71
    static bool positive(Value a) {return false;}
alpar@1835
    72
    ///Returns \c true if \c a is \e surely negative
alpar@1835
    73
    static bool negative(Value a) {return false;}
alpar@1835
    74
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
    75
    static bool nonZero(Value a) {return false;}
alpar@1835
    76
alpar@1835
    77
    ///@}
alpar@1835
    78
alpar@1835
    79
    ///Returns the zero value.
alpar@1835
    80
    static Value zero() {return T();}
alpar@1835
    81
alpar@1835
    82
    //   static bool finite(Value a) {}
alpar@1835
    83
    //   static Value big() {}
alpar@1835
    84
    //   static Value negativeBig() {}
alpar@1835
    85
  };
alpar@1835
    86
alpar@1835
    87
alpar@1897
    88
  ///Float specialization of \ref Tolerance.
alpar@1897
    89
alpar@1897
    90
  ///Float specialization of \ref Tolerance.
alpar@1897
    91
  ///\sa Tolerance
alpar@1897
    92
  ///\relates Tolerance
alpar@1897
    93
  template<>
alpar@1897
    94
  class Tolerance<float>
alpar@1897
    95
  {
alpar@1897
    96
    static float def_epsilon;
alpar@1897
    97
    float _epsilon;
alpar@1897
    98
  public:
alpar@1897
    99
    ///\e
alpar@1897
   100
    typedef float Value;
alpar@1897
   101
alpar@1897
   102
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1897
   103
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1897
   104
    ///Constructor setting the epsilon tolerance.
alpar@1897
   105
    Tolerance(float e) : _epsilon(e) {}
alpar@1897
   106
alpar@1897
   107
    ///Return the epsilon value.
alpar@1897
   108
    Value epsilon() {return _epsilon;}
alpar@1897
   109
    ///Set the epsilon value.
alpar@1897
   110
    void epsilon(Value e) {_epsilon=e;}
alpar@1897
   111
alpar@1897
   112
    ///Return the default epsilon value.
alpar@1897
   113
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1897
   114
    ///Set the default epsilon value.
alpar@1897
   115
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1897
   116
alpar@1897
   117
    ///\name Comparisons
alpar@1897
   118
    ///See class Tolerance for more details.
alpar@1897
   119
alpar@1897
   120
    ///@{
alpar@1897
   121
alpar@1897
   122
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1897
   123
    bool less(Value a,Value b) {return a+_epsilon<b;}
alpar@1897
   124
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1897
   125
    bool different(Value a,Value b) { return less(a,b)||less(b,a); }
alpar@1897
   126
    ///Returns \c true if \c a is \e surely positive
alpar@1897
   127
    bool positive(Value a) { return _epsilon<a; }
alpar@1897
   128
    ///Returns \c true if \c a is \e surely negative
alpar@1897
   129
    bool negative(Value a) { return -_epsilon>a; }
alpar@1897
   130
    ///Returns \c true if \c a is \e surely non-zero
alpar@1897
   131
    Value nonZero(Value a) { return positive(a)||negative(a); };
alpar@1897
   132
alpar@1897
   133
    ///@}
alpar@1897
   134
alpar@1897
   135
    ///Returns zero
alpar@1897
   136
    static Value zero() {return 0;}
alpar@1897
   137
  };
alpar@1897
   138
alpar@1835
   139
  ///Double specialization of \ref Tolerance.
alpar@1835
   140
alpar@1835
   141
  ///Double specialization of \ref Tolerance.
alpar@1835
   142
  ///\sa Tolerance
alpar@1835
   143
  ///\relates Tolerance
alpar@1835
   144
  template<>
alpar@1835
   145
  class Tolerance<double>
alpar@1835
   146
  {
alpar@1835
   147
    static double def_epsilon;
alpar@1835
   148
    double _epsilon;
alpar@1835
   149
  public:
alpar@1835
   150
    ///\e
alpar@1835
   151
    typedef double Value;
alpar@1835
   152
alpar@1835
   153
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1835
   154
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1835
   155
    ///Constructor setting the epsilon tolerance.
alpar@1835
   156
    Tolerance(double e) : _epsilon(e) {}
alpar@1835
   157
alpar@1835
   158
    ///Return the epsilon value.
alpar@1835
   159
    Value epsilon() {return _epsilon;}
alpar@1835
   160
    ///Set the epsilon value.
alpar@1835
   161
    void epsilon(Value e) {_epsilon=e;}
alpar@1835
   162
alpar@1835
   163
    ///Return the default epsilon value.
alpar@1835
   164
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1835
   165
    ///Set the default epsilon value.
alpar@1835
   166
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1835
   167
alpar@1835
   168
    ///\name Comparisons
alpar@1835
   169
    ///See class Tolerance for more details.
alpar@1835
   170
alpar@1835
   171
    ///@{
alpar@1835
   172
alpar@1835
   173
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   174
    bool less(Value a,Value b) {return a+_epsilon<b;}
alpar@1835
   175
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   176
    bool different(Value a,Value b) { return less(a,b)||less(b,a); }
alpar@1835
   177
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   178
    bool positive(Value a) { return _epsilon<a; }
alpar@1835
   179
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   180
    bool negative(Value a) { return -_epsilon>a; }
alpar@1835
   181
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   182
    Value nonZero(Value a) { return positive(a)||negative(a); };
alpar@1835
   183
alpar@1835
   184
    ///@}
alpar@1835
   185
alpar@1835
   186
    ///Returns zero
alpar@1835
   187
    static Value zero() {return 0;}
alpar@1835
   188
  };
alpar@1835
   189
alpar@1897
   190
  ///Long double specialization of \ref Tolerance.
alpar@1835
   191
alpar@1897
   192
  ///Long double specialization of \ref Tolerance.
alpar@1835
   193
  ///\sa Tolerance
alpar@1835
   194
  ///\relates Tolerance
alpar@1835
   195
  template<>
alpar@1897
   196
  class Tolerance<long double>
alpar@1835
   197
  {
alpar@1897
   198
    static long double def_epsilon;
alpar@1897
   199
    long double _epsilon;
alpar@1835
   200
  public:
alpar@1835
   201
    ///\e
alpar@1897
   202
    typedef long double Value;
alpar@1835
   203
alpar@1835
   204
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1835
   205
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1835
   206
    ///Constructor setting the epsilon tolerance.
alpar@1897
   207
    Tolerance(long double e) : _epsilon(e) {}
alpar@1835
   208
alpar@1835
   209
    ///Return the epsilon value.
alpar@1835
   210
    Value epsilon() {return _epsilon;}
alpar@1835
   211
    ///Set the epsilon value.
alpar@1835
   212
    void epsilon(Value e) {_epsilon=e;}
alpar@1835
   213
alpar@1835
   214
    ///Return the default epsilon value.
alpar@1835
   215
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1835
   216
    ///Set the default epsilon value.
alpar@1835
   217
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1835
   218
alpar@1835
   219
    ///\name Comparisons
alpar@1835
   220
    ///See class Tolerance for more details.
alpar@1835
   221
alpar@1835
   222
    ///@{
alpar@1835
   223
alpar@1835
   224
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   225
    bool less(Value a,Value b) {return a+_epsilon<b;}
alpar@1835
   226
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   227
    bool different(Value a,Value b) { return less(a,b)||less(b,a); }
alpar@1835
   228
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   229
    bool positive(Value a) { return _epsilon<a; }
alpar@1835
   230
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   231
    bool negative(Value a) { return -_epsilon>a; }
alpar@1835
   232
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   233
    Value nonZero(Value a) { return positive(a)||negative(a); };
alpar@1835
   234
alpar@1835
   235
    ///@}
alpar@1835
   236
alpar@1835
   237
    ///Returns zero
alpar@1835
   238
    static Value zero() {return 0;}
alpar@1835
   239
  };
alpar@1835
   240
alpar@1835
   241
  ///Integer specialization of \ref Tolerance.
alpar@1835
   242
alpar@1835
   243
  ///Integer specialization of \ref Tolerance.
alpar@1835
   244
  ///\sa Tolerance
alpar@1835
   245
  template<>
alpar@1835
   246
  class Tolerance<int>
alpar@1835
   247
  {
alpar@1835
   248
  public:
alpar@1835
   249
    ///\e
alpar@1835
   250
    typedef int Value;
alpar@1835
   251
alpar@1835
   252
    ///\name Comparisons
alpar@1835
   253
    ///See \ref Tolerance for more details.
alpar@1835
   254
alpar@1835
   255
    ///@{
alpar@1835
   256
alpar@1835
   257
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   258
    static bool less(Value a,Value b) { return a<b;}
alpar@1835
   259
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   260
    static bool different(Value a,Value b) { return a!=b; }
alpar@1835
   261
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   262
    static bool positive(Value a) { return 0<a; }
alpar@1835
   263
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   264
    static bool negative(Value a) { return 0>a; }
alpar@1835
   265
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   266
    static Value nonZero(Value a) { return a!=0;};
alpar@1835
   267
alpar@1835
   268
    ///@}
alpar@1835
   269
alpar@1835
   270
    ///Returns zero
alpar@1835
   271
    static Value zero() {return 0;}
alpar@1835
   272
  };
alpar@1835
   273
deba@1965
   274
#ifndef __STRICT_ANSI__
deba@1965
   275
alpar@1835
   276
  ///Long long integer specialization of \ref Tolerance.
alpar@1835
   277
alpar@1835
   278
  ///Long long integer specialization of \ref Tolerance.
deba@1965
   279
  ///\warning This class is not ansi compatible.
alpar@1835
   280
  ///\sa Tolerance
alpar@1835
   281
  template<>
alpar@1835
   282
  class Tolerance<long long int>
alpar@1835
   283
  {
alpar@1835
   284
  public:
alpar@1835
   285
    ///\e
alpar@1835
   286
    typedef long long int Value;
alpar@1835
   287
alpar@1835
   288
    ///\name Comparisons
alpar@1835
   289
    ///See \ref Tolerance for more details.
alpar@1835
   290
alpar@1835
   291
    ///@{
alpar@1835
   292
alpar@1835
   293
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   294
    static bool less(Value a,Value b) { return a<b;}
alpar@1835
   295
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   296
    static bool different(Value a,Value b) { return a!=b; }
alpar@1835
   297
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   298
    static bool positive(Value a) { return 0<a; }
alpar@1835
   299
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   300
    static bool negative(Value a) { return 0>a; }
alpar@1835
   301
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   302
    static Value nonZero(Value a) { return a!=0;};
alpar@1835
   303
alpar@1835
   304
    ///@}
alpar@1835
   305
alpar@1835
   306
    ///Returns zero
alpar@1835
   307
    static Value zero() {return 0;}
alpar@1835
   308
  };
alpar@1835
   309
deba@1965
   310
#endif
deba@1965
   311
alpar@1835
   312
  /// @}
alpar@1835
   313
alpar@1835
   314
} //namespace lemon
alpar@1835
   315
alpar@1835
   316
#endif //LEMON_TOLERANCE_H