1

I made a Matrix class. and added some methods for matrix calculation. I met some problems related with floating point operation. First time, I made a eqaul method like below:

bool
FloatMatrix4::operator ==(const FloatMatrix4& rhs) const
{
  if (this == &rhs)
    return true;
  return (matrix[0][0] == rhs.matrix[0][0] &&
          matrix[0][1] == rhs.matrix[0][1] &&
          matrix[0][2] == rhs.matrix[0][2] &&
          matrix[0][3] == rhs.matrix[0][3] &&
          matrix[1][0] == rhs.matrix[1][0] &&
          matrix[1][1] == rhs.matrix[1][1] &&
          matrix[1][2] == rhs.matrix[1][2] &&
          matrix[1][3] == rhs.matrix[1][3] &&
          matrix[2][0] == rhs.matrix[2][0] &&
          matrix[2][1] == rhs.matrix[2][1] &&
          matrix[2][2] == rhs.matrix[2][2] &&
          matrix[2][3] == rhs.matrix[2][3] &&
          matrix[3][0] == rhs.matrix[3][0] &&
          matrix[3][1] == rhs.matrix[3][1] &&
          matrix[3][2] == rhs.matrix[3][2] &&
          matrix[3][3] == rhs.matrix[3][3]);
}

Under case, it returned wrong answer.

m1[4][4] = 
 1.000000, 0.000000, 0.000000, 1.000000,
 2.000000, 1.000000, 0.000000, 1.000000,
 3.000000, 1.100000, 1.000000, 1.000000,
 4.000000, 0.000000, 0.000000, 1.000000`

m2[4][4] = 
1.000000, 0.000000, 0.000000, 1.000000,
2.000000, 1.000000, 0.000000, 1.000000,
3.000000, 1.100000, 1.000000, 1.000000,
4.000000, 0.000000, -0.000000, 1.000000

I expected m1 and m2 are same, because m2 was made with 2-times inverted m1.

So I though matrix comparison operation should include floating pointer comparison algorithm. like a AlMostEquals() or floatHardCompare() method.

bool matrixCompare(const FloatMatrix4& m1, const FloatMatrix4& m2)
{
  return (
    floatCompare(m1.matrix[0][0], m2.matrix[0][0], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[0][1], m2.matrix[0][1], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[0][2], m2.matrix[0][2], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[0][3], m2.matrix[0][3], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[1][0], m2.matrix[1][0], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[1][1], m2.matrix[1][1], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[1][2], m2.matrix[1][2], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[1][3], m2.matrix[1][3], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[2][0], m2.matrix[2][0], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[2][1], m2.matrix[2][1], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[2][2], m2.matrix[2][2], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[2][3], m2.matrix[2][3], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[3][0], m2.matrix[3][0], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[3][1], m2.matrix[3][1], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[3][2], m2.matrix[3][2], ALMOST_ZERO)==0 &&
    floatCompare(m1.matrix[3][3], m2.matrix[3][3], ALMOST_ZERO)==0);
}

ALMOST_ZERO = 0.0000000001 or DOUBLE_EPSILON

I'd like to ask that. Generally, are the matrix4 implementations considered floating point comparison logic in the graphics library or graphic engine (include game engine)? or not?

Anyone has more good idea?

Ahmad Khan
  • 2,655
  • 19
  • 25
mika
  • 11
  • 1
  • Possible duplicate of [What is the most effective way for float and double comparison?](https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison) – KjMag Sep 18 '17 at 11:13
  • I wanna know , how did other framework or engine solve this issue? like a unity or unreal engine, or cairo.. and etc... If i use floating point comparison logic in matrix comparison, it would make performance issue. bool FloatMatrix4::operator == (const FloatMatrix4& m2) { return matrixCompare(*this, m2); } – mika Sep 19 '17 at 01:07

0 Answers0