lemon/tolerance.h
author Balazs Dezso <deba@inf.elte.hu>
Fri, 11 Jul 2008 15:01:49 +0200
changeset 203 215bfc30b14f
parent 49 9a556af88710
child 209 765619b7cbb2
permissions -rw-r--r--
Cleaning of heap test and bug fix in heap concept check (ticket #100)

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