
Boost : 
From: Paul A Bristow (pbristow_at_[hidden])
Date: 20040726 12:33:18
In setting up a test for math function accuracy, I incorrectly coded thus:
BOOST_CHECK_EQUAL(sin(0.5), 0.479426);
and got the somewhat puzzling  apparently wrong  message
error in "call_test_main": test sin(0.5) == 0.479426 failed [0.479426 !=
0.479426]
On most days of the week, one would expect [0.479426 == 0.479426]!
I should of course have written
BOOST_CHECK_EQUAL(sin(0.5), 0.47942553860420301);
(or used
BOOST_CHECK_CLOSE(sin(0.5), 0.47942553860420301, 1e16);)
But I wonder if the test program would be even more helpful if it output all
possibly significant digits.
In test_tools.hpp, I have added an increase to the ostream precision
(default only 6 digits) int print_log_value thus
template<typename T>
struct print_log_value {
void operator()( std::ostream& ostr, T const& t )
{
if(std::numeric_limits<T>::is_specialized &&
std::numeric_limits<T>::radix == 2)
{ // Show all possibly significant digits (for example, 17 for 64bit
double).
ostr.precision(2 + std::numeric_limits<T>::digits * 301/1000);
}
ostr << t; // by default print the value.
}
};
so that the message is no longer
test sin(0.5) == 0.479426 failed [0.47942553860420301 !=
0.47942600000000002]
and my folly obvious.
And after the original mistake is corrected
BOOST_CHECK_EQUAL(sin(0.5), 0.47942553860420301);
The log file reports:
info: sin(0.5) == 0.47942553860420301 passed.
Paul
PS (It does not appear to possible to change the precision 'externally' by
calling
cout.precision(17);
myTestLog.precision(17);
probably because ostr in test is contructed with default precision).
Paul A Bristow
Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB
+44 1539 561830 +44 7714 330204
mailto: pbristow_at_[hidden]
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk