提升精神浮点数解析器的精度

时间:2013-06-30 14:26:42

标签: c++ boost-spirit lexical-cast

在比较boost :: lexical_cast和boost spirit解析时,我注意到了一些奇怪的东西。 我试图将字符串解析为float。由于某种原因,精神给出了非常不精确的结果。例如:解析字符串" 219721.03839999999"使用lexical_cast我得到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;
}

1 个答案:

答案 0 :(得分:5)

所以它可能是“float”类型解析器的限制/ bug。尝试使用double_ parser。

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

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