条件为真时,if语句返回false

时间:2019-09-01 19:08:27

标签: c++

我遇到的if语句在满足条件时并不总是返回true的麻烦(请参见下面的代码)。

此if语句处于一个循环中,该循环嵌入另一个循环中,该循环运行7次(6次,实际上是因为最后一个为空)迭代,并且我注意到它对于前3次迭代正常工作。引起我注意的是

  • 在循环1的第一次迭代中,条件在循环2的第三次迭代中得到满足,并且返回true
  • 在循环1的第二次迭代中,条件在循环2的第二次迭代中得到满足,并且返回true
  • 在循环2的第三次迭代中,条件在循环2的第一次迭代中得到满足,并且返回true
  • 在第4次迭代中,条件在所有情况下均为true,但始终返回false
  • 在第5次迭代中,条件在所有情况下均为false,并且返回false
  • 在第6次迭代中相同
  • 循环的7次迭代为空

void compute_neighbours(vector<vector<double>> &positions,double &GPmin, double &GPmax)
{
 vector<int> neighbours_i;
 neighbours.assign(positions.size(),neighbours_i);

 for (unsigned i=0; i<positions.size();i++)
   {
     cout << "Grid point " << i << " looking at :" << endl;
     for (unsigned k=i+1; k<positions.size();k++)
     {
      cout << " " << k << " distance: ";
      double r=delta(positions[k],positions[i]).modulo();
      cout << r << ", GPmin: " << GPmin << ", GPmax: " << GPmax;
      if ((r>=GPmin) and (r<=GPmax))
      {
        cout << "--adding point ";
        neighbours[i].push_back(k);
        neighbours[k].push_back(i);
      }

      cout << endl;
     }
   }
}

这是我获得的输出:

Grid point 0 looking at :
 1 distance: 0.212132, GPmin: 0, GPmax: 0.15
 2 distance: 0.212132, GPmin: 0, GPmax: 0.15
 3 distance: 0.15, GPmin: 0, GPmax: 0.15--adding point 
 4 distance: 0.212132, GPmin: 0, GPmax: 0.15
 5 distance: 0.212132, GPmin: 0, GPmax: 0.15
 6 distance: 0.3, GPmin: 0, GPmax: 0.15
Grid point 1 looking at :
 2 distance: 0.212132, GPmin: 0, GPmax: 0.15
 3 distance: 0.15, GPmin: 0, GPmax: 0.15--adding point 
 4 distance: 0.212132, GPmin: 0, GPmax: 0.15
 5 distance: 0.3, GPmin: 0, GPmax: 0.15
 6 distance: 0.212132, GPmin: 0, GPmax: 0.15
Grid point 2 looking at :
 3 distance: 0.15, GPmin: 0, GPmax: 0.15--adding point 
 4 distance: 0.3, GPmin: 0, GPmax: 0.15
 5 distance: 0.212132, GPmin: 0, GPmax: 0.15
 6 distance: 0.212132, GPmin: 0, GPmax: 0.15
Grid point 3 looking at :
 4 distance: 0.15, GPmin: 0, GPmax: 0.15
 5 distance: 0.15, GPmin: 0, GPmax: 0.15
 6 distance: 0.15, GPmin: 0, GPmax: 0.15
Grid point 4 looking at :
 5 distance: 0.212132, GPmin: 0, GPmax: 0.15
 6 distance: 0.212132, GPmin: 0, GPmax: 0.15
Grid point 5 looking at :
 6 distance: 0.212132, GPmin: 0, GPmax: 0.15
Grid point 6 looking at :
Grid Point 0's neighbours are: 3,
Grid Point 1's neighbours are: 3,
Grid Point 2's neighbours are: 3,
Grid Point 3's neighbours are: 0,1,2,
Grid Point 4's neighbours are: 
Grid Point 5's neighbours are: 
Grid Point 6's neighbours are: 
Maximum number of neighbours: 3

网格点3也应该与4、5和6相邻,但是由于某种原因,即使距离落在指定范围内,if语句也会返回false(在循环的其他迭代中不会发生)。

我尝试用手动编码的欧几里德距离替换delta()。modulo()函数,并获得相同的结果。

任何关于出问题的想法将不胜感激!

1 个答案:

答案 0 :(得分:4)

您遇到了舍入错误。 它打印0.15,但实际上是稍微较小或较大,因此比较失败。

例如,

#include <iostream>

int main() {
  double x = 0.15, y = 0.14999999999;
  if (x <= y)
    std::cout << "Equation returns true." << std::endl;
  else
    std::cout << "x = " << x << "; y = " << y << "; but x <= y is false." << std::endl;
}

可打印

  

x = 0.15; y = 0.15;但是x <= y为假。

请参见https://ideone.com/GoRkqD