Boost logo

Boost :

From: Paul A. Bristow (boost_at_[hidden])
Date: 2003-08-28 09:57:50


There has been some discussion about a minor improvement to lexical_cast to
ensure that a loopback test like:

any_type any_value == lexical_cast<T>(lexical_cast<string>(any_value))

is always true

The following replacement for the original code (using digits10+1) works for
specialised radix 2 types:

if(std::numeric_limits<Target>::is_specialized
  && std::numeric_limits<Target>::radix == 2)
{
  stream.precision(2 + std::numeric_limits<Target>::digits * 301/1000);
}
else if(std::numeric_limits<Source>::is_specialized
  && std::numeric_limits<Source>::radix == 2)
{
  stream.precision(2 + std::numeric_limits<Source>::digits * 301/1000);
}

As in the current version, for other types the default precision is used, and
you may not get what you expect. I think this is OK, but one could throw an
exception. I propose a comment instead

// Warning: else the default stream precision (usually 6) will be used,
// which may not be enough significant decimal digits to avoid losing precision,
// for example in a loopback test like
// some_type any_value == lexical_cast<T>(lexical_cast<string>(any_value));

If there are no objections, can someone with sufficient authority patch
lexical_cast.hpp?

Paul

Paul A Bristow, Prizet Farmhouse, Kendal, Cumbria, LA8 8AB UK
+44 1539 561830 Mobile +44 7714 33 02 04
mailto:pbristow_at_[hidden]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk