我使用的是ScalaTest (3.0.4),我找不到任何可以比较浮点数和容差的例子。这就是我所拥有的:
import org.scalatest.{MustMatchers, WordSpec}
class DetectionClusteringSpec extends WordSpec with MustMatchers {
"EmbeddingsGroup.vecdist" should {
"correctly compute vector distance" in {
val dist = EmbeddingsGroup.vecdist(TestDetections.emb1,TestDetections.emb4)
// Note: The above method returns a Float.
dist mustBe 3.058 +- 0.1
}
}
上面的代码可以编译,但是当我运行测试时,我得到了以下失败:
3.0579379 was not equal to 3.058 +- 0.1
Expected :3.058 +- 0.1
Actual :3.0579379
我还使用了下面的断言:
assert(dist === 3.058)
但是,这也不起作用,并给出以下失败:
3.0579379 did not equal 3.058
Expected :3.058
Actual :3.0579379
我读过很多用上述两种语法比较浮点数的例子,看起来它们应该可以工作。我的第一个示例直接来自the documentation。
我做错了什么?
发布于 2019-05-30 09:35:59
由于dist
是Float
,而其他参数是Double
,因此尝试将dist
转换为Double
,如下所示
dist.toDouble mustBe 3.058D +- 0.1
或者让其他参数像这样浮动
dist mustBe 3.058f +- 0.1f
https://stackoverflow.com/questions/56363432
复制