为什么assertAlmostEqual(-inf,-inf) 失败?

发布于 2024-10-23 22:16:57 字数 359 浏览 2 评论 0原文

Numpy 的 log 方法为 log(0) 提供 -inf。这个值是可比的:

>>> np.log(0) == np.log(0)
True

现在在单元测试中,以下工作正常:

self.assertEqual(np.log(0),np.log(0))

但这失败了:

self.assertAlmostEqual(np.log(0),np.log(0))

为什么这种行为是这样的?这是一个错误还是有意为之?如果有意的话,如何检查两个浮点值几乎相等,并且对于 -inf 也能正常工作?

Numpy's log method gives -inf for log(0). This value is comparable:

>>> np.log(0) == np.log(0)
True

Now in unittesting the following works fine:

self.assertEqual(np.log(0),np.log(0))

but this fails:

self.assertAlmostEqual(np.log(0),np.log(0))

Why is this behaviour like this? Is this a bug or intended? If intended, how can I check two float values to be almost equal, working also correctly for -inf?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2024-10-30 22:16:58

来自unittest assertAlmostEqual(a, b) 的文档是默认相当于round(ab, 7) == 0。所以在你的情况下你有:

In [8]: np.log(0) - np.log(0)
Out[8]: nan

In [9]: round(np.log(0) - np.log(0), 7)
Out[9]: nan

In [11]: np.nan == 0
Out[11]: False

这解释了你的测试失败的原因。

为了使其工作,请使用 unittest2 这是一个示例:

import unittest2
import numpy as np

class Test_Assertions(unittest2.TestCase):
    def test_float_inf(self):
        self.assertAlmostEqual(float('inf'), float('inf'))

    def test_numpy_inf(self):
        self.assertAlmostEqual(np.log(0),np.log(0))


unittest2.main()

Output:

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

NB: In unittest2 assertAlmostEqual () 首先测试两个对象是否相等,如果是,则结果为是,否则执行魔法(几乎相等),这就是它起作用的原因。它也应该在新的 python 版本(2.7 >)中工作,因为它们中的大多数都实现了 unittest2 功能(我不确定这一点,因为我的工作站中没有 python 2.7 >)。

希望这能有所帮助:)

From the doc of unittest assertAlmostEqual(a, b) is by default equivalent to round(a-b, 7) == 0. so in your case you have :

In [8]: np.log(0) - np.log(0)
Out[8]: nan

In [9]: round(np.log(0) - np.log(0), 7)
Out[9]: nan

In [11]: np.nan == 0
Out[11]: False

That explain why your test fail.

For making it work use unittest2 here is an example:

import unittest2
import numpy as np

class Test_Assertions(unittest2.TestCase):
    def test_float_inf(self):
        self.assertAlmostEqual(float('inf'), float('inf'))

    def test_numpy_inf(self):
        self.assertAlmostEqual(np.log(0),np.log(0))


unittest2.main()

Output:

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

N.B: In unittest2 assertAlmostEqual() first test if the two objects are equal if yes so the result is yes else do the magic (almost equal) , this is why it work . It also should work in new python version (2.7 >) because most of them have the unittest2 functionality implemented (i'm not sure about this because i don't have python 2.7 > in my work station).

Hope this can help :)

只怪假的太真实 2024-10-30 22:16:58

Inf 和任何有限值之间的差异是 Inf 或 -Inf。这是 IEEE754 规范的一部分。由于 assertAlmostEqual 使用减法,这解释了这种行为。

以下是 Intel x86 文档中有关 FSUB 的相关表格:

在此处输入图像描述

要解决您的问题,您将需要特殊的Inf 的案件处理。

The difference between an Inf and any finite value is either Inf or -Inf. That's part of the IEEE754 specification. Since assertAlmostEqual uses subtraction this explains the behaviour.

Here's the relevant table from the Intel x86 documentation for FSUB:

enter image description here

To solve your problem you are going to need special case handling for Inf.

江城子 2024-10-30 22:16:58

我想说-∞和-∞之间的差异可以达到∞。因此,它们并不是真正的“几乎平等”。

如果您想忽略这种特殊情况,那么这样的事情可能会有用:

if valueA != valueB:
  self.assertAlmostEqual(valueA, valueB)

I'd say that the difference between -∞ and -∞ can be as much as ∞. Therefore, they aren't really "almost equal".

If you want to ignore this special case, then something like this might be useful:

if valueA != valueB:
  self.assertAlmostEqual(valueA, valueB)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文