Reputation: 3944
I am using ScalaTest (3.0.4), and can't get any examples to work that I've found of comparing floating point numbers with a tolerance. Here's what I have:
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
}
}
The above compiles, but when I run the test, I get the following failure:
3.0579379 was not equal to 3.058 +- 0.1
Expected :3.058 +- 0.1
Actual :3.0579379
I also tied the following assertion instead:
assert(dist === 3.058)
However, that does not work either, and gives the following failure:
3.0579379 did not equal 3.058
Expected :3.058
Actual :3.0579379
I've read numerous examples of comparing floating point numbers with both of the above syntaxes, and it seems like they should work. My first example is straight from the documentation.
What am I doing wrong?
Upvotes: 1
Views: 696
Reputation: 48410
Since dist
is a Float
whilst other arguments are Double
s try converting dist
to Double
like so
dist.toDouble mustBe 3.058D +- 0.1
or make other arguments floats like so
dist mustBe 3.058f +- 0.1f
Possibly related to Comparison matchers fail on mixed numeric types
Upvotes: 1