使用词法强制转换在'string to double'中设置double变量的精度

Sjs*_*Sjs 2 c++ precision point-cloud-library boost-lexicalcast

我有一个字符串,其值为"496934.079".我使用boost :: lexical_cast()将其转换为双变量(名为pVar).

但是,转换后存储在pVar中的值是"496934"而不是"496934.079".缺少小数点后的数字.我看过的std :: setprecision()在输出窗口写有所需精度的双重价值在这里.但它只显示给定精度的值,并且不会将其保存在具有给定精度的变量中.

但是我希望将双精度值完全保存在字符串变量str中,以便pVar中的值为"496934.079".如何以与输入字符串中相同的精度将值存储在变量中,以便我可以使用该变量进行进一步处理?

我的代码片段是:

int func()
{
    string str = "496934.079";
    double pVar = boost::lexical_cast<double> (str);
    cout << pVar << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:从字符串到浮点数的转换是我正在开发的代码的一部分,它逐行解析文本文件中的值并将它们写入另一个文件(.pcd格式).我正在添加我用于此转换的整个代码以供参考.

int txt2pcd()
{
    ifstream is("input_text_file.txt");                                         //read input text file
    string str;
    using namespace boost::algorithm;
    pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
    while(getline(is, str))                                                     //parsing the text file contents line-by-line
    {   
        std::string s = str.c_str();
        std::vector<std::string> result;            
        std::istringstream iss(s);
        for(std::string s; iss >> s; )
            result.push_back(s);

        pcl::PointXYZ point;

        point.x = boost::lexical_cast<double> (result[0]);                      //string to float conversion for adding the X coordinate of the point
        point.y = boost::lexical_cast<double> (result[1]);                      //string to float conversion for adding the Y coordinate of the point
        point.z = boost::lexical_cast<double> (result[2]);                      //string to float conversion for adding the Z coordinate of the point

        point_cloud_ptr->points.push_back (point);
        point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();
        point_cloud_ptr->height = 1;            
    }   
    pcl::io::savePCDFile ("output_pcd_file.pcd", *point_cloud_ptr, false);    //this is where the converted float values are stored (with digits altered after decimal point when compared to text file)
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

下面是文本文件中几行示例和转换后的'.pcd'文件中的相应行.

在文本文件中:

496934.999 5419547.239 265.179

496933.981 5419542.579 263.819

496934.891 5419545.399 264.849

496934.939 5419546.329 265.111

496934.829 5419544.489 264.781

在".pcd"文件中(由boost :: lexical_cast()生成的相应值)

496935 5419547 265.17899

496933.97 5419542.5 263.819

496934.91 5419545.5 264.849

496934.94 5419546.5 265.11099

496934.84 5419544.5 264.78101

请注意,这些值要么四舍五入,要么在输出'.pcd'文件中更改小数点后的数字.这可能是什么原因?

San*_*ker 6

boost::lexical_cast<double>(str)已经返回完整的值496934.079作为double.该完整值存储在其中pVar,并可用于进一步处理.

string str = "496934.079";
double pVar = boost::lexical_cast<double>(str); // pVar now contains 496934.079
Run Code Online (Sandbox Code Playgroud)

std::setprecision不影响存储的价值-它只是设置的位数(小数点前后)被显示.默认显示精度为6,所以:

std::cout << pVar << std::endl; // this prints "496934" (6 digits)
Run Code Online (Sandbox Code Playgroud)

以更高的精度显示存储的值(例如10)给出:

#include <iomanip>

std::cout << std::setprecision(10) << pVar << std::endl; // this prints "496934.079"
Run Code Online (Sandbox Code Playgroud)

但重新迭代:这些都不会影响存储的价值.pVar仍然包含496934.079.例如 :

std::cout << (int) (pVar * 1000) << std::endl; // this displays 496934079
Run Code Online (Sandbox Code Playgroud)

编辑

问题中更新的代码显示实际上,a float用于存储值,而不是double(ref.pcl::PointXYZ).

A float没有足够的精度来存储整个值496934.079- 相反它会存储496934.09375它可以表示的最佳近似值(参考例如,float和double之间有什么区别?).

如果您想确保您可以保留全部价值(未经修改),请务必坚持double.

  • 这个答案与这个问题不符.似乎`cout`的用法仅用于阐述; OP正在使用`boost :: lexical_cast`并特别询问了这一点.你必须显示替代品(例如`stringstream`和`.str()`). (2认同)
  • @Sjs:你的更新代码使用`float`,而不是`double`(ref.[`pcl :: PointXYZ`](http://docs.pointclouds.org/trunk/structpcl_1_1_point_x_y_z.html)).`float`没有精度来存储要存储在其中的完整值.参考.例如./sf/ask/356899091/ (2认同)