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)
6 // See library home page at http://www.boost.org/libs/numeric/conversion
8 // Contact the author at: fernando_cacciola@hotmail.com
10 #ifndef BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP
11 #define BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP
13 #include <typeinfo> // for std::bad_cast
15 #include <boost/config.hpp>
16 #include <boost/config/no_tr1/cmath.hpp> // for std::floor and std::ceil
17 #include <boost/throw_exception.hpp>
21 #include "boost/type_traits/is_arithmetic.hpp"
23 #include "boost/mpl/if.hpp"
24 #include "boost/mpl/integral_c.hpp"
26 namespace boost { namespace numeric
32 typedef S source_type ;
34 typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
36 static source_type nearbyint ( argument_type s )
38 #if !defined(BOOST_NO_STDC_NAMESPACE)
43 return s < static_cast<S>(0) ? ceil(s) : floor(s) ;
46 typedef mpl::integral_c< std::float_round_style, std::round_toward_zero> round_style ;
54 typedef S source_type ;
56 typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
58 static source_type nearbyint ( argument_type s )
60 #if !defined(BOOST_NO_STDC_NAMESPACE)
67 typedef mpl::integral_c< std::float_round_style, std::round_toward_neg_infinity> round_style ;
73 typedef S source_type ;
75 typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
77 static source_type nearbyint ( argument_type s )
79 #if !defined(BOOST_NO_STDC_NAMESPACE)
86 typedef mpl::integral_c< std::float_round_style, std::round_toward_infinity> round_style ;
92 typedef S source_type ;
94 typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
96 static source_type nearbyint ( argument_type s )
98 // Algorithm contributed by Guillaume Melquiond
100 #if !defined(BOOST_NO_STDC_NAMESPACE)
105 // only works inside the range not at the boundaries
109 S rt = (s - prev) - (next - s); // remainder type
116 else if ( rt > zero )
120 bool is_prev_even = two * floor(prev / two) == prev ;
121 return ( is_prev_even ? prev : next ) ;
125 typedef mpl::integral_c< std::float_round_style, std::round_to_nearest> round_style ;
129 enum range_check_result
136 class bad_numeric_cast : public std::bad_cast
140 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
141 { return "bad numeric conversion: overflow"; }
144 class negative_overflow : public bad_numeric_cast
148 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
149 { return "bad numeric conversion: negative overflow"; }
151 class positive_overflow : public bad_numeric_cast
155 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
156 { return "bad numeric conversion: positive overflow"; }
159 struct def_overflow_handler
161 void operator() ( range_check_result r ) // throw(negative_overflow,positive_overflow)
163 #ifndef BOOST_NO_EXCEPTIONS
164 if ( r == cNegOverflow )
165 throw negative_overflow() ;
166 else if ( r == cPosOverflow )
167 throw positive_overflow() ;
169 if ( r == cNegOverflow )
170 ::boost::throw_exception(negative_overflow()) ;
171 else if ( r == cPosOverflow )
172 ::boost::throw_exception(positive_overflow()) ;
177 struct silent_overflow_handler
179 void operator() ( range_check_result ) {} // throw()
182 template<class Traits>
185 typedef typename Traits::result_type result_type ;
186 typedef typename Traits::argument_type argument_type ;
188 static result_type low_level_convert ( argument_type s ) { return static_cast<result_type>(s) ; }
191 struct UseInternalRangeChecker {} ;
193 } } // namespace boost::numeric