java 如何在 JUnit 中使用 assertEquals 和 Epsilon 断言两个双打?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11808849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Assert Two doubles in JUnit Using assertEquals with Epsilon?
提问by Dragon
assertEquals for doubles is deprecated. I found that the form with Epsilon should be used. It is because of impossible 100% strictness of doubles. But anyway I need to compare two doubles (expected and actual result), but I don't know how to do it.
不推荐使用双打的 assertEquals。我发现应该使用带有 Epsilon 的表单。这是因为不可能 100% 严格的双打。但无论如何我需要比较两个双打(预期和实际结果),但我不知道该怎么做。
At the moment my test looks like:
目前我的测试看起来像:
@Test
public void testCalcPossibleDistancePercentageCount() {
int percentage = 100;
assertEquals("Wrong max possible value for %" + percentage, 110.42, processor.calcPossibleValue(percentage));
percentage = 75;
/*corresponding assertions*/
}
Here are 3 double values I receive and which I want to check with JUnit: 110.42, 2760.5 and 10931.58. How should JUnit test look like with assertions for them? I receive them as a result of calculation in a method:
这是我收到的 3 个双精度值,我想用 JUnit 检查它们:110.42、2760.5 和 10931.58。JUnit 测试应该如何使用断言?我通过一种方法计算得到它们:
processor.calcPossibleValue(allowed_percentage){return /*Some weird formulae here*/;}
回答by Jon Newmuis
You need to add a fourth parameter to the assertEquals
call: the threshold within which two doubles should be considered "equal". Your call should look like this:
您需要向assertEquals
调用添加第四个参数:应将两个双打视为“相等”的阈值。您的呼叫应如下所示:
assertEquals("Wrong max possible value for %" + percentage, 110.42,
processor.calcPossibleValue(percentage), 0.01);
The above call would indicate that if the value returned by processor.calcPossibleValue(percentage)
is within ± 0.01 of 110.42
, then the two values are considered equal. You can change this value to make it as small as is necessary for your application.
上面的调用表明,如果 返回的值processor.calcPossibleValue(percentage)
在 的 ± 0.01 以内110.42
,则认为这两个值相等。您可以更改此值,使其尽可能小,以满足您的应用程序的需要。
See the JUnit documentationfor more information.
有关更多信息,请参阅JUnit 文档。
回答by Nathan
A Java double uses the IEEE 754 64-bit format. This format has 52 mantissa bits. When comparing 2 values the epsilon should take into account the magnitude of the expected value.
Java double 使用IEEE 754 64 位格式。这种格式有 52 个尾数位。比较 2 个值时,epsilon 应考虑预期值的大小。
For example, 0.01 might work okay for 110.42 however it won't work if the expected value is > 252. The magnitude of 252is so large that 0.01 would be lost due to precision (i.e. only 52-bit mantissa bits). For example, 252+ 0.01 == 252.
例如, 0.01 可能适用于 110.42 ,但如果预期值 > 2 52,它将不起作用。2 52的大小是如此之大,以至于由于精度(即只有 52 位尾数位)会丢失 0.01。例如, 2 52+ 0.01 == 2 52。
With that in mind, epsilon should be scaled to the expected value. For example, expected value ÷ 252 - 3or 110.42 ÷ 252 - 3= 1.96... x 10-13. I chose 252 - 3since this will give a tolerance in the 3 least significant bits in the mantissa.
考虑到这一点,epsilon 应该缩放到预期值。例如,预期值 ÷ 2 52 - 3或 110.42 ÷ 2 52 - 3= 1.96... x 10 -13。我选择了 2 52 - 3,因为这将在尾数中的 3 个最低有效位中给出容差。
One caution is that if the expected value is 0.0 then this formula computes epsilon as 0.0 which may be too strict for a particular case.
一个警告是,如果预期值为 0.0,则此公式将 epsilon 计算为 0.0,这对于特定情况可能过于严格。
Another caution is that NaN and ±∞ are not handled.
另一个注意事项是不处理 NaN 和 ±∞。
回答by Abhijeet
Assert.assertTrue("Not equals", expectedDouble - actualDouble == 0);
There is no need to involve epsilon or delta.
无需涉及 epsilon 或 delta。
回答by abhinavxeon
Assert is Deprecated
断言已弃用
assertTrue("message like not equal ",expectedresult-actual == 0);