对于 double 值,assertEquals 的 delta 或 epsilon 参数的含义

2022-01-09 00:00:00 floating-point java junit

我有一个关于 JUnit assertEquals 以测试 double 值的问题.阅读 API 文档 我可以看到:

<块引用>

@Deprecatedpublic static void assertEquals(预期双倍,实际双倍)

已弃用.请改用 assertEquals(double expected, double actual, double delta).

(注意:在旧的文档版本中,delta 参数称为 epsilon)

delta(或epsilon)参数是什么意思?

解决方案

Epsilon 是两个数字可以关闭的值.所以只要 Math.abs(expected - actual) <= epsilon

它就会断言为真

I have a question about JUnit assertEquals to test double values. Reading the API doc I can see:

@Deprecated
public static void assertEquals(double expected, double actual)

Deprecated. Use assertEquals(double expected, double actual, double delta) instead.

(Note: in older documentation versions, the delta parameter is called epsilon)

What does the delta (or epsilon) parameter mean?

解决方案

Epsilon is the value that the 2 numbers can be off by. So it will assert to true as long as Math.abs(expected - actual) <= epsilon

相关文章