NaN / +Inf components accepted at add_item; get_distance then returns NaN / Infinity that participate in ranking (angular)
What happens?
add_item accepts vectors containing NaN / +Inf components without error. After build, get_distance returns NaN / Infinity for pairs involving those rows, and get_nns_by_vector returns them ranked with those non-finite scores interleaved among correct finite results.
rows: 0 = [0.5, 1], 1 = [NaN, 1], 2 = [Inf, 1]; query = rows[0]
get_nns_by_vector(query, 3, include_distances=True)
-> [(0, 0.0), (1, 1.4142135), (2, NaN)]The self-match is correct and one neighbor gets a finite √2, but a NaN distance is returned for the +Inf row. Precedent inside this project shows these edges are considered worth guarding: #277 ("avoid nan when normalizing a zero vector") and #249 (zero vectors) fixed the zero-vector family; NaN/Inf input components remain unguarded.
What do you expect?
Either reject non-finite components at add_item with a clear error, or define their distance semantics — NaN leaking into ranked results makes any threshold/top-k consumer silently unreliable.
How to reproduce?
reproduce.py --output observed.json (Python 3 + annoy only) builds the index, queries, prints the results, and exits non-zero unless all distances are finite with the self-match first. Deterministic; observed.json is from an actual run, executed twice with identical results.
from annoy import AnnoyIndex
idx = AnnoyIndex(2, "angular")
idx.add_item(0, [0.5, 1.0])
idx.add_item(1, [float('nan'), 1.0])
idx.add_item(2, [float('inf'), 1.0])
idx.build(1)
print(idx.get_nns_by_vector([0.5, 1.0], 3, include_distances=True))
# [(0, 0.0), (1, 1.4142135), (2, nan)]Environment
- annoy 1.17.3 (Windows build), Python 3.12, Windows 11 x64
- angular metric, 1 tree
Source: spotify/annoy