Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我输入的数字只能读到6位有效数字?_C++_Double_Decimal_Precision - Fatal编程技术网

C++ 为什么我输入的数字只能读到6位有效数字?

C++ 为什么我输入的数字只能读到6位有效数字?,c++,double,decimal,precision,C++,Double,Decimal,Precision,我正在尝试运行以下程序,但当我输入的值超过6位小数时,它会不断舍入/截断,例如2.9999999-->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 "

我正在尝试运行以下程序,但当我输入的值超过6位小数时,它会不断舍入/截断,例如2.9999999-->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;

}
intmain()
{
双n=0,x=0;
while(cin>>n>>x)//将一直运行,直到未输入整数为止
{

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


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


cout您确定它是在输入时舍入/截断,还是在输出时发生?IIRC,
cout
的双精度标准格式默认为6个有效位左右,除非您设置适当的标志来告诉它不是这样……是的,它是在输出时发生的……两个建议。首先,注释和文本参考“integer”,但值的类型为double;这可能是固定的。其次,如果使用
abs(x-n),测试可以变得更简单
这样就只需要一个测试了。是的,很抱歉我在剪切和更改代码,我想我忘了更改文本和注释XD。感谢abs提示…我刚刚开始学习,正在阅读stroustrup的书,我还没有遇到过这样的问题。你确定这是输入的舍入/截断,还是在o上发生的utput?IIRC,
cout
的双精度标准格式默认为6个有效位置左右,除非您设置了相应的标志告诉它不是这样…是的,它发生在输出上…两个建议。首先,注释和文本指的是“整数”,但值的类型为double;这可能应该是固定的。其次,如果使用
abs(x-n),测试可以变得更简单
这样就只需要一个测试了。是的,很抱歉我在剪切和更改代码,我想我忘了更改文本和注释XD。感谢abs提示…我刚刚开始学习,正在阅读stroustrup的书,我还没有发现,是的,在cout语句之前没有办法设置精度,即a我声明n和x的起始位置是否正确?@user1542646绝对-您可以在此网站上自由移动
cout-Wow-yes-sorry-total newb!!在cout语句之前,即在我声明n和x的起始位置,是否无法设置精度?@user1542646绝对-您可以在此网站上自由移动
cout-Wow-yes-sorry-total newb!!
cout << "You entered the two integers " << setprecision(20) << x
     << " and " << n << endl;