set min similarity for constant zeros (#331)

* set min similarity for constant zeros
* bump version
This commit is contained in:
Lorenzo
2025-10-02 15:41:18 +01:00
committed by GitHub
parent b6e32fb328
commit e633afa520
2 changed files with 4 additions and 4 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
name = "smartcore" name = "smartcore"
description = "Machine Learning in Rust." description = "Machine Learning in Rust."
homepage = "https://smartcorelib.org" homepage = "https://smartcorelib.org"
version = "0.4.3" version = "0.4.4"
authors = ["smartcore Developers"] authors = ["smartcore Developers"]
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
+3 -3
View File
@@ -92,7 +92,7 @@ impl<T: Number> Cosine<T> {
let magnitude_y = Self::magnitude(y); let magnitude_y = Self::magnitude(y);
if magnitude_x == 0.0 || magnitude_y == 0.0 { 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) dot_product / (magnitude_x * magnitude_y)
@@ -188,12 +188,12 @@ mod tests {
wasm_bindgen_test::wasm_bindgen_test wasm_bindgen_test::wasm_bindgen_test
)] )]
#[test] #[test]
#[should_panic(expected = "Cannot compute cosine distance for zero-magnitude vectors.")]
fn cosine_distance_zero_vector() { fn cosine_distance_zero_vector() {
let a = vec![0, 0, 0]; let a = vec![0, 0, 0];
let b = vec![1, 2, 3]; 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( #[cfg_attr(