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

Tim Fletcher twoggle@REDACTED
Thu Aug 14 11:33:07 CEST 2008

> The only alternative I can think of would be having some kind of matching
> feature, similar to that available in ets, which was able to perform loose
> float comparisons.

Sure, like a match spec. Anything like that would work, but I don't
think it's worth implementing (I doubt it would be of much use outside
of testing).

> Your example was that you'd get "something like [{1, 0.1}]", but do
> you only require that e.g. {1, 2, 3} compares equal to {1.0, 2.0, 3.0},
> i.e., that the only differences that may occur are due to integer <->
> float conversions, or do you need to handle actual computed values, so
> that {1,2,3} should compare equal to {1.0000001,2.0000001,3.0000001}?

The float it a computed value, so the 0.1 would need to be considered
equal to 0.100000001490116. But just to make it awkward the first
element of the tuple (the 1) must be an integer, so really shouldn't
be considered equal to 1.0, or 1.0000001.

> In the latter case you really have to unpack the structure and
> handle each comparison separately.

Yep, that's what i've ended up doing. I think any other way would be a
bit ambiguous and/or overcomplicated.

> I'm just a bit surprised that most frameworks seem to provide only an
> absolute error assertion, since I thought the relative error was often
> a better measure. I suppose I should provide macros for both variants.

Choice is good, so I would vote for both. But, again, I'm not a
"floating point person", or a "test framework expert" :)

I've implemented a relative/percentage-based function for my immediate
use, but if you could add some assertions to eunit that would be very
helpful in the long run.

Thanks both.

More information about the erlang-questions mailing list