lemon/tolerance.h
author deba
Tue, 25 Mar 2008 16:28:06 +0000
changeset 2598 71f4bd3a9ae8
parent 2413 21eb3ccdc3df
permissions -rw-r--r--
Minor bug 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@2553
     5
 * Copyright (C) 2003-2008
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
  };
deba@2413
   308
  
deba@2413
   309
deba@2413
   310
  ///Long integer specialization of \ref Tolerance.
deba@2413
   311
deba@2413
   312
  ///Long integer specialization of \ref Tolerance.
deba@2413
   313
  ///\sa Tolerance
deba@2413
   314
  template<>
deba@2413
   315
  class Tolerance<long int>
deba@2413
   316
  {
deba@2413
   317
  public:
deba@2413
   318
    ///\e
deba@2413
   319
    typedef long int Value;
deba@2413
   320
deba@2413
   321
    ///\name Comparisons
deba@2413
   322
    ///See \ref Tolerance for more details.
deba@2413
   323
deba@2413
   324
    ///@{
deba@2413
   325
deba@2413
   326
    ///Returns \c true if \c a is \e surely strictly less than \c b
deba@2413
   327
    static bool less(Value a,Value b) { return a<b;}
deba@2413
   328
    ///Returns \c true if \c a is \e surely different from \c b
deba@2413
   329
    static bool different(Value a,Value b) { return a!=b; }
deba@2413
   330
    ///Returns \c true if \c a is \e surely positive
deba@2413
   331
    static bool positive(Value a) { return 0<a; }
deba@2413
   332
    ///Returns \c true if \c a is \e surely negative
deba@2413
   333
    static bool negative(Value a) { return 0>a; }
deba@2413
   334
    ///Returns \c true if \c a is \e surely non-zero
deba@2413
   335
    static bool nonZero(Value a) { return a!=0;};
deba@2413
   336
deba@2413
   337
    ///@}
deba@2413
   338
deba@2413
   339
    ///Returns zero
deba@2413
   340
    static Value zero() {return 0;}
deba@2413
   341
  };
deba@2413
   342
deba@2413
   343
  ///Unsigned long integer specialization of \ref Tolerance.
deba@2413
   344
deba@2413
   345
  ///Unsigned long integer specialization of \ref Tolerance.
deba@2413
   346
  ///\sa Tolerance
deba@2413
   347
  template<>
deba@2413
   348
  class Tolerance<unsigned long int>
deba@2413
   349
  {
deba@2413
   350
  public:
deba@2413
   351
    ///\e
deba@2413
   352
    typedef unsigned long int Value;
deba@2413
   353
deba@2413
   354
    ///\name Comparisons
deba@2413
   355
    ///See \ref Tolerance for more details.
deba@2413
   356
deba@2413
   357
    ///@{
deba@2413
   358
deba@2413
   359
    ///Returns \c true if \c a is \e surely strictly less than \c b
deba@2413
   360
    static bool less(Value a,Value b) { return a<b;}
deba@2413
   361
    ///Returns \c true if \c a is \e surely different from \c b
deba@2413
   362
    static bool different(Value a,Value b) { return a!=b; }
deba@2413
   363
    ///Returns \c true if \c a is \e surely positive
deba@2413
   364
    static bool positive(Value a) { return 0<a; }
deba@2413
   365
    ///Returns \c true if \c a is \e surely negative
deba@2413
   366
    static bool negative(Value) { return false; }
deba@2413
   367
    ///Returns \c true if \c a is \e surely non-zero
deba@2413
   368
    static bool nonZero(Value a) { return a!=0;};
deba@2413
   369
deba@2413
   370
    ///@}
deba@2413
   371
deba@2413
   372
    ///Returns zero
deba@2413
   373
    static Value zero() {return 0;}
deba@2413
   374
  };
alpar@2154
   375
deba@2164
   376
#if defined __GNUC__ && !defined __STRICT_ANSI__
deba@1965
   377
alpar@1835
   378
  ///Long long integer specialization of \ref Tolerance.
alpar@1835
   379
alpar@1835
   380
  ///Long long integer specialization of \ref Tolerance.
alpar@2154
   381
  ///\warning This class (more exactly, type <tt>long long</tt>)
alpar@2154
   382
  ///is not ansi compatible.
alpar@1835
   383
  ///\sa Tolerance
alpar@1835
   384
  template<>
alpar@1835
   385
  class Tolerance<long long int>
alpar@1835
   386
  {
alpar@1835
   387
  public:
alpar@1835
   388
    ///\e
alpar@1835
   389
    typedef long long int Value;
alpar@1835
   390
alpar@1835
   391
    ///\name Comparisons
alpar@1835
   392
    ///See \ref Tolerance for more details.
alpar@1835
   393
alpar@1835
   394
    ///@{
alpar@1835
   395
alpar@1835
   396
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@1835
   397
    static bool less(Value a,Value b) { return a<b;}
alpar@1835
   398
    ///Returns \c true if \c a is \e surely different from \c b
alpar@1835
   399
    static bool different(Value a,Value b) { return a!=b; }
alpar@1835
   400
    ///Returns \c true if \c a is \e surely positive
alpar@1835
   401
    static bool positive(Value a) { return 0<a; }
alpar@1835
   402
    ///Returns \c true if \c a is \e surely negative
alpar@1835
   403
    static bool negative(Value a) { return 0>a; }
alpar@1835
   404
    ///Returns \c true if \c a is \e surely non-zero
alpar@2073
   405
    static bool nonZero(Value a) { return a!=0;};
alpar@1835
   406
alpar@1835
   407
    ///@}
alpar@1835
   408
alpar@1835
   409
    ///Returns zero
alpar@1835
   410
    static Value zero() {return 0;}
alpar@1835
   411
  };
alpar@1835
   412
alpar@2154
   413
  ///Unsigned long long integer specialization of \ref Tolerance.
alpar@2154
   414
alpar@2154
   415
  ///Unsigned long long integer specialization of \ref Tolerance.
alpar@2154
   416
  ///\warning This class (more exactly, type <tt>unsigned long long</tt>)
alpar@2154
   417
  ///is not ansi compatible.
alpar@2154
   418
  ///\sa Tolerance
alpar@2154
   419
  template<>
alpar@2154
   420
  class Tolerance<unsigned long long int>
alpar@2154
   421
  {
alpar@2154
   422
  public:
alpar@2154
   423
    ///\e
alpar@2154
   424
    typedef unsigned long long int Value;
alpar@2154
   425
alpar@2154
   426
    ///\name Comparisons
alpar@2154
   427
    ///See \ref Tolerance for more details.
alpar@2154
   428
alpar@2154
   429
    ///@{
alpar@2154
   430
alpar@2154
   431
    ///Returns \c true if \c a is \e surely strictly less than \c b
alpar@2154
   432
    static bool less(Value a,Value b) { return a<b;}
alpar@2154
   433
    ///Returns \c true if \c a is \e surely different from \c b
alpar@2154
   434
    static bool different(Value a,Value b) { return a!=b; }
alpar@2154
   435
    ///Returns \c true if \c a is \e surely positive
alpar@2154
   436
    static bool positive(Value a) { return 0<a; }
alpar@2154
   437
    ///Returns \c true if \c a is \e surely negative
alpar@2154
   438
    static bool negative(Value) { return false; }
alpar@2154
   439
    ///Returns \c true if \c a is \e surely non-zero
alpar@2154
   440
    static bool nonZero(Value a) { return a!=0;};
alpar@2154
   441
alpar@2154
   442
    ///@}
alpar@2154
   443
alpar@2154
   444
    ///Returns zero
alpar@2154
   445
    static Value zero() {return 0;}
alpar@2154
   446
  };
alpar@2154
   447
deba@1965
   448
#endif
deba@1965
   449
alpar@1835
   450
  /// @}
alpar@1835
   451
alpar@1835
   452
} //namespace lemon
alpar@1835
   453
alpar@1835
   454
#endif //LEMON_TOLERANCE_H