pub struct Probability { /* private fields */ }Expand description
A validated probability value in the closed interval [0, 1].
Implementations§
Source§impl Probability
impl Probability
Sourcepub const fn new(value: f64) -> Probability
pub const fn new(value: f64) -> Probability
Creates a probability without validation.
Sourcepub const fn try_new(value: f64) -> Result<Probability, ProbabilityError>
pub const fn try_new(value: f64) -> Result<Probability, ProbabilityError>
Creates a probability from a finite value in [0, 1].
§Errors
Returns ProbabilityError::NonFiniteProbability when value is
NaN or infinite, and ProbabilityError::ProbabilityOutOfRange when
value is outside [0, 1].
§Examples
use use_probability::{Probability, ProbabilityError};
let probability = Probability::try_new(0.25)?;
assert_eq!(probability, Probability::new(0.25));
assert!(matches!(
Probability::try_new(1.5),
Err(ProbabilityError::ProbabilityOutOfRange(1.5))
));Sourcepub fn from_fraction(
part: u64,
total: u64,
) -> Result<Probability, ProbabilityError>
pub fn from_fraction( part: u64, total: u64, ) -> Result<Probability, ProbabilityError>
Creates a probability from part / total.
§Errors
Returns ProbabilityError::ZeroTotal when total == 0, and
ProbabilityError::PartExceedsTotal when part > total.
§Examples
use use_probability::Probability;
let probability = Probability::from_fraction(1, 4)?;
assert!((probability.value() - 0.25).abs() < 1.0e-12);Sourcepub const fn validate(self) -> Result<Probability, ProbabilityError>
pub const fn validate(self) -> Result<Probability, ProbabilityError>
Validates that an existing probability remains normalized.
§Errors
Returns the same error variants as Self::try_new.
Sourcepub const fn as_percentage(&self) -> f64
pub const fn as_percentage(&self) -> f64
Returns the stored probability as a percentage.
Sourcepub const fn complement(self) -> Probability
pub const fn complement(self) -> Probability
Returns the complementary probability 1 - p.
Sourcepub const fn impossible() -> Probability
pub const fn impossible() -> Probability
Returns the impossible event probability 0.
Sourcepub const fn certainty() -> Probability
pub const fn certainty() -> Probability
Returns the certain event probability 1.
Sourcepub const fn intersection_independent(self, other: Probability) -> Probability
pub const fn intersection_independent(self, other: Probability) -> Probability
Returns the probability of two independent events both happening.
Sourcepub fn union_independent(self, other: Probability) -> Probability
pub fn union_independent(self, other: Probability) -> Probability
Returns the probability of at least one of two independent events happening.
Trait Implementations§
Source§impl Clone for Probability
impl Clone for Probability
Source§fn clone(&self) -> Probability
fn clone(&self) -> Probability
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Probability
impl Debug for Probability
Source§impl Default for Probability
impl Default for Probability
Source§fn default() -> Probability
fn default() -> Probability
Source§impl Display for Probability
impl Display for Probability
Source§impl PartialEq for Probability
impl PartialEq for Probability
Source§fn eq(&self, other: &Probability) -> bool
fn eq(&self, other: &Probability) -> bool
self and other values to be equal, and is used by ==.