1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//! Defines global constants and limits used in vector math and indexing.
use VectorDimension;
// ============================================================================
// Constants
// ============================================================================
/// Default tolerance for floating-point comparisons in normalization operations.
///
/// This tolerance (1e-6) is appropriate for most f32 operations where accumulated
/// floating-point errors are expected to be small. It's used as the default for
/// functions like [`crate::core::vector::is_normalized`] when checking if a vector has unit magnitude.
///
/// For stricter or looser comparisons, functions accept an explicit tolerance parameter.
pub const NORMALIZATION_TOLERANCE: f32 = 1e-6;
/// Squared magnitude threshold for detecting near-zero vectors.
///
/// Vectors with squared magnitude below this threshold are treated as zero vectors
/// in normalization operations. This prevents numerical instability from denormal
/// numbers and avoids division by very small values that could cause overflow.
///
/// Value: 1e-30 corresponds to magnitude ≈ 1e-15, allowing normalization of
/// small but valid vectors (e.g., 1e-15 components). This is well above the
/// smallest normal f32 value (1.17e-38) and provides sufficient headroom
/// before denormal range.
pub const SQUARED_MAGNITUDE_THRESHOLD: f32 = 1e-30;
/// Maximum allowed vector dimension.
///
/// This limit (100,000) far exceeds typical embedding sizes and exists to prevent
/// DoS attacks via memory exhaustion during deserialization.
pub const MAX_VECTOR_DIMENSIONS: usize = 100_000;
/// Type tag for Vector (dense f32 array) value.
pub const TAG_VECTOR: u8 = 7;
/// Type tag for SparseVector value.
pub const TAG_SPARSE_VECTOR: u8 = 8;
/// Maximum allowed vector dimension (typed).
///
/// Re-exported from [`MAX_VECTOR_DIMENSIONS`] for convenience.
pub const MAX_DIMENSION: VectorDimension = new;