Difference between BOOST_CHECK_CLOSE and BOOST_CHECK_CLOSE_FRACTION?

前端 未结 3 1365
时光说笑
时光说笑 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: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 ;-)

提交回复
热议问题