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