Difference between BOOST_CHECK_CLOSE and BOOST_CHECK_CLOSE_FRACTION?

前端 未结 3 1325
时光说笑
时光说笑 2021-01-04 02:22

Can anyone describe the difference in behavior between BOOST_CHECK_CLOSE and BOOST_CHECK_CLOSE_FRACTION? The documentation implies the that both m

相关标签:
3条回答
  • 2021-01-04 03:03

    According to this discussion, one (BOOST_CHECK_CLOSE) treats the third parameter as expressing a percent, while the other (BOOST_CHECK_CLOSE_FRACTION) treats it as expressing a fraction. So, .01 in the first should be equivalent to .0001 in the second.

    Not certain if that explains your problem -- do you get the same odd result with BOOST_CHECK_CLOSE? I wouldn't be shocked if the 0 caused an issue -- but I don't have first hand experience with the macros.

    0 讨论(0)
  • 2021-01-04 03:12

    Yes. Zero is not "close" to any value. You can use BOOST_CHECK_SMALL instead.

    0 讨论(0)
  • 2021-01-04 03:21

    @Gennadiy: Zero can be close to any small value. :-) Relative differences grow arbitrarily large if the expected value is very close to zero.

    Here is a workaround function I use to unit-test double values: if the expected value is very small or zero then I check the smallness of the observed value, otherwise I check closeness:

    void dbl_check_close(
        double expected, double observed, 
        double small, double pct_tol
    ) {
        if (std::fabs(expected) < small) {
            BOOST_CHECK_SMALL(observed, small);
        } else {
            BOOST_CHECK_CLOSE(expected, observed, pct_tol);
        }
    }
    

    Of course it would be great to have a BOOST_CHECK_SMALL_OR_CLOSE macro that does this automatically. Gennadiy could perhaps talk to the author of Boost.Test ;-)

    0 讨论(0)
提交回复
热议问题