c# - Class with overridden equals method not returning true for SOME objects that are equal -


I have a vector square on which it is the equivalent (object object) method overridden So that I can compare them.

  public square vector 3f {public float x, y, z; Public vector 3f (float x, float y, float z) {this.x = x; this. Y = y; This.z = z; } Public static vector 3f operator + (vector 3fa, vector 3fb) {new vector 3f (ax + bx, ai be, aj.bz) ); } Public Static Vector 3f Operator - (Vector 3FA, Vector 3FB) {New Vector 3F (Ax-BX, AI-BEE, AJ-BJJ .); } Public override baul par (object obj) {Vector3f other = (vector 3f) obj; Return x == other X & amp; Y == Other End & amp; Z == Other Z; } Public override string toring () {return string.format ("& lt; {0}, {1}, {2}>", x, y, z); }}   

Plus operator works as expected in my unit tests. However, when I decrease the two vectors, it says that they are not equal

  test 'RTTests.Testies.vector_subtraction_works' failed: expected: & lt; & Lt; 1.1.0.1.0.1 & gt; & Gt; But it was: & lt; & Lt; 1.1.0.1,0.1 & gt; & Gt; Testies.cs (60,0): at RTTests.Testies.vector_sub_works ()   

I did not understand that this comparison is working for this addition and does not reduce, especially Since the output values ​​are the same, in both cases?

Edit: My test for this

  [test] Public zero vector_addition_works () {Vector3f v1 = New vector 3f (1.0f, 1.0f, 1.0f) ; Vector 3f v2 = new vector 3f (1.6 f, 3.2 f, 4.7 f); Expecting vector 3F = new vector 3f (2.6 f, 4.2 f, 5.7 f); Vector 3f real = v1 + v2; Extraordinary (Actual, expected); } [Test] Public Zero vector_sub_works () {Vector3f v1 = New vector 3f (1.1f, 1.1f, 1.1f); Vector 3f v2 = new vector 3f (0.0 f, 1.0 f, 1.0 f); Expecting vector 3F = new vector 3f (1.1f, 0.1f, 0.1f); Vector 3f real = v1 - v2; Extraordinary (Actual, expected); }    

Your problem should be a spherical / typing error. It happens all the time with floating point operations, especially subtraction. When you test for equality, instead of a == B, a-b & lt; SmallConstant You can also use double precision or decimal, however cut errors will return at the end, but you can make them less common.

Comments