views:

91

answers:

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?

+2  A: 

Please see my answer here.

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

Brian R. Bondy
A: 

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
+1  A: 

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
You forgot the Math.Abs() around the difference.
Cheeso
+5  A: 

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
Its NUnit. The code is not accessible at the moment. Does anyone know if NUnit has something similar?
Finglas
Darin Dimitrov
Cheers, that's a great help.
Finglas
After all Microsoft haven't reinvented the wheel :-)
Darin Dimitrov