floating_point_comparison.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // (C) Copyright Gennadiy Rozental 2001-2008.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 57992 $
  10. //
  11. // Description : defines algoirthms for comparing 2 floating point values
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_FLOATING_POINT_COMPARISON_HPP_071894GER
  14. #define BOOST_TEST_FLOATING_POINT_COMPARISON_HPP_071894GER
  15. // Boost.Test
  16. #include <boost/test/detail/global_typedef.hpp>
  17. #include <boost/test/utils/class_properties.hpp>
  18. #include <boost/test/predicate_result.hpp>
  19. // Boost
  20. #include <boost/limits.hpp> // for std::numeric_limits
  21. #include <boost/numeric/conversion/conversion_traits.hpp> // for numeric::conversion_traits
  22. #include <boost/static_assert.hpp>
  23. #include <boost/test/detail/suppress_warnings.hpp>
  24. //____________________________________________________________________________//
  25. namespace boost {
  26. namespace test_tools {
  27. using unit_test::readonly_property;
  28. // ************************************************************************** //
  29. // ************** floating_point_comparison_type ************** //
  30. // ************************************************************************** //
  31. enum floating_point_comparison_type {
  32. FPC_STRONG, // "Very close" - equation 1' in docs, the default
  33. FPC_WEAK // "Close enough" - equation 2' in docs.
  34. };
  35. // ************************************************************************** //
  36. // ************** details ************** //
  37. // ************************************************************************** //
  38. namespace tt_detail {
  39. // FPT is Floating-Point Type: float, double, long double or User-Defined.
  40. template<typename FPT>
  41. inline FPT
  42. fpt_abs( FPT fpv )
  43. {
  44. return fpv < static_cast<FPT>(0) ? -fpv : fpv;
  45. }
  46. //____________________________________________________________________________//
  47. template<typename FPT>
  48. struct fpt_limits {
  49. static FPT min_value()
  50. {
  51. return std::numeric_limits<FPT>::is_specialized
  52. ? (std::numeric_limits<FPT>::min)()
  53. : 0;
  54. }
  55. static FPT max_value()
  56. {
  57. return std::numeric_limits<FPT>::is_specialized
  58. ? (std::numeric_limits<FPT>::max)()
  59. : static_cast<FPT>(1000000); // for the our purpuses it doesn't really matter what value is returned here
  60. }
  61. };
  62. //____________________________________________________________________________//
  63. // both f1 and f2 are unsigned here
  64. template<typename FPT>
  65. inline FPT
  66. safe_fpt_division( FPT f1, FPT f2 )
  67. {
  68. // Avoid overflow.
  69. if( (f2 < static_cast<FPT>(1)) && (f1 > f2*fpt_limits<FPT>::max_value()) )
  70. return fpt_limits<FPT>::max_value();
  71. // Avoid underflow.
  72. if( (f1 == static_cast<FPT>(0)) ||
  73. ((f2 > static_cast<FPT>(1)) && (f1 < f2*fpt_limits<FPT>::min_value())) )
  74. return static_cast<FPT>(0);
  75. return f1/f2;
  76. }
  77. //____________________________________________________________________________//
  78. } // namespace tt_detail
  79. // ************************************************************************** //
  80. // ************** tolerance presentation types ************** //
  81. // ************************************************************************** //
  82. template<typename FPT>
  83. struct percent_tolerance_t {
  84. explicit percent_tolerance_t( FPT v ) : m_value( v ) {}
  85. FPT m_value;
  86. };
  87. //____________________________________________________________________________//
  88. template<typename Out,typename FPT>
  89. Out& operator<<( Out& out, percent_tolerance_t<FPT> t )
  90. {
  91. return out << t.m_value;
  92. }
  93. //____________________________________________________________________________//
  94. template<typename FPT>
  95. inline percent_tolerance_t<FPT>
  96. percent_tolerance( FPT v )
  97. {
  98. return percent_tolerance_t<FPT>( v );
  99. }
  100. //____________________________________________________________________________//
  101. template<typename FPT>
  102. struct fraction_tolerance_t {
  103. explicit fraction_tolerance_t( FPT v ) : m_value( v ) {}
  104. FPT m_value;
  105. };
  106. //____________________________________________________________________________//
  107. template<typename Out,typename FPT>
  108. Out& operator<<( Out& out, fraction_tolerance_t<FPT> t )
  109. {
  110. return out << t.m_value;
  111. }
  112. //____________________________________________________________________________//
  113. template<typename FPT>
  114. inline fraction_tolerance_t<FPT>
  115. fraction_tolerance( FPT v )
  116. {
  117. return fraction_tolerance_t<FPT>( v );
  118. }
  119. //____________________________________________________________________________//
  120. // ************************************************************************** //
  121. // ************** close_at_tolerance ************** //
  122. // ************************************************************************** //
  123. template<typename FPT>
  124. class close_at_tolerance {
  125. public:
  126. // Public typedefs
  127. typedef bool result_type;
  128. // Constructor
  129. template<typename ToleranceBaseType>
  130. explicit close_at_tolerance( percent_tolerance_t<ToleranceBaseType> tolerance,
  131. floating_point_comparison_type fpc_type = FPC_STRONG )
  132. : p_fraction_tolerance( tt_detail::fpt_abs( static_cast<FPT>(0.01)*tolerance.m_value ) )
  133. , p_strong_or_weak( fpc_type == FPC_STRONG )
  134. , m_report_modifier( 100. )
  135. {}
  136. template<typename ToleranceBaseType>
  137. explicit close_at_tolerance( fraction_tolerance_t<ToleranceBaseType> tolerance,
  138. floating_point_comparison_type fpc_type = FPC_STRONG )
  139. : p_fraction_tolerance( tt_detail::fpt_abs( tolerance.m_value ) )
  140. , p_strong_or_weak( fpc_type == FPC_STRONG )
  141. , m_report_modifier( 1. )
  142. {}
  143. predicate_result operator()( FPT left, FPT right ) const
  144. {
  145. FPT diff = tt_detail::fpt_abs( left - right );
  146. FPT d1 = tt_detail::safe_fpt_division( diff, tt_detail::fpt_abs( right ) );
  147. FPT d2 = tt_detail::safe_fpt_division( diff, tt_detail::fpt_abs( left ) );
  148. predicate_result res( p_strong_or_weak
  149. ? (d1 <= p_fraction_tolerance.get() && d2 <= p_fraction_tolerance.get())
  150. : (d1 <= p_fraction_tolerance.get() || d2 <= p_fraction_tolerance.get()) );
  151. if( !res )
  152. res.message() << (( d1 <= p_fraction_tolerance.get() ? d2 : d1 ) * m_report_modifier);
  153. return res;
  154. }
  155. // Public properties
  156. readonly_property<FPT> p_fraction_tolerance;
  157. readonly_property<bool> p_strong_or_weak;
  158. private:
  159. // Data members
  160. FPT m_report_modifier;
  161. };
  162. //____________________________________________________________________________//
  163. // ************************************************************************** //
  164. // ************** check_is_close ************** //
  165. // ************************************************************************** //
  166. struct BOOST_TEST_DECL check_is_close_t {
  167. // Public typedefs
  168. typedef bool result_type;
  169. template<typename FPT1, typename FPT2, typename ToleranceBaseType>
  170. predicate_result
  171. operator()( FPT1 left, FPT2 right, percent_tolerance_t<ToleranceBaseType> tolerance,
  172. floating_point_comparison_type fpc_type = FPC_STRONG ) const
  173. {
  174. // deduce "better" type from types of arguments being compared
  175. // if one type is floating and the second integral we use floating type and
  176. // value of integral type is promoted to the floating. The same for float and double
  177. // But we don't want to compare two values of integral types using this tool.
  178. typedef typename numeric::conversion_traits<FPT1,FPT2>::supertype FPT;
  179. BOOST_STATIC_ASSERT( !is_integral<FPT>::value );
  180. close_at_tolerance<FPT> pred( tolerance, fpc_type );
  181. return pred( left, right );
  182. }
  183. template<typename FPT1, typename FPT2, typename ToleranceBaseType>
  184. predicate_result
  185. operator()( FPT1 left, FPT2 right, fraction_tolerance_t<ToleranceBaseType> tolerance,
  186. floating_point_comparison_type fpc_type = FPC_STRONG ) const
  187. {
  188. // same as in a comment above
  189. typedef typename numeric::conversion_traits<FPT1,FPT2>::supertype FPT;
  190. BOOST_STATIC_ASSERT( !is_integral<FPT>::value );
  191. close_at_tolerance<FPT> pred( tolerance, fpc_type );
  192. return pred( left, right );
  193. }
  194. };
  195. namespace {
  196. check_is_close_t const& check_is_close = unit_test::ut_detail::static_constant<check_is_close_t>::value;
  197. }
  198. //____________________________________________________________________________//
  199. // ************************************************************************** //
  200. // ************** check_is_small ************** //
  201. // ************************************************************************** //
  202. struct BOOST_TEST_DECL check_is_small_t {
  203. // Public typedefs
  204. typedef bool result_type;
  205. template<typename FPT>
  206. bool
  207. operator()( FPT fpv, FPT tolerance ) const
  208. {
  209. return tt_detail::fpt_abs( fpv ) < tt_detail::fpt_abs( tolerance );
  210. }
  211. };
  212. namespace {
  213. check_is_small_t const& check_is_small = unit_test::ut_detail::static_constant<check_is_small_t>::value;
  214. }
  215. //____________________________________________________________________________//
  216. } // namespace test_tools
  217. } // namespace boost
  218. //____________________________________________________________________________//
  219. #include <boost/test/detail/enable_warnings.hpp>
  220. #endif // BOOST_FLOATING_POINT_COMAPARISON_HPP_071894GER