Change cpp_dec_float string conversion to generate a better error message when conversion fails.

Add generic inter-conversions and tests for them.
Fix bug in cpp_int convert_to.

[SVN r77630]
This commit is contained in:
John Maddock
2012-03-29 18:00:06 +00:00
parent 4397548816
commit 3913771ea0
6 changed files with 414 additions and 5 deletions

View File

@@ -1804,6 +1804,8 @@ std::string cpp_dec_float<Digits10>::str(boost::intmax_t number_of_digits, std::
template <unsigned Digits10>
bool cpp_dec_float<Digits10>::rd_string(const char* const s)
{
try{
std::string str(s);
// Get a possible exponent and remove it.
@@ -1849,7 +1851,7 @@ bool cpp_dec_float<Digits10>::rd_string(const char* const s)
*this = this->inf();
return true;
}
if((str.size() >= 3) && ((str.substr(0, 3) == "nan") || (str.substr(0, 3) == "NAN")))
if((str.size() >= 3) && ((str.substr(0, 3) == "nan") || (str.substr(0, 3) == "NAN") || (str.substr(0, 3) == "NaN")))
{
*this = this->nan();
return true;
@@ -2056,6 +2058,16 @@ bool cpp_dec_float<Digits10>::rd_string(const char* const s)
}
}
}
catch(const bad_lexical_cast&)
{
// Rethrow with better error message:
std::string msg = "Unable to parse the string \"";
msg += s;
msg += "\" as a floating point value.";
throw std::runtime_error(msg);
}
return true;
}

View File

@@ -2025,12 +2025,10 @@ inline typename enable_if<is_integral<R>, void>::type eval_convert_to(R* result,
{
*result = static_cast<R>(backend.limbs()[0]);
unsigned shift = cpp_int_backend<MinBits, Signed, Allocator>::limb_bits;
for(unsigned i = 1; i < backend.size(); ++i)
for(unsigned i = 1; (i < backend.size()) && (shift < static_cast<unsigned>(std::numeric_limits<R>::digits)); ++i)
{
*result += static_cast<R>(backend.limbs()[i]) << shift;
shift += cpp_int_backend<MinBits, Signed, Allocator>::limb_bits;
if(shift > static_cast<unsigned>(std::numeric_limits<R>::digits))
break;
}
if(backend.sign())
{

View File

@@ -0,0 +1,221 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2011 John Maddock. Distributed under 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)
#ifndef BOOST_MP_GENERIC_INTERCONVERT_HPP
#define BOOST_MP_GENERIC_INTERCONVERT_HPP
#include <boost/multiprecision/detail/default_ops.hpp>
namespace boost{ namespace multiprecision{ namespace detail{
template <class To, class From>
void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/)
{
using default_ops::eval_get_sign;
using default_ops::eval_bitwise_and;
using default_ops::eval_convert_to;
using default_ops::eval_right_shift;
using default_ops::eval_ldexp;
using default_ops::eval_add;
// smallest unsigned type handled natively by "From" is likely to be it's limb_type:
typedef typename canonical<unsigned char, From>::type limb_type;
// get the corresponding type that we can assign to "To":
typedef typename canonical<limb_type, To>::type to_type;
From t(from);
bool is_neg = eval_get_sign(t) < 0;
if(is_neg)
t.negate();
// Pick off the first limb:
limb_type limb;
limb_type mask = ~static_cast<limb_type>(0);
From fl;
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
to = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<limb_type>::digits);
//
// Then keep picking off more limbs until "t" is zero:
//
To l;
unsigned shift = std::numeric_limits<limb_type>::digits;
while(!eval_is_zero(t))
{
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
l = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<limb_type>::digits);
eval_ldexp(l, l, shift);
eval_add(to, l);
shift += std::numeric_limits<limb_type>::digits;
}
//
// Finish off by setting the sign:
//
if(is_neg)
to.negate();
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_integer>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/)
{
using default_ops::eval_get_sign;
using default_ops::eval_bitwise_and;
using default_ops::eval_convert_to;
using default_ops::eval_right_shift;
using default_ops::eval_left_shift;
using default_ops::eval_bitwise_or;
// smallest unsigned type handled natively by "From" is likely to be it's limb_type:
typedef typename canonical<unsigned char, From>::type limb_type;
// get the corresponding type that we can assign to "To":
typedef typename canonical<limb_type, To>::type to_type;
From t(from);
bool is_neg = eval_get_sign(t) < 0;
if(is_neg)
t.negate();
// Pick off the first limb:
limb_type limb;
limb_type mask = ~static_cast<limb_type>(0);
From fl;
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
to = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<limb_type>::digits);
//
// Then keep picking off more limbs until "t" is zero:
//
To l;
unsigned shift = std::numeric_limits<limb_type>::digits;
while(!eval_is_zero(t))
{
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
l = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<limb_type>::digits);
eval_left_shift(l, shift);
eval_bitwise_or(to, l);
shift += std::numeric_limits<limb_type>::digits;
}
//
// Finish off by setting the sign:
//
if(is_neg)
to.negate();
}
template <class To, class From>
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*/)
{
//
// 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 convertion when the radix is other
// than 2:
//
if(std::numeric_limits<mp_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<long long>::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);
long long 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));
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_rational>& /*to_type*/, const mpl::int_<number_kind_rational>& /*from_type*/)
{
typedef typename component_type<mp_number<From> >::type from_component_type;
typedef typename component_type<mp_number<To> >::type to_component_type;
mp_number<From> t(from);
to_component_type n(numerator(t)), d(denominator(t));
using default_ops::assign_components;
assign_components(to, n.backend(), d.backend());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_rational>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/)
{
typedef typename component_type<mp_number<To> >::type to_component_type;
mp_number<From> t(from);
to_component_type n(t), d(1);
using default_ops::assign_components;
assign_components(to, n.backend(), d.backend());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_rational>& /*from_type*/)
{
typedef typename component_type<mp_number<From> >::type from_component_type;
using default_ops::eval_divide;
mp_number<From> t(from);
from_component_type n(numerator(t)), d(denominator(t));
mp_number<To> fn(n), fd(d);
eval_divide(to, fn.backend(), fd.backend());
}
}}} // namespaces
#endif // BOOST_MP_GENERIC_INTERCONVERT_HPP

View File

@@ -19,7 +19,7 @@
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/throw_exception.hpp>
#include <boost/multiprecision/detail/default_ops.hpp>
#include <boost/multiprecision/detail/generic_interconvert.hpp>
namespace boost{ namespace multiprecision{
@@ -63,6 +63,14 @@ public:
{
m_backend = val.backend();
}
template <class Other, bool ET>
mp_number(const mp_number<Other, ET>& val, typename disable_if<boost::is_convertible<Other, Backend> >::type* dummy1 = 0)
{
//
// Attempt a generic interconvertion:
//
detail::generic_interconvert(backend(), val.backend(), number_category<Backend>(), number_category<Other>());
}
template <class V>
mp_number(V v1, V v2, typename enable_if<mpl::or_<boost::is_arithmetic<V>, is_same<std::string, V>, is_convertible<V, const char*> > >::type* dummy1 = 0)
{
@@ -125,6 +133,17 @@ public:
return *this;
}
template <class Other>
typename disable_if<is_convertible<Other, Backend>, mp_number<Backend, ExpressionTemplates>& >::type
operator=(const mp_number<Other>& v)
{
//
// Attempt a generic interconvertion:
//
detail::generic_interconvert(backend(), v.backend(), number_category<Backend>(), number_category<Other>());
return *this;
}
template <class tag, class Arg1, class Arg2, class Arg3>
mp_number(const detail::mp_exp<tag, Arg1, Arg2, Arg3>& e)
{