I recently designed a time series module where my time series is essentially a SortedDictionnary<DateTime, double>.

Now I would like to create unit tests to make sure that this module is always working and producing the expected result.

A common operation is to compute the performance between the points in the time series.

So what I do is create a time series with, say, {1.0, 2.0, 4.0} (at some dates), and I expect the result to be {100%, 100%}.

The thing is, if I manually create a time series with the values {1.0, 1.0} and I check for equality (by comparing each point), the test would not pass, as there will always be inaccuracies when working with binary representations of real numbers.

Just a note that this answer is NUnit specific and showcases the "Constraing-based" assertion model. The classic assertion model would look like: Assert.AreEqual(expected, actual, tolerance);
–
RichardMFeb 7 '12 at 14:16

@RichardM: Post that as an answer and I will select it accept it.
–
SRKXFeb 7 '12 at 14:22

The answer by @dasblinkenlight is correct, just adding some detail (since it may not be clear - the classic assertion model is also NUnit). Other test frameworks (not MSTest) likely have their own assert model to deal with floating point values.
–
RichardMFeb 7 '12 at 14:26

@quant_dev You are absolutely right. Since OP talks about calculating returns as percentages, I assumed that abs(expected) would be single to double digits. I also assumed the tolerance in the vicinity of 1E-9. Under these assumptions this admittedly simplistic approach could serve you reasonably well (I use Is.InRange in my tests).
–
dasblinkenlightFeb 7 '12 at 16:05

It depends what you do with the numbers. If you are testing a method which is supposed to e.g. select an appropriate value from an input set based on some criteria, then you should test for strict equality. If you're doing floating-point calculations, usually you will need to test with a non-zero tolerance. How big the tolerance is depends on the calculations, but with double precision a good starting point is to choose 1E-14 relative tolerance for simple calculations and 1E-8 (tolerance) for more complicated ones. YMMV of course, and you need to add some small absolute tolerance if the expected result is 0.