[erlang-questions] how: best way to deal with floats in eunit?

Gustavo Niemeyer <>
Tue Aug 12 16:31:46 CEST 2008


Hello Richard,

> I could easily add a special assertion for this, but since I don't
> work a lot with floating-point data, I'd appreciate some input as
> regards how such an assertion should work. Is it sufficient to just
> give a target value and an epsilon such that the expected value should
> be within [target-epsilon,target+epsilon], or are other variants needed
> such as relative precision, etc.? If you could have just one form,
> which would be the most flexible?

That's exactly how it's done in a few other unittesting frameworks.
Here is the one from Twisted's Trial (http://twistedmatrix.com/), for
instance:

def failUnlessApproximates(self, first, second, tolerance, msg=None):
    """asserts that C{first} - C{second} > C{tolerance}

    @param msg: if msg is None, then the failure message will be
                '%r ~== %r' % (first, second)
    """
    if abs(first - second) > tolerance:
        raise self.failureException(msg or "%s ~== %s" % (first, second))
    return first
assertApproximates = failUnlessApproximates

-- 
Gustavo Niemeyer
http://niemeyer.net



More information about the erlang-questions mailing list