From: JoeyLYZ666 (@JoeyLYZ666) <noreply+JoeyLYZ666@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #1052: [Bug] Sparse scalar distances overflow on finite `sparse
Date: Sat, 31 Oct 2026 08:56:55 +0000
Message-ID: <gh-pgvector-pgvector-1052@github.com> (raw)
## Describe the bug
pgvector 0.8.7 returns `Infinity` or `NaN` from sparsevec scalar distance
functions for finite nonzero elements whose exact result is representable by
`double precision`.
## Reproduce
Run `testcase.py` against PostgreSQL with pgvector 0.8.7:
```powershell
python testcase.py
```
The testcase compares two finite `sparsevec(2)` values with opposite `1e38`
components and computes L1, L2, and self cosine distance.
[testcase.py](https://github.com/user-attachments/files/34213348/testcase.py)
## Expected behavior
The expected results are finite: approximately `4e38`, `2.828427e38`, and `0`
for self cosine distance.
## Actual behavior
The affected release returns:
```text
Infinity, Infinity, NaN
```
The sparsevec implementation accumulates intermediate values in `float` before
returning `double precision`.
## Impact
Finite sparse vectors can produce non-finite or invalid distances, breaking
ordering, thresholds, and scalar correctness checks.
## Environment
- pgvector 0.8.7
- PostgreSQL 18
- `sparsevec(2)` values
`vector.c` report because the SQL type and implementation path differ.