lemon/tolerance.h
author deba
Mon, 19 Dec 2005 09:47:10 +0000
changeset 1865 dcefd1d1377f
child 1875 98698b69a902
permissions -rw-r--r--
Doc fix
alpar@1835
     1
/* -*- C++ -*-
alpar@1835
     2
 * lemon/tolerance.h - Part of LEMON, a generic C++ optimization library
alpar@1835
     3
 *
alpar@1835
     4
 * Copyright (C) 2005 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@1835
    47
  ///\sa Tolerance<int>
alpar@1835
    48
  ///\sa Tolerance<long long int>
alpar@1835
    49
alpar@1835
    50
  template<class T>
alpar@1835
    51
  class Tolerance
alpar@1835
    52
  {
alpar@1835
    53
  public:
alpar@1835
    54
    typedef T Value;
alpar@1835
    55
alpar@1835
    56
    ///\name Comparisons
alpar@1835
    57
    ///The concept is that these bool functions return with \c true only if
alpar@1835
    58
    ///the related comparisons hold even if some numerical error appeared
alpar@1835
    59
    ///during the computations.
alpar@1835
    60
alpar@1835
    61
    ///@{
alpar@1835
    62
alpar@1835
    63
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
    64
    static bool less(Value a,Value b) {return false;}
alpar@1835
    65
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
    66
    static bool different(Value a,Value b) {return false;}
alpar@1835
    67
    ///Returns \c true if \c a is \e surely positive
alpar@1835
    68
    static bool positive(Value a) {return false;}
alpar@1835
    69
    ///Returns \c true if \c a is \e surely negative
alpar@1835
    70
    static bool negative(Value a) {return false;}
alpar@1835
    71
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
    72
    static bool nonZero(Value a) {return false;}
alpar@1835
    73
alpar@1835
    74
    ///@}
alpar@1835
    75
alpar@1835
    76
    ///Returns the zero value.
alpar@1835
    77
    static Value zero() {return T();}
alpar@1835
    78
alpar@1835
    79
    //   static bool finite(Value a) {}
alpar@1835
    80
    //   static Value big() {}
alpar@1835
    81
    //   static Value negativeBig() {}
alpar@1835
    82
  };
alpar@1835
    83
alpar@1835
    84
alpar@1835
    85
  ///Double specialization of \ref Tolerance.
alpar@1835
    86
alpar@1835
    87
  ///Double specialization of \ref Tolerance.
alpar@1835
    88
  ///\sa Tolerance
alpar@1835
    89
  ///\relates Tolerance
alpar@1835
    90
  template<>
alpar@1835
    91
  class Tolerance<double>
alpar@1835
    92
  {
alpar@1835
    93
    static double def_epsilon;
alpar@1835
    94
    double _epsilon;
alpar@1835
    95
  public:
alpar@1835
    96
    ///\e
alpar@1835
    97
    typedef double Value;
alpar@1835
    98
alpar@1835
    99
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1835
   100
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1835
   101
    ///Constructor setting the epsilon tolerance.
alpar@1835
   102
    Tolerance(double e) : _epsilon(e) {}
alpar@1835
   103
alpar@1835
   104
    ///Return the epsilon value.
alpar@1835
   105
    Value epsilon() {return _epsilon;}
alpar@1835
   106
    ///Set the epsilon value.
alpar@1835
   107
    void epsilon(Value e) {_epsilon=e;}
alpar@1835
   108
alpar@1835
   109
    ///Return the default epsilon value.
alpar@1835
   110
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1835
   111
    ///Set the default epsilon value.
alpar@1835
   112
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1835
   113
alpar@1835
   114
    ///\name Comparisons
alpar@1835
   115
    ///See class Tolerance for more details.
alpar@1835
   116
alpar@1835
   117
    ///@{
alpar@1835
   118
alpar@1835
   119
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   120
    bool less(Value a,Value b) {return a+_epsilon<b;}
alpar@1835
   121
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   122
    bool different(Value a,Value b) { return less(a,b)||less(b,a); }
alpar@1835
   123
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   124
    bool positive(Value a) { return _epsilon<a; }
alpar@1835
   125
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   126
    bool negative(Value a) { return -_epsilon>a; }
alpar@1835
   127
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   128
    Value nonZero(Value a) { return positive(a)||negative(a); };
alpar@1835
   129
alpar@1835
   130
    ///@}
alpar@1835
   131
alpar@1835
   132
    ///Returns zero
alpar@1835
   133
    static Value zero() {return 0;}
alpar@1835
   134
  };
alpar@1835
   135
alpar@1835
   136
  ///Float specialization of \ref Tolerance.
alpar@1835
   137
alpar@1835
   138
  ///Float specialization of \ref Tolerance.
alpar@1835
   139
  ///\sa Tolerance
alpar@1835
   140
  ///\relates Tolerance
alpar@1835
   141
  template<>
alpar@1835
   142
  class Tolerance<float>
alpar@1835
   143
  {
alpar@1835
   144
    static float def_epsilon;
alpar@1835
   145
    float _epsilon;
alpar@1835
   146
  public:
alpar@1835
   147
    ///\e
alpar@1835
   148
    typedef float Value;
alpar@1835
   149
alpar@1835
   150
    ///Constructor setting the epsilon tolerance to the default value.
alpar@1835
   151
    Tolerance() : _epsilon(def_epsilon) {}
alpar@1835
   152
    ///Constructor setting the epsilon tolerance.
alpar@1835
   153
    Tolerance(float e) : _epsilon(e) {}
alpar@1835
   154
alpar@1835
   155
    ///Return the epsilon value.
alpar@1835
   156
    Value epsilon() {return _epsilon;}
alpar@1835
   157
    ///Set the epsilon value.
alpar@1835
   158
    void epsilon(Value e) {_epsilon=e;}
alpar@1835
   159
alpar@1835
   160
    ///Return the default epsilon value.
alpar@1835
   161
    static Value defaultEpsilon() {return def_epsilon;}
alpar@1835
   162
    ///Set the default epsilon value.
alpar@1835
   163
    static void defaultEpsilon(Value e) {def_epsilon=e;}
alpar@1835
   164
alpar@1835
   165
    ///\name Comparisons
alpar@1835
   166
    ///See class Tolerance for more details.
alpar@1835
   167
alpar@1835
   168
    ///@{
alpar@1835
   169
alpar@1835
   170
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   171
    bool less(Value a,Value b) {return a+_epsilon<b;}
alpar@1835
   172
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   173
    bool different(Value a,Value b) { return less(a,b)||less(b,a); }
alpar@1835
   174
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   175
    bool positive(Value a) { return _epsilon<a; }
alpar@1835
   176
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   177
    bool negative(Value a) { return -_epsilon>a; }
alpar@1835
   178
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   179
    Value nonZero(Value a) { return positive(a)||negative(a); };
alpar@1835
   180
alpar@1835
   181
    ///@}
alpar@1835
   182
alpar@1835
   183
    ///Returns zero
alpar@1835
   184
    static Value zero() {return 0;}
alpar@1835
   185
  };
alpar@1835
   186
alpar@1835
   187
  ///Integer specialization of \ref Tolerance.
alpar@1835
   188
alpar@1835
   189
  ///Integer specialization of \ref Tolerance.
alpar@1835
   190
  ///\sa Tolerance
alpar@1835
   191
  template<>
alpar@1835
   192
  class Tolerance<int>
alpar@1835
   193
  {
alpar@1835
   194
  public:
alpar@1835
   195
    ///\e
alpar@1835
   196
    typedef int Value;
alpar@1835
   197
alpar@1835
   198
    ///\name Comparisons
alpar@1835
   199
    ///See \ref Tolerance for more details.
alpar@1835
   200
alpar@1835
   201
    ///@{
alpar@1835
   202
alpar@1835
   203
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   204
    static bool less(Value a,Value b) { return a<b;}
alpar@1835
   205
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   206
    static bool different(Value a,Value b) { return a!=b; }
alpar@1835
   207
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   208
    static bool positive(Value a) { return 0<a; }
alpar@1835
   209
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   210
    static bool negative(Value a) { return 0>a; }
alpar@1835
   211
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   212
    static Value nonZero(Value a) { return a!=0;};
alpar@1835
   213
alpar@1835
   214
    ///@}
alpar@1835
   215
alpar@1835
   216
    ///Returns zero
alpar@1835
   217
    static Value zero() {return 0;}
alpar@1835
   218
  };
alpar@1835
   219
alpar@1835
   220
  ///Long long integer specialization of \ref Tolerance.
alpar@1835
   221
alpar@1835
   222
  ///Long long integer specialization of \ref Tolerance.
alpar@1835
   223
  ///\sa Tolerance
alpar@1835
   224
  template<>
alpar@1835
   225
  class Tolerance<long long int>
alpar@1835
   226
  {
alpar@1835
   227
  public:
alpar@1835
   228
    ///\e
alpar@1835
   229
    typedef long long int Value;
alpar@1835
   230
alpar@1835
   231
    ///\name Comparisons
alpar@1835
   232
    ///See \ref Tolerance for more details.
alpar@1835
   233
alpar@1835
   234
    ///@{
alpar@1835
   235
alpar@1835
   236
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   237
    static bool less(Value a,Value b) { return a<b;}
alpar@1835
   238
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   239
    static bool different(Value a,Value b) { return a!=b; }
alpar@1835
   240
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   241
    static bool positive(Value a) { return 0<a; }
alpar@1835
   242
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   243
    static bool negative(Value a) { return 0>a; }
alpar@1835
   244
    ///Returns \c true if \c a is \e surely non-zero
alpar@1835
   245
    static Value nonZero(Value a) { return a!=0;};
alpar@1835
   246
alpar@1835
   247
    ///@}
alpar@1835
   248
alpar@1835
   249
    ///Returns zero
alpar@1835
   250
    static Value zero() {return 0;}
alpar@1835
   251
  };
alpar@1835
   252
alpar@1835
   253
  /// @}
alpar@1835
   254
alpar@1835
   255
} //namespace lemon
alpar@1835
   256
alpar@1835
   257
#endif //LEMON_TOLERANCE_H