c++opencvgdbarithmeticexception

gdb arithmetic exception - no division by zero - runtime error


In order to get the mean value of a disparity map I had to write an own method to ignore negative disparity values:

float Utility::calcMeanDisparity(cv::Mat const& matrix)
{
  int total = 0;
  int numElements = 0;
  for(int r = 0; r < matrix.rows; ++r)
  {
    for(int c = 0; c < matrix.cols; ++c)
    {
      if(static_cast<float>(matrix.at<short>(r,c)) > 0)
      {
        total += static_cast<float>(matrix.at<short>(r,c));
        ++numElements;
      }
    }
  } 
  float mean = total / abs(numElements);
  std::cout << total << " / " << numElements << " = " << mean << std::endl;
  return mean;
}

but my code simply crashes after a certain time with the gdb error:

Program received signal SIGFPE, Arithmetic exception.
0x000000000044a992 in Utility::calcMeanDisparity (matrix=...) at src/utility.cpp:250
250   float mean = total / abs(numElements);

But there is no way that I am doing a division by zero. These are three of the last lines my code is trying to calculate:

950149 / 4275 = 222
804412 / 4429 = 181
873770 / 4253 = 205

I also added the abs(foo) stuff to be completely safe of divisions by zero.

Actually I do not have any idea what to do at the moment.

Maybe one of you guys has an idea for that.


Solution

  • Thanks to all the help, but the problem was easier than expected.

    From time to time the disparity map contained areas with zero / negative values in it. so the whole submatrix I passed into the function had a mean of 0 this is why the if condition was ignored. So the final Division was 0 / 0 --> arithmetic exception.