Chris@16: // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004 Chris@16: // Use, modification, and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See library home page at http://www.boost.org/libs/numeric/conversion Chris@16: // Chris@16: // Contact the author at: fernando_cacciola@hotmail.com Chris@16: // Chris@16: #ifndef BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP Chris@16: #define BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP Chris@16: Chris@16: #include // for std::bad_cast Chris@16: Chris@16: #include // for std::floor and std::ceil Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #include "boost/type_traits/is_arithmetic.hpp" Chris@16: Chris@16: #include "boost/mpl/if.hpp" Chris@16: #include "boost/mpl/integral_c.hpp" Chris@16: Chris@16: namespace boost { namespace numeric Chris@16: { Chris@16: Chris@16: template Chris@16: struct Trunc Chris@16: { Chris@16: typedef S source_type ; Chris@16: Chris@16: typedef typename mpl::if_< is_arithmetic,S,S const&>::type argument_type ; Chris@16: Chris@16: static source_type nearbyint ( argument_type s ) Chris@16: { Chris@16: #if !defined(BOOST_NO_STDC_NAMESPACE) Chris@16: using std::floor ; Chris@16: using std::ceil ; Chris@16: #endif Chris@16: Chris@16: return s < static_cast(0) ? ceil(s) : floor(s) ; Chris@16: } Chris@16: Chris@16: typedef mpl::integral_c< std::float_round_style, std::round_toward_zero> round_style ; Chris@16: } ; Chris@16: Chris@16: Chris@16: Chris@16: template Chris@16: struct Floor Chris@16: { Chris@16: typedef S source_type ; Chris@16: Chris@16: typedef typename mpl::if_< is_arithmetic,S,S const&>::type argument_type ; Chris@16: Chris@16: static source_type nearbyint ( argument_type s ) Chris@16: { Chris@16: #if !defined(BOOST_NO_STDC_NAMESPACE) Chris@16: using std::floor ; Chris@16: #endif Chris@16: Chris@16: return floor(s) ; Chris@16: } Chris@16: Chris@16: typedef mpl::integral_c< std::float_round_style, std::round_toward_neg_infinity> round_style ; Chris@16: } ; Chris@16: Chris@16: template Chris@16: struct Ceil Chris@16: { Chris@16: typedef S source_type ; Chris@16: Chris@16: typedef typename mpl::if_< is_arithmetic,S,S const&>::type argument_type ; Chris@16: Chris@16: static source_type nearbyint ( argument_type s ) Chris@16: { Chris@16: #if !defined(BOOST_NO_STDC_NAMESPACE) Chris@16: using std::ceil ; Chris@16: #endif Chris@16: Chris@16: return ceil(s) ; Chris@16: } Chris@16: Chris@16: typedef mpl::integral_c< std::float_round_style, std::round_toward_infinity> round_style ; Chris@16: } ; Chris@16: Chris@16: template Chris@16: struct RoundEven Chris@16: { Chris@16: typedef S source_type ; Chris@16: Chris@16: typedef typename mpl::if_< is_arithmetic,S,S const&>::type argument_type ; Chris@16: Chris@16: static source_type nearbyint ( argument_type s ) Chris@16: { Chris@16: // Algorithm contributed by Guillaume Melquiond Chris@16: Chris@16: #if !defined(BOOST_NO_STDC_NAMESPACE) Chris@16: using std::floor ; Chris@16: using std::ceil ; Chris@16: #endif Chris@16: Chris@16: // only works inside the range not at the boundaries Chris@16: S prev = floor(s); Chris@16: S next = ceil(s); Chris@16: Chris@16: S rt = (s - prev) - (next - s); // remainder type Chris@16: Chris@16: S const zero(0.0); Chris@16: S const two(2.0); Chris@16: Chris@16: if ( rt < zero ) Chris@16: return prev; Chris@16: else if ( rt > zero ) Chris@16: return next; Chris@16: else Chris@16: { Chris@16: bool is_prev_even = two * floor(prev / two) == prev ; Chris@16: return ( is_prev_even ? prev : next ) ; Chris@16: } Chris@16: } Chris@16: Chris@16: typedef mpl::integral_c< std::float_round_style, std::round_to_nearest> round_style ; Chris@16: } ; Chris@16: Chris@16: Chris@16: enum range_check_result Chris@16: { Chris@16: cInRange = 0 , Chris@16: cNegOverflow = 1 , Chris@16: cPosOverflow = 2 Chris@16: } ; Chris@16: Chris@16: class bad_numeric_cast : public std::bad_cast Chris@16: { Chris@16: public: Chris@16: Chris@16: virtual const char * what() const throw() Chris@16: { return "bad numeric conversion: overflow"; } Chris@16: }; Chris@16: Chris@16: class negative_overflow : public bad_numeric_cast Chris@16: { Chris@16: public: Chris@16: Chris@16: virtual const char * what() const throw() Chris@16: { return "bad numeric conversion: negative overflow"; } Chris@16: }; Chris@16: class positive_overflow : public bad_numeric_cast Chris@16: { Chris@16: public: Chris@16: Chris@16: virtual const char * what() const throw() Chris@16: { return "bad numeric conversion: positive overflow"; } Chris@16: }; Chris@16: Chris@16: struct def_overflow_handler Chris@16: { Chris@16: void operator() ( range_check_result r ) // throw(negative_overflow,positive_overflow) Chris@16: { Chris@16: #ifndef BOOST_NO_EXCEPTIONS Chris@16: if ( r == cNegOverflow ) Chris@16: throw negative_overflow() ; Chris@16: else if ( r == cPosOverflow ) Chris@16: throw positive_overflow() ; Chris@16: #else Chris@16: if ( r == cNegOverflow ) Chris@16: ::boost::throw_exception(negative_overflow()) ; Chris@16: else if ( r == cPosOverflow ) Chris@16: ::boost::throw_exception(positive_overflow()) ; Chris@16: #endif Chris@16: } Chris@16: } ; Chris@16: Chris@16: struct silent_overflow_handler Chris@16: { Chris@16: void operator() ( range_check_result ) {} // throw() Chris@16: } ; Chris@16: Chris@16: template Chris@16: struct raw_converter Chris@16: { Chris@16: typedef typename Traits::result_type result_type ; Chris@16: typedef typename Traits::argument_type argument_type ; Chris@16: Chris@16: static result_type low_level_convert ( argument_type s ) { return static_cast(s) ; } Chris@16: } ; Chris@16: Chris@16: struct UseInternalRangeChecker {} ; Chris@16: Chris@16: } } // namespace boost::numeric Chris@16: Chris@16: #endif