From: JoeyLYZ666 (@JoeyLYZ666) <noreply+JoeyLYZ666@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #1051: [Bug] Dense scalar distances overflow on finite `vector` inputs
Date: Sat, 31 Oct 2026 08:55:24 +0000
Message-ID: <gh-pgvector-pgvector-1051@github.com> (raw)
## Describe the bug
pgvector 0.8.7 returns `Infinity` or `NaN` from dense `vector` scalar distance
functions for finite input values whose exact result is still representable as
`double precision`.
## Reproduce
Run `testcase.py` against PostgreSQL with pgvector 0.8.7:
```powershell
python testcase.py
```
The query evaluates `l1_distance`, `l2_distance`, and `cosine_distance` on
finite dense vectors containing `1e38` and `1e20`.
[testcase.py](https://github.com/user-attachments/files/34213300/testcase.py)
## Expected behavior
All results should be finite. For the selected values, the expected results are
approximately `4e38`, `2.828427e38`, and `0` for the identical cosine input.
## Actual behavior
The affected release returns:
```text
Infinity, Infinity, NaN
```
The dense implementation accumulates intermediate values in `float` before
converting the result to the declared `double precision` return type.
## Impact
Finite vectors can produce non-finite scalar distances, breaking ordering,
threshold checks, and any oracle requiring finite non-negative distances.
## Environment
- pgvector 0.8.7
- PostgreSQL 18
- dense `vector(2)` values