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

Richard Carlsson richardc@REDACTED
Tue Aug 12 15:58:48 CEST 2008


Tim Fletcher wrote:
> I'm trying to write a test for a function that should return something
> like [{1, 0.1}], but because a float is being returned the exact value
> won't always be the same (and so I can't use ?_assertEqual).
> 
> eunit doesn't appear to have anything built-in that will handle a case
> like this, and the nested structure makes writing a custom assertion a
> bit awkward.
> 
> I can't imagine this is the first time anyone has used floats in
> eunit... any suggestions as to the best way to tackle this?

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?

     /Richard


-- 
  "Having users is like optimization: the wise course is to delay it."
    -- Paul Graham



More information about the erlang-questions mailing list