public equals ( Matrix matrix, int precision ) : bool | ||
matrix | Matrix | The other matrix to compare. |
precision | int | The number of decimal places of precision to use. |
return | bool |
public bool equals(Matrix matrix, int precision)
{
if (precision < 0)
{
throw new MatrixError("Precision can't be a negative number.");
}
double test = Math.Pow(10.0, precision);
if (double.IsInfinity(test) || (test > long.MaxValue))
{
throw new MatrixError("Precision of " + precision
+ " decimal places is not supported.");
}
precision = (int) Math.Pow(10, precision);
double[][] otherMatrix = matrix.Data;
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Cols; c++)
{
if ((long) (this.matrix[r][c]*precision) != (long) (otherMatrix[r][c]*precision))
{
return false;
}
}
}
return true;
}
public void MatrixEqualsPrecision() { double[][] m1 = { new[] { 1.1234, 2.123 }, new[] { 3.123, 4.123 } }; double[][] m2 = { new[] { 1.123, 2.123 }, new[] { 3.123, 4.123 } }; var matrix1 = new Matrix(m1); var matrix2 = new Matrix(m2); Assert.IsTrue(matrix1.equals(matrix2, 3)); Assert.IsFalse(matrix1.equals(matrix2, 4)); double[][] m3 = { new[] { 1.1, 2.1 }, new[] { 3.1, 4.1 } }; double[][] m4 = { new[] { 1.2, 2.1 }, new[] { 3.1, 4.1 } }; var matrix3 = new Matrix(m3); var matrix4 = new Matrix(m4); Assert.IsTrue(matrix3.equals(matrix4, 0)); Assert.IsFalse(matrix3.equals(matrix4, 1)); try { matrix3.equals(matrix4, -1); Assert.IsTrue(false); } catch (MatrixError) { } try { matrix3.equals(matrix4, 19); Assert.IsTrue(false); } catch (MatrixError) { } }