There is something strange I noticed when comparing boost::lexical_cast and boost spirit parsing. I'm trying to parse a string into float. for some reason spirit gives very imprecise result. for example: when parsing string "219721.03839999999" using lexical_cast i get 219721.03 which is more or less OK. but when I use spirit (see code below) I get "219721.11" which is far from bein OK. Any idea why it happens?
template<>
inline float LexicalCastWithTag(const std::string& arg)
{
float result = 0;
if(arg.empty())
{
throw BadLexicalCast("Cannot convert from to std::string to float");
}
auto itBeg = arg.begin();
auto itEnd = arg.end();
if(!boost::spirit::qi::parse(itBeg, itEnd, boost::spirit::qi::float_, result) || itBeg != itEnd)
{
throw BadLexicalCast("Cannot convert from to std::string to float");
}
return result;
}