我正在尝试运行下面的程序,它可以工作,但当我输入一个超过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;
}发布于 2012-09-13 05:40:06
可以使用std::setprecision更改打印值的精度
cout << "You entered the two integers " << setprecision(20) << x
<< " and " << n << endl;Link to ideone.
https://stackoverflow.com/questions/12396437
复制相似问题