在c++中设置double会得到奇怪的数字

Setting double in c++ gives strange numbers

本文关键字:数字 c++ 设置 double      更新时间:2023-10-16

我正在c++中初始化一个2D双数组,将一些元素设置为0,其他元素设置为100。然而,在初始化该值之后,我将其打印出来,并得到真正奇怪的数字(如下所示):

6.91599e-310
2.96439e-323
6.95262e-310
3.20006e-319
6.52231e-319
4.94066e-324
1.63971e-319

我怎样才能使系统输出0而不是这个?我已经尝试过fixed << setprecision(4) << setw(10),但我仍然会得到宽度远远大于10的数字,小数点后超过4位。我知道计算机有浮点的问题,但为什么我得到一些值打印为0和其他如上面的?我真的很困惑。

编辑:

这是我的(相关)代码:

void initializeArray (double Array[20][20]) {
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            if ((j == 0) || (j == 19)) {
                Array[i][j] = 0.0;
            }
            else if ((i == 0) || (i == 19)) {
                Array[i][j] = 100.0;
            }
        }
    }
}
void printArray (double Array[20][20]) {
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 19; j++) {
            cout << setprecision(4) << fixed << setw(10) << Array[i][j] << ",";
        }
        cout << setprecision(4) << fixed << setw(10) << Array[i][19] << endl;
    }
}

下面的代码块似乎有问题。

         if ((j == 0) || (j == 19)) {
            Array[i][j] = 0.0;
         }
         else if ((i == 0) || (i == 19)) {
            Array[i][j] = 100.0;
         }

它忽略了数组中的许多元素。我敢打赌,这些元素都没有初始化。

下面是一个更新后的程序,演示了许多数组元素没有初始化。

#include <iostream>
void initializeArray (double Array[20][20]) {
   for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
         if ((j == 0) || (j == 19)) {
            Array[i][j] = 0.0;
         }
         else if ((i == 0) || (i == 19)) {
            Array[i][j] = 100.0;
         }
         else
         {
            std::cout << "Not setting the value of Array[" << i << "][" << j << "]n";
         }
      }
   }
}
int main()
{
   double Array[20][20];
   initializeArray (Array);
}
输出:

Not setting the value of Array[1][1]
Not setting the value of Array[1][2]
Not setting the value of Array[1][3]
Not setting the value of Array[1][4]
Not setting the value of Array[1][5]
Not setting the value of Array[1][6]
Not setting the value of Array[1][7]
Not setting the value of Array[1][8]
Not setting the value of Array[1][9]
Not setting the value of Array[1][10]
Not setting the value of Array[1][11]
Not setting the value of Array[1][12]
Not setting the value of Array[1][13]
Not setting the value of Array[1][14]
Not setting the value of Array[1][15]
Not setting the value of Array[1][16]
Not setting the value of Array[1][17]
Not setting the value of Array[1][18]
Not setting the value of Array[2][1]
Not setting the value of Array[2][2]
Not setting the value of Array[2][3]
Not setting the value of Array[2][4]
Not setting the value of Array[2][5]
Not setting the value of Array[2][6]
Not setting the value of Array[2][7]
Not setting the value of Array[2][8]
Not setting the value of Array[2][9]
Not setting the value of Array[2][10]
Not setting the value of Array[2][11]
Not setting the value of Array[2][12]
Not setting the value of Array[2][13]
Not setting the value of Array[2][14]
Not setting the value of Array[2][15]
Not setting the value of Array[2][16]
Not setting the value of Array[2][17]
Not setting the value of Array[2][18]
Not setting the value of Array[3][1]
Not setting the value of Array[3][2]
Not setting the value of Array[3][3]
Not setting the value of Array[3][4]
Not setting the value of Array[3][5]
Not setting the value of Array[3][6]
Not setting the value of Array[3][7]
Not setting the value of Array[3][8]
Not setting the value of Array[3][9]
Not setting the value of Array[3][10]
Not setting the value of Array[3][11]
Not setting the value of Array[3][12]
Not setting the value of Array[3][13]
Not setting the value of Array[3][14]
Not setting the value of Array[3][15]
Not setting the value of Array[3][16]
Not setting the value of Array[3][17]
Not setting the value of Array[3][18]
Not setting the value of Array[4][1]
Not setting the value of Array[4][2]
Not setting the value of Array[4][3]
Not setting the value of Array[4][4]
Not setting the value of Array[4][5]
Not setting the value of Array[4][6]
Not setting the value of Array[4][7]
Not setting the value of Array[4][8]
Not setting the value of Array[4][9]
Not setting the value of Array[4][10]
Not setting the value of Array[4][11]
Not setting the value of Array[4][12]
Not setting the value of Array[4][13]
Not setting the value of Array[4][14]
Not setting the value of Array[4][15]
Not setting the value of Array[4][16]
Not setting the value of Array[4][17]
Not setting the value of Array[4][18]
Not setting the value of Array[5][1]
Not setting the value of Array[5][2]
Not setting the value of Array[5][3]
Not setting the value of Array[5][4]
Not setting the value of Array[5][5]
Not setting the value of Array[5][6]
Not setting the value of Array[5][7]
Not setting the value of Array[5][8]
Not setting the value of Array[5][9]
Not setting the value of Array[5][10]
Not setting the value of Array[5][11]
Not setting the value of Array[5][12]
Not setting the value of Array[5][13]
Not setting the value of Array[5][14]
Not setting the value of Array[5][15]
Not setting the value of Array[5][16]
Not setting the value of Array[5][17]
Not setting the value of Array[5][18]
Not setting the value of Array[6][1]
Not setting the value of Array[6][2]
Not setting the value of Array[6][3]
Not setting the value of Array[6][4]
Not setting the value of Array[6][5]
Not setting the value of Array[6][6]
Not setting the value of Array[6][7]
Not setting the value of Array[6][8]
Not setting the value of Array[6][9]
Not setting the value of Array[6][10]
Not setting the value of Array[6][11]
Not setting the value of Array[6][12]
Not setting the value of Array[6][13]
Not setting the value of Array[6][14]
Not setting the value of Array[6][15]
Not setting the value of Array[6][16]
Not setting the value of Array[6][17]
Not setting the value of Array[6][18]
Not setting the value of Array[7][1]
Not setting the value of Array[7][2]
Not setting the value of Array[7][3]
Not setting the value of Array[7][4]
Not setting the value of Array[7][5]
Not setting the value of Array[7][6]
Not setting the value of Array[7][7]
Not setting the value of Array[7][8]
Not setting the value of Array[7][9]
Not setting the value of Array[7][10]
Not setting the value of Array[7][11]
Not setting the value of Array[7][12]
Not setting the value of Array[7][13]
Not setting the value of Array[7][14]
Not setting the value of Array[7][15]
Not setting the value of Array[7][16]
Not setting the value of Array[7][17]
Not setting the value of Array[7][18]
Not setting the value of Array[8][1]
Not setting the value of Array[8][2]
Not setting the value of Array[8][3]
Not setting the value of Array[8][4]
Not setting the value of Array[8][5]
Not setting the value of Array[8][6]
Not setting the value of Array[8][7]
Not setting the value of Array[8][8]
Not setting the value of Array[8][9]
Not setting the value of Array[8][10]
Not setting the value of Array[8][11]
Not setting the value of Array[8][12]
Not setting the value of Array[8][13]
Not setting the value of Array[8][14]
Not setting the value of Array[8][15]
Not setting the value of Array[8][16]
Not setting the value of Array[8][17]
Not setting the value of Array[8][18]
Not setting the value of Array[9][1]
Not setting the value of Array[9][2]
Not setting the value of Array[9][3]
Not setting the value of Array[9][4]
Not setting the value of Array[9][5]
Not setting the value of Array[9][6]
Not setting the value of Array[9][7]
Not setting the value of Array[9][8]
Not setting the value of Array[9][9]
Not setting the value of Array[9][10]
Not setting the value of Array[9][11]
Not setting the value of Array[9][12]
Not setting the value of Array[9][13]
Not setting the value of Array[9][14]
Not setting the value of Array[9][15]
Not setting the value of Array[9][16]
Not setting the value of Array[9][17]
Not setting the value of Array[9][18]
Not setting the value of Array[10][1]
Not setting the value of Array[10][2]
Not setting the value of Array[10][3]
Not setting the value of Array[10][4]
Not setting the value of Array[10][5]
Not setting the value of Array[10][6]
Not setting the value of Array[10][7]
Not setting the value of Array[10][8]
Not setting the value of Array[10][9]
Not setting the value of Array[10][10]
Not setting the value of Array[10][11]
Not setting the value of Array[10][12]
Not setting the value of Array[10][13]
Not setting the value of Array[10][14]
Not setting the value of Array[10][15]
Not setting the value of Array[10][16]
Not setting the value of Array[10][17]
Not setting the value of Array[10][18]
Not setting the value of Array[11][1]
Not setting the value of Array[11][2]
Not setting the value of Array[11][3]
Not setting the value of Array[11][4]
Not setting the value of Array[11][5]
Not setting the value of Array[11][6]
Not setting the value of Array[11][7]
Not setting the value of Array[11][8]
Not setting the value of Array[11][9]
Not setting the value of Array[11][10]
Not setting the value of Array[11][11]
Not setting the value of Array[11][12]
Not setting the value of Array[11][13]
Not setting the value of Array[11][14]
Not setting the value of Array[11][15]
Not setting the value of Array[11][16]
Not setting the value of Array[11][17]
Not setting the value of Array[11][18]
Not setting the value of Array[12][1]
Not setting the value of Array[12][2]
Not setting the value of Array[12][3]
Not setting the value of Array[12][4]
Not setting the value of Array[12][5]
Not setting the value of Array[12][6]
Not setting the value of Array[12][7]
Not setting the value of Array[12][8]
Not setting the value of Array[12][9]
Not setting the value of Array[12][10]
Not setting the value of Array[12][11]
Not setting the value of Array[12][12]
Not setting the value of Array[12][13]
Not setting the value of Array[12][14]
Not setting the value of Array[12][15]
Not setting the value of Array[12][16]
Not setting the value of Array[12][17]
Not setting the value of Array[12][18]
Not setting the value of Array[13][1]
Not setting the value of Array[13][2]
Not setting the value of Array[13][3]
Not setting the value of Array[13][4]
Not setting the value of Array[13][5]
Not setting the value of Array[13][6]
Not setting the value of Array[13][7]
Not setting the value of Array[13][8]
Not setting the value of Array[13][9]
Not setting the value of Array[13][10]
Not setting the value of Array[13][11]
Not setting the value of Array[13][12]
Not setting the value of Array[13][13]
Not setting the value of Array[13][14]
Not setting the value of Array[13][15]
Not setting the value of Array[13][16]
Not setting the value of Array[13][17]
Not setting the value of Array[13][18]
Not setting the value of Array[14][1]
Not setting the value of Array[14][2]
Not setting the value of Array[14][3]
Not setting the value of Array[14][4]
Not setting the value of Array[14][5]
Not setting the value of Array[14][6]
Not setting the value of Array[14][7]
Not setting the value of Array[14][8]
Not setting the value of Array[14][9]
Not setting the value of Array[14][10]
Not setting the value of Array[14][11]
Not setting the value of Array[14][12]
Not setting the value of Array[14][13]
Not setting the value of Array[14][14]
Not setting the value of Array[14][15]
Not setting the value of Array[14][16]
Not setting the value of Array[14][17]
Not setting the value of Array[14][18]
Not setting the value of Array[15][1]
Not setting the value of Array[15][2]
Not setting the value of Array[15][3]
Not setting the value of Array[15][4]
Not setting the value of Array[15][5]
Not setting the value of Array[15][6]
Not setting the value of Array[15][7]
Not setting the value of Array[15][8]
Not setting the value of Array[15][9]
Not setting the value of Array[15][10]
Not setting the value of Array[15][11]
Not setting the value of Array[15][12]
Not setting the value of Array[15][13]
Not setting the value of Array[15][14]
Not setting the value of Array[15][15]
Not setting the value of Array[15][16]
Not setting the value of Array[15][17]
Not setting the value of Array[15][18]
Not setting the value of Array[16][1]
Not setting the value of Array[16][2]
Not setting the value of Array[16][3]
Not setting the value of Array[16][4]
Not setting the value of Array[16][5]
Not setting the value of Array[16][6]
Not setting the value of Array[16][7]
Not setting the value of Array[16][8]
Not setting the value of Array[16][9]
Not setting the value of Array[16][10]
Not setting the value of Array[16][11]
Not setting the value of Array[16][12]
Not setting the value of Array[16][13]
Not setting the value of Array[16][14]
Not setting the value of Array[16][15]
Not setting the value of Array[16][16]
Not setting the value of Array[16][17]
Not setting the value of Array[16][18]
Not setting the value of Array[17][1]
Not setting the value of Array[17][2]
Not setting the value of Array[17][3]
Not setting the value of Array[17][4]
Not setting the value of Array[17][5]
Not setting the value of Array[17][6]
Not setting the value of Array[17][7]
Not setting the value of Array[17][8]
Not setting the value of Array[17][9]
Not setting the value of Array[17][10]
Not setting the value of Array[17][11]
Not setting the value of Array[17][12]
Not setting the value of Array[17][13]
Not setting the value of Array[17][14]
Not setting the value of Array[17][15]
Not setting the value of Array[17][16]
Not setting the value of Array[17][17]
Not setting the value of Array[17][18]
Not setting the value of Array[18][1]
Not setting the value of Array[18][2]
Not setting the value of Array[18][3]
Not setting the value of Array[18][4]
Not setting the value of Array[18][5]
Not setting the value of Array[18][6]
Not setting the value of Array[18][7]
Not setting the value of Array[18][8]
Not setting the value of Array[18][9]
Not setting the value of Array[18][10]
Not setting the value of Array[18][11]
Not setting the value of Array[18][12]
Not setting the value of Array[18][13]
Not setting the value of Array[18][14]
Not setting the value of Array[18][15]
Not setting the value of Array[18][16]
Not setting the value of Array[18][17]
Not setting the value of Array[18][18]

我无法建议修复,因为你的初始化逻辑对我来说不清楚。