Skip to content

Commit 0dbf3da

Browse files
fix: fix crash when all samples have the same value
1 parent 9f20fd0 commit 0dbf3da

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

  • crates/criterion_compat/criterion_fork/src/stats/univariate/kde

crates/criterion_compat/criterion_fork/src/stats/univariate/kde/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ impl Bandwidth {
8484
let n = A::cast(sample.len());
8585
let sigma = sample.std_dev(None);
8686

87-
sigma * (factor / n).powf(exponent)
87+
let h = sigma * (factor / n).powf(exponent);
88+
// CodSpeed addition:
89+
// Silverman's rule of thumb can return zero if all samples have the same value,
90+
// so we return epsilon in that case to avoid division by zero in the KDE
91+
if h > A::cast(0) {
92+
h
93+
} else {
94+
A::epsilon()
95+
}
8896
}
8997
}
9098
}

0 commit comments

Comments
 (0)