By default, ptree
stores its values as std::string
and convert them using basic_stringstream
with precision:
s.precision(std::numeric_limits<double>::digits10+1);
This problem appears when it converts DBL_MAX
to std::string
because it rounds the number to an invalid value. You can check it with the following code:
ptree pt;
pt.put("min", -DBL_MAX);
pt.put("max", DBL_MAX);
cout << "Min=" << pt.get<string>("min") << std::endl;
cout << "Max=" << pt.get<string>("max") << std::endl;
Using Visual Studio, it prints:
Min=-1.797693134862316e+308
Max= 1.797693134862316e+308
However, DBL_MAX
is defined as 1.7976931348623158e+308
so the printed value is out of limits.
There are several workarounds but none is perfect:
- Use a different default value smaller than
DBL_MAX
. For example 1.797693134862315e+308
.
- Catch the
bad_data
exception and assume that it means default.
- Register a new type with your custom converter. You can see an example here.
Reduce the precision of the stored value. You can perform this using the following code:
namespace boost { namespace property_tree
{
template <typename Ch, typename Traits>
struct customize_stream<Ch, Traits, double, void>
{
static void insert(std::basic_ostream<Ch, Traits>& s, const double& e) {
s.precision(std::numeric_limits<double>::digits10-1);
s << e;
}
static void extract(std::basic_istream<Ch, Traits>& s, double& e) {
s >> e;
if(!s.eof()) {
s >> std::ws;
}
}
};
}