提升unittest:BOOST_CHECK_CLOSE返回1.#INF%

时间:2020-12-23 17:43:16

I'm using the Boost unit test-framework to compare doubles with each other. The results are as expected, but BOOST_CHECK_CLOSE does not recognize them correctly I guess. The code is essentially as follows:

我正在使用Boost单元测试框架来比较双打。结果如预期的那样,但BOOST_CHECK_CLOSE无法正确识别它们。代码基本如下:

BOOST_AUTO_TEST_CASE(FooBarTest)
{
    double foo = 2.2500000047015632e-006;
    double bar = 0.0;
    double tolerance = 90.0;
    BOOST_CHECK_CLOSE(foo, bar, tolerance);
}

This fails with the following message:

此操作失败,并显示以下消息:

error in [...]: difference{1.#INF%} between foo{2.2500000047015632e-006} and bar{0} exceeds 90%

I'm using Boost 1.55 with VC100 (Visual Studio 10 compiler). I'm compiling Win32 Release.

我正在使用Boost 1.55和VC100(Visual Studio 10编译器)。我正在编译Win32 Release。

Is this a result I should expect? I would imagine that these values are close enough to each other and it should return success. Is Boost internally dividing by 0.0?

这是我应该期待的结果吗?我会想象这些值彼此足够接近,它应该会成功。 Boost内部除以0.0吗?

2 个解决方案

#1


3  

You can use BOOST_CHECK_SMALL to check closeness to zero. For more background information, see: Boost UTF documentation, article on floating-point comparison algorithms.

您可以使用BOOST_CHECK_SMALL来检查零接近度。有关更多背景信息,请参阅:Boost UTF文档,有关浮点比较算法的文章。

#2


1  

That's because bar is zero. BOOST_CHECK_CLOSE checks if the two values are 'close', that is, in your case, with 90% of each other. For that, you use division, and you can't divide by 0.

那是因为bar为零。 BOOST_CHECK_CLOSE检查两个值是否“接近”,即在您的情况下,相互之间的值是90%。为此,你使用除法,你不能除以0。

Or, as this this answer so succinctly explains - zero is not close to anything.

或者,正如这个答案如此简洁地解释 - 零并不接近任何事情。

#1


3  

You can use BOOST_CHECK_SMALL to check closeness to zero. For more background information, see: Boost UTF documentation, article on floating-point comparison algorithms.

您可以使用BOOST_CHECK_SMALL来检查零接近度。有关更多背景信息,请参阅:Boost UTF文档,有关浮点比较算法的文章。

#2


1  

That's because bar is zero. BOOST_CHECK_CLOSE checks if the two values are 'close', that is, in your case, with 90% of each other. For that, you use division, and you can't divide by 0.

那是因为bar为零。 BOOST_CHECK_CLOSE检查两个值是否“接近”,即在您的情况下,相互之间的值是90%。为此,你使用除法,你不能除以0。

Or, as this this answer so succinctly explains - zero is not close to anything.

或者,正如这个答案如此简洁地解释 - 零并不接近任何事情。