nodejs mocha如何处理近似匹配

nodejs mocha how to deal with approximate matching

我在 nodejs 中使用 mocha 进行测试。

问题是我不知道如何处理匹配小数点第n位的精度。例如下面的代码

value.should.equal((0.8+0.4+1/3)/3)

失败并显示错误消息

 AssertionError: expected 0.5111111111111111 to equal 0.5111111111111112
      + expected - actual

      -0.5111111111111111
      +0.5111111111111112

但这对我来说是一样的东西。如何处理这个精度?

您可以使用 chai 断言库和 closeTo() 断言

value.should.be.closeTo(expected, delta)