test icon indicating copy to clipboard operation
test copied to clipboard

Floating point tolerance not applied correctly in per_element test

Open kschlegel opened this issue 4 years ago • 0 comments

I am working on a numerical optimization project, using MKL. When testing my computed Jacobians I found that element-wise comparison of vectors/matrices with a given tolerance isn't working when elements to be tested are small. Here is what I do:

vnl_matrix< double > Jacobian(rows, cols);
obj_fn->gradf(Jacobian);   // Jacobian now hold the Jacobian from my to be tested code

vnl_matrix< double > ExpectedJacobian(rows, cols);
ComputeFiniteDifferences(obj_fn, ExpectedJacobian);   // ExpectedJacobian now holds a numeric approximation of the Jacobian, 
                                                      // computed by using difference quotients
// The following test occasionally fails even at much larger tolerance than I would like to test at. When it fails the elements of the
// Jacobian where it fails are always very small (on the order of 10^(-5) or below)
BOOST_TEST( Jacobian == ExpectedJacobian, boost::test_tools::tolerance( 1e-3 ) << boost::test_tools::per_element() );
// This test in turn always passes at much smaller tolerance, confirming that the Jacobian and ExpectedJacobian are indeed within
// tolerance of each other (This is also further confirmed when looking in detail at the output of the failed test, see below)
BOOST_TEST( (Jacobian - ExpectedJacobian).array_inf_norm(), boost::test_tools::tolerance( 1e-6 ) );

Here is an example output of when the test failed:

check Jacobian == ExpectedJacobian has failed
  - mismatch at position 4210: [2.9551696555663032e-06 == 2.9700686354772188e-06] is false: relative difference exceeds tolerance [0.00504167 > 0.001]

Note that

  • The absolute values where it fails are both very small and clearly within tolerance of each other
  • The reported difference is completely wrong, not only is it seemingly missing the magnitude of the values themselves, manually computing the difference I get ~0.014899e-06
  • It clearly should be passing the test

kschlegel avatar Oct 05 '22 10:10 kschlegel