2

I was doing some unit testing at work and a peculiar error popped up for one of the assertions. Note that expectedValue and actualValue are both doubles.

Assert.AreEqual(expectedValue, actualValue);

The exception stated that they were not equal, elaborating that "expected value: <6.8> actual value: <6.8>."

The expected value is a hard coded 6.8 and the actual value is formulated using database values going through our classification methods (such as Equal Records, or Jenks Natural Breaks).

My guess is that the difference is probably that the mantissas of the 2 values are similar up until the least significant bit. I updated the tests to include an epsilon to find if the two values are close enough, but I'm curious to if there is a way to force the mantissa to match with the display value of if I displayed that double. Does such a mantissa correction exist?

asked Aug 16, 2010 at 16:36
2
  • People aren't reading your question correctly, you know what is going on with the precision, you are just asking about mantissa handling the problem better, right? Commented Aug 16, 2010 at 17:11
  • I'm asking for a possible way to refactor the mantisa so that it holds a value closer to what it displays. Sort of like val = double.Parse(val.ToString()); except perhaps more efficient. Commented Aug 16, 2010 at 19:13

3 Answers 3

3

I'm not entirely sure what you mean by forcing the mantissa to match the display value... there are no double values which are exactly 0.1, for example.

If you want some code to display the exact value of a double, however, I have a DoubleConverter.cs file which makes it easy:

 double d = 0.1;
 string x = DoubleConverter.ToExactString(d);

Another alternative is to use the round-trip format specifier ("r") when converting a double to string - that guarantees that the result has enough information to reproduce the same exact value later. In other words, if x != y, then x.ToString("r") != y.ToString("r").

answered Aug 16, 2010 at 16:39
Sign up to request clarification or add additional context in comments.

1 Comment

I mean that I had 2 different values that both displayed in Visual Studio as 6.8, but their exact representation were different. How would I figure out which one is more correct (if that's even possible) and have them both be represented that way so an equality check would come out true?
1

You could convert both to a string : actualValue.ToString("0.000") and compare those strings.

That could be made to match your requirements closely.

answered Aug 16, 2010 at 16:40

Comments

0

If you want to test whether the default display values match, just compare the default display values:

Assert.AreEqual(expectedValue.ToString(), actualValue.ToString());
answered Aug 16, 2010 at 17:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.