mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
Merge pull request #1265 from weicut/master
This commit is contained in:
@ -77,12 +77,28 @@ func ComparatorUint64(a, b interface{}) int {
|
||||
|
||||
// ComparatorFloat32 provides a basic comparison on float32.
|
||||
func ComparatorFloat32(a, b interface{}) int {
|
||||
return int(gconv.Float32(a) - gconv.Float32(b))
|
||||
aFloat := gconv.Float64(a)
|
||||
bFloat := gconv.Float64(b)
|
||||
if aFloat == bFloat {
|
||||
return 0
|
||||
}
|
||||
if aFloat > bFloat {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// ComparatorFloat64 provides a basic comparison on float64.
|
||||
func ComparatorFloat64(a, b interface{}) int {
|
||||
return int(gconv.Float64(a) - gconv.Float64(b))
|
||||
aFloat := gconv.Float64(a)
|
||||
bFloat := gconv.Float64(b)
|
||||
if aFloat == bFloat {
|
||||
return 0
|
||||
}
|
||||
if aFloat > bFloat {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// ComparatorByte provides a basic comparison on byte.
|
||||
|
||||
@ -160,3 +160,20 @@ func Test_ComparatorTime(t *testing.T) {
|
||||
t.Assert(l, -1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ComparatorFloat32OfFixed(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gutil.ComparatorFloat32(0.1, 0.1), 0)
|
||||
t.Assert(gutil.ComparatorFloat32(1.1, 2.1), -1)
|
||||
t.Assert(gutil.ComparatorFloat32(2.1, 1.1), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ComparatorFloat64OfFixed(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gutil.ComparatorFloat64(0.1, 0.1), 0)
|
||||
t.Assert(gutil.ComparatorFloat64(1.1, 2.1), -1)
|
||||
t.Assert(gutil.ComparatorFloat64(2.1, 1.1), 1)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user