4

A vector is out by 0.000000001f so the test is failing.

Doing the logic in my head yeilds -1.0f, -1.0f but when I wrote the code to pass the test (Using the built in Vector methods) the answer comes back as -0.999999999f, -0.999999999f which is still 'right'.

This is C# (Vector2 class provided by the XNA framework) by the way, so I tried adding an epsilon value to each the x and y parameter of the Vector but this didn't work.

The way I see it is that by coding the answer, I'm writing the production code. E.g. it would be the same. Surely this is not the right.

Any advice?

Finglas
  • 15,518
  • 10
  • 56
  • 89

4 Answers4

6

Unit testing frameworks usually provide asserts for floats that take a precision. For example in Visual Studio Test you can write:

Assert.AreEqual(1.0f, 0.9999999999f, 1e-3); // passes
Assert.AreEqual(1.0f, 0.9f, 1e-3); // fails
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

Please see my answer here.

It is not about C#, but the same logic applies.

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
1

Using an epsilon value should work, you could post the code that doesn't work to make your problem clearer. You should do something like this:

if ((TestSomething()->Vector.x - 1.0f) <= EPSILON) return true;

instead of

if (TestSomething()->Vector.x = 1.0f) return true;
schnaader
  • 49,103
  • 10
  • 104
  • 136
0

Floating point numbers can have precision issues, besides comparison to 0 you rarely would want to compare two floating point numbers using ==.

Trying to add an epsilon value to the vectors won't work - you'll still fall for the same precision issues.

Instead, you need to see if the difference between the two is below some threshold:

if (Math.Abs(x - y) < epsilon)
{
     // Equal!
}
Michael
  • 54,279
  • 5
  • 125
  • 144