void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_floating_point>& /*from_type*/)
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4127)
#endif
// The code here only works when the radix of "From" is 2, we could try shifting by other
// radixes but it would complicate things.... use a string conversion when the radix is other
// than 2:
if(std::numeric_limits<number<From> >::radix != 2)
to = from.str(0, std::ios_base::fmtflags()).c_str();
return;
typedef typename canonical<unsigned char, To>::type ui_type;
using default_ops::eval_fpclassify;
using default_ops::eval_add;
using default_ops::eval_subtract;
using default_ops::eval_convert_to;
// First classify the input, then handle the special cases:
int c = eval_fpclassify(from);
if(c == FP_ZERO)
to = ui_type(0);
return;
else if(c == FP_NAN)
to = "nan";
return;
else if(c == FP_INFINITE)
to = "inf";
if(eval_get_sign(from) < 0)
to.negate();
return;
typename From::exponent_type e;
From f, term;
to = ui_type(0);
eval_frexp(f, from, &e);
static const int shift = std::numeric_limits<boost::intmax_t>::digits - 1;
while(!eval_is_zero(f))
// extract int sized bits from f:
eval_ldexp(f, f, shift);
eval_floor(term, f);
e -= shift;
eval_ldexp(to, to, shift);
typename boost::multiprecision::detail::canonical<boost::intmax_t, To>::type ll;
eval_convert_to(&ll, term);
eval_add(to, ll);
eval_subtract(f, term);
typedef typename To::exponent_type to_exponent;
if((e > (std::numeric_limits<to_exponent>::max)()) || (e < (std::numeric_limits<to_exponent>::min)()))
to = "inf";
if(eval_get_sign(from) < 0)
to.negate();
return;
eval_ldexp(to, to, static_cast<to_exponent>(e));
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
}
// Pass by reference for lvalue of 'move_ptr'.
// Also note 'auto_ptr' isn't copy-initializable from one which has
// a different 'element_type'. See http://tinyurl.com/yo8a7w (defect report #84).
template< class To, class From > inline
To move_to(From& from)
return To(from.release());
}