我正在尝试运行以下程序并且可以运行,但是当我输入的值超过小数点后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;
}
最佳答案
您可以使用 std::setprecision
更改打印值的精度:
cout << "You entered the two integers " << setprecision(20) << x
<< " and " << n << endl;
Link to ideone.