简体   繁体   English

从文本文件C ++读入时奇怪的数字舍入

[英]Strange rounding of numbers when reading in from text file C++

I have a text line containing only the following lines. 我有一个只包含以下行的文本行。

0.01180994648909809 0.0118339243907452 0.01153905217670122

0.0376759911531237 0.03771224865527065 0.03765957194275842

I used the following code to read this data and output it to terminal 我使用以下代码读取此数据并将其输出到终端

using namespace std;

    int main(int argc, char *argv[])
    {

      ifstream infile(argv[1]);
      string line;
      double a,b,c;

      while(getline(infile,line))
      {
        istringstream iss(line);
        iss >> a >> b >> c;
        cout<<a<<"\t"<< b << "\t"<<c<<endl;
       }

  return 0;}

The output I got was 我得到的输出是

0.0118099   0.0118339   0.0115391
0.037676    0.0377122   0.0376596

Why is it that in the output the numbers have been rounded to 7 digits after the decimal? 为什么在输出中数字已经舍入到小数点后的7位数? Is this rounding performed only while displaying to standard output? 这种舍入仅在显示标准输出时执行吗?

EDIT: Moving the proposed solution at top of the relevant information. 编辑:在相关信息的顶部移动建议的解决方案。

You can use set::precision to see the proper precision. 您可以使用set :: precision查看正确的精度。

Apart from the answer above, It is important to note that Whenever, You use float and decimal numbers Rounding Errors & Precision are an definite factor. 除了上面的答案,重要的是要注意,无论何时,你使用浮点数和十进制数舍入误差精度是一个明确的因素。

What is an Precision Error? 什么是精度误差?

The precision of a floating point number is how many digits it can represent without losing any information it contains. 浮点数的精度是它可以表示的位数,而不会丢失它包含的任何信息。

Consider the fraction 1/3 . 考虑1/3的分数。 The decimal representation of this number is 0.33333333333333… with 3′s going out to infinity. 这个数字的十进制表示是0.33333333333333…其中3表示无穷大。 An infinite length number would require infinite memory to be depicted with exact precision, but float or double data types typically only have 4 or 8 bytes. 无限长度数将要求以精确精度描绘无限存储器,但floatdouble数据类型通常仅具有48个字节。 Thus Floating point & double numbers can only store a certain number of digits, and the rest are bound to get lost. 因此,浮点数和双数字只能存储一定数量的数字,其余数字必然会丢失。 Thus, there is no definite accurate way of representing float or double numbers with numbers that require more precision than the variables can hold. 因此,没有明确准确的方法来表示浮点数或双数字,其数字需要比变量可以容纳更多的精度。

What is a Rounding Error? 什么是舍入误差?

There is a non-obvious differences between binary and decimal (base 10) numbers. binarydecimal (base 10)之间存在非明显差异。
Consider the fraction 1/10 . 考虑1/10的分数。 In decimal , this can be easily represented as 0.1 , and 0.1 can be thought of as an easily representable number. decimal ,这可以很容易地表示为0.1 ,并且0.1可以被认为是易于表示的数字。 However, in binary, 0.1 is represented by the infinite sequence: 0.00011001100110011… 但是,在二进制中, 0.1由无限序列表示: 0.00011001100110011…

An example: 一个例子:

#include <iomanip>
int main()
{
    using namespace std;
    cout << setprecision(17);
    double dValue = 0.1;
    cout << dValue << endl;
}

This output is: 这个输出是:

0.10000000000000001

And not 并不是

0.1.

This is because the double had to truncate the approximation due to it's limited memory, which results in a number that is not exactly 0.1 . 这是因为双重必须截断近似值,因为它的内存有限,导致数字不完全0.1 Such an scenario is called a Rounding error . 这种情况称为舍入误差

So be aware of these errors when you use floar or double. 因此,在使用floar或double时请注意这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM