4

在比较 boost::lexical_cast 和 boost 精神解析时,我注意到了一些奇怪的事情。我正在尝试将字符串解析为浮点数。由于某种原因,精神给出了非常不精确的结果。例如:当使用 lexical_cast 解析字符串“219721.03839999999”时,我得到 219721.03,这或多或少是可以的。但是当我使用精神(见下面的代码)时,我得到“219721.11”,这远非正常。知道为什么会这样吗?

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;
}
4

1 回答 1

6

所以这可能是“浮动”类型解析器的限制/错误。尝试使用 double_ 解析器。

#include<iostream>
#include<iomanip>
#include<string>
#include<boost/spirit/include/qi.hpp>

int main()
{
    std::cout.precision(20);

    //float x=219721.03839999999f;  
    //std::cout << x*1.0f << std::endl;  
    //gives 219721.03125  

    double resultD;
    std::string arg="219721.03839999999";

    auto itBeg = arg.begin();
    auto itEnd = arg.end();
    if(!boost::spirit::qi::parse(itBeg, itEnd,boost::spirit::qi::double_,resultD) || itBeg != itEnd)
        std::cerr << "Cannot convert from std::string to double" << std::endl;
    else
        std::cout << "qi::double_:" << resultD << std::endl;

    float resultF;
    itBeg = arg.begin();
    itEnd = arg.end();
    if(!boost::spirit::qi::parse(itBeg, itEnd,boost::spirit::qi::float_,resultF) || itBeg != itEnd)
        std::cerr << "Cannot convert from std::string to float" << std::endl;
    else
        std::cout << "qi::float_ :" << resultF << std::endl;

    return 0;
}

输出:
qi::double_:219721.03839999999036
qi::float_:219721.109375

于 2013-06-30T17:35:42.377 回答