1

我正在尝试运行以下程序并且它可以工作,但是当我输入一个超过 6 位小数的值时,它会不断被舍入/截断,例如 2.999999 --> 3。如何设置它以使其停止这样做?

int main()
{

    double n=0, x=0; 

    while (cin >> n >> x) //will keep going until an integer is not entered
    {
       cout << "You entered the two integers " << x << " and " << n << endl;

       if (x-n <= (1.0/10000000) && n-x <= (1.0/10000000)) 
          cout << "The numbers are almost equal" << endl;
    }

return 0;

}
4

1 回答 1

1

您可以使用以下方法更改打印值的精度std::setprecision

cout << "You entered the two integers " << setprecision(20) << x
     << " and " << n << endl;

链接到ideone。

于 2012-09-12T21:40:05.580 回答