Build a vector search engine
Stage 2 of 5v1 · 2d718e84

Measure proximity

Implement Euclidean and cosine distance with normalized results.

Two geometries

Add distance, a stateless command with metric, a, and b. Euclidean distance is the square root of the sum of squared differences. Cosine distance is 1 - dot(a,b)/(norm(a)*norm(b)).

Round the result to six decimal places and serialize integers without .0. Cosine distance for a zero vector is undefined; return the exact error in the tests. Validate dimensions and finite numbers too.

Acceptance criteria

  • Both metrics follow their mathematical definitions.
  • Numeric formatting is portable across languages.
  • Errors never emit NaN or infinity in JSON.