Reputation: 46651
I have a unit test and I the number that gets stored in the actual variable from the Distance calcuation is 6.07328028312884, yet it is still saying the test is failing. Why?
double expected = 6.07328028312884;
double actual;
actual = target.Distance((double)latitude, (double)longitude);
actual = actual / 1000;
Assert.AreEqual(expected, actual);
Upvotes: 1
Views: 136
Reputation: 33272
As Matt said, is not a good idea to compare for equality two floats, use Math.Abs(expected-actual)<epsilon
with a small epsilon.
Upvotes: 5
Reputation: 133092
I am not sure about Microsoft UT's, but in CPPUNIT there is a special macro
CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual)
which probably checks that the absolute value of the difference of expected and actual is less than some epsilon. There has to be an analogous function in Microsoft UTs
Upvotes: 0
Reputation: 62057
floating point numbers are inaccurate by their very design. To test to that much accuracy is probably not going to work. What is the value of actual
? you'll probably find it's off by expected just a tad, due to rounding and the general nature of floating point on computers.
Upvotes: 5