聊聊gost的DeltaCompare


本文主要研究一下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

https://www.xiaohongshu.com/discovery/item/6036cb820000000001007d82
https://www.xiaohongshu.com/discovery/item/6036cb720000000001008a60
https://www.xiaohongshu.com/discovery/item/6036c909000000000101d385
https://www.xiaohongshu.com/discovery/item/6036cb820000000001007d82
https://www.xiaohongshu.com/discovery/item/6036cb720000000001008a60
https://www.xiaohongshu.com/discovery/item/6036c909000000000101d385
https://www.xiaohongshu.com/discovery/item/6036ce610000000001008abc
https://www.xiaohongshu.com/discovery/item/6036cdb20000000001007dbb
https://www.xiaohongshu.com/discovery/item/6036ce000000000001000276
https://www.xiaohongshu.com/discovery/item/6036ce240000000001007dd7
https://www.xiaohongshu.com/discovery/item/6036ce080000000001000277
https://www.xiaohongshu.com/discovery/item/6036ce460000000001007de0

上一篇:SpringBoot使用Junit单元测试


下一篇:解决:ESLint: Expected indentation of 4 spaces but found 6.(indent)