在 Vector 上进行 push_back 时设置精度
我正在逐行读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是浮动的问题。您无法准确表示 0.8,但不用担心 - 只需输出具有所需精度的值:
或者,您可以使用
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:
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.
您正在使用
atof
,这意味着您正在使用float
来保存数据。浮点值不能像您期望的那样准确地保存以 10 为基数的值。像这样简单的数字可能没有很好的二进制表示。您有几个选择:
正确处理不精确问题。在处理浮点数时,您必须始终注意精度,因此,如果您想将该数字显示为最接近的小数点后两位,请进行适当的舍入,它总是会按照您想要的方式工作。
仅使用整数。如果您只需要小数点后 2 位精度,只需将值存储为
int
并乘以 100。因此0.08
存储为8< /代码>。编写您自己的函数以直接解析为这种格式。
You are using
atof
, which implies you're usingfloat
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:
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.
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. So0.08
is stored as8
. Write your own functions to parse directly into this format.