c++ - 在 C++ 中设置 double 会给出奇怪的数字

标签 c++ floating-point precision iostream

我正在用 C++ 初始化一个二维 double 组,将一些元素设置为 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 位的数字。我知道计算机有 float 问题,但为什么我得到的一些值打印为 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]

我无法提出修复建议,因为我不清楚您的初始化逻辑。

关于c++ - 在 C++ 中设置 double 会给出奇怪的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39886849/

相关文章:

c++ - fwrite 在一台计算机上 float ,在另一台计算机上被 fread

c++ - iPhone 拇指和 VFP

python - 稳健的 numpy.float64 相等性测试

c++ - std::function 的类型别名

c++ - 在 C++ 中使用 For 循环查找素数

c - 这个数字如何表示为 2^-22*(1/10) 的倍数?那是怎么推导出来的?

C++ 如何避免尾随零?

c++ - 在 map C++ 中迭代和使用 find()

c++ - 无法在 Qt Creator 中修改选项卡设置

C# 十进制,如何添加尾随零