Skip to content

Commit ee3ee47

Browse files
authored
Merge pull request #101 from ptondereau/secure-float-distance
Numerical stability improvements for Cosine Distance
2 parents 68ed432 + 6a09118 commit ee3ee47

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/distance/cosine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ impl Distance for Cosine {
4040
let qn = q.header.norm;
4141
let pq = dot_product(&p.vector, &q.vector);
4242
let pnqn = pn * qn;
43-
if pnqn != 0.0 {
43+
if pnqn > f32::EPSILON {
4444
let cos = pq / pnqn;
45+
let cos = cos.clamp(-1.0, 1.0);
4546
// cos is [-1; 1]
4647
// cos = 0. -> 0.5
4748
// cos = -1. -> 1.0

0 commit comments

Comments
 (0)