Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CompareFloatingPoint.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 #ifndef _CompareFloatingPoint_hh_
22 #define _CompareFloatingPoint_hh_
23 
24 #include <bias_config.h>
25 #include <limits>
26 #ifdef WIN32
27 #undef max
28 #undef min
29 #endif
30 #include <cmath>
31 #ifndef WIN32
32 #include <algorithm>
33 #endif
34 
35 namespace BIAS {
36 
37 /** @brief comparison function for floating point values
38  See http://www.boost.org/libs/test/doc/components/test_tools/
39  floating_point_comparison.html
40  for discussion
41  @author woelk 02/2006 */
42 template <class T> // left == right
43 bool Equal(const T left, const T right,
44  const T eps = std::numeric_limits<T>::epsilon());
45 
46  /** @brief comparison function for floating point values
47  * @test tested with TestCompareFloatingPoint.cpp
48  @author woelk 02/2006 */
49  template <typename T> // left < right
50  inline bool Less(const T left, const T right,
51  const T eps = std::numeric_limits<T>::epsilon())
52  { if (left==0. || right==0.) return (right-left) > eps;
53  return ((right-left) > eps * std::min(right,left)); }
54 
55  /** @brief comparison function for floating point values
56  @author woelk 02/2006 */
57  template <typename T> // left <= right
58  inline bool LessEqual(const T left, const T right,
59  const T eps = std::numeric_limits<T>::epsilon())
60  { return (Less(left, right, eps) || Equal(left, right, eps)); }
61 
62  /** @brief comparison function for floating point values
63  @author woelk 02/2006 */
64  template <class T> // left > right
65  inline bool Greater(const T left, const T right,
66  const T eps = std::numeric_limits<T>::epsilon())
67  { if (left==0. || right==0.) return (left-right) > eps;
68  return ((left-right) > eps * std::min(right,left)); }
69 
70  /** @brief comparison function for floating point values
71  @author woelk 02/2006 */
72  template <class T> // left >= right
73  inline bool GreaterEqual(const T left, const T right,
74  const T eps = std::numeric_limits<T>::epsilon())
75  { return (Greater(left,right) || Equal(left, right, eps)); }
76 
77  /** @brief correct casting to storage type, incorporates rint for integer
78  values and simple casting for floating point values */
79  template <typename T> BIASCommon_EXPORT
80  T Cast(const double input);
81 
82 }
83 
84 #endif // _CompareFloatingPoint_hh_
bool Less(const T left, const T right, const T eps=std::numeric_limits< T >::epsilon())
comparison function for floating point values
T Cast(const double input)
correct casting to storage type, incorporates rint for integer values and simple casting for floating...
bool Greater(const T left, const T right, const T eps=std::numeric_limits< T >::epsilon())
comparison function for floating point values
bool GreaterEqual(const T left, const T right, const T eps=std::numeric_limits< T >::epsilon())
comparison function for floating point values
bool LessEqual(const T left, const T right, const T eps=std::numeric_limits< T >::epsilon())
comparison function for floating point values
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...