converter_policies.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
  2. // Use, modification, and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See library home page at http://www.boost.org/libs/numeric/conversion
  6. //
  7. // Contact the author at: fernando_cacciola@hotmail.com
  8. //
  9. #ifndef BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP
  10. #define BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP
  11. #include <functional>
  12. #include <typeinfo> // for std::bad_cast
  13. #include <boost/config.hpp>
  14. #include <boost/config/no_tr1/cmath.hpp> // for std::floor and std::ceil
  15. #include <boost/throw_exception.hpp>
  16. #include "boost/type_traits/is_arithmetic.hpp"
  17. #include "boost/mpl/if.hpp"
  18. #include "boost/mpl/integral_c.hpp"
  19. namespace boost { namespace numeric
  20. {
  21. template<class S>
  22. struct Trunc
  23. {
  24. typedef S source_type ;
  25. typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
  26. static source_type nearbyint ( argument_type s )
  27. {
  28. #if !defined(BOOST_NO_STDC_NAMESPACE)
  29. using std::floor ;
  30. using std::ceil ;
  31. #endif
  32. return s < static_cast<S>(0) ? ceil(s) : floor(s) ;
  33. }
  34. typedef mpl::integral_c< std::float_round_style, std::round_toward_zero> round_style ;
  35. } ;
  36. template<class S>
  37. struct Floor
  38. {
  39. typedef S source_type ;
  40. typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
  41. static source_type nearbyint ( argument_type s )
  42. {
  43. #if !defined(BOOST_NO_STDC_NAMESPACE)
  44. using std::floor ;
  45. #endif
  46. return floor(s) ;
  47. }
  48. typedef mpl::integral_c< std::float_round_style, std::round_toward_neg_infinity> round_style ;
  49. } ;
  50. template<class S>
  51. struct Ceil
  52. {
  53. typedef S source_type ;
  54. typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
  55. static source_type nearbyint ( argument_type s )
  56. {
  57. #if !defined(BOOST_NO_STDC_NAMESPACE)
  58. using std::ceil ;
  59. #endif
  60. return ceil(s) ;
  61. }
  62. typedef mpl::integral_c< std::float_round_style, std::round_toward_infinity> round_style ;
  63. } ;
  64. template<class S>
  65. struct RoundEven
  66. {
  67. typedef S source_type ;
  68. typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
  69. static source_type nearbyint ( argument_type s )
  70. {
  71. // Algorithm contributed by Guillaume Melquiond
  72. #if !defined(BOOST_NO_STDC_NAMESPACE)
  73. using std::floor ;
  74. using std::ceil ;
  75. #endif
  76. // only works inside the range not at the boundaries
  77. S prev = floor(s);
  78. S next = ceil(s);
  79. S rt = (s - prev) - (next - s); // remainder type
  80. S const zero(0.0);
  81. S const two(2.0);
  82. if ( rt < zero )
  83. return prev;
  84. else if ( rt > zero )
  85. return next;
  86. else
  87. {
  88. bool is_prev_even = two * floor(prev / two) == prev ;
  89. return ( is_prev_even ? prev : next ) ;
  90. }
  91. }
  92. typedef mpl::integral_c< std::float_round_style, std::round_to_nearest> round_style ;
  93. } ;
  94. enum range_check_result
  95. {
  96. cInRange = 0 ,
  97. cNegOverflow = 1 ,
  98. cPosOverflow = 2
  99. } ;
  100. class bad_numeric_cast : public std::bad_cast
  101. {
  102. public:
  103. const char * what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE
  104. { return "bad numeric conversion: overflow"; }
  105. };
  106. class negative_overflow : public bad_numeric_cast
  107. {
  108. public:
  109. const char * what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE
  110. { return "bad numeric conversion: negative overflow"; }
  111. };
  112. class positive_overflow : public bad_numeric_cast
  113. {
  114. public:
  115. const char * what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE
  116. { return "bad numeric conversion: positive overflow"; }
  117. };
  118. struct def_overflow_handler
  119. {
  120. void operator() ( range_check_result r ) // throw(negative_overflow,positive_overflow)
  121. {
  122. #ifndef BOOST_NO_EXCEPTIONS
  123. if ( r == cNegOverflow )
  124. throw negative_overflow() ;
  125. else if ( r == cPosOverflow )
  126. throw positive_overflow() ;
  127. #else
  128. if ( r == cNegOverflow )
  129. ::boost::throw_exception(negative_overflow()) ;
  130. else if ( r == cPosOverflow )
  131. ::boost::throw_exception(positive_overflow()) ;
  132. #endif
  133. }
  134. } ;
  135. struct silent_overflow_handler
  136. {
  137. void operator() ( range_check_result ) {} // throw()
  138. } ;
  139. template<class Traits>
  140. struct raw_converter
  141. {
  142. typedef typename Traits::result_type result_type ;
  143. typedef typename Traits::argument_type argument_type ;
  144. static result_type low_level_convert ( argument_type s ) { return static_cast<result_type>(s) ; }
  145. } ;
  146. struct UseInternalRangeChecker {} ;
  147. } } // namespace boost::numeric
  148. #endif