- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/Bcps-0.91.0/CoinUtils/src/CoinFloatEqual.hpp

Go to the documentation of this file.

    00001 // Copyright (C) 2000, International Business Machines
    00002 // Corporation and others.  All Rights Reserved.
    00003 #ifndef CoinFloatEqual_H
    00004 #define CoinFloatEqual_H
    00005
    00006 #include <algorithm>
    00007 #
    00008 #include <cmath>
    00009
    00010 #include "CoinFinite.hpp"
    00011
    00044 class CoinAbsFltEq
    00045 {
    00046   public:
    00047
    00049
    00050   inline bool operator() (const double f1, const double f2) const
    00051
    00052   { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
    00053     if (f1 == f2) return true ;
    00054     return (fabs(f1-f2) < epsilon_) ; }
    00055
    00058
    00060
    00061   CoinAbsFltEq () : epsilon_(1.e-10) {}
    00062
    00064
    00065   CoinAbsFltEq (const double epsilon) : epsilon_(epsilon) {}
    00066
    00068
    00069   virtual ~CoinAbsFltEq () {}
    00070
    00072
    00073   CoinAbsFltEq (const CoinAbsFltEq& src) : epsilon_(src.epsilon_) {}
    00074
    00076
    00077   CoinAbsFltEq& operator= (const CoinAbsFltEq& rhs)
    00078
    00079   { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
    00080     return (*this) ; }
    00081
    00083
    00084   private:
    00085
    00088
    00090
    00091   double epsilon_ ;
    00092
    00094
    00095 } ;
    00096
    00097
    00098
    00105 class CoinRelFltEq
    00106 {
    00107   public:
    00108
    00110
    00111   inline bool operator() (const double f1, const double f2) const
    00112
    00113   { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
    00114     if (f1 == f2) return true ;
    00115     if (!CoinFinite(f1) || !CoinFinite(f2)) return false ;
    00116
    00117     double tol = (fabs(f1)>fabs(f2))?fabs(f1):fabs(f2) ;
    00118
    00119     return (fabs(f1-f2) <= epsilon_*(1+tol)) ; }
    00120
    00123
    00125
    00126 #ifndef COIN_FLOAT
    00127   CoinRelFltEq () : epsilon_(1.e-10) {}
    00128 #else
    00129   CoinRelFltEq () : epsilon_(1.e-6) {} ; // as float
    00130 #endif
    00131
    00133
    00134   CoinRelFltEq (const double epsilon) : epsilon_(epsilon) {}
    00135
    00137
    00138   virtual ~CoinRelFltEq () {}
    00139
    00141
    00142   CoinRelFltEq (const CoinRelFltEq & src) : epsilon_(src.epsilon_) {}
    00143
    00145
    00146   CoinRelFltEq& operator= (const CoinRelFltEq& rhs)
    00147
    00148   { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
    00149     return (*this) ; }
    00150
    00152
    00153 private:
    00154
    00157
    00159
    00160   double epsilon_ ;
    00161
    00163
    00164 } ;
    00165
    00166 #endif