mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-02-05 01:50:27 +08:00
fix(vector-store): add epsilon tolerance for norm checks
Replace exact zero comparison with epsilon-based check (< 1e-10) in _cosine_similarity to handle floating point precision issues. Also check for product underflow to prevent inf/nan from division by very small numbers. Solution-ID: SOL-20251228113619 Issue-ID: ISS-1766921318981-11 Task-ID: T1
This commit is contained in:
@@ -46,10 +46,16 @@ def _cosine_similarity(a: List[float], b: List[float]) -> float:
|
||||
norm_a = np.linalg.norm(a_arr)
|
||||
norm_b = np.linalg.norm(b_arr)
|
||||
|
||||
if norm_a == 0 or norm_b == 0:
|
||||
EPSILON = 1e-10
|
||||
# Use epsilon tolerance to avoid division by (near-)zero due to floating point precision.
|
||||
if norm_a < EPSILON or norm_b < EPSILON:
|
||||
return 0.0
|
||||
|
||||
return float(np.dot(a_arr, b_arr) / (norm_a * norm_b))
|
||||
denom = norm_a * norm_b
|
||||
if denom < EPSILON:
|
||||
return 0.0
|
||||
|
||||
return float(np.dot(a_arr, b_arr) / denom)
|
||||
|
||||
|
||||
class VectorStore:
|
||||
|
||||
Reference in New Issue
Block a user