聊聊gost的DeltaCompare

Posted codecraft

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聊聊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

以上是关于聊聊gost的DeltaCompare的主要内容,如果未能解决你的问题,请参考以下文章

聊聊gost的GoUnterminated

聊聊gost的Pager

聊聊gost的GenericTaskPool

聊聊gost的HashSet

聊聊gost的ObjectPool

聊聊gost的CountWatch