Boost logo

Boost Users :

Subject: Re: [Boost-users] unit test BOOST_CHECK_CLOSE absolute
From: Pete Bartlett (pete_at_[hidden])
Date: 2015-01-14 18:10:00

>>> For example, I am comparing coordinates in 2d spaces and the relative
>>> value is not important, the meaningfull value is the absolute difference
>>> between coordinates.
>> It is almost never true. Related error gives you much better estimate of
>> closeness.
>I agree with that most of the time but this is not always true. Just
>imagine the case you compare lattitude, longitude. In this case, only
>absolute values make sense.

Sure, 0deg East and 180deg East are physically the same, but it is a fact of
floating point life that you can record/measure a point close to 0deg East
to more precision than one near 180 East.

This is why people always suggest using custom types (e.g. a currency type
for finance) when this thread comes up.

>I can probably write my own macro combining BOOST_TEST_MESSAGE() and
>some glues to obtain what I want but I was wondering if someone had the
>same problem and a solution.

Be careful using the naïve solution of something like

#define MY_TEST_ABS(x,y,tol,msg) BOOST_CHECK_MESSAGE( std::abs((x)-(y)) <
tol , build_test_abs_error_message(x,y,tol,msg) )

Here x and y are repeated in the macro expansion so when x = f(a,b,c) this
leads to subtle bugs or unexpected performance (f is called twice).

To do it properly you have to follow what is done for BOOST_CHECK_CLOSE and
a define a new predicate analagous to close_at_tolerance (found in

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at