[Boost-Users][serialization] Floating point precision
Is there a way to specify the precision of doubles when writing to xml_oarchive? Refer to the output snippet below. <Control class_id="3" tracking_level="0" version="1"> <Type>0</Type> <LowMZ>5.0499999999999998</LowMZ> <HiMZ>5.0999999999999996</HiMZ> I would like 5.05 and 5.1 for Low and High. Stepping through the code with the debugger I ended up in basic_text_oprimitive::save( const double t). void save(const double t) { if(os.fail()) boost::throw_exception(archive_exception(archive_exception::stream_error)); os << std::setprecision(std::numeric_limits<double>::digits10 + 2); os << t; } where it seems the precision is fixed. The only solution I can seem to think of is to split the serialization, and on the output convert my double to string (with the correct precsion) and then serialize the string. Is there a different or better way?
You should have the ostream reference around somewhere. In which case you could just os.setprecision(..) somewhere. If you did it before attaching to the archive it would be global. If you did it within a "save" it would be local - but in theory not portable - in practice it might be just fine for you. Robert Ramey jsanga@cox.net wrote:
Is there a way to specify the precision of doubles when writing to xml_oarchive? Refer to the output snippet below.
<Control class_id="3" tracking_level="0" version="1"> <Type>0</Type> <LowMZ>5.0499999999999998</LowMZ> <HiMZ>5.0999999999999996</HiMZ>
I would like 5.05 and 5.1 for Low and High. Stepping through the code with the debugger I ended up in basic_text_oprimitive::save( const double t).
void save(const double t) { if(os.fail())
boost::throw_exception(archive_exception(archive_exception::stream_error)); os << std::setprecision(std::numeric_limits<double>::digits10 + 2); os << t; }
where it seems the precision is fixed. The only solution I can seem to think of is to split the serialization, and on the output convert my double to string (with the correct precsion) and then serialize the string. Is there a different or better way?
participants (2)
-
jsanga@cox.net
-
Robert Ramey