共计 990 个字符,预计需要花费 3 分钟才能阅读完成。
序
本文次要钻研一下 gost 的 DeltaCompare
DeltaCompare
gost/math/compare.go
// equivalent to math.Abs(expected - actual) <= delta
func DeltaCompareFloat64(expected float64, actual float64, delta float64) bool {
if expected > actual {return expected-actual <= delta}
return actual-expected <= delta
}
func DeltaCompareFloat32(expected float32, actual float32, delta float32) bool {
if expected > actual {return expected-actual <= delta}
return actual-expected <= delta
}
DeltaCompareFloat64 及 DeltaCompareFloat32 比照两个 float 类型的差值是否小于等于指定的 delta
实例
gost/math/compare_test.go
func TestFloat64(t *testing.T) {assert.True(t, DeltaCompareFloat64(12.3334, 12.3344, 0.01))
assert.True(t, DeltaCompareFloat64(12.3334, 12.32981, 0.01))
assert.False(t, DeltaCompareFloat64(12.3334, 12.0325, 0.01))
}
func TestCompareFloat32(t *testing.T) {assert.True(t, DeltaCompareFloat32(12.3334, 12.3344, 0.01))
assert.True(t, DeltaCompareFloat32(12.3334, 12.32981, 0.01))
assert.False(t, DeltaCompareFloat64(12.3334, 12.0325, 0.01))
}
小结
gost 的提供了 DeltaCompareFloat32、DeltaCompareFloat64 办法用于比照两个 float 类型的差值是否小于等于指定的 delta。
doc
- gost
正文完