我已经阅读了很多答案,但我找不到能解决问题的答案。
我编写了以下Matrix
类,其中包含两个检查Equality的方法:一个从基类中重写,另一个从IEquatable<Matrix>
重写。
以下是方法:
public override bool Equals(object obj)
{
return Equals(obj as Matrix);
}
public bool Equals(Matrix other)
{
if (RowsCount != other.RowsCount || ColumnsCount != other.ColumnsCount)
return false;
for (int i = 0; i < RowsCount; i++)
{
for (int j = 0; j < ColumnsCount; j++)
{
if (Math.Abs(Values[i][j] - other.Values[i][j]) > EqDiff)
return false;
}
}
return true;
}
当我在这样的代码中检查相等性时,它按预期工作:
Matrix M1 = new Matrix(new[]{
new double[] {1, 2, 0},
new double[] {-1, 1, 1},
new double[] {1, 0, 0}
});
Matrix M2 = new Matrix(new[]{
new double[] {0, 5, 3},
new double[] {1, 2, 8},
new double[] {4, 5, 6}
});
Matrix M1sumM2 = new Matrix(new[]{
new double[] {1, 7, 3},
new double[] {0, 3, 9},
new double[] {5, 5, 6}
});
Matrix r = M1 + M2;
bool e = M1sumM2.Equals(r); //here I got true
但是当我调用Assert.AreEqual时,它返回false。