get_distance returns the orthogonal distance (√2) for any pair involving a small-but-representable stored vector, including self-distance (angular)
Suggested title: get_distance returns the orthogonal distance (√2) for any pair involving a small-but-representable stored vector, including self-distance (angular)
What happens?
Vectors that are added to an angular index with small-but-valid float32 components lose all direction information: get_distance returns ~1.4142135 (= √2, the orthogonal distance) for every pair involving them — including the distance of the vector to itself — and antipodal tiny vectors become indistinguishable from orthogonal ones. add_item raises no error or warning.
With an angular index (AnnoyIndex(2, "angular"), 1 tree) and these stored pairs:
| pair | stored vectors | true angular distance | returned |
|---|---|---|---|
| unit self | [1,0] vs [1,0] |
0.0 | 0.0 ✓ |
| collinear control | [1,0] vs [2,0] |
0.0 | 0.0 ✓ |
| tiny 1e-12 self | [1e-12,0] vs [1e-12,0] |
0.0 | 1.4142135 |
| tiny 1e-19 self | [1e-19,0] vs [1e-19,0] |
0.0 | 1.4142135 |
| tiny 1e-25 self | [1e-25,0] vs [1e-25,0] |
0.0 | 1.4142135 |
| antipodal tiny | [1e-25,0] vs [-1e-25,0] |
2.0 | 1.4142135 |
| unit vs tiny | [1,0] vs [1e-25,0] |
0.0 | 1.4142135 |
All components are representable float32 values; nothing in the input underflows. The self-distance case alone (get_distance(i, i) != 0) contradicts the basic contract of a distance, and the antipodal case shows direction is not merely lost but flipped into "orthogonal".
An interesting asymmetry: using the same magnitudes as query vectors via get_nns_by_vector still ranks the true collinear neighbor first — only stored items are corrupted.
What do you expect?
Angular distance is scale-invariant: d(a*x, a*y) == d(x, y), d(v, v) == 0, d(v, -v) == 2 for every nonzero representable v. Either the distance should be computed in a way that keeps tiny vectors' direction (e.g. wider accumulation), or add_item should reject/warn on vectors whose norm cannot be handled — silent corruption of the index content is the worst of the options.
How to reproduce?
reproduce.py --output observed.json (Python 3 + annoy only) builds one index with all pairs above, prints the distance table, and exits non-zero on mismatch. Deterministic; observed.json is from an actual run. Minimal core:
from annoy import AnnoyIndex
idx = AnnoyIndex(2, "angular")
idx.add_item(0, [1e-25, 0.0])
idx.build(1)
print(idx.get_distance(0, 0)) # 1.4142135381698608, expected 0.0Environment
- annoy 1.17.3 (Windows wheel/native build), Python 3.12, Windows 11 x64
- angular metric; 1 tree; behavior identical with more trees
The issue was found while checking that positively scaling stored data preserves all pairwise distances (a metamorphic relation). Apparent mechanism: normalization at add_item keeps the raw float32 vector (its norm² = 1e-50 underflows float32), and the distance formula's dot(a,a)·dot(b,b) term then underflows to 0, so the cosine term is guarded to 0 → distance √2. The threshold (1e-12 already fails, i.e. norm²·norm² ≈ 1e-48 < FLT32 min subnormal) matches a float32 product of the two squared norms.
Source: spotify/annoy