Boost logo

Boost :

Subject: Re: [boost] boost-test, why is there no DOUBLES_EQUAL?
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2011-04-08 12:49:05


On 4/5/2011 2:25 PM, Sebastian Nowozin wrote:
> Hello Grennadiy,
>
> On Tue, Mar 29, 2011 at 6:19 PM, Gennadiy Rozental<rogeeff_at_[hidden]> wrote:
>
>>> BOOST_CHECK_DOUBLES_EQUAL(first, second, absolute_tolerance);
>> Use BOOST_CHECK_SMALL.
>
> This does not do the job, as
> BOOST_CHECK_SMALL(std::fabs(first - second), 1.0e-3)
> will be about as useful as
> BOOST_CHECK(std::fabs(first - second)<= 1.0e-3)
>
> It is not useful because in case this check fails I do not get to see
> the original values of both 'first' and 'second', but only their
> difference.
>
> In my application, first and second are in the range of 0 to 1, but
> can in fact be quite close to zero. The qualitative test is correct
> if the absolute difference between first and second is small enough,
> irrespectively of the absolute value of first. Therefore, I fail to
> see why the boost test library provides only BOOST_CHECK_CLOSE and
> BOOST_CHECK_SMALL, but no analog to CPPUNIT_ASSERT_DOUBLES_EQUAL.

You can implement any check with a minor amount of code. Here, for
example, is the test I have for making sure a quantity is within some
tolerance of equality:

template < typename Quantity >
boost::test_tools::predicate_result
compare_quantity(Quantity a, Quantity b, double tolerance)
{
   double diff = abs(a.value() - b.value());
   if ( diff > tolerance)
   {
     boost::test_tools::predicate_result res( false );
     res.message() << "Quantities [" << a << "] and [" << b << "] do not
fall within [" << tolerance << "]. Difference is [" << diff << "]";
     return res;
   }

   return true;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk