From: JoeyLYZ666 (@JoeyLYZ666) <noreply+JoeyLYZ666@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #1044: sparsevec `<=>` returns NaN self-distance and 1.0 for a collinear pair
Date: Wed, 21 Oct 2026 12:53:27 +0000
Message-ID: <gh-pgvector-pgvector-1044@github.com> (raw)
## Description
For the finite sparse vector `{1:3e19,2:4e19}/2` (each value legal float4;
squared norm `25e38` overflows float4):
- self cosine distance `v <=> v` → `NaN` (0/0 after norm overflow)
- cosine distance to the collinear normal vector `{1:3,2:4}/2` → `1.0`
(maximum distance) instead of `0`
Normal-scale control (`{1:3,2:4}/2 <=> {1:3,2:4}/2`) is exactly `0`.
A NaN self-distance also poisons ordering: it sorts inconsistently and
breaks `ORDER BY v <=> q` result expectations.
## Environment
- pgvector 0.8.6 on PostgreSQL 18; deterministic;
## Steps to reproduce
```sql
SELECT '{1:3,2:4}/2'::sparsevec <=> '{1:3,2:4}/2'::sparsevec; -- 0 (control)
SELECT '{1:3e19,2:4e19}/2'::sparsevec <=> '{1:3e19,2:4e19}/2'::sparsevec; -- NaN
SELECT '{1:3e19,2:4e19}/2'::sparsevec <=> '{1:3,2:4}/2'::sparsevec; -- 1.0 (true 0)
```
[reproduce.py](https://github.com/user-attachments/files/33769334/reproduce.py)
## Expected behavior
`0` and `0` (compute norms in float8, or pre-scale).
## Actual behavior
`NaN` and `1.0`.