Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/noasm/cosine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func CosineF32(a, b []float32) (d float32) {
panic("vector length must be multiple of 4")
}

e := float32(1e-8)
ab := float32(0.0)
aa := float32(0.0)
bb := float32(0.0)
Expand Down Expand Up @@ -53,6 +54,9 @@ func CosineF32(a, b []float32) (d float32) {
}

s := math32.Sqrt(aa) * math32.Sqrt(bb)
if s < e {
s = e
}

d = (1 - ab/s) / 2

Expand Down
4 changes: 4 additions & 0 deletions internal/pure/cosine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func CosineF32(a, b []float32) (d float32) {
panic("vectors must have equal lengths")
}

e := float32(1e-8)
ab := float32(0.0)
aa := float32(0.0)
bb := float32(0.0)
Expand All @@ -31,6 +32,9 @@ func CosineF32(a, b []float32) (d float32) {
}

s := math32.Sqrt(aa) * math32.Sqrt(bb)
if s < e {
s = e
}

d = (1 - ab/s) / 2

Expand Down
37 changes: 37 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package vector_test

import (
"math"
"math/rand"
"testing"

Expand Down Expand Up @@ -45,6 +46,14 @@ func init() {
n2 = Node{ID: 2, Vector: b}
}

func zeroF32() vector.F32 {
v := make(vector.F32, n)
for i := 0; i < n; i++ {
v[i] = 0.0
}
return v
}

func randF32() vector.F32 {
v := make(vector.F32, n)
for i := 0; i < n; i++ {
Expand Down Expand Up @@ -133,6 +142,34 @@ func TestNoAsmCosineF32(t *testing.T) {
}
}

func TestPureCosineZeroF32(t *testing.T) {
sut := pure.Cosine(0)

for i := 0; i < n*100; i++ {
a := zeroF32()
b := randF32()

d := sut.Distance(a, b)
if math.IsNaN(float64(d)) {
t.Errorf("failed distance")
}
}
}

func TestNoAsmCosineZeroF32(t *testing.T) {
sut := noasm.Cosine(0)

for i := 0; i < n*100; i++ {
a := zeroF32()
b := randF32()

d := sut.Distance(a, b)
if math.IsNaN(float64(d)) {
t.Errorf("failed distance")
}
}
}

//
// Benchmark
//
Expand Down