ScalaTest - 测试具有误差的两个浮点数组之间的相等性 [英] ScalaTest - testing equality between two floating point arrays with error margin

查看:134
本文介绍了ScalaTest - 测试具有误差的两个浮点数组之间的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个函数返回一个双精度数组。我想测试这个功能,并已经手工计算出正确的值。但是,由于是浮点数,我不能直接比较,所以有ScalaTest的甜语法,使我能够比较双数组与epsilion /错误的边界?



感谢
解决方案

好像我害怕ScalaTest中没有很好的语法,我会接受我自己的答案一个非常基本的解决方案。

  val Eps = 1e-3 //我们的epsilon 

val res = testObject.test //你想测试的结果。
val expected = Array(...)//预期的返回值。 (i < - 0,直到res.size)res(i)应该是(预期的(i)+ bs),应该是(expected.size)

。 - Eps)

正如所见,这是有效的。那么你可以通过定义一个隐式方法来使它更好。


Let's say I have a function that returns a array of doubles. I want to test this function and have calculated the correct value by hand. However since it's floating point numbers, I can't do direct comparisons so is there any sweet syntax by ScalaTest that makes me able to compare double arrays with an epsilion/error margin?

Thanks

解决方案

Well as I feared there is no nice syntax in ScalaTest for this, and I will accept my own answer with a very basic solution.

val Eps = 1e-3 // Our epsilon

val res = testObject.test // Result you want to test.
val expected = Array(...) // Expected returning value.

res.size should be (expected.size)

for (i <- 0 until res.size) res(i) should be (expected(i) +- Eps)

As seen, this works. Then you can make it nicer by perhaps defining an implicit method.

这篇关于ScalaTest - 测试具有误差的两个浮点数组之间的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆