简体   繁体   English

如何使用Epsilon的assertEquals在JUnit中断言两个双精度?

[英]How to Assert Two doubles in JUnit Using assertEquals with Epsilon?

assertEquals for doubles is deprecated. 不推荐使用双精度的assertEquals。 I found that the form with Epsilon should be used. 我发现应该使用Epsilon的表格。 It is because of impossible 100% strictness of doubles. 这是因为不可能100%严格的双打。 But anyway I need to compare two doubles (expected and actual result), but I don't know how to do it. 但无论如何我需要比较两个双打(预期和实际结果),但我不知道该怎么做。

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. 以下是我收到的3个双值,我想用JUnit检查:110.42,2760.5和10931.58。 How should JUnit test look like with assertions for them? JUnit测试应该如何使用断言? I receive them as a result of calculation in a method: 我在一个方法中计算得到它们:

processor.calcPossibleValue(allowed_percentage){return /*Some weird formulae here*/;}

You need to add a fourth parameter to the assertEquals call: the threshold within which two doubles should be considered "equal". 您需要在assertEquals调用中添加第四个参数:两个双精度应被视为“相等”的阈值。 Your call should look like this: 你的电话应该是这样的:

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. 上面的调用表明,如果processor.calcPossibleValue(percentage)返回的值在110.42 ±0.01范围内,那么这两个值被认为是相等的。 You can change this value to make it as small as is necessary for your application. 您可以更改此值,使其尽可能小到应用程序所需的值。

See the JUnit documentation for more information. 有关更多信息,请参阅JUnit文档

A Java double uses the IEEE 754 64-bit format . Java double使用IEEE 754 64位格式 This format has 52 mantissa bits. 这种格式有52个尾数位。 When comparing 2 values the epsilon should take into account the magnitude of the expected value. 比较2个值时,epsilon应考虑预期值的大小。

For example, 0.01 might work okay for 110.42 however it won't work if the expected value is > 2 52 . 例如,0.01可能适用于110.42但是如果预期值> 2 52则它将不起作用。 The magnitude of 2 52 is so large that 0.01 would be lost due to precision (ie only 52-bit mantissa bits). 2 52的幅度是如此之大,以至于由于精度(即仅52位尾数位)将丢失0.01。 For example, 2 52 + 0.01 == 2 52 . 例如,2 52 + 0.01 == 2 52

With that in mind, epsilon should be scaled to the expected value. 考虑到这一点,epsilon应该缩放到预期值。 For example, expected value ÷ 2 52 - 3 or 110.42 ÷ 2 52 - 3 = 1.96... x 10 -13 . 例如,预期值÷2 52 - 3或110.42÷2 52 - 3 = 1.96 ...×10 -13。 I chose 2 52 - 3 since this will give a tolerance in the 3 least significant bits in the mantissa. 我选择了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和±∞。

Assert.assertTrue("Not equals", expectedDouble -  actualDouble == 0);

不需要涉及epsilon或delta。

断言是不推荐的

assertTrue("message like not equal ",expectedresult-actual == 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM