From cfb2c6a49e544d4d003bb7901f7daa473af74053 Mon Sep 17 00:00:00 2001 From: Lorenzo Mec-iS Date: Thu, 2 Oct 2025 15:26:24 +0100 Subject: [PATCH 1/2] set min similarity for constant zeros --- src/metrics/distance/cosine.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/metrics/distance/cosine.rs b/src/metrics/distance/cosine.rs index ea065a07..8c7a2c00 100644 --- a/src/metrics/distance/cosine.rs +++ b/src/metrics/distance/cosine.rs @@ -92,7 +92,7 @@ impl Cosine { let magnitude_y = Self::magnitude(y); if magnitude_x == 0.0 || magnitude_y == 0.0 { - panic!("Cannot compute cosine distance for zero-magnitude vectors."); + return f64::MIN; } dot_product / (magnitude_x * magnitude_y) @@ -188,12 +188,12 @@ mod tests { wasm_bindgen_test::wasm_bindgen_test )] #[test] - #[should_panic(expected = "Cannot compute cosine distance for zero-magnitude vectors.")] fn cosine_distance_zero_vector() { let a = vec![0, 0, 0]; let b = vec![1, 2, 3]; - let _dist: f64 = Cosine::new().distance(&a, &b); + let dist: f64 = Cosine::new().distance(&a, &b); + assert!(dist > 1e300) } #[cfg_attr( From 87267f4742a7c74d2464757d221720dd663ab153 Mon Sep 17 00:00:00 2001 From: Lorenzo Mec-iS Date: Thu, 2 Oct 2025 15:35:16 +0100 Subject: [PATCH 2/2] bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bd9db328..88463e47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "smartcore" description = "Machine Learning in Rust." homepage = "https://smartcorelib.org" -version = "0.4.3" +version = "0.4.4" authors = ["smartcore Developers"] edition = "2021" license = "Apache-2.0"