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

- File List
- File Members

# /home/coin/SVN-release/Vol-1.2.0/CoinUtils/src/CoinFloatEqual.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinFloatEqual.hpp 1315 2010-08-29 00:57:57Z lou $ */
    00002 // Copyright (C) 2000, International Business Machines
    00003 // Corporation and others.  All Rights Reserved.
    00004 #ifndef CoinFloatEqual_H
    00005 #define CoinFloatEqual_H
    00006
    00007 #include <algorithm>
    00008 #
    00009 #include <cmath>
    00010
    00011 #include "CoinFinite.hpp"
    00012
    00045 class CoinAbsFltEq
    00046 {
    00047   public:
    00048
    00050
    00051   inline bool operator() (const double f1, const double f2) const
    00052
    00053   { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
    00054     if (f1 == f2) return true ;
    00055     return (fabs(f1-f2) < epsilon_) ; }
    00056
    00059
    00065   CoinAbsFltEq () : epsilon_(1.e-10) {}
    00066
    00068
    00069   CoinAbsFltEq (const double epsilon) : epsilon_(epsilon) {}
    00070
    00072
    00073   virtual ~CoinAbsFltEq () {}
    00074
    00076
    00077   CoinAbsFltEq (const CoinAbsFltEq& src) : epsilon_(src.epsilon_) {}
    00078
    00080
    00081   CoinAbsFltEq& operator= (const CoinAbsFltEq& rhs)
    00082
    00083   { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
    00084     return (*this) ; }
    00085
    00087
    00088   private:
    00089
    00092
    00094
    00095   double epsilon_ ;
    00096
    00098
    00099 } ;
    00100
    00101
    00102
    00109 class CoinRelFltEq
    00110 {
    00111   public:
    00112
    00114
    00115   inline bool operator() (const double f1, const double f2) const
    00116
    00117   { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
    00118     if (f1 == f2) return true ;
    00119     if (!CoinFinite(f1) || !CoinFinite(f2)) return false ;
    00120
    00121     double tol = (fabs(f1)>fabs(f2))?fabs(f1):fabs(f2) ;
    00122
    00123     return (fabs(f1-f2) <= epsilon_*(1+tol)) ; }
    00124
    00127
    00128 #ifndef COIN_FLOAT
    00129
    00133   CoinRelFltEq () : epsilon_(1.e-10) {}
    00134 #else
    00135
    00139   CoinRelFltEq () : epsilon_(1.e-6) {} ; // as float
    00140 #endif
    00141
    00143
    00144   CoinRelFltEq (const double epsilon) : epsilon_(epsilon) {}
    00145
    00147
    00148   virtual ~CoinRelFltEq () {}
    00149
    00151
    00152   CoinRelFltEq (const CoinRelFltEq & src) : epsilon_(src.epsilon_) {}
    00153
    00155
    00156   CoinRelFltEq& operator= (const CoinRelFltEq& rhs)
    00157
    00158   { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
    00159     return (*this) ; }
    00160
    00162
    00163 private:
    00164
    00167
    00169
    00170   double epsilon_ ;
    00171
    00173
    00174 } ;
    00175
    00176 #endif