Skip to main content

cosine

Function cosine 

Source
pub fn cosine(a: &[f32], b: &[f32]) -> f32
Expand description

Cosine similarity of two equal-length vectors (0 when either is zero).

Accumulates in f64: an f32 sum-of-squares underflows to exactly 0.0 for vectors of tiny finite components (all ≲ 5e-23), which made cosine(v, v) return 0 for such vectors — maximum “novelty” for a byte-identical repeat, defeating the loop breaker whenever a degenerate embedder emitted tiny activations. The breaker’s hostile- embedding gate only catches exact zeros and non-finite values, so the underflow case must be correct here.