00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef SGE_MATH_COMPARE_HPP_INCLUDED
00021 #define SGE_MATH_COMPARE_HPP_INCLUDED
00022
00023 #include "diff.hpp"
00024 #include <boost/utility/enable_if.hpp>
00025 #include <boost/type_traits/is_floating_point.hpp>
00026 #include <limits>
00027
00028 namespace sge
00029 {
00030 namespace math
00031 {
00035 template<typename T>
00036 inline bool nearly_equals(const T& a, const T& b)
00037 {
00038 return diff(a, b) < std::numeric_limits<T>::epsilon();
00039 }
00040
00044 template<typename T>
00045 inline typename boost::disable_if<boost::is_floating_point<T>, bool>::type compare(const T& a, const T& b)
00046 {
00047 return a == b;
00048 }
00049
00053 template<typename T>
00054 inline typename boost::enable_if<boost::is_floating_point<T>, bool>::type compare(const T& a, const T& b)
00055 {
00056 return nearly_equals(a, b);
00057 }
00058
00062 template<typename T>
00063 inline bool almost_zero(const T t)
00064 {
00065 return compare(t, static_cast<T>(0));
00066 }
00067
00071 template<typename T>
00072 inline bool in_closed_interval(const T &t,const T &l,const T &r)
00073 {
00074 return t >= l && t <= r;
00075 }
00076 }
00077 }
00078
00079 #endif