在 Vector 上进行 push_back 时设置精度

发布于 2024-12-17 06:44:44 字数 694 浏览 3 评论 0原文

我正在逐行读取 CSV,并对每个逗号分隔值进行标记。每个标记都是字符串类型。我将它放入 float 类型的向量中。在下面的示例中,如果 csv 中的值是 "0.08" , *beg = "0.08" ,但在向量 v 中它是 "0.079999998"

是否可以通过某种方式将向量中的精度设置为小数点后 3 位地方或者什么的。

例子:

string line;
boost::char_separator<char> sep(",");
typedef boost::tokenizer< boost::char_separator<char> > t_tokenizer;
ifstream myfile (fileName);

if(myfile.is_open())
{
    while (myfile.good())
    {
        getline (myfile,line);
        t_tokenizer tok(line, sep);

        for (t_tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
             string temp = *beg;
             this->v.push_back(::atof(temp.c_str()));
        }

I'm reading from a CSV, line by line and tokenizing each comma separated value. each token is a string type. and I'm putting it into a vector of type float. In the example below, if for example if the value in the csv is "0.08" , *beg = "0.08" , but in vector v it is "0.079999998"

Is there someway that I can set the precision in the vector to 3 decimal places or something.

example:

string line;
boost::char_separator<char> sep(",");
typedef boost::tokenizer< boost::char_separator<char> > t_tokenizer;
ifstream myfile (fileName);

if(myfile.is_open())
{
    while (myfile.good())
    {
        getline (myfile,line);
        t_tokenizer tok(line, sep);

        for (t_tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
             string temp = *beg;
             this->v.push_back(::atof(temp.c_str()));
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-12-24 06:44:44

这不是浮动的问题。您无法准确表示 0.8,但不用担心 - 只需输出具有所需精度的值:

#include <iomanip>   // for fixed and setprecision
#include <iostream>  // for cout
#include <cstdio>    // for printf

for (auto it = v.cbegin(), end = v.cend(); it != end; ++it)
{
  std::cout << std::fixed << std::setprecision(3) << *it << std::endl;
}

或者,您可以使用 std::printf("%.3f\n", *it )

如果您确实想在数据结构中存储精确值,则不能使用普通浮点数。您可以使用某种整数的定点解释(例如,以 1/1000 为单位测量所有内容),也可以使用小数浮点数(很少见),或者可以存储有理数(商)的整数)。如果您只进行加法和减法,那么定点将是自然的选择。

This isn't a problem with the float. You cannot represent 0.8 exactly, but no worries -- simply output the values with the desired precision:

#include <iomanip>   // for fixed and setprecision
#include <iostream>  // for cout
#include <cstdio>    // for printf

for (auto it = v.cbegin(), end = v.cend(); it != end; ++it)
{
  std::cout << std::fixed << std::setprecision(3) << *it << std::endl;
}

Alternatively, you can use std::printf("%.3f\n", *it).

If you truly want to store exact values in your data structure, you cannot use normal floats. You can either use some sort of fixed-point interpretation of integers (e.g. measure everything in units of 1/1000), or you can use decimal floats (rare), or you can store rational numbers (quotients of integers). If you only do addition and subtraction, fixed-point would be the natural way to go.

不顾 2024-12-24 06:44:44

您正在使用 atof,这意味着您正在使用 float 来保存数据。浮点值不能像您期望的那样准确地保存以 10 为基数的值。像这样简单的数字可能没有很好的二进制表示。

您有几个选择:

  1. 正确处理不精确问题。在处理浮点数时,您必须始终注意精度,因此,如果您想将该数字显示为最接近的小数点后两位,请进行适当的舍入,它总是会按照您想要的方式工作。

  2. 仅使用整数。如果您只需要小数点后 2 位精度,只需将值存储为 int 并乘以 100。因此 0.08 存储为 8< /代码>。编写您自己的函数以直接解析为这种格式。

You are using atof, which implies you're using float to hold the data. Floating point values do not hold base-10 values as accurately as you might expect. So simple numbers like this may not have a good binary representation.

You have a couple options:

  1. Deal with imprecision properly. You must always be aware of precision when dealing with floating point, so if you want to display that number to the nearest 2 decimal places, do the proper rounding and it will always work like you want.

  2. Use integers only. If you only ever need 2 digits of precision after the decimal point, just store the values as int which are multiplied by 100. So 0.08 is stored as 8. Write your own functions to parse directly into this format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文