Skip to content

vec_distance_L2 returns Infinity (large) / 0 (tiny) for finite float32 inputs; vec_distance_L1 is exact on the same inputs #334

Open

Description

@JoeyLYZ666

Description

vec_distance_l2 accumulates squared differences in float32:

  • large: vec_distance_l2(vec_f32(x'..1e20..'), vec_f32(x'..0..')) → Infinity
    (true 1e20, representable)
  • tiny: distinct vectors at 1e-25 separation → 0.0 (true 1e-25)

vec_distance_l1 with the identical inputs returns exact finite values
(2.0000000400817547e+20, 2e-25), which isolates the fault to the
squaring step. The tiny side silently corrupts KNN ranking and
distance <= 0 comparisons; the large side propagates Infinity into
user expressions.

Environment

  • sqlite-vec 0.1.9 (pip wheel, loadable extension), Python 3.12, Windows x64
  • deterministic; observed 2026-09-29

Steps to reproduce

import sqlite3, sqlite_vec
db = sqlite3.connect(":memory:")
db.enable_load_extension(True); sqlite_vec.load(db); db.enable_load_extension(False)
b = sqlite_vec.serialize_float32
db.execute("SELECT vec_distance_L2(?, ?)", [b([3.0, 0.0]), b([0.0, 0.0])]).fetchone()   # 3.0 control
db.execute("SELECT vec_distance_L1(?, ?)", [b([1e20, 1e20]), b([0.0, 0.0])]).fetchone() # finite contrast
db.execute("SELECT vec_distance_L2(?, ?)", [b([1e20, 0.0]), b([0.0, 0.0])]).fetchone()  # inf
db.execute("SELECT vec_distance_L2(?, ?)", [b([1e-25, 0.0]), b([0.0, 0.0])]).fetchone() # 0.0

reproduce.py

Expected behavior

1e20 / 1e-25 (accumulate in double, or pre-scale like the L1 path effectively does).

Actual behavior

Infinity / 0.0; L1 exact on identical inputs.

Activity

  1. vlasky commented on Nov 9, 2026

    @vlasky

    For anyone hitting this: the community fork vlasky/sqlite-vec fixes it in v0.2.9-alpha (changelog). The float32 kernels now return the sum of squared differences and the caller redoes a sum in double when it is Inf or below 2^24 * FLT_MIN, the bound under which subnormal terms can have mattered, so vec_distance_l2() returns 1e20 and 1e-25 for these inputs on the scalar, NEON and AVX paths. Ordinary vectors keep the same kernels and results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions