How can I check if the first 15 relevant digits in double values are the same in Boost Test framework?

StackOverflow https://stackoverflow.com/questions/14520752

Question

I tried to use what seems to be designed for the job: BOOST_CHECK_CLOSE, so I have the following test:

BOOST_AUTO_TEST_CASE( MultivariateNormalDensityTest )
{
  double TOLLERANCE=1e-14;  

  Eigen::Vector3d mu(0.0, 1.0, 2.0);
  Eigen::Matrix3d sigma;
  sigma << 2.0, 1.0, 0.5,
           1.0, 2.3, 0.7,
       0.5, 0.7, 1.7;

  MultivariateNormalDensity<3> mnd(mu, sigma);

  BOOST_CHECK_CLOSE(0.027671392189542814988, mnd(Eigen::Vector3d(0.0, 1.0, 2.0)), TOLLERANCE);
  BOOST_CHECK_CLOSE(0.0027063822550173750707, mnd(Eigen::Vector3d(2.0, 1.0, 0.5)), TOLLERANCE);
  BOOST_CHECK_CLOSE(0.024708597088231143424, mnd(Eigen::Vector3d(0.5, 1.5, 2.5)), TOLLERANCE);
  BOOST_CHECK_CLOSE(0.026554587643995836849, mnd(Eigen::Vector3d(-0.3, 0.6, 1.8)), TOLLERANCE);
  //examples calculated using R
}

However, the first check fails with the error:

/home/ga1009/PhD/cpp/grzesLib/test/multivariatenormaltests.cpp(36): error in "MultivariateNormalDensityTest": difference{3.76141e-14%} between 0.027671392189542814988{0.027671392189542815} and mnd(Eigen::Vector3d(0.0, 1.0, 2.0)){0.027671392189542805} exceeds 1e-14%

When I do the math I get:

(0.027671392189542815-0.027671392189542805)/0.027671392189542805=0.00000000000000036138406

which is less than 1e-14. What am I doing wrong here?

Was it helpful?

Solution

The tolerance is specified as a percentage, you're off by 100. Fix:

double TOLERANCE=1e-12;   // Percent
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top