Skip to content

Commit 2345251

Browse files
committed
fix failing test
1 parent 8cff422 commit 2345251

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,20 @@ fn get_percentage(input: &str) -> Option<f64> {
123123
///
124124
/// * `prior` - The prior probability as a decimal fraction.
125125
/// * `likelihood` - The likelihood as a decimal fraction.
126-
/// * `evidence` - The evidence as a decimal fraction.
126+
/// * `evidence` - The evidence as a decimal fraction. A value of zero indicates
127+
/// that the evidence is not observed, which results in an undefined
128+
/// posterior probability.
127129
///
128130
/// # Returns
129131
///
130132
/// * A f64 value representing the posterior probability as a decimal fraction.
133+
/// If `evidence` is zero, the function returns NaN (Not-a-Number).
131134
fn bayesian(prior: f64, likelihood: f64, evidence: f64) -> f64 {
132-
(prior * likelihood) / evidence
135+
if evidence == 0.0 {
136+
std::f64::NAN
137+
} else {
138+
(prior * likelihood) / evidence
139+
}
133140
}
134141

135142
#[cfg(test)]
@@ -185,7 +192,7 @@ mod tests {
185192
#[test]
186193
fn test_bayesian_zero_evidence() {
187194
let prior = 0.5;
188-
let likelihood = 0.8;
195+
let likelihood = 0.7;
189196
let evidence = 0.0;
190197
let calculated_posterior = bayesian(prior, likelihood, evidence);
191198
assert!(calculated_posterior.is_nan());

0 commit comments

Comments
 (0)