Fix calc_distance TANIMOTO bug (#7140)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
pull/7123/head
groot 2021-08-18 11:16:10 +08:00 committed by GitHub
parent 38beb124c9
commit b4eb20f2c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View File

@ -239,11 +239,12 @@ func CalcTanimotoCoefficient(dim int64, hamming []int32) ([]float32, error) {
array := make([]float32, len(hamming))
for i := 0; i < len(hamming); i++ {
if hamming[i] >= int32(dim)*2 {
if hamming[i] > int32(dim) {
err := errors.New("Invalid hamming for tanimoto")
return nil, err
}
array[i] = float32(hamming[i]) / (float32(dim)*2 - float32(hamming[i]))
equalBits := int32(dim) - hamming[i]
array[i] = float32(equalBits) / (float32(dim)*2 - float32(equalBits))
}
return array, nil

View File

@ -228,7 +228,7 @@ func TestCalcHamming(t *testing.T) {
hamming := make([]int32, 1)
hamming[0] = n
tanimoto, err := CalcTanimotoCoefficient(dim, hamming)
realTanimoto := float64(n) / (float64(dim)*2.0 - float64(n))
realTanimoto := float64(int32(dim)-n) / (float64(dim)*2.0 - float64(int32(dim)-n))
assert.Nil(t, err)
assert.Less(t, math.Abs(float64(tanimoto[0])-realTanimoto), float64(PRECISION))
}