Boost::propertytree DBL_MAX
-
I'm programming a ptree and at some point I need to put DBL_MAX in (as a default value). I see the right number when I open the generated xml-file.
But when I use ptree.get to get the number an exception is thrown:conversion of data to type "d" failed.
can anybody help me with that?
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include "float.h" using namespace std; int main() { using boost::property_tree::ptree; ptree pt; double d=-DBL_MAX; double d2=-1.797693134862316e+308; double d3=-1.79769e+308; cout<<d<<endl; cout<<d2<<endl; cout<<d3<<endl; pt.put<double>("double", d); write_xml("test.xml", pt); cout << "from xml-file: " <<pt.get<double>("double")<<endl;//doesn't work with d and d2, but works with d3 }
-
The problem, as often, is rounding. DBL_MAX will usually be defined something like
#define DBL_MAX double(1.79769313486231570815e+308L)
but if you actually do the conversion and write this number to the xml-file, you get 1.797693134862316e+308 as your output. As you can see, this number is slightly larger than 1.79769313486231570815e+308. As 1.79769313486231570815e+308 is already the maximum finite value for a double, 1.797693134862316e+308 cannot be represented as a double.
-
Ah, ok I understand. Thanks a lot. Do you know how I could avoid that problem? Like is the something in c++ that allows me to bring DBL_MAX down to a number with a slightly smaller mantissa?