Module testing failed due to inadequacy of 0.000000001f

The vector is displayed at 0.000000001f, so the test does not work.

Following 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 response returns as -0.999999999f, -0.999999999f, which is still 'right'.

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

I see that, when coding an answer, I am writing production code. For instance. that would be the same. Of course, this is wrong.

Any tips?

+4
source share
4 answers

Please see my answer here .

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

+2
source

Unit testing frameworks usually provide statements for floats that make accuracy. 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 
+6
source

Using the epsilon value should work, you can post code that does not work to make your problem more clear. You should do something like this:

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

instead

 if (TestSomething()->Vector.x = 1.0f) return true; 
+1
source

Floating point numbers can have accuracy problems, besides comparing with 0, you would rarely want to compare two floating point numbers using ==.

Trying to add an epsilon value to vectors will not work - you will still encounter the same precision problems.

Instead, you need to see if the difference between them is below a certain threshold:

 if (Math.Abs(x - y) < epsilon) { // Equal! } 
0
source

All Articles