CoinUtils trunk
CoinFloatEqual.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 // Copyright (C) 2000, International Business Machines
00003 // Corporation and others.  All Rights Reserved.
00004 // This code is licensed under the terms of the Eclipse Public License (EPL).
00005 
00006 #ifndef CoinFloatEqual_H
00007 #define CoinFloatEqual_H
00008 
00009 #include <algorithm>
00010 #include <cmath>
00011 
00012 #include "CoinFinite.hpp"
00013 
00046 class CoinAbsFltEq
00047 {
00048   public:
00049 
00051 
00052   inline bool operator() (const double f1, const double f2) const
00053 
00054   { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
00055     if (f1 == f2) return true ;
00056     return (fabs(f1-f2) < epsilon_) ; } 
00057 
00060 
00066   CoinAbsFltEq () : epsilon_(1.e-10) {} 
00067 
00069 
00070   CoinAbsFltEq (const double epsilon) : epsilon_(epsilon) {} 
00071 
00073 
00074   virtual ~CoinAbsFltEq () {} 
00075 
00077 
00078   CoinAbsFltEq (const CoinAbsFltEq& src) : epsilon_(src.epsilon_) {} 
00079 
00081 
00082   CoinAbsFltEq& operator= (const CoinAbsFltEq& rhs)
00083 
00084   { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
00085     return (*this) ; } 
00086 
00088 
00089   private:  
00090 
00093 
00095 
00096   double epsilon_ ;
00097 
00099 
00100 } ;
00101 
00102 
00103 
00110 class CoinRelFltEq
00111 {
00112   public:
00113 
00115 
00116   inline bool operator() (const double f1, const double f2) const
00117 
00118   { if (CoinIsnan(f1) || CoinIsnan(f2)) return false ;
00119     if (f1 == f2) return true ;
00120     if (!CoinFinite(f1) || !CoinFinite(f2)) return false ;
00121 
00122     double tol = (fabs(f1)>fabs(f2))?fabs(f1):fabs(f2) ;
00123 
00124     return (fabs(f1-f2) <= epsilon_*(1+tol)) ; }
00125 
00128 
00129 #ifndef COIN_FLOAT
00130 
00134   CoinRelFltEq () : epsilon_(1.e-10) {} 
00135 #else
00136 
00140   CoinRelFltEq () : epsilon_(1.e-6) {} ; // as float
00141 #endif
00142 
00144 
00145   CoinRelFltEq (const double epsilon) : epsilon_(epsilon) {} 
00146 
00148 
00149   virtual ~CoinRelFltEq () {} 
00150 
00152 
00153   CoinRelFltEq (const CoinRelFltEq & src) : epsilon_(src.epsilon_) {} 
00154 
00156 
00157   CoinRelFltEq& operator= (const CoinRelFltEq& rhs)
00158 
00159   { if (this != &rhs) epsilon_ = rhs.epsilon_ ;
00160     return (*this) ; } 
00161 
00163 
00164 private: 
00165 
00168 
00170 
00171   double epsilon_ ;
00172 
00174 
00175 } ;
00176 
00177 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines